From 55daa29a7ce84de03196a822c4ebd9c39dd436cc Mon Sep 17 00:00:00 2001 From: Romuald Conty Date: Mon, 17 Oct 2011 13:03:56 +0000 Subject: [PATCH 001/113] Allow to connect to a device using a connection string: - Provide a nfc_get_default_device() that allow to grab the connstring stored in LIBNFC_DEFAULT_DEVICE environnement variable or returns the first available device if not set; - nfc_connect(NULL) now takes the default device (see nfc_get_default_device()); - Removes nfc_driver_desc_t from public types - Defines nfc_connstring as char[1024] - examples/*: use nfc_connstring - examples/nfc-poll: now uses only the default device (instead of all devices availables) - Removes parse_args() from nfc-utils.[hc] --- examples/nfc-dep-target.c | 8 +- examples/nfc-poll.c | 83 +++++------- examples/nfc-relay.c | 13 +- examples/pn53x-diagnose.c | 11 +- include/nfc/nfc-types.h | 19 +-- include/nfc/nfc.h | 5 +- libnfc/drivers/acr122.c | 102 +++++++++++---- libnfc/drivers/acr122.h | 4 +- libnfc/drivers/arygon.c | 126 ++++++++++++++----- libnfc/drivers/arygon.h | 4 +- libnfc/drivers/pn532_uart.c | 122 +++++++++++++----- libnfc/drivers/pn532_uart.h | 4 +- libnfc/drivers/pn53x_usb.c | 244 +++++++++++++++++++++++------------- libnfc/drivers/pn53x_usb.h | 4 +- libnfc/nfc-internal.h | 8 +- libnfc/nfc.c | 101 ++++++++++----- utils/nfc-list.c | 19 +-- utils/nfc-relay-picc.c | 15 +-- utils/nfc-utils.c | 45 ------- utils/nfc-utils.h | 2 - 20 files changed, 559 insertions(+), 380 deletions(-) diff --git a/examples/nfc-dep-target.c b/examples/nfc-dep-target.c index d902c56..031db8f 100644 --- a/examples/nfc-dep-target.c +++ b/examples/nfc-dep-target.c @@ -66,16 +66,16 @@ main (int argc, const char *argv[]) size_t szDeviceFound; byte_t abtTx[] = "Hello Mars!"; #define MAX_DEVICE_COUNT 2 - nfc_device_desc_t pnddDevices[MAX_DEVICE_COUNT]; - nfc_list_devices (pnddDevices, MAX_DEVICE_COUNT, &szDeviceFound); + nfc_connstring connstrings[MAX_DEVICE_COUNT]; + nfc_list_devices (connstrings, MAX_DEVICE_COUNT, &szDeviceFound); // Little hack to allow using nfc-dep-initiator & nfc-dep-target from // the same machine: if there is more than one readers connected // nfc-dep-target will connect to the second reader // (we hope they're always detected in the same order) if (szDeviceFound == 1) { - pnd = nfc_connect (&(pnddDevices[0])); + pnd = nfc_connect (connstrings[0]); } else if (szDeviceFound > 1) { - pnd = nfc_connect (&(pnddDevices[1])); + pnd = nfc_connect (connstrings[1]); } else { printf("No device found."); return EXIT_FAILURE; diff --git a/examples/nfc-poll.c b/examples/nfc-poll.c index 2843654..a5c27f1 100644 --- a/examples/nfc-poll.c +++ b/examples/nfc-poll.c @@ -65,15 +65,10 @@ void stop_polling (int sig) int main (int argc, const char *argv[]) { - size_t szFound; - size_t i; bool verbose = false; - nfc_device_desc_t *pnddDevices; signal (SIGINT, stop_polling); - pnddDevices = parse_args (argc, argv, &szFound, &verbose); - // Display libnfc version const char *acLibnfcVersion = nfc_version (); @@ -83,59 +78,43 @@ main (int argc, const char *argv[]) printf ("%s uses libnfc %s\n", argv[0], acLibnfcVersion); - if (szFound == 0) { - if (!(pnddDevices = malloc (MAX_DEVICE_COUNT * sizeof (*pnddDevices)))) { - fprintf (stderr, "malloc() failed\n"); - exit (EXIT_FAILURE); - } + const uint8_t uiPollNr = 20; + const uint8_t uiPeriod = 2; + const nfc_modulation_t nmModulations[5] = { + { .nmt = NMT_ISO14443A, .nbr = NBR_106 }, + { .nmt = NMT_ISO14443B, .nbr = NBR_106 }, + { .nmt = NMT_FELICA, .nbr = NBR_212 }, + { .nmt = NMT_FELICA, .nbr = NBR_424 }, + { .nmt = NMT_JEWEL, .nbr = NBR_106 }, + }; + const size_t szModulations = 5; + + nfc_target_t nt; + bool res; + + pnd = nfc_connect (NULL); + + if (pnd == NULL) { + ERR ("%s", "Unable to connect to NFC device."); + exit (EXIT_FAILURE); } - nfc_list_devices (pnddDevices, MAX_DEVICE_COUNT, &szFound); + nfc_initiator_init (pnd); - if (szFound == 0) { - printf ("No NFC device found.\n"); - } - - for (i = 0; i < szFound; i++) { - const uint8_t uiPollNr = 20; - const uint8_t uiPeriod = 2; - const nfc_modulation_t nmModulations[5] = { - { .nmt = NMT_ISO14443A, .nbr = NBR_106 }, - { .nmt = NMT_ISO14443B, .nbr = NBR_106 }, - { .nmt = NMT_FELICA, .nbr = NBR_212 }, - { .nmt = NMT_FELICA, .nbr = NBR_424 }, - { .nmt = NMT_JEWEL, .nbr = NBR_106 }, - }; - const size_t szModulations = 5; - - nfc_target_t nt; - bool res; - - pnd = nfc_connect (&(pnddDevices[i])); - - if (pnd == NULL) { - ERR ("%s", "Unable to connect to NFC device."); + printf ("Connected to NFC reader: %s\n", pnd->acName); + printf ("NFC device will poll during %ld ms (%u pollings of %lu ms for %zd modulations)\n", (unsigned long) uiPollNr * szModulations * uiPeriod * 150, uiPollNr, (unsigned long) uiPeriod * 150, szModulations); + res = nfc_initiator_poll_target (pnd, nmModulations, szModulations, uiPollNr, uiPeriod, &nt); + if (res) { + print_nfc_target ( nt, verbose ); + } else { + if (pnd->iLastError) { + nfc_perror (pnd, "nfc_initiator_poll_targets"); + nfc_disconnect (pnd); exit (EXIT_FAILURE); - } - nfc_initiator_init (pnd); - - printf ("Connected to NFC reader: %s\n", pnd->acName); - printf ("NFC device will poll during %ld ms (%u pollings of %lu ms for %zd modulations)\n", (unsigned long) uiPollNr * szModulations * uiPeriod * 150, uiPollNr, (unsigned long) uiPeriod * 150, szModulations); - res = nfc_initiator_poll_target (pnd, nmModulations, szModulations, uiPollNr, uiPeriod, &nt); - if (res) { - print_nfc_target ( nt, verbose ); } else { - if (pnd->iLastError) { - nfc_perror (pnd, "nfc_initiator_poll_targets"); - nfc_disconnect (pnd); - exit (EXIT_FAILURE); - } else { - printf ("No target found.\n"); - } + printf ("No target found.\n"); } - nfc_disconnect (pnd); } - - free (pnddDevices); + nfc_disconnect (pnd); exit (EXIT_SUCCESS); } diff --git a/examples/nfc-relay.c b/examples/nfc-relay.c index 720b094..7ec4ec5 100644 --- a/examples/nfc-relay.c +++ b/examples/nfc-relay.c @@ -83,7 +83,6 @@ main (int argc, char *argv[]) int arg; bool quiet_output = false; size_t szFound; - nfc_device_desc_t *pnddDevices; const char *acLibnfcVersion = nfc_version (); // Get commandline options @@ -109,20 +108,16 @@ main (int argc, char *argv[]) signal (SIGINT, (void (*)()) intr_hdlr); #endif - // Allocate memory to put the result of available devices listing - if (!(pnddDevices = malloc (MAX_DEVICE_COUNT * sizeof (*pnddDevices)))) { - fprintf (stderr, "malloc() failed\n"); - return EXIT_FAILURE; - } + nfc_connstring connstrings[MAX_DEVICE_COUNT]; // List available devices - nfc_list_devices (pnddDevices, MAX_DEVICE_COUNT, &szFound); + nfc_list_devices (connstrings, MAX_DEVICE_COUNT, &szFound); if (szFound < 2) { ERR ("%zd device found but two connected devices are needed to relay NFC.", szFound); return EXIT_FAILURE; } // Try to open the NFC emulator device - pndTag = nfc_connect (&(pnddDevices[0])); + pndTag = nfc_connect (connstrings[0]); if (pndTag == NULL) { printf ("Error connecting NFC emulator device\n"); return EXIT_FAILURE; @@ -165,7 +160,7 @@ main (int argc, char *argv[]) printf ("%s", "Done, emulated tag is initialized"); // Try to open the NFC reader - pndReader = nfc_connect (&(pnddDevices[1])); + pndReader = nfc_connect (connstrings[1]); printf ("Connected to the NFC reader device: %s", pndReader->acName); printf ("%s", "Configuring NFC reader settings..."); diff --git a/examples/pn53x-diagnose.c b/examples/pn53x-diagnose.c index c11a236..bc28be0 100644 --- a/examples/pn53x-diagnose.c +++ b/examples/pn53x-diagnose.c @@ -55,7 +55,6 @@ main (int argc, const char *argv[]) size_t szFound; size_t i; nfc_device_t *pnd; - nfc_device_desc_t *pnddDevices; const char *acLibnfcVersion; bool result; @@ -72,19 +71,15 @@ main (int argc, const char *argv[]) acLibnfcVersion = nfc_version (); printf ("%s uses libnfc %s\n", argv[0], acLibnfcVersion); - if (!(pnddDevices = malloc (MAX_DEVICE_COUNT * sizeof (*pnddDevices)))) { - fprintf (stderr, "malloc() failed\n"); - return EXIT_FAILURE; - } - - nfc_list_devices (pnddDevices, MAX_DEVICE_COUNT, &szFound); + nfc_connstring connstrings[MAX_DEVICE_COUNT]; + nfc_list_devices (connstrings, MAX_DEVICE_COUNT, &szFound); if (szFound == 0) { printf ("No NFC device found.\n"); } for (i = 0; i < szFound; i++) { - pnd = nfc_connect (&(pnddDevices[i])); + pnd = nfc_connect (connstrings[i]); if (pnd == NULL) { ERR ("%s", "Unable to connect to NFC device."); diff --git a/include/nfc/nfc-types.h b/include/nfc/nfc-types.h index 04b2120..15b9367 100644 --- a/include/nfc/nfc-types.h +++ b/include/nfc/nfc-types.h @@ -71,24 +71,7 @@ typedef struct { int iLastError; } nfc_device_t; -/** - * @struct nfc_device_desc_t - * @brief NFC device description - * - * This struct is used to try to connect to a specified nfc device when nfc_connect(...) - */ -typedef struct { - /** Device name (e.g. "ACS ACR 38U-CCID 00 00") */ - char acDevice[DEVICE_NAME_LENGTH]; - /** Driver name (e.g. "PN532_UART")*/ - char *pcDriver; - /** Port (e.g. "/dev/ttyUSB0") */ - char acPort[DEVICE_PORT_LENGTH]; - /** Port speed (e.g. "115200") */ - uint32_t uiSpeed; - /** Device index for backward compatibility (used to choose one specific device in USB or PSCS devices list) */ - uint32_t uiBusIndex; -} nfc_device_desc_t; +typedef char nfc_connstring[1024]; // Compiler directive, set struct alignment to 1 byte_t for compatibility # pragma pack(1) diff --git a/include/nfc/nfc.h b/include/nfc/nfc.h index 051f691..011e475 100644 --- a/include/nfc/nfc.h +++ b/include/nfc/nfc.h @@ -63,10 +63,11 @@ extern "C" { # endif // __cplusplus /* NFC Device/Hardware manipulation */ - NFC_EXPORT nfc_device_t *nfc_connect (nfc_device_desc_t * pndd); + NFC_EXPORT bool nfc_get_default_device (nfc_connstring *connstring); + NFC_EXPORT nfc_device_t *nfc_connect (const nfc_connstring connstring); NFC_EXPORT void nfc_disconnect (nfc_device_t * pnd); NFC_EXPORT bool nfc_abort_command (nfc_device_t * pnd); - NFC_EXPORT void nfc_list_devices (nfc_device_desc_t pnddDevices[], size_t szDevices, size_t * pszDeviceFound); + NFC_EXPORT void nfc_list_devices (nfc_connstring connstrings[], size_t connstrings_len, size_t * pszDeviceFound); NFC_EXPORT bool nfc_configure (nfc_device_t * pnd, const nfc_device_option_t ndo, const bool bEnable); NFC_EXPORT bool nfc_idle (nfc_device_t * pnd); diff --git a/libnfc/drivers/acr122.c b/libnfc/drivers/acr122.c index 0732275..419f7ad 100644 --- a/libnfc/drivers/acr122.c +++ b/libnfc/drivers/acr122.c @@ -40,7 +40,7 @@ // Bus #include -# define ACR122_DRIVER_NAME "ACR122" +#define ACR122_DRIVER_NAME "acr122" #if defined (_WIN32) # define IOCTL_CCID_ESCAPE_SCARD_CTL_CODE SCARD_CTL_CODE(3500) @@ -133,7 +133,7 @@ acr122_free_scardcontext (void) * @return true if succeeded, false otherwise. */ bool -acr122_probe (nfc_device_desc_t pnddDevices[], size_t szDevices, size_t * pszDeviceFound) +acr122_probe (nfc_connstring connstrings[], size_t connstrings_len, size_t * pszDeviceFound) { size_t szPos = 0; char acDeviceNames[256 + 64 * PCSC_MAX_DEVICES]; @@ -158,9 +158,7 @@ acr122_probe (nfc_device_desc_t pnddDevices[], size_t szDevices, size_t * pszDev if (SCardListReaders (*pscc, NULL, acDeviceNames, &dwDeviceNamesLen) != SCARD_S_SUCCESS) return false; - // DBG("%s", "PCSC reports following device(s):"); - - while ((acDeviceNames[szPos] != '\0') && ((*pszDeviceFound) < szDevices)) { + while ((acDeviceNames[szPos] != '\0') && ((*pszDeviceFound) < connstrings_len)) { uiBusIndex++; // DBG("- %s (pos=%ld)", acDeviceNames + szPos, (unsigned long) szPos); @@ -173,9 +171,7 @@ acr122_probe (nfc_device_desc_t pnddDevices[], size_t szDevices, size_t * pszDev if (bSupported) { // Supported ACR122 device found - strncpy (pnddDevices[*pszDeviceFound].acDevice, acDeviceNames + szPos, DEVICE_NAME_LENGTH - 1); - pnddDevices[*pszDeviceFound].pcDriver = ACR122_DRIVER_NAME; - pnddDevices[*pszDeviceFound].uiBusIndex = uiBusIndex; + snprintf (connstrings[*pszDeviceFound], sizeof(nfc_connstring), "%s:%s:%"PRIu32, ACR122_DRIVER_NAME, acDeviceNames + szPos, uiBusIndex); (*pszDeviceFound)++; } else { log_put (LOG_CATEGORY, NFC_PRIORITY_TRACE, "PCSC device [%s] is not NFC capable or not supported by libnfc.", acDeviceNames + szPos); @@ -189,9 +185,71 @@ acr122_probe (nfc_device_desc_t pnddDevices[], size_t szDevices, size_t * pszDev return true; } -nfc_device_t * -acr122_connect (const nfc_device_desc_t * pndd) +struct acr122_descriptor { + char pcsc_device_name[512]; + int bus_index; +}; + +int +acr122_connstring_decode (const nfc_connstring connstring, struct acr122_descriptor *desc) { + char *cs = malloc (strlen (connstring) + 1); + if (!cs) { + perror ("malloc"); + return -1; + } + strcpy (cs, connstring); + const char *driver_name = strtok (cs, ":"); + if (!driver_name) { + // Parse error + free (cs); + return -1; + } + + if (0 != strcmp (driver_name, ACR122_DRIVER_NAME)) { + // Driver name does not match. + free (cs); + return 0; + } + + const char *device_name = strtok (NULL, ":"); + if (!device_name) { + // Only driver name was specified (or parsing error) + free (cs); + return 1; + } + strncpy (desc->pcsc_device_name, device_name, sizeof(desc->pcsc_device_name)-1); + desc->pcsc_device_name[sizeof(desc->pcsc_device_name)-1] = '\0'; + + const char *bus_index_s = strtok (NULL, ":"); + if (!bus_index_s) { + // bus index not specified (or parsing error) + free (cs); + return 2; + } + unsigned long bus_index; + if (sscanf (bus_index_s, "%lu", &bus_index) != 1) { + // bus_index_s is not a number + free (cs); + return 2; + } + desc->bus_index = bus_index; + + free (cs); + return 3; +} + +nfc_device_t * +acr122_connect (const nfc_connstring connstring) +{ + struct acr122_descriptor ndd; + int connstring_decode_level = acr122_connstring_decode (connstring, &ndd); + + if (connstring_decode_level < 2) { + return NULL; + } + // FIXME: acr122_connect() does not take care about bus index + char *pcFirmware; nfc_device_t *pnd = nfc_device_new (); pnd->driver_data = malloc (sizeof (struct acr122_data)); @@ -200,15 +258,15 @@ acr122_connect (const nfc_device_desc_t * pndd) pn53x_data_new (pnd, &acr122_io); SCARDCONTEXT *pscc; - - log_put (LOG_CATEGORY, NFC_PRIORITY_TRACE, "Attempt to connect to %s", pndd->acDevice); + + log_put (LOG_CATEGORY, NFC_PRIORITY_TRACE, "Attempt to connect to %s", ndd.pcsc_device_name); // Test if context succeeded if (!(pscc = acr122_get_scardcontext ())) goto error; // Test if we were able to connect to the "emulator" card - if (SCardConnect (*pscc, pndd->acDevice, SCARD_SHARE_EXCLUSIVE, SCARD_PROTOCOL_T0 | SCARD_PROTOCOL_T1, &(DRIVER_DATA (pnd)->hCard), (void *) &(DRIVER_DATA (pnd)->ioCard.dwProtocol)) != SCARD_S_SUCCESS) { + if (SCardConnect (*pscc, ndd.pcsc_device_name, SCARD_SHARE_EXCLUSIVE, SCARD_PROTOCOL_T0 | SCARD_PROTOCOL_T1, &(DRIVER_DATA (pnd)->hCard), (void *) &(DRIVER_DATA (pnd)->ioCard.dwProtocol)) != SCARD_S_SUCCESS) { // Connect to ACR122 firmware version >2.0 - if (SCardConnect (*pscc, pndd->acDevice, SCARD_SHARE_DIRECT, 0, &(DRIVER_DATA (pnd)->hCard), (void *) &(DRIVER_DATA (pnd)->ioCard.dwProtocol)) != SCARD_S_SUCCESS) { + if (SCardConnect (*pscc, ndd.pcsc_device_name, SCARD_SHARE_DIRECT, 0, &(DRIVER_DATA (pnd)->hCard), (void *) &(DRIVER_DATA (pnd)->ioCard.dwProtocol)) != SCARD_S_SUCCESS) { // We can not connect to this device. log_put (LOG_CATEGORY, NFC_PRIORITY_TRACE, "%s", "PCSC connect failed"); goto error; @@ -222,7 +280,7 @@ acr122_connect (const nfc_device_desc_t * pndd) if (strstr (pcFirmware, FIRMWARE_TEXT) != NULL) { // Done, we found the reader we are looking for - snprintf (pnd->acName, sizeof (pnd->acName), "%s / %s", pndd->acDevice, pcFirmware); + snprintf (pnd->acName, sizeof (pnd->acName), "%s / %s", ndd.pcsc_device_name, pcFirmware); // 50: empirical tuning on Touchatag // 46: empirical tuning on ACR122U @@ -407,11 +465,11 @@ const struct pn53x_io acr122_io = { }; const struct nfc_driver_t acr122_driver = { - .name = ACR122_DRIVER_NAME, - .probe = acr122_probe, - .connect = acr122_connect, - .disconnect = acr122_disconnect, - .strerror = pn53x_strerror, + .name = ACR122_DRIVER_NAME, + .probe = acr122_probe, + .connect = acr122_connect, + .disconnect = acr122_disconnect, + .strerror = pn53x_strerror, .initiator_init = pn53x_initiator_init, .initiator_select_passive_target = pn53x_initiator_select_passive_target, @@ -431,7 +489,7 @@ const struct nfc_driver_t acr122_driver = { .configure = pn53x_configure, - .abort_command = NULL, - .idle = NULL, + .abort_command = NULL, // FIXME: abort is not supported in this driver + .idle = NULL, // FIXME: idle is not supported in this driver }; diff --git a/libnfc/drivers/acr122.h b/libnfc/drivers/acr122.h index db05e2f..0a2c087 100644 --- a/libnfc/drivers/acr122.h +++ b/libnfc/drivers/acr122.h @@ -26,10 +26,10 @@ # include -bool acr122_probe (nfc_device_desc_t pnddDevices[], size_t szDevices, size_t * pszDeviceFound); +bool acr122_probe (nfc_connstring connstrings[], size_t connstrings_len, size_t * pszDeviceFound); // Functions used by developer to handle connection to this device -nfc_device_t *acr122_connect (const nfc_device_desc_t * pndd); +nfc_device_t *acr122_connect (const nfc_connstring connstring); bool acr122_send (nfc_device_t * pnd, const byte_t * pbtData, const size_t szData, struct timeval *timeout); int acr122_receive (nfc_device_t * pnd, byte_t * pbtData, const size_t szData, struct timeval *timeout); void acr122_disconnect (nfc_device_t * pnd); diff --git a/libnfc/drivers/arygon.c b/libnfc/drivers/arygon.c index 537ff29..2ff78c6 100644 --- a/libnfc/drivers/arygon.c +++ b/libnfc/drivers/arygon.c @@ -26,6 +26,9 @@ * This driver can handle ARYGON readers that use UART as bus. * UART connection can be direct (host<->arygon_uc) or could be provided by internal USB to serial interface (e.g. host<->ftdi_chip<->arygon_uc) */ + +/* vim: set ts=2 sw=2 et: */ + #ifdef HAVE_CONFIG_H # include "config.h" #endif // HAVE_CONFIG_H @@ -33,6 +36,7 @@ #include "arygon.h" #include +#include #include #include #include @@ -64,7 +68,7 @@ #define DEV_ARYGON_PROTOCOL_TAMA_WAB '3' #define ARYGON_DEFAULT_SPEED 9600 -#define ARYGON_DRIVER_NAME "ARYGON" +#define ARYGON_DRIVER_NAME "arygon" #define LOG_CATEGORY "libnfc.driver.arygon" #define DRIVER_DATA(pnd) ((struct arygon_data*)(pnd->driver_data)) @@ -88,16 +92,16 @@ bool arygon_reset_tama (nfc_device_t * pnd); void arygon_firmware (nfc_device_t * pnd, char * str); bool -arygon_probe (nfc_device_desc_t pnddDevices[], size_t szDevices, size_t * pszDeviceFound) +arygon_probe (nfc_connstring connstrings[], size_t connstrings_len, size_t * pszDeviceFound) { /** @note: Due to UART bus we can't know if its really an ARYGON without * sending some commands. But using this way to probe devices, we can * have serious problem with other device on this bus */ #ifndef SERIAL_AUTOPROBE_ENABLED - (void) pnddDevices; - (void) szDevices; + (void) connstrings; + (void) connstrings_len; *pszDeviceFound = 0; - log_put (LOG_CATEGORY, NFC_PRIORITY_TRACE, "%s", "Serial auto-probing have been disabled at compile time. Skipping autoprobe."); + log_put (LOG_CATEGORY, NFC_PRIORITY_INFO, "Serial auto-probing have been disabled at compile time. Skipping autoprobe."); return false; #else /* SERIAL_AUTOPROBE_ENABLED */ *pszDeviceFound = 0; @@ -135,24 +139,18 @@ arygon_probe (nfc_device_desc_t pnddDevices[], size_t szDevices, size_t * pszDev pn53x_data_free (pnd); nfc_device_free (pnd); uart_close (sp); - if(!res) + if(!res) { continue; + } // ARYGON reader is found - snprintf (pnddDevices[*pszDeviceFound].acDevice, DEVICE_NAME_LENGTH - 1, "%s (%s)", "Arygon", acPort); - pnddDevices[*pszDeviceFound].pcDriver = ARYGON_DRIVER_NAME; - strncpy (pnddDevices[*pszDeviceFound].acPort, acPort, DEVICE_PORT_LENGTH - 1); pnddDevices[*pszDeviceFound].acPort[DEVICE_PORT_LENGTH - 1] = '\0'; - pnddDevices[*pszDeviceFound].uiSpeed = ARYGON_DEFAULT_SPEED; + snprintf (connstrings[*pszDeviceFound], sizeof(nfc_connstring), "%s:%s:%"PRIu32, ARYGON_DRIVER_NAME, acPort, ARYGON_DEFAULT_SPEED); (*pszDeviceFound)++; // Test if we reach the maximum "wanted" devices - if ((*pszDeviceFound) >= szDevices) + if ((*pszDeviceFound) >= connstrings_len) break; } - if (sp == INVALID_SERIAL_PORT) - log_put (LOG_CATEGORY, NFC_PRIORITY_TRACE, "Invalid serial port: %s", acPort); - if (sp == CLAIMED_SERIAL_PORT) - log_put (LOG_CATEGORY, NFC_PRIORITY_TRACE, "Serial port already claimed: %s", acPort); } iDevice = 0; while ((acPort = acPorts[iDevice++])) { @@ -163,33 +161,96 @@ arygon_probe (nfc_device_desc_t pnddDevices[], size_t szDevices, size_t * pszDev return true; } -nfc_device_t * -arygon_connect (const nfc_device_desc_t * pndd) +struct arygon_descriptor { + char port[128]; + uint32_t speed; +}; + +int +arygon_connstring_decode (const nfc_connstring connstring, struct arygon_descriptor *desc) { + char *cs = malloc (strlen (connstring) + 1); + if (!cs) { + perror ("malloc"); + return -1; + } + strcpy (cs, connstring); + const char *driver_name = strtok (cs, ":"); + if (!driver_name) { + // Parse error + free (cs); + return -1; + } + + if (0 != strcmp (driver_name, ARYGON_DRIVER_NAME)) { + // Driver name does not match. + free (cs); + return 0; + } + + const char *port = strtok (NULL, ":"); + if (!port) { + // Only driver name was specified (or parsing error) + free (cs); + return 1; + } + strncpy (desc->port, port, sizeof(desc->port)-1); + desc->port[sizeof(desc->port)-1] = '\0'; + + const char* speed_s = strtok (NULL, ":"); + if (!speed_s) { + // speed not specified (or parsing error) + free (cs); + return 2; + } + unsigned long speed; + if (sscanf (speed_s, "%lu", &speed) != 1) { + // speed_s is not a number + free (cs); + return 2; + } + desc->speed = speed; + + free (cs); + return 3; +} + +nfc_device_t * +arygon_connect (const nfc_connstring connstring) +{ + struct arygon_descriptor ndd; + int connstring_decode_level = arygon_connstring_decode (connstring, &ndd); + + if (connstring_decode_level < 2) { + return NULL; + } + if (connstring_decode_level < 3) { + ndd.speed = ARYGON_DEFAULT_SPEED; + } serial_port sp; nfc_device_t *pnd = NULL; - log_put (LOG_CATEGORY, NFC_PRIORITY_TRACE, "Attempt to connect to: %s at %d bauds.", pndd->acPort, pndd->uiSpeed); - sp = uart_open (pndd->acPort); + log_put (LOG_CATEGORY, NFC_PRIORITY_TRACE, "Attempt to connect to: %s at %d bauds.", ndd.port, ndd.speed); + sp = uart_open (ndd.port); if (sp == INVALID_SERIAL_PORT) - log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "Invalid serial port: %s", pndd->acPort); + log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "Invalid serial port: %s", ndd.port); if (sp == CLAIMED_SERIAL_PORT) - log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "Serial port already claimed: %s", pndd->acPort); + log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "Serial port already claimed: %s", ndd.port); if ((sp == CLAIMED_SERIAL_PORT) || (sp == INVALID_SERIAL_PORT)) return NULL; // We need to flush input to be sure first reply does not comes from older byte transceive uart_flush_input (sp); - uart_set_speed (sp, pndd->uiSpeed); + uart_set_speed (sp, ndd.speed); // We have a connection pnd = nfc_device_new (); - strncpy (pnd->acName, pndd->acDevice, sizeof (pnd->acName)); + snprintf (pnd->acName, sizeof (pnd->acName), "%s:%s", ARYGON_DRIVER_NAME, ndd.port); pnd->driver_data = malloc(sizeof(struct arygon_data)); DRIVER_DATA (pnd)->port = sp; - + // Alloc and init chip's data pn53x_data_new (pnd, &arygon_tama_io); @@ -209,7 +270,7 @@ arygon_connect (const nfc_device_desc_t * pndd) // Check communication using "Reset TAMA" command if (!arygon_reset_tama(pnd)) { - nfc_device_free (pnd); + arygon_disconnect (pnd); return NULL; } @@ -314,7 +375,7 @@ arygon_tama_receive (nfc_device_t * pnd, byte_t * pbtData, const size_t szDataLe #ifndef WIN32 abort_p = &(DRIVER_DATA (pnd)->iAbortFds[1]); #else - abort_p = &(DRIVER_DATA (pnd)->abort_flag); + abort_p = (void*)&(DRIVER_DATA (pnd)->abort_flag); #endif pnd->iLastError = uart_receive (DRIVER_DATA (pnd)->port, abtRxBuf, 5, abort_p, timeout); @@ -500,11 +561,11 @@ const struct pn53x_io arygon_tama_io = { }; const struct nfc_driver_t arygon_driver = { - .name = ARYGON_DRIVER_NAME, - .probe = arygon_probe, - .connect = arygon_connect, - .disconnect = arygon_disconnect, - .strerror = pn53x_strerror, + .name = ARYGON_DRIVER_NAME, + .probe = arygon_probe, + .connect = arygon_connect, + .disconnect = arygon_disconnect, + .strerror = pn53x_strerror, .initiator_init = pn53x_initiator_init, .initiator_select_passive_target = pn53x_initiator_select_passive_target, @@ -525,7 +586,6 @@ const struct nfc_driver_t arygon_driver = { .configure = pn53x_configure, .abort_command = arygon_abort_command, - // FIXME Implement me - .idle = NULL, + .idle = NULL, // FIXME arygon driver does not support idle() }; diff --git a/libnfc/drivers/arygon.h b/libnfc/drivers/arygon.h index 11318b8..6ef6ff3 100644 --- a/libnfc/drivers/arygon.h +++ b/libnfc/drivers/arygon.h @@ -30,9 +30,9 @@ # include -bool arygon_probe (nfc_device_desc_t pnddDevices[], size_t szDevices, size_t * pszDeviceFound); +bool arygon_probe (nfc_connstring connstrings[], size_t connstrings_len, size_t * pszDeviceFound); -nfc_device_t *arygon_connect (const nfc_device_desc_t * pndd); +nfc_device_t *arygon_connect (const nfc_connstring connstring); void arygon_disconnect (nfc_device_t * pnd); bool arygon_tama_send (nfc_device_t * pnd, const byte_t * pbtData, const size_t szData, struct timeval *timeout); diff --git a/libnfc/drivers/pn532_uart.c b/libnfc/drivers/pn532_uart.c index 83c2a80..688323d 100644 --- a/libnfc/drivers/pn532_uart.c +++ b/libnfc/drivers/pn532_uart.c @@ -32,6 +32,7 @@ #include "pn532_uart.h" #include +#include #include #include @@ -44,7 +45,7 @@ #include "uart.h" #define PN532_UART_DEFAULT_SPEED 115200 -#define PN532_UART_DRIVER_NAME "PN532_UART" +#define PN532_UART_DRIVER_NAME "pn532_uart" #define LOG_CATEGORY "libnfc.driver.pn532_uart" int pn532_uart_ack (nfc_device_t * pnd); @@ -64,14 +65,14 @@ struct pn532_uart_data { #define DRIVER_DATA(pnd) ((struct pn532_uart_data*)(pnd->driver_data)) bool -pn532_uart_probe (nfc_device_desc_t pnddDevices[], size_t szDevices, size_t * pszDeviceFound) +pn532_uart_probe (nfc_connstring connstrings[], size_t connstrings_len, size_t * pszDeviceFound) { /** @note: Due to UART bus we can't know if its really a pn532 without * sending some PN53x commands. But using this way to probe devices, we can * have serious problem with other device on this bus */ #ifndef SERIAL_AUTOPROBE_ENABLED - (void) pnddDevices; - (void) szDevices; + (void) connstrings; + (void) connstrings_len; *pszDeviceFound = 0; log_put (LOG_CATEGORY, NFC_PRIORITY_INFO, "Serial auto-probing have been disabled at compile time. Skipping autoprobe."); return false; @@ -124,18 +125,14 @@ pn532_uart_probe (nfc_device_desc_t pnddDevices[], size_t szDevices, size_t * ps continue; } - snprintf (pnddDevices[*pszDeviceFound].acDevice, DEVICE_NAME_LENGTH - 1, "%s (%s)", "PN532", acPort); - pnddDevices[*pszDeviceFound].pcDriver = PN532_UART_DRIVER_NAME; - strncpy (pnddDevices[*pszDeviceFound].acPort, acPort, DEVICE_PORT_LENGTH - 1); pnddDevices[*pszDeviceFound].acPort[DEVICE_PORT_LENGTH - 1] = '\0'; - pnddDevices[*pszDeviceFound].uiSpeed = PN532_UART_DEFAULT_SPEED; + snprintf (connstrings[*pszDeviceFound], sizeof(nfc_connstring), "%s:%s:%"PRIu32, PN532_UART_DRIVER_NAME, acPort, PN532_UART_DEFAULT_SPEED); (*pszDeviceFound)++; // Test if we reach the maximum "wanted" devices - if ((*pszDeviceFound) >= szDevices) + if ((*pszDeviceFound) >= connstrings_len) break; } } - iDevice = 0; while ((acPort = acPorts[iDevice++])) { free ((void*)acPort); @@ -145,33 +142,96 @@ pn532_uart_probe (nfc_device_desc_t pnddDevices[], size_t szDevices, size_t * ps return true; } -nfc_device_t * -pn532_uart_connect (const nfc_device_desc_t * pndd) +struct pn532_uart_descriptor { + char port[128]; + uint32_t speed; +}; + +int +pn532_connstring_decode (const nfc_connstring connstring, struct pn532_uart_descriptor *desc) { + char *cs = malloc (strlen (connstring) + 1); + if (!cs) { + perror ("malloc"); + return -1; + } + strcpy (cs, connstring); + const char *driver_name = strtok (cs, ":"); + if (!driver_name) { + // Parse error + free (cs); + return -1; + } + + if (0 != strcmp (driver_name, PN532_UART_DRIVER_NAME)) { + // Driver name does not match. + free (cs); + return 0; + } + + const char *port = strtok (NULL, ":"); + if (!port) { + // Only driver name was specified (or parsing error) + free (cs); + return 1; + } + strncpy (desc->port, port, sizeof(desc->port)-1); + desc->port[sizeof(desc->port)-1] = '\0'; + + const char* speed_s = strtok (NULL, ":"); + if (!speed_s) { + // speed not specified (or parsing error) + free (cs); + return 2; + } + unsigned long speed; + if (sscanf (speed_s, "%lu", &speed) != 1) { + // speed_s is not a number + free (cs); + return 2; + } + desc->speed = speed; + + free (cs); + return 3; +} + +nfc_device_t * +pn532_uart_connect (const nfc_connstring connstring) +{ + struct pn532_uart_descriptor ndd; + int connstring_decode_level = pn532_connstring_decode (connstring, &ndd); + + if (connstring_decode_level < 2) { + return NULL; + } + if (connstring_decode_level < 3) { + ndd.speed = PN532_UART_DEFAULT_SPEED; + } serial_port sp; nfc_device_t *pnd = NULL; - log_put (LOG_CATEGORY, NFC_PRIORITY_TRACE, "Attempt to connect to: %s at %d bauds.", pndd->acPort, pndd->uiSpeed); - sp = uart_open (pndd->acPort); + log_put (LOG_CATEGORY, NFC_PRIORITY_TRACE, "Attempt to connect to: %s at %d bauds.", ndd.port, ndd.speed); + sp = uart_open (ndd.port); if (sp == INVALID_SERIAL_PORT) - log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "Invalid serial port: %s", pndd->acPort); + log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "Invalid serial port: %s", ndd.port); if (sp == CLAIMED_SERIAL_PORT) - log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "Serial port already claimed: %s", pndd->acPort); + log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "Serial port already claimed: %s", ndd.port); if ((sp == CLAIMED_SERIAL_PORT) || (sp == INVALID_SERIAL_PORT)) return NULL; // We need to flush input to be sure first reply does not comes from older byte transceive uart_flush_input (sp); - uart_set_speed (sp, pndd->uiSpeed); + uart_set_speed (sp, ndd.speed); // We have a connection pnd = nfc_device_new (); - strncpy (pnd->acName, pndd->acDevice, DEVICE_NAME_LENGTH - 1); - pnd->acName[DEVICE_NAME_LENGTH - 1] = '\0'; + snprintf (pnd->acName, sizeof (pnd->acName), "%s:%s", PN532_UART_DRIVER_NAME, ndd.port); pnd->driver_data = malloc(sizeof(struct pn532_uart_data)); - DRIVER_DATA(pnd)->port = sp; + DRIVER_DATA (pnd)->port = sp; + // Alloc and init chip's data pn53x_data_new (pnd, &pn532_uart_io); // SAMConfiguration command if needed to wakeup the chip and pn53x_SAMConfiguration check if the chip is a PN532 @@ -193,7 +253,7 @@ pn532_uart_connect (const nfc_device_desc_t * pndd) // Check communication using "Diagnose" command, with "Communication test" (0x00) if (!pn53x_check_communication (pnd)) { nfc_perror (pnd, "pn53x_check_communication"); - pn532_uart_disconnect(pnd); + pn532_uart_disconnect (pnd); return NULL; } @@ -304,7 +364,7 @@ pn532_uart_receive (nfc_device_t * pnd, byte_t * pbtData, const size_t szDataLen abort_p = (void*)&(DRIVER_DATA (pnd)->abort_flag); #endif - pnd->iLastError = uart_receive (DRIVER_DATA(pnd)->port, abtRxBuf, 5, abort_p, timeout); + pnd->iLastError = uart_receive (DRIVER_DATA (pnd)->port, abtRxBuf, 5, abort_p, timeout); if (abort_p && (EOPABORT == pnd->iLastError)) { pn532_uart_ack (pnd); @@ -325,7 +385,7 @@ pn532_uart_receive (nfc_device_t * pnd, byte_t * pbtData, const size_t szDataLen if ((0x01 == abtRxBuf[3]) && (0xff == abtRxBuf[4])) { // Error frame - uart_receive (DRIVER_DATA(pnd)->port, abtRxBuf, 3, 0, timeout); + uart_receive (DRIVER_DATA (pnd)->port, abtRxBuf, 3, 0, timeout); log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Application level error detected"); pnd->iLastError = EFRAISERRFRAME; return -1; @@ -362,7 +422,7 @@ pn532_uart_receive (nfc_device_t * pnd, byte_t * pbtData, const size_t szDataLen } // TFI + PD0 (CC+1) - pnd->iLastError = uart_receive (DRIVER_DATA(pnd)->port, abtRxBuf, 2, 0, timeout); + pnd->iLastError = uart_receive (DRIVER_DATA (pnd)->port, abtRxBuf, 2, 0, timeout); if (pnd->iLastError != 0) { log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Unable to receive data. (RX)"); return -1; @@ -381,14 +441,14 @@ pn532_uart_receive (nfc_device_t * pnd, byte_t * pbtData, const size_t szDataLen } if (len) { - pnd->iLastError = uart_receive (DRIVER_DATA(pnd)->port, pbtData, len, 0, timeout); + pnd->iLastError = uart_receive (DRIVER_DATA (pnd)->port, pbtData, len, 0, timeout); if (pnd->iLastError != 0) { log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Unable to receive data. (RX)"); return -1; } } - pnd->iLastError = uart_receive (DRIVER_DATA(pnd)->port, abtRxBuf, 2, 0, timeout); + pnd->iLastError = uart_receive (DRIVER_DATA (pnd)->port, abtRxBuf, 2, 0, timeout); if (pnd->iLastError != 0) { log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Unable to receive data. (RX)"); return -1; @@ -446,11 +506,11 @@ const struct pn53x_io pn532_uart_io = { }; const struct nfc_driver_t pn532_uart_driver = { - .name = PN532_UART_DRIVER_NAME, - .probe = pn532_uart_probe, - .connect = pn532_uart_connect, - .disconnect = pn532_uart_disconnect, - .strerror = pn53x_strerror, + .name = PN532_UART_DRIVER_NAME, + .probe = pn532_uart_probe, + .connect = pn532_uart_connect, + .disconnect = pn532_uart_disconnect, + .strerror = pn53x_strerror, .initiator_init = pn53x_initiator_init, .initiator_select_passive_target = pn53x_initiator_select_passive_target, diff --git a/libnfc/drivers/pn532_uart.h b/libnfc/drivers/pn532_uart.h index 5ecb21c..5f8f344 100644 --- a/libnfc/drivers/pn532_uart.h +++ b/libnfc/drivers/pn532_uart.h @@ -29,9 +29,9 @@ # include -bool pn532_uart_probe (nfc_device_desc_t pnddDevices[], size_t szDevices, size_t * pszDeviceFound); +bool pn532_uart_probe (nfc_connstring connstrings[], size_t connstrings_len, size_t * pszDeviceFound); -nfc_device_t *pn532_uart_connect (const nfc_device_desc_t * pndd); +nfc_device_t *pn532_uart_connect (const nfc_connstring connstring); void pn532_uart_disconnect (nfc_device_t * pnd); bool pn532_uart_send (nfc_device_t * pnd, const byte_t * pbtData, const size_t szData, struct timeval *timeout); int pn532_uart_receive (nfc_device_t * pnd, byte_t * pbtData, const size_t szData, struct timeval *timeout); diff --git a/libnfc/drivers/pn53x_usb.c b/libnfc/drivers/pn53x_usb.c index f4f3e81..aae9203 100644 --- a/libnfc/drivers/pn53x_usb.c +++ b/libnfc/drivers/pn53x_usb.c @@ -32,10 +32,11 @@ Thanks to d18c7db and Okko for example code */ -#include -#include #include #include +#include +#include +#include #ifndef _WIN32 // Under POSIX system, we use libusb (>= 0.1.12) @@ -58,7 +59,7 @@ Thanks to d18c7db and Okko for example code #include "chips/pn53x-internal.h" #include "drivers/pn53x_usb.h" -#define PN53X_USB_DRIVER_NAME "PN53x USB" +#define PN53X_USB_DRIVER_NAME "pn53x_usb" #define LOG_CATEGORY "libnfc.driver.pn53x_usb" #define USB_INFINITE_TIMEOUT 0 @@ -222,7 +223,7 @@ pn53x_usb_get_end_points (struct usb_device *dev, struct pn53x_usb_data *data) } bool -pn53x_usb_probe (nfc_device_desc_t pnddDevices[], size_t szDevices, size_t * pszDeviceFound) +pn53x_usb_probe (nfc_connstring connstrings[], size_t connstrings_len, size_t * pszDeviceFound) { usb_init (); @@ -275,13 +276,13 @@ pn53x_usb_probe (nfc_device_desc_t pnddDevices[], size_t szDevices, size_t * psz continue; } - pn53x_usb_get_usb_device_name (dev, udev, pnddDevices[*pszDeviceFound].acDevice, sizeof (pnddDevices[*pszDeviceFound].acDevice)); + // pn53x_usb_get_usb_device_name (dev, udev, pnddDevices[*pszDeviceFound].acDevice, sizeof (pnddDevices[*pszDeviceFound].acDevice)); + log_put (LOG_CATEGORY, NFC_PRIORITY_TRACE, "device found: Bus %s Device %s", bus->dirname, dev->filename); usb_close (udev); - pnddDevices[*pszDeviceFound].pcDriver = PN53X_USB_DRIVER_NAME; - pnddDevices[*pszDeviceFound].uiBusIndex = uiBusIndex; + snprintf (connstrings[*pszDeviceFound], sizeof(nfc_connstring), "%s:%s:%s", PN53X_USB_DRIVER_NAME, bus->dirname, dev->filename); (*pszDeviceFound)++; // Test if we reach the maximum "wanted" devices - if ((*pszDeviceFound) == szDevices) { + if ((*pszDeviceFound) == connstrings_len) { return true; } } @@ -292,6 +293,64 @@ pn53x_usb_probe (nfc_device_desc_t pnddDevices[], size_t szDevices, size_t * psz return true; } +struct pn53x_usb_descriptor { + uint16_t bus; + uint16_t dev; +}; + +int +pn53x_usb_connstring_decode (const nfc_connstring connstring, struct pn53x_usb_descriptor *desc) +{ + char *cs = malloc (strlen (connstring) + 1); + if (!cs) { + perror ("malloc"); + return -1; + } + strcpy (cs, connstring); + const char *driver_name = strtok (cs, ":"); + if (!driver_name) { + // Parse error + free (cs); + return -1; + } + + if (0 != strcmp (driver_name, PN53X_USB_DRIVER_NAME)) { + // Driver name does not match. + free (cs); + return 0; + } + + const char *bus_s = strtok (NULL, ":"); + if (!bus_s) { + // bus not specified (or parsing error) + free (cs); + return 1; + } + unsigned int bus; + if (sscanf (bus_s, "%u", &bus) != 1) { + // bus_s is not a number + free (cs); + return 1; + } + desc->bus = bus; + + const char *dev_s = strtok (NULL, ":"); + if (!dev_s) { + // dev not specified (or parsing error) + free (cs); + return 2; + } + unsigned int dev; + if (sscanf (dev_s, "%u", &dev) != 1) { + // dev_s is not a number + free (cs); + return 2; + } + desc->dev = dev; + free (cs); + return 3; +} + bool pn53x_usb_get_usb_device_name (struct usb_device *dev, usb_dev_handle *udev, char *buffer, size_t len) { @@ -320,8 +379,15 @@ pn53x_usb_get_usb_device_name (struct usb_device *dev, usb_dev_handle *udev, cha } nfc_device_t * -pn53x_usb_connect (const nfc_device_desc_t *pndd) +pn53x_usb_connect (const nfc_connstring connstring) { + struct pn53x_usb_descriptor desc; + int connstring_decode_level = pn53x_usb_connstring_decode (connstring, &desc); + log_put (LOG_CATEGORY, NFC_PRIORITY_TRACE, "%d element(s) have been decoded from \"%s\"", connstring_decode_level, connstring); + if (connstring_decode_level < 1) { + return NULL; + } + nfc_device_t *pnd = NULL; struct pn53x_usb_data data = { .pudh = NULL, @@ -330,82 +396,90 @@ pn53x_usb_connect (const nfc_device_desc_t *pndd) }; struct usb_bus *bus; struct usb_device *dev; - uint32_t uiBusIndex; usb_init (); - uiBusIndex = pndd->uiBusIndex; - for (bus = usb_get_busses (); bus; bus = bus->next) { - for (dev = bus->devices; dev; dev = dev->next, uiBusIndex--) { - log_put (LOG_CATEGORY, NFC_PRIORITY_TRACE, "Checking device %04x:%04x", dev->descriptor.idVendor, dev->descriptor.idProduct); - if (uiBusIndex == 0) { - // Open the USB device - data.pudh = usb_open (dev); - // Retrieve end points - pn53x_usb_get_end_points (dev, &data); - // Set configuration - int res = usb_set_configuration (data.pudh, 1); - if (res < 0) { - log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "Unable to set USB configuration (%s)", _usb_strerror (res)); - if (EPERM == -res) { - log_put (LOG_CATEGORY, NFC_PRIORITY_WARN, "Please double check USB permissions for device %04x:%04x", dev->descriptor.idVendor, dev->descriptor.idProduct); - } - usb_close (data.pudh); - // we failed to use the specified device - return NULL; - } - - res = usb_claim_interface (data.pudh, 0); - if (res < 0) { - log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "Unable to claim USB interface (%s)", _usb_strerror (res)); - usb_close (data.pudh); - // we failed to use the specified device - return NULL; - } - data.model = pn53x_usb_get_device_model (dev->descriptor.idVendor, dev->descriptor.idProduct); - // Allocate memory for the device info and specification, fill it and return the info - pnd = nfc_device_new (); - pn53x_usb_get_usb_device_name (dev, data.pudh, pnd->acName, sizeof (pnd->acName)); - - pnd->driver_data = malloc(sizeof(struct pn53x_usb_data)); - *DRIVER_DATA (pnd) = data; - - // Alloc and init chip's data - pn53x_data_new (pnd, &pn53x_usb_io); - - switch (DRIVER_DATA (pnd)->model) { - // empirical tuning - case ASK_LOGO: - CHIP_DATA (pnd)->timer_correction = 50; - break; - case SCM_SCL3711: - case NXP_PN533: - CHIP_DATA (pnd)->timer_correction = 46; - break; - case NXP_PN531: - CHIP_DATA (pnd)->timer_correction = 50; - break; - case SONY_PN531: - CHIP_DATA (pnd)->timer_correction = 54; - break; - default: - break; - } - pnd->driver = &pn53x_usb_driver; - - // HACK1: Send first an ACK as Abort command, to reset chip before talking to it: - pn53x_usb_ack (pnd); - - // HACK2: Then send a GetFirmware command to resync USB toggle bit between host & device - // in case host used set_configuration and expects the device to have reset its toggle bit, which PN53x doesn't do - if (!pn53x_usb_init (pnd)) { - usb_close (data.pudh); - goto error; - } - DRIVER_DATA (pnd)->abort_flag = false; - return pnd; + if (connstring_decode_level > 1) { + // A specific bus have been specified + unsigned int bus_current; + sscanf (bus->dirname, "%u", &bus_current); + if (bus_current != desc.bus) + continue; + } + for (dev = bus->devices; dev; dev = dev->next) { + if (connstring_decode_level > 2) { + // A specific dev have been specified + unsigned int dev_current; + sscanf (dev->filename, "%u", &dev_current); + if (dev_current != desc.dev) + continue; } + // Open the USB device + data.pudh = usb_open (dev); + // Retrieve end points + pn53x_usb_get_end_points (dev, &data); + // Set configuration + int res = usb_set_configuration (data.pudh, 1); + if (res < 0) { + log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "Unable to set USB configuration (%s)", _usb_strerror (res)); + if (EPERM == -res) { + log_put (LOG_CATEGORY, NFC_PRIORITY_WARN, "Please double check USB permissions for device %04x:%04x", dev->descriptor.idVendor, dev->descriptor.idProduct); + } + usb_close (data.pudh); + // we failed to use the specified device + return NULL; + } + + res = usb_claim_interface (data.pudh, 0); + if (res < 0) { + log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "Unable to claim USB interface (%s)", _usb_strerror (res)); + usb_close (data.pudh); + // we failed to use the specified device + return NULL; + } + data.model = pn53x_usb_get_device_model (dev->descriptor.idVendor, dev->descriptor.idProduct); + // Allocate memory for the device info and specification, fill it and return the info + pnd = nfc_device_new (); + pn53x_usb_get_usb_device_name (dev, data.pudh, pnd->acName, sizeof (pnd->acName)); + + pnd->driver_data = malloc(sizeof(struct pn53x_usb_data)); + *DRIVER_DATA (pnd) = data; + + // Alloc and init chip's data + pn53x_data_new (pnd, &pn53x_usb_io); + + switch (DRIVER_DATA (pnd)->model) { + // empirical tuning + case ASK_LOGO: + CHIP_DATA (pnd)->timer_correction = 50; + break; + case SCM_SCL3711: + case NXP_PN533: + CHIP_DATA (pnd)->timer_correction = 46; + break; + case NXP_PN531: + CHIP_DATA (pnd)->timer_correction = 50; + break; + case SONY_PN531: + CHIP_DATA (pnd)->timer_correction = 54; + break; + default: + break; + } + pnd->driver = &pn53x_usb_driver; + + // HACK1: Send first an ACK as Abort command, to reset chip before talking to it: + pn53x_usb_ack (pnd); + + // HACK2: Then send a GetFirmware command to resync USB toggle bit between host & device + // in case host used set_configuration and expects the device to have reset its toggle bit, which PN53x doesn't do + if (!pn53x_usb_init (pnd)) { + usb_close (data.pudh); + goto error; + } + DRIVER_DATA (pnd)->abort_flag = false; + return pnd; } } // We ran out of devices before the index required @@ -744,11 +818,11 @@ const struct pn53x_io pn53x_usb_io = { }; const struct nfc_driver_t pn53x_usb_driver = { - .name = PN53X_USB_DRIVER_NAME, - .probe = pn53x_usb_probe, - .connect = pn53x_usb_connect, - .disconnect = pn53x_usb_disconnect, - .strerror = pn53x_strerror, + .name = PN53X_USB_DRIVER_NAME, + .probe = pn53x_usb_probe, + .connect = pn53x_usb_connect, + .disconnect = pn53x_usb_disconnect, + .strerror = pn53x_strerror, .initiator_init = pn53x_initiator_init, .initiator_select_passive_target = pn53x_initiator_select_passive_target, diff --git a/libnfc/drivers/pn53x_usb.h b/libnfc/drivers/pn53x_usb.h index b2bff4c..2450ea5 100644 --- a/libnfc/drivers/pn53x_usb.h +++ b/libnfc/drivers/pn53x_usb.h @@ -29,8 +29,8 @@ # include -bool pn53x_usb_probe (nfc_device_desc_t pnddDevices[], size_t szDevices, size_t * pszDeviceFound); -nfc_device_t *pn53x_usb_connect (const nfc_device_desc_t * pndd); +bool pn53x_usb_probe (nfc_connstring connstrings[], size_t connstrings_len, size_t * pszDeviceFound); +nfc_device_t *pn53x_usb_connect (const nfc_connstring connstring); bool pn53x_usb_send (nfc_device_t * pnd, const byte_t * pbtData, const size_t szData, struct timeval *timeout); int pn53x_usb_receive (nfc_device_t * pnd, byte_t * pbtData, const size_t szData, struct timeval *timeout); void pn53x_usb_disconnect (nfc_device_t * pnd); diff --git a/libnfc/nfc-internal.h b/libnfc/nfc-internal.h index 9abc8ba..425c341 100644 --- a/libnfc/nfc-internal.h +++ b/libnfc/nfc-internal.h @@ -126,10 +126,10 @@ struct nfc_driver_t { const char *name; - bool (*probe)(nfc_device_desc_t pnddDevices[], size_t szDevices, size_t * pszDeviceFound); - nfc_device_t * (*connect)(const nfc_device_desc_t * pndd); - void (*disconnect)(nfc_device_t * pnd); - const char *(*strerror)(const nfc_device_t * pnd); + bool (*probe)(nfc_connstring connstrings[], size_t connstrings_len, size_t * pszDeviceFound); + nfc_device_t * (*connect) (const nfc_connstring connstring); + void (*disconnect) (nfc_device_t * pnd); + const char *(*strerror) (const nfc_device_t * pnd); bool (*initiator_init) (nfc_device_t * pnd); bool (*initiator_select_passive_target) (nfc_device_t * pnd, const nfc_modulation_t nm, const byte_t * pbtInitData, const size_t szInitData, nfc_target_t * pnt); diff --git a/libnfc/nfc.c b/libnfc/nfc.c index a6cc66a..5f1810f 100644 --- a/libnfc/nfc.c +++ b/libnfc/nfc.c @@ -61,63 +61,98 @@ const struct nfc_driver_t *nfc_drivers[] = { NULL }; +/** + * @brief Get the defaut NFC device + * @param connstring \a nfc_connstring pointer where the default connection string will be stored + * @return \e true on success + * + * This function fill \e connstring with the LIBNFC_DEFAULT_DEVICE environment variable content + * if is set otherwise it will search for the first available device, and fill + * \e connstring with the corresponding \a nfc_connstring value. + * + * This function returns true when LIBNFC_DEFAULT_DEVICE is set or an available device is found. + * + * @note The \e connstring content can be invalid if LIBNFC_DEFAULT_DEVICE is + * set with incorrect value. + */ +bool +nfc_get_default_device (nfc_connstring *connstring) +{ + char * env_default_connstring = getenv ("LIBNFC_DEFAULT_DEVICE"); + if (NULL == env_default_connstring) { + // LIBNFC_DEFAULT_DEVICE is not set, we fallback on probing for the first available device + size_t szDeviceFound; + nfc_connstring listed_cs[1]; + nfc_list_devices (listed_cs, 1, &szDeviceFound); + if (szDeviceFound) { + strncpy (*connstring, listed_cs[0], sizeof(nfc_connstring)); + } else { + return false; + } + } else { + strncpy (*connstring, env_default_connstring, sizeof(nfc_connstring)); + } + return true; +} + /** * @brief Connect to a NFC device - * @param pndd device description if specific device is wanted, \c NULL otherwise + * @param connstring The device connection string if specific device is wanted, \c NULL otherwise * @return Returns pointer to a \a nfc_device_t struct if successfull; otherwise returns \c NULL value. * - * If \e pndd is \c NULL, the first available NFC device is claimed. - * It will automatically search the system using all available drivers to determine a device is NFC-enabled. + * If \e connstring is \c NULL, the \a nfc_get_default_device() function is used. + * + * If \e connstring is set, this function will try to claim the right device using information provided by \e connstring. * - * If \e pndd is passed then this function will try to claim the right device using information provided by the \a nfc_device_desc_t struct. - * - * When it has successfully claimed a NFC device, memory is allocated to save the device information. It will return a pointer to a \a nfc_device_t struct. + * When it has successfully claimed a NFC device, memory is allocated to save the device information. + * It will return a pointer to a \a nfc_device_t struct. * This pointer should be supplied by every next functions of libnfc that should perform an action with this device. * - * @note Depending on the desired operation mode, the device needs to be configured - * by using nfc_initiator_init() or nfc_target_init(), optionally followed by manual tuning of the parameters if the default parameters are not suiting your goals. + * @note Depending on the desired operation mode, the device needs to be configured by using nfc_initiator_init() or nfc_target_init(), + * optionally followed by manual tuning of the parameters if the default parameters are not suiting your goals. */ nfc_device_t * -nfc_connect (nfc_device_desc_t * pndd) +nfc_connect (const nfc_connstring connstring) { + log_init (); nfc_device_t *pnd = NULL; - if (pndd == NULL) { - size_t szDeviceFound; - nfc_device_desc_t ndd[1]; - nfc_list_devices (ndd, 1, &szDeviceFound); - if (szDeviceFound) { - pndd = &ndd[0]; - } - } - - if (pndd == NULL) + nfc_connstring ncs; + if (connstring == NULL) { + if (!nfc_get_default_device (&ncs)) { + log_fini (); return NULL; - - log_init (); + } + } else { + strncpy (ncs, connstring, sizeof (nfc_connstring)); + } + // Search through the device list for an available device const struct nfc_driver_t *ndr; const struct nfc_driver_t **pndr = nfc_drivers; while ((ndr = *pndr)) { - // Specific device is requested: using device description pndd - if (0 != strcmp (ndr->name, pndd->pcDriver)) { + // Specific device is requested: using device description + if (0 != strncmp (ndr->name, ncs, strlen(ndr->name))) { pndr++; continue; - } else { - pnd = ndr->connect (pndd); } + pnd = ndr->connect (ncs); // Test if the connection was successful - if (pnd != NULL) { - log_put (LOG_CATEGORY, NFC_PRIORITY_TRACE, "[%s] has been claimed.", pnd->acName); + if (pnd == NULL) { + log_put (LOG_CATEGORY, NFC_PRIORITY_TRACE, "Unable to connect to \"%s\".", ncs); + log_fini (); return pnd; - } else { - log_put (LOG_CATEGORY, NFC_PRIORITY_TRACE, "No device found using driver: %s", ndr->name); } - pndr++; + + log_put (LOG_CATEGORY, NFC_PRIORITY_TRACE, "[%s] has been claimed.", pnd->acName); + log_fini (); + return pnd; } + + // Too bad, no driver can decode connstring + log_put (LOG_CATEGORY, NFC_PRIORITY_TRACE, "No driver available to handle \"%s\".", ncs); log_fini (); - // Too bad, no reader is ready to be claimed return NULL; } @@ -147,7 +182,7 @@ nfc_disconnect (nfc_device_t * pnd) * @param[out] pszDeviceFound number of devices found. */ void -nfc_list_devices (nfc_device_desc_t pnddDevices[], size_t szDevices, size_t * pszDeviceFound) +nfc_list_devices (nfc_connstring connstrings[] , size_t szDevices, size_t * pszDeviceFound) { size_t szN; *pszDeviceFound = 0; @@ -157,7 +192,7 @@ nfc_list_devices (nfc_device_desc_t pnddDevices[], size_t szDevices, size_t * ps log_init (); while ((ndr = *pndr)) { szN = 0; - if (ndr->probe (pnddDevices + (*pszDeviceFound), szDevices - (*pszDeviceFound), &szN)) { + if (ndr->probe (connstrings + (*pszDeviceFound), szDevices - (*pszDeviceFound), &szN)) { *pszDeviceFound += szN; log_put (LOG_CATEGORY, NFC_PRIORITY_TRACE, "%ld device(s) found using %s driver", (unsigned long) szN, ndr->name); if (*pszDeviceFound == szDevices) diff --git a/utils/nfc-list.c b/utils/nfc-list.c index 8925f26..342b5f2 100644 --- a/utils/nfc-list.c +++ b/utils/nfc-list.c @@ -62,18 +62,16 @@ static nfc_device_t *pnd; int main (int argc, const char *argv[]) { + (void) argc; const char *acLibnfcVersion; - size_t szDeviceFound; size_t szTargetFound; size_t i; bool verbose = false; - nfc_device_desc_t *pnddDevices; // Display libnfc version acLibnfcVersion = nfc_version (); printf ("%s uses libnfc %s\n", argv[0], acLibnfcVersion); - pnddDevices = parse_args (argc, argv, &szDeviceFound, &verbose); #ifdef HAVE_LIBUSB # ifdef DEBUG usb_set_debug (4); @@ -101,15 +99,9 @@ main (int argc, const char *argv[]) strcpy(ndd.acDevice, "SCM Micro / SCL3711-NFC&RW"); pnd = nfc_connect (&ndd); #endif - - if (szDeviceFound == 0) { - if (!(pnddDevices = malloc (MAX_DEVICE_COUNT * sizeof (*pnddDevices)))) { - fprintf (stderr, "malloc() failed\n"); - return EXIT_FAILURE; - } - - nfc_list_devices (pnddDevices, MAX_DEVICE_COUNT, &szDeviceFound); - } + size_t szDeviceFound; + nfc_connstring connstrings[MAX_DEVICE_COUNT]; + nfc_list_devices (connstrings, MAX_DEVICE_COUNT, &szDeviceFound); if (szDeviceFound == 0) { printf ("No NFC device found.\n"); @@ -117,7 +109,7 @@ main (int argc, const char *argv[]) for (i = 0; i < szDeviceFound; i++) { nfc_target_t ant[MAX_TARGET_COUNT]; - pnd = nfc_connect (&(pnddDevices[i])); + pnd = nfc_connect (connstrings[i]); if (pnd == NULL) { ERR ("%s", "Unable to connect to NFC device."); @@ -243,6 +235,5 @@ main (int argc, const char *argv[]) nfc_disconnect (pnd); } - free (pnddDevices); return 0; } diff --git a/utils/nfc-relay-picc.c b/utils/nfc-relay-picc.c index 614e787..cbf27b7 100644 --- a/utils/nfc-relay-picc.c +++ b/utils/nfc-relay-picc.c @@ -150,7 +150,6 @@ main (int argc, char *argv[]) { int arg; size_t szFound; - nfc_device_desc_t *pnddDevices; const char *acLibnfcVersion = nfc_version (); nfc_target_t ntRealTarget; @@ -192,13 +191,9 @@ main (int argc, char *argv[]) signal (SIGINT, (void (*)()) intr_hdlr); #endif - // Allocate memory to put the result of available devices listing - if (!(pnddDevices = malloc (MAX_DEVICE_COUNT * sizeof (*pnddDevices)))) { - fprintf (stderr, "malloc() failed\n"); - return EXIT_FAILURE; - } + nfc_connstring connstrings[MAX_DEVICE_COUNT]; // List available devices - nfc_list_devices (pnddDevices, MAX_DEVICE_COUNT, &szFound); + nfc_list_devices (connstrings, MAX_DEVICE_COUNT, &szFound); if (initiator_only_mode || target_only_mode) { if (szFound < 1) { @@ -222,9 +217,9 @@ main (int argc, char *argv[]) // if there is more than one readers connected we connect to the second reader // (we hope they're always detected in the same order) if (szFound == 1) { - pndInitiator = nfc_connect (&(pnddDevices[0])); + pndInitiator = nfc_connect (connstrings[0]); } else { - pndInitiator = nfc_connect (&(pnddDevices[1])); + pndInitiator = nfc_connect (connstrings[1]); } if (!pndInitiator) { @@ -348,7 +343,7 @@ main (int argc, char *argv[]) print_nfc_iso14443a_info (ntEmulatedTarget.nti.nai, false); // Try to open the NFC emulator device - pndTarget = nfc_connect (&(pnddDevices[0])); + pndTarget = nfc_connect (connstrings[0]); if (pndTarget == NULL) { printf ("Error connecting NFC emulator device\n"); if (!target_only_mode) { diff --git a/utils/nfc-utils.c b/utils/nfc-utils.c index a3416e4..4321169 100644 --- a/utils/nfc-utils.c +++ b/utils/nfc-utils.c @@ -670,51 +670,6 @@ print_nfc_dep_info (const nfc_dep_info_t ndi, bool verbose) } } -/** - * @brief Tries to parse arguments to find device descriptions. - * @return Returns the list of found device descriptions. - */ -nfc_device_desc_t * -parse_args (int argc, const char *argv[], size_t * szFound, bool * verbose) -{ - nfc_device_desc_t *pndd = 0; - int arg; - *szFound = 0; - - // Get commandline options - for (arg = 1; arg < argc; arg++) { - - if (0 == strcmp (argv[arg], "--device")) { - // FIXME: this device selection by command line options is terrible & does not support USB/PCSC drivers - if (argc > arg + 1) { - char buffer[256]; - - pndd = malloc (sizeof (nfc_device_desc_t)); - - strncpy (buffer, argv[++arg], 256); - - // Driver. - pndd->pcDriver = (char *) malloc (256); - strcpy (pndd->pcDriver, strtok (buffer, ":")); - - // Port. - strcpy (pndd->acPort, strtok (NULL, ":")); - - // Speed. - sscanf (strtok (NULL, ":"), "%u", &pndd->uiSpeed); - - *szFound = 1; - } else { - errx (1, "usage: %s [--device driver:port:speed]", argv[0]); - } - } - if ((0 == strcmp (argv[arg], "-v")) || (0 == strcmp (argv[arg], "--verbose"))) { - *verbose = true; - } - } - return pndd; -} - const char * str_nfc_baud_rate (const nfc_baud_rate_t nbr) { diff --git a/utils/nfc-utils.h b/utils/nfc-utils.h index 3f08177..ace6473 100644 --- a/utils/nfc-utils.h +++ b/utils/nfc-utils.h @@ -97,6 +97,4 @@ void print_nfc_dep_info (const nfc_dep_info_t ndi, bool verbose); void print_nfc_target (const nfc_target_t nt, bool verbose); -nfc_device_desc_t *parse_args (int argc, const char *argv[], size_t * szFound, bool * verbose); - #endif From c718fafee74450ad64458e773bdc19943885ffce Mon Sep 17 00:00:00 2001 From: Audrey Diacre Date: Wed, 23 Nov 2011 15:55:40 +0000 Subject: [PATCH 002/113] Massive code clean up: (Fixes Issue 161) - Remove typedef from internal structs - Remove _t suffix from types - Fix tests using connstrings --- examples/doc/quick_start_example1.c | 6 +- examples/nfc-anticol.c | 2 +- examples/nfc-dep-initiator.c | 4 +- examples/nfc-dep-target.c | 4 +- examples/nfc-emulate-tag.c | 50 ++++--- examples/nfc-emulate-uid.c | 6 +- examples/nfc-poll.c | 6 +- examples/nfc-relay.c | 6 +- examples/pn53x-diagnose.c | 2 +- examples/pn53x-sam.c | 8 +- examples/pn53x-tamashell.c | 2 +- include/nfc/nfc-emulation.h | 4 +- include/nfc/nfc-types.h | 90 ++++++------- include/nfc/nfc.h | 48 +++---- libnfc/buses/uart_posix.c | 34 ++--- libnfc/buses/uart_win32.c | 26 ++-- libnfc/chips/pn53x.c | 196 ++++++++++++++-------------- libnfc/chips/pn53x.h | 126 +++++++++--------- libnfc/drivers/acr122.c | 16 +-- libnfc/drivers/acr122.h | 8 +- libnfc/drivers/arygon.c | 24 ++-- libnfc/drivers/arygon.h | 8 +- libnfc/drivers/pn532_uart.c | 22 ++-- libnfc/drivers/pn532_uart.h | 8 +- libnfc/drivers/pn53x_usb.c | 22 ++-- libnfc/drivers/pn53x_usb.h | 8 +- libnfc/nfc-device.c | 8 +- libnfc/nfc-emulation.c | 2 +- libnfc/nfc-internal.c | 3 +- libnfc/nfc-internal.h | 46 +++---- libnfc/nfc.c | 120 ++++++++--------- test/test_access_storm.c | 14 +- test/test_dep.c | 20 +-- test/test_register_access.c | 8 +- test/test_register_endianness.c | 8 +- utils/mifare.c | 8 +- utils/mifare.h | 20 +-- utils/nfc-emulate-forum-tag2.c | 4 +- utils/nfc-emulate-forum-tag4.c | 4 +- utils/nfc-list.c | 6 +- utils/nfc-mfclassic.c | 6 +- utils/nfc-mfsetuid.c | 2 +- utils/nfc-mfultralight.c | 6 +- utils/nfc-read-forum-tag3.c | 10 +- utils/nfc-relay-picc.c | 10 +- utils/nfc-utils.c | 20 +-- utils/nfc-utils.h | 18 +-- 47 files changed, 546 insertions(+), 533 deletions(-) diff --git a/examples/doc/quick_start_example1.c b/examples/doc/quick_start_example1.c index 9d32642..d828bd0 100644 --- a/examples/doc/quick_start_example1.c +++ b/examples/doc/quick_start_example1.c @@ -10,8 +10,8 @@ int main (int argc, const char *argv[]) { - nfc_device_t *pnd; - nfc_target_info_t nti; + nfc_device *pnd; + nfc_target_info nti; // Display libnfc version const char *acLibnfcVersion = nfc_version (); @@ -30,7 +30,7 @@ main (int argc, const char *argv[]) printf ("Connected to NFC reader: %s\n", pnd->acName); // Poll for a ISO14443A (MIFARE) tag - const nfc_modulation_t nmMifare = { + const nfc_modulation nmMifare = { .nmt = NMT_ISO14443A, .nbr = NBR_106, }; diff --git a/examples/nfc-anticol.c b/examples/nfc-anticol.c index a1f906f..4566c98 100644 --- a/examples/nfc-anticol.c +++ b/examples/nfc-anticol.c @@ -60,7 +60,7 @@ static byte_t abtSak; static byte_t abtAts[MAX_FRAME_LEN]; static byte_t szAts = 0; static size_t szCL = 1;//Always start with Cascade Level 1 (CL1) -static nfc_device_t *pnd; +static nfc_device *pnd; bool quiet_output = false; bool force_rats = false; diff --git a/examples/nfc-dep-initiator.c b/examples/nfc-dep-initiator.c index db8ac9a..37cf78c 100644 --- a/examples/nfc-dep-initiator.c +++ b/examples/nfc-dep-initiator.c @@ -48,7 +48,7 @@ #define MAX_FRAME_LEN 264 -static nfc_device_t *pnd; +static nfc_device *pnd; void stop_dep_communication (int sig) { @@ -62,7 +62,7 @@ void stop_dep_communication (int sig) int main (int argc, const char *argv[]) { - nfc_target_t nt; + nfc_target nt; byte_t abtRx[MAX_FRAME_LEN]; size_t szRx = sizeof(abtRx); byte_t abtTx[] = "Hello World!"; diff --git a/examples/nfc-dep-target.c b/examples/nfc-dep-target.c index 031db8f..a46bf45 100644 --- a/examples/nfc-dep-target.c +++ b/examples/nfc-dep-target.c @@ -47,7 +47,7 @@ #define MAX_FRAME_LEN 264 -static nfc_device_t *pnd; +static nfc_device *pnd; void stop_dep_communication (int sig) { @@ -86,7 +86,7 @@ main (int argc, const char *argv[]) return EXIT_FAILURE; } - nfc_target_t nt = { + nfc_target nt = { .nm = { .nmt = NMT_DEP, .nbr = NBR_UNDEFINED diff --git a/examples/nfc-emulate-tag.c b/examples/nfc-emulate-tag.c index 79792c8..133701d 100644 --- a/examples/nfc-emulate-tag.c +++ b/examples/nfc-emulate-tag.c @@ -56,7 +56,7 @@ static byte_t abtRx[MAX_FRAME_LEN]; static size_t szRx = sizeof(abtRx); -static nfc_device_t *pnd; +static nfc_device *pnd; static bool quiet_output = false; static bool init_mfc_auth = false; @@ -71,7 +71,7 @@ intr_hdlr (void) } bool -target_io( nfc_target_t * pnt, const byte_t * pbtInput, const size_t szInput, byte_t * pbtOutput, size_t *pszOutput ) +target_io( nfc_target * pnt, const byte_t * pbtInput, const size_t szInput, byte_t * pbtOutput, size_t *pszOutput ) { bool loop = true; *pszOutput = 0; @@ -134,7 +134,7 @@ target_io( nfc_target_t * pnt, const byte_t * pbtInput, const size_t szInput, by } bool -nfc_target_emulate_tag(nfc_device_t* pnd, nfc_target_t * pnt) +nfc_target_emulate_tag(nfc_device* pnd, nfc_target * pnt) { size_t szTx; byte_t abtTx[MAX_FRAME_LEN]; @@ -203,7 +203,7 @@ main (int argc, char *argv[]) // Example of a Mifare Classic Mini // Note that crypto1 is not implemented in this example - nfc_target_t nt = { + nfc_target nt = { .nm = { .nmt = NMT_ISO14443A, .nbr = NBR_UNDEFINED, @@ -220,25 +220,37 @@ main (int argc, char *argv[]) }; /* // Example of a FeliCa - nfc_target_t nt = { - .nm.nmt = NMT_FELICA, - .nm.nbr = NBR_UNDEFINED, - .nti.nfi.abtId = { 0x01, 0xFE, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xFF }, - .nti.nfi.abtPad = { 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xFF }, - .nti.nfi.abtSysCode = { 0xFF, 0xFF }, + nfc_target nt = { + .nm = { + .nmt = NMT_FELICA, + .nbr = NBR_UNDEFINED, + }, + .nti = { + .nfi = { + .abtId = { 0x01, 0xFE, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xFF }, + .abtPad = { 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xFF }, + .abtSysCode = { 0xFF, 0xFF }, + }, + }, }; */ /* // Example of a ISO14443-4 (DESfire) - nfc_target_t nt = { - .nm.nmt = NMT_ISO14443A, - .nm.nbr = NBR_UNDEFINED, - .nti.nai.abtAtqa = { 0x03, 0x44 }, - .nti.nai.abtUid = { 0x08, 0xab, 0xcd, 0xef }, - .nti.nai.btSak = 0x20, - .nti.nai.szUidLen = 4, - .nti.nai.abtAts = { 0x75, 0x77, 0x81, 0x02, 0x80 }, - .nti.nai.szAtsLen = 5, + nfc_target nt = { + .nm = { + .nmt = NMT_ISO14443A, + .nbr = NBR_UNDEFINED, + }, + .nti = { + .nai = { + abtAtqa = { 0x03, 0x44 }, + abtUid = { 0x08, 0xab, 0xcd, 0xef }, + btSak = 0x20, + .szUidLen = 4, + .abtAts = { 0x75, 0x77, 0x81, 0x02, 0x80 }, + .szAtsLen = 5, + }, + }, }; */ diff --git a/examples/nfc-emulate-uid.c b/examples/nfc-emulate-uid.c index 4548dc8..a6f8387 100644 --- a/examples/nfc-emulate-uid.c +++ b/examples/nfc-emulate-uid.c @@ -58,7 +58,7 @@ static byte_t abtRecv[MAX_FRAME_LEN]; static size_t szRecvBits; -static nfc_device_t *pnd; +static nfc_device *pnd; // ISO14443A Anti-Collision response byte_t abtAtqa[2] = { 0x04, 0x00 }; @@ -139,8 +139,8 @@ main (int argc, char *argv[]) printf ("[+] To do this, please send any command after the anti-collision\n"); printf ("[+] For example, send a RATS command or use the \"nfc-anticol\" or \"nfc-list\" tool.\n"); - // Note: We have to build a "fake" nfc_target_t in order to do exactly the same that was done before the new nfc_target_init() was introduced. - nfc_target_t nt = { + // Note: We have to build a "fake" nfc_target in order to do exactly the same that was done before the new nfc_target_init() was introduced. + nfc_target nt = { .nm = { .nmt = NMT_ISO14443A, .nbr = NBR_UNDEFINED, diff --git a/examples/nfc-poll.c b/examples/nfc-poll.c index a5c27f1..c3b414c 100644 --- a/examples/nfc-poll.c +++ b/examples/nfc-poll.c @@ -51,7 +51,7 @@ #define MAX_DEVICE_COUNT 16 -static nfc_device_t *pnd = NULL; +static nfc_device *pnd = NULL; void stop_polling (int sig) { @@ -80,7 +80,7 @@ main (int argc, const char *argv[]) const uint8_t uiPollNr = 20; const uint8_t uiPeriod = 2; - const nfc_modulation_t nmModulations[5] = { + const nfc_modulation nmModulations[5] = { { .nmt = NMT_ISO14443A, .nbr = NBR_106 }, { .nmt = NMT_ISO14443B, .nbr = NBR_106 }, { .nmt = NMT_FELICA, .nbr = NBR_212 }, @@ -89,7 +89,7 @@ main (int argc, const char *argv[]) }; const size_t szModulations = 5; - nfc_target_t nt; + nfc_target nt; bool res; pnd = nfc_connect (NULL); diff --git a/examples/nfc-relay.c b/examples/nfc-relay.c index 7ec4ec5..631a63a 100644 --- a/examples/nfc-relay.c +++ b/examples/nfc-relay.c @@ -56,8 +56,8 @@ static size_t szReaderRxBits; static byte_t abtTagRx[MAX_FRAME_LEN]; static byte_t abtTagRxPar[MAX_FRAME_LEN]; static size_t szTagRxBits; -static nfc_device_t *pndReader; -static nfc_device_t *pndTag; +static nfc_device *pndReader; +static nfc_device *pndTag; static bool quitting = false; void @@ -130,7 +130,7 @@ main (int argc, char *argv[]) printf ("[+] To do this, please send any command after the anti-collision\n"); printf ("[+] For example, send a RATS command or use the \"nfc-anticol\" tool\n"); - nfc_target_t nt = { + nfc_target nt = { .nm = { .nmt = NMT_ISO14443A, .nbr = NBR_UNDEFINED, diff --git a/examples/pn53x-diagnose.c b/examples/pn53x-diagnose.c index bc28be0..0bf53c6 100644 --- a/examples/pn53x-diagnose.c +++ b/examples/pn53x-diagnose.c @@ -54,7 +54,7 @@ main (int argc, const char *argv[]) { size_t szFound; size_t i; - nfc_device_t *pnd; + nfc_device *pnd; const char *acLibnfcVersion; bool result; diff --git a/examples/pn53x-sam.c b/examples/pn53x-sam.c index ee9d664..113cac0 100644 --- a/examples/pn53x-sam.c +++ b/examples/pn53x-sam.c @@ -72,7 +72,7 @@ wait_one_minute () int main (int argc, const char *argv[]) { - nfc_device_t *pnd; + nfc_device *pnd; (void) argc; (void) argv; @@ -125,7 +125,7 @@ main (int argc, const char *argv[]) case PSM_WIRED_CARD: { - nfc_target_t nt; + nfc_target nt; // Set connected NFC device to initiator mode nfc_initiator_init (pnd); @@ -136,7 +136,7 @@ main (int argc, const char *argv[]) exit (EXIT_FAILURE); } // Read the SAM's info - const nfc_modulation_t nmSAM = { + const nfc_modulation nmSAM = { .nmt = NMT_ISO14443A, .nbr = NBR_106, }; @@ -156,7 +156,7 @@ main (int argc, const char *argv[]) byte_t abtRx[MAX_FRAME_LEN]; size_t szRx = sizeof(abtRx); - nfc_target_t nt = { + nfc_target nt = { .nm = { .nmt = NMT_ISO14443A, .nbr = NBR_UNDEFINED, diff --git a/examples/pn53x-tamashell.c b/examples/pn53x-tamashell.c index 1d3fe94..1929697 100644 --- a/examples/pn53x-tamashell.c +++ b/examples/pn53x-tamashell.c @@ -71,7 +71,7 @@ int main(int argc, const char* argv[]) { - nfc_device_t* pnd; + nfc_device* pnd; byte_t abtRx[MAX_FRAME_LEN]; byte_t abtTx[MAX_FRAME_LEN]; size_t szRx = sizeof(abtRx); diff --git a/include/nfc/nfc-emulation.h b/include/nfc/nfc-emulation.h index ab92d6c..a4b087d 100644 --- a/include/nfc/nfc-emulation.h +++ b/include/nfc/nfc-emulation.h @@ -32,7 +32,7 @@ struct nfc_emulation_state_machine; struct nfc_emulator { - nfc_target_t *target; + nfc_target *target; struct nfc_emulation_state_machine *state_machine; void *user_data; }; @@ -42,7 +42,7 @@ struct nfc_emulation_state_machine { void *data; }; -NFC_EXPORT int nfc_emulate_target (nfc_device_t* pnd, struct nfc_emulator *emulator); +NFC_EXPORT int nfc_emulate_target (nfc_device* pnd, struct nfc_emulator *emulator); #ifdef __cplusplus } diff --git a/include/nfc/nfc-types.h b/include/nfc/nfc-types.h index 15b9367..1a3b7ad 100644 --- a/include/nfc/nfc-types.h +++ b/include/nfc/nfc-types.h @@ -38,7 +38,7 @@ typedef uint8_t byte_t; # define DEVICE_PORT_LENGTH 64 /** - * @struct nfc_device_t + * @struct nfc_device * @brief NFC device information */ typedef struct { @@ -69,7 +69,7 @@ typedef struct { * +----------- Driver-level general error (common to all drivers) */ int iLastError; -} nfc_device_t; +} nfc_device; typedef char nfc_connstring[1024]; @@ -77,7 +77,7 @@ typedef char nfc_connstring[1024]; # pragma pack(1) /** - * @enum nfc_device_option_t + * @enum nfc_device_option * @brief NFC device option */ typedef enum { @@ -137,20 +137,20 @@ typedef enum { NDO_FORCE_ISO14443_B = 0x43, /** Force the chip to run at 106 kbps */ NDO_FORCE_SPEED_106 = 0x50, -} nfc_device_option_t; +} nfc_device_option; /** - * @enum nfc_dep_mode_t + * @enum nfc_dep_mode * @brief NFC D.E.P. (Data Exchange Protocol) active/passive mode */ typedef enum { NDM_UNDEFINED = 0, NDM_PASSIVE, NDM_ACTIVE, -} nfc_dep_mode_t; +} nfc_dep_mode; /** - * @struct nfc_dep_info_t + * @struct nfc_dep_info * @brief NFC target information in D.E.P. (Data Exchange Protocol) see ISO/IEC 18092 (NFCIP-1) */ typedef struct { @@ -170,11 +170,11 @@ typedef struct { byte_t abtGB[48]; size_t szGB; /** DEP mode */ - nfc_dep_mode_t ndm; -} nfc_dep_info_t; + nfc_dep_mode ndm; +} nfc_dep_info; /** - * @struct nfc_iso14443a_info_t + * @struct nfc_iso14443a_info * @brief NFC ISO14443A tag (MIFARE) information */ typedef struct { @@ -184,10 +184,10 @@ typedef struct { byte_t abtUid[10]; size_t szAtsLen; byte_t abtAts[254]; // Maximal theoretical ATS is FSD-2, FSD=256 for FSDI=8 in RATS -} nfc_iso14443a_info_t; +} nfc_iso14443a_info; /** - * @struct nfc_felica_info_t + * @struct nfc_felica_info * @brief NFC FeLiCa tag information */ typedef struct { @@ -196,10 +196,10 @@ typedef struct { byte_t abtId[8]; byte_t abtPad[8]; byte_t abtSysCode[2]; -} nfc_felica_info_t; +} nfc_felica_info; /** - * @struct nfc_iso14443b_info_t + * @struct nfc_iso14443b_info * @brief NFC ISO14443B tag information */ typedef struct { @@ -211,10 +211,10 @@ typedef struct { byte_t abtProtocolInfo[3]; /** ui8CardIdentifier store CID (Card Identifier) attributted by PCD to the PICC */ uint8_t ui8CardIdentifier; -} nfc_iso14443b_info_t; +} nfc_iso14443b_info; /** - * @struct nfc_iso14443bi_info_t + * @struct nfc_iso14443bi_info * @brief NFC ISO14443B' tag information */ typedef struct { @@ -227,52 +227,52 @@ typedef struct { /** ATR, if any */ size_t szAtrLen; byte_t abtAtr[33]; -} nfc_iso14443bi_info_t; +} nfc_iso14443bi_info; /** - * @struct nfc_iso14443b2sr_info_t + * @struct nfc_iso14443b2sr_info * @brief NFC ISO14443-2B ST SRx tag information */ typedef struct { byte_t abtUID[8]; -} nfc_iso14443b2sr_info_t; +} nfc_iso14443b2sr_info; /** - * @struct nfc_iso14443b2ct_info_t + * @struct nfc_iso14443b2ct_info * @brief NFC ISO14443-2B ASK CTx tag information */ typedef struct { byte_t abtUID[4]; byte_t btProdCode; byte_t btFabCode; -} nfc_iso14443b2ct_info_t; +} nfc_iso14443b2ct_info; /** - * @struct nfc_jewel_info_t + * @struct nfc_jewel_info * @brief NFC Jewel tag information */ typedef struct { byte_t btSensRes[2]; byte_t btId[4]; -} nfc_jewel_info_t; +} nfc_jewel_info; /** - * @union nfc_target_info_t + * @union nfc_target_info * @brief Union between all kind of tags information structures. */ typedef union { - nfc_iso14443a_info_t nai; - nfc_felica_info_t nfi; - nfc_iso14443b_info_t nbi; - nfc_iso14443bi_info_t nii; - nfc_iso14443b2sr_info_t nsi; - nfc_iso14443b2ct_info_t nci; - nfc_jewel_info_t nji; - nfc_dep_info_t ndi; -} nfc_target_info_t; + nfc_iso14443a_info nai; + nfc_felica_info nfi; + nfc_iso14443b_info nbi; + nfc_iso14443bi_info nii; + nfc_iso14443b2sr_info nsi; + nfc_iso14443b2ct_info nci; + nfc_jewel_info nji; + nfc_dep_info ndi; +} nfc_target_info; /** - * @enum nfc_baud_rate_t + * @enum nfc_baud_rate * @brief NFC baud rate enumeration */ typedef enum { @@ -281,10 +281,10 @@ typedef enum { NBR_212, NBR_424, NBR_847, -} nfc_baud_rate_t; +} nfc_baud_rate; /** - * @enum nfc_modulation_type_t + * @enum nfc_modulationype * @brief NFC modulation type enumeration */ typedef enum { @@ -296,25 +296,25 @@ typedef enum { NMT_ISO14443B2CT, // ISO14443-2B ASK CTx NMT_FELICA, NMT_DEP, -} nfc_modulation_type_t; +} nfc_modulationype; /** - * @struct nfc_modulation_t + * @struct nfc_modulation * @brief NFC modulation structure */ typedef struct { - nfc_modulation_type_t nmt; - nfc_baud_rate_t nbr; -} nfc_modulation_t; + nfc_modulationype nmt; + nfc_baud_rate nbr; +} nfc_modulation; /** - * @struct nfc_target_t + * @struct nfc_target * @brief NFC target structure */ typedef struct { - nfc_target_info_t nti; - nfc_modulation_t nm; -} nfc_target_t; + nfc_target_info nti; + nfc_modulation nm; +} nfc_target; // Reset struct alignment to default # pragma pack() diff --git a/include/nfc/nfc.h b/include/nfc/nfc.h index 011e475..c90fa17 100644 --- a/include/nfc/nfc.h +++ b/include/nfc/nfc.h @@ -64,39 +64,39 @@ extern "C" { /* NFC Device/Hardware manipulation */ NFC_EXPORT bool nfc_get_default_device (nfc_connstring *connstring); - NFC_EXPORT nfc_device_t *nfc_connect (const nfc_connstring connstring); - NFC_EXPORT void nfc_disconnect (nfc_device_t * pnd); - NFC_EXPORT bool nfc_abort_command (nfc_device_t * pnd); + NFC_EXPORT nfc_device *nfc_connect (const nfc_connstring connstring); + NFC_EXPORT void nfc_disconnect (nfc_device * pnd); + NFC_EXPORT bool nfc_abort_command (nfc_device * pnd); NFC_EXPORT void nfc_list_devices (nfc_connstring connstrings[], size_t connstrings_len, size_t * pszDeviceFound); - NFC_EXPORT bool nfc_configure (nfc_device_t * pnd, const nfc_device_option_t ndo, const bool bEnable); - NFC_EXPORT bool nfc_idle (nfc_device_t * pnd); + NFC_EXPORT bool nfc_configure (nfc_device * pnd, const nfc_device_option ndo, const bool bEnable); + NFC_EXPORT bool nfc_idle (nfc_device * pnd); /* NFC initiator: act as "reader" */ - NFC_EXPORT bool nfc_initiator_init (nfc_device_t * pnd); - NFC_EXPORT bool nfc_initiator_select_passive_target (nfc_device_t * pnd, const nfc_modulation_t nm, const byte_t * pbtInitData, const size_t szInitData, nfc_target_t * pnt); - NFC_EXPORT bool nfc_initiator_list_passive_targets (nfc_device_t * pnd, const nfc_modulation_t nm, nfc_target_t ant[], const size_t szTargets, size_t * pszTargetFound); - NFC_EXPORT bool nfc_initiator_poll_target (nfc_device_t * pnd, const nfc_modulation_t * pnmTargetTypes, const size_t szTargetTypes, const uint8_t uiPollNr, const uint8_t uiPeriod, nfc_target_t * pnt); - NFC_EXPORT bool nfc_initiator_select_dep_target (nfc_device_t * pnd, const nfc_dep_mode_t ndm, const nfc_baud_rate_t nbr, const nfc_dep_info_t * pndiInitiator, nfc_target_t * pnt); - NFC_EXPORT bool nfc_initiator_deselect_target (nfc_device_t * pnd); - NFC_EXPORT bool nfc_initiator_transceive_bytes (nfc_device_t * pnd, const byte_t * pbtTx, const size_t szTx, byte_t * pbtRx, size_t * pszRx, struct timeval *timeout); - NFC_EXPORT bool nfc_initiator_transceive_bits (nfc_device_t * pnd, const byte_t * pbtTx, const size_t szTxBits, const byte_t * pbtTxPar, byte_t * pbtRx, size_t * pszRxBits, byte_t * pbtRxPar); - NFC_EXPORT bool nfc_initiator_transceive_bytes_timed (nfc_device_t * pnd, const byte_t * pbtTx, const size_t szTx, byte_t * pbtRx, size_t * pszRx, uint32_t * cycles); - NFC_EXPORT bool nfc_initiator_transceive_bits_timed (nfc_device_t * pnd, const byte_t * pbtTx, const size_t szTxBits, const byte_t * pbtTxPar, byte_t * pbtRx, size_t * pszRxBits, byte_t * pbtRxPar, uint32_t * cycles); + NFC_EXPORT bool nfc_initiator_init (nfc_device * pnd); + NFC_EXPORT bool nfc_initiator_select_passive_target (nfc_device * pnd, const nfc_modulation nm, const byte_t * pbtInitData, const size_t szInitData, nfc_target * pnt); + NFC_EXPORT bool nfc_initiator_list_passive_targets (nfc_device * pnd, const nfc_modulation nm, nfc_target ant[], const size_t szTargets, size_t * pszTargetFound); + NFC_EXPORT bool nfc_initiator_poll_target (nfc_device * pnd, const nfc_modulation * pnmTargetTypes, const size_t szTargetTypes, const uint8_t uiPollNr, const uint8_t uiPeriod, nfc_target * pnt); + NFC_EXPORT bool nfc_initiator_select_dep_target (nfc_device * pnd, const nfc_dep_mode ndm, const nfc_baud_rate nbr, const nfc_dep_info * pndiInitiator, nfc_target * pnt); + NFC_EXPORT bool nfc_initiator_deselect_target (nfc_device * pnd); + NFC_EXPORT bool nfc_initiator_transceive_bytes (nfc_device * pnd, const byte_t * pbtTx, const size_t szTx, byte_t * pbtRx, size_t * pszRx, struct timeval *timeout); + NFC_EXPORT bool nfc_initiator_transceive_bits (nfc_device * pnd, const byte_t * pbtTx, const size_t szTxBits, const byte_t * pbtTxPar, byte_t * pbtRx, size_t * pszRxBits, byte_t * pbtRxPar); + NFC_EXPORT bool nfc_initiator_transceive_bytes_timed (nfc_device * pnd, const byte_t * pbtTx, const size_t szTx, byte_t * pbtRx, size_t * pszRx, uint32_t * cycles); + NFC_EXPORT bool nfc_initiator_transceive_bits_timed (nfc_device * pnd, const byte_t * pbtTx, const size_t szTxBits, const byte_t * pbtTxPar, byte_t * pbtRx, size_t * pszRxBits, byte_t * pbtRxPar, uint32_t * cycles); /* NFC target: act as tag (i.e. MIFARE Classic) or NFC target device. */ - NFC_EXPORT bool nfc_target_init (nfc_device_t * pnd, nfc_target_t * pnt, byte_t * pbtRx, size_t * pszRx); - NFC_EXPORT bool nfc_target_send_bytes (nfc_device_t * pnd, const byte_t * pbtTx, const size_t szTx, struct timeval *timout); - NFC_EXPORT bool nfc_target_receive_bytes (nfc_device_t * pnd, byte_t * pbtRx, size_t * pszRx, struct timeval *timout); - NFC_EXPORT bool nfc_target_send_bits (nfc_device_t * pnd, const byte_t * pbtTx, const size_t szTxBits, const byte_t * pbtTxPar); - NFC_EXPORT bool nfc_target_receive_bits (nfc_device_t * pnd, byte_t * pbtRx, size_t * pszRxBits, byte_t * pbtRxPar); + NFC_EXPORT bool nfc_target_init (nfc_device * pnd, nfc_target * pnt, byte_t * pbtRx, size_t * pszRx); + NFC_EXPORT bool nfc_target_send_bytes (nfc_device * pnd, const byte_t * pbtTx, const size_t szTx, struct timeval *timout); + NFC_EXPORT bool nfc_target_receive_bytes (nfc_device * pnd, byte_t * pbtRx, size_t * pszRx, struct timeval *timout); + NFC_EXPORT bool nfc_target_send_bits (nfc_device * pnd, const byte_t * pbtTx, const size_t szTxBits, const byte_t * pbtTxPar); + NFC_EXPORT bool nfc_target_receive_bits (nfc_device * pnd, byte_t * pbtRx, size_t * pszRxBits, byte_t * pbtRxPar); /* Error reporting */ - NFC_EXPORT const char *nfc_strerror (const nfc_device_t * pnd); - NFC_EXPORT int nfc_strerror_r (const nfc_device_t * pnd, char *pcStrErrBuf, size_t szBufLen); - NFC_EXPORT void nfc_perror (const nfc_device_t * pnd, const char *pcString); + NFC_EXPORT const char *nfc_strerror (const nfc_device * pnd); + NFC_EXPORT int nfc_strerror_r (const nfc_device * pnd, char *pcStrErrBuf, size_t szBufLen); + NFC_EXPORT void nfc_perror (const nfc_device * pnd, const char *pcString); /* Special data accessors */ - NFC_EXPORT const char *nfc_device_name (nfc_device_t * pnd); + NFC_EXPORT const char *nfc_device_name (nfc_device * pnd); /* Misc. functions */ NFC_EXPORT void iso14443a_crc (byte_t * pbtData, size_t szLen, byte_t * pbtCrc); diff --git a/libnfc/buses/uart_posix.c b/libnfc/buses/uart_posix.c index 78cf67b..2fc3b47 100644 --- a/libnfc/buses/uart_posix.c +++ b/libnfc/buses/uart_posix.c @@ -61,18 +61,20 @@ char *serial_ports_device_radix[] = { "ttyUSB", "ttyS", NULL }; // Work-around to claim uart interface using the c_iflag (software input processing) from the termios struct # define CCLAIMED 0x80000000 -typedef struct { +struct serial_port_unix{ int fd; // Serial port file descriptor struct termios termios_backup; // Terminal info before using the port struct termios termios_new; // Terminal info during the transaction -} serial_port_unix; +}; + +// TODO: #define UART_FD( X ) (((struct serial_port_unix *) X)->fd) void uart_close_ext (const serial_port sp, const bool restore_termios); serial_port uart_open (const char *pcPortName) { - serial_port_unix *sp = malloc (sizeof (serial_port_unix)); + struct serial_port_unix *sp = malloc (sizeof (struct serial_port_unix)); if (sp == 0) return INVALID_SERIAL_PORT; @@ -114,12 +116,12 @@ void uart_flush_input (serial_port sp) { // This line seems to produce absolutely no effect on my system (GNU/Linux 2.6.35) - tcflush (((serial_port_unix *) sp)->fd, TCIFLUSH); + tcflush (((struct serial_port_unix *) sp)->fd, TCIFLUSH); // So, I wrote this byte-eater // Retrieve the count of the incoming bytes int available_bytes_count = 0; int res; - res = ioctl (((serial_port_unix *) sp)->fd, FIONREAD, &available_bytes_count); + res = ioctl (((struct serial_port_unix *) sp)->fd, FIONREAD, &available_bytes_count); if (res != 0) { return; } @@ -128,7 +130,7 @@ uart_flush_input (serial_port sp) } char* rx = malloc (available_bytes_count); // There is something available, read the data - res = read (((serial_port_unix *) sp)->fd, rx, available_bytes_count); + res = read (((struct serial_port_unix *) sp)->fd, rx, available_bytes_count); log_put (LOG_CATEGORY, NFC_PRIORITY_TRACE, "%d bytes have eatten.", available_bytes_count); free (rx); } @@ -137,7 +139,7 @@ void uart_set_speed (serial_port sp, const uint32_t uiPortSpeed) { log_put (LOG_CATEGORY, NFC_PRIORITY_TRACE, "Serial port speed requested to be set to %d bauds.", uiPortSpeed); - serial_port_unix *spu = (serial_port_unix *) sp; + struct serial_port_unix *spu = (struct serial_port_unix *) sp; // Portability note: on some systems, B9600 != 9600 so we have to do // uint32_t <=> speed_t associations by hand. @@ -190,7 +192,7 @@ uint32_t uart_get_speed (serial_port sp) { uint32_t uiPortSpeed = 0; - const serial_port_unix *spu = (serial_port_unix *) sp; + const struct serial_port_unix *spu = (struct serial_port_unix *) sp; switch (cfgetispeed (&spu->termios_new)) { case B9600: uiPortSpeed = 9600; @@ -229,10 +231,10 @@ uart_get_speed (serial_port sp) void uart_close_ext (const serial_port sp, const bool restore_termios) { - if (((serial_port_unix *) sp)->fd >= 0) { + if (((struct serial_port_unix *) sp)->fd >= 0) { if (restore_termios) - tcsetattr (((serial_port_unix *) sp)->fd, TCSANOW, &((serial_port_unix *) sp)->termios_backup); - close (((serial_port_unix *) sp)->fd); + tcsetattr (((struct serial_port_unix *) sp)->fd, TCSANOW, &((struct serial_port_unix *) sp)->termios_backup); + close (((struct serial_port_unix *) sp)->fd); } free (sp); } @@ -261,7 +263,7 @@ uart_receive (serial_port sp, byte_t * pbtRx, const size_t szRx, void * abort_p, select: // Reset file descriptor FD_ZERO (&rfds); - FD_SET (((serial_port_unix *) sp)->fd, &rfds); + FD_SET (((struct serial_port_unix *) sp)->fd, &rfds); if (iAbortFd) { FD_SET (iAbortFd, &rfds); @@ -277,7 +279,7 @@ select: timeout = &fixed_timeout; } - res = select (MAX(((serial_port_unix *) sp)->fd, iAbortFd) + 1, &rfds, NULL, NULL, timeout); + res = select (MAX(((struct serial_port_unix *) sp)->fd, iAbortFd) + 1, &rfds, NULL, NULL, timeout); if ((res < 0) && (EINTR == errno)) { // The system call was interupted by a signal and a signal handler was @@ -304,12 +306,12 @@ select: } // Retrieve the count of the incoming bytes - res = ioctl (((serial_port_unix *) sp)->fd, FIONREAD, &available_bytes_count); + res = ioctl (((struct serial_port_unix *) sp)->fd, FIONREAD, &available_bytes_count); if (res != 0) { return ECOMIO; } // There is something available, read the data - res = read (((serial_port_unix *) sp)->fd, pbtRx + received_bytes_count, MIN(available_bytes_count, (expected_bytes_count - received_bytes_count))); + res = read (((struct serial_port_unix *) sp)->fd, pbtRx + received_bytes_count, MIN(available_bytes_count, (expected_bytes_count - received_bytes_count))); // Stop if the OS has some troubles reading the data if (res <= 0) { return ECOMIO; @@ -331,7 +333,7 @@ uart_send (serial_port sp, const byte_t * pbtTx, const size_t szTx, struct timev { (void) timeout; LOG_HEX ("TX", pbtTx, szTx); - if ((int) szTx == write (((serial_port_unix *) sp)->fd, pbtTx, szTx)) + if ((int) szTx == write (((struct serial_port_unix *) sp)->fd, pbtTx, szTx)) return 0; else return ECOMIO; diff --git a/libnfc/buses/uart_win32.c b/libnfc/buses/uart_win32.c index 29977c6..339be57 100644 --- a/libnfc/buses/uart_win32.c +++ b/libnfc/buses/uart_win32.c @@ -31,17 +31,17 @@ #include "contrib/windows.h" #define delay_ms( X ) Sleep( X ) -typedef struct { +struct serial_port_windows { HANDLE hPort; // Serial port handle DCB dcb; // Device control settings COMMTIMEOUTS ct; // Serial port time-out configuration -} serial_port_windows; +}; serial_port uart_open (const char *pcPortName) { char acPortName[255]; - serial_port_windows *sp = malloc (sizeof (serial_port_windows)); + struct serial_port_windows *sp = malloc (sizeof (struct serial_port_windows)); // Copy the input "com?" to "\\.\COM?" format sprintf (acPortName, "\\\\.\\%s", pcPortName); @@ -85,8 +85,8 @@ uart_open (const char *pcPortName) void uart_close (const serial_port sp) { - if (((serial_port_windows *) sp)->hPort != INVALID_HANDLE_VALUE) { - CloseHandle (((serial_port_windows *) sp)->hPort); + if (((struct serial_port_windows *) sp)->hPort != INVALID_HANDLE_VALUE) { + CloseHandle (((struct serial_port_windows *) sp)->hPort); } free (sp); } @@ -94,13 +94,13 @@ uart_close (const serial_port sp) void uart_flush_input (const serial_port sp) { - PurgeComm(((serial_port_windows *) sp)->hPort, PURGE_RXABORT | PURGE_RXCLEAR); + PurgeComm(((struct serial_port_windows *) sp)->hPort, PURGE_RXABORT | PURGE_RXCLEAR); } void uart_set_speed (serial_port sp, const uint32_t uiPortSpeed) { - serial_port_windows *spw; + struct serial_port_windows *spw; log_put (LOG_CATEGORY, NFC_PRIORITY_TRACE, "Serial port speed requested to be set to %d bauds.", uiPortSpeed); // Set port speed (Input and Output) @@ -117,7 +117,7 @@ uart_set_speed (serial_port sp, const uint32_t uiPortSpeed) log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "Unable to set serial port speed to %d bauds. Speed value must be one of these constants: 9600 (default), 19200, 38400, 57600, 115200, 230400 or 460800.", uiPortSpeed); return; }; - spw = (serial_port_windows *) sp; + spw = (struct serial_port_windows *) sp; // Set baud rate spw->dcb.BaudRate = uiPortSpeed; @@ -131,7 +131,7 @@ uart_set_speed (serial_port sp, const uint32_t uiPortSpeed) uint32_t uart_get_speed (const serial_port sp) { - const serial_port_windows *spw = (serial_port_windows *) sp; + const struct serial_port_windows *spw = (struct serial_port_windows *) sp; if (!GetCommState (spw->hPort, (serial_port) & spw->dcb)) return spw->dcb.BaudRate; @@ -155,7 +155,7 @@ uart_receive (serial_port sp, byte_t * pbtRx, const size_t szRx, void * abort_p, timeouts.WriteTotalTimeoutMultiplier = 0; timeouts.WriteTotalTimeoutConstant = timeout_ms; - if (!SetCommTimeouts (((serial_port_windows *) sp)->hPort, &timeouts)) { + if (!SetCommTimeouts (((struct serial_port_windows *) sp)->hPort, &timeouts)) { log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "Unable to apply new timeout settings."); return ECOMIO; } @@ -166,7 +166,7 @@ uart_receive (serial_port sp, byte_t * pbtRx, const size_t szRx, void * abort_p, volatile bool * abort_flag_p = (volatile bool *)abort_p; do { log_put (LOG_CATEGORY, NFC_PRIORITY_TRACE, "ReadFile"); - res = ReadFile (((serial_port_windows *) sp)->hPort, pbtRx + dwTotalBytesReceived, + res = ReadFile (((struct serial_port_windows *) sp)->hPort, pbtRx + dwTotalBytesReceived, dwBytesToGet, &dwBytesReceived, NULL); @@ -205,13 +205,13 @@ uart_send (serial_port sp, const byte_t * pbtTx, const size_t szTx, struct timev timeouts.WriteTotalTimeoutMultiplier = 0; timeouts.WriteTotalTimeoutConstant = timeout ? ((timeout->tv_sec * 1000) + (timeout->tv_usec / 1000)) : 0; - if (!SetCommTimeouts (((serial_port_windows *) sp)->hPort, &timeouts)) { + if (!SetCommTimeouts (((struct serial_port_windows *) sp)->hPort, &timeouts)) { log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "Unable to apply new timeout settings."); return ECOMIO; } LOG_HEX ("TX", pbtTx, szTx); - if (!WriteFile (((serial_port_windows *) sp)->hPort, pbtTx, szTx, &dwTxLen, NULL)) { + if (!WriteFile (((struct serial_port_windows *) sp)->hPort, pbtTx, szTx, &dwTxLen, NULL)) { return ECOMIO; } if (!dwTxLen) diff --git a/libnfc/chips/pn53x.c b/libnfc/chips/pn53x.c index 246d839..7de9520 100644 --- a/libnfc/chips/pn53x.c +++ b/libnfc/chips/pn53x.c @@ -51,15 +51,15 @@ const byte_t pn53x_nack_frame[] = { 0x00, 0x00, 0xff, 0xff, 0x00, 0x00 }; static const byte_t pn53x_error_frame[] = { 0x00, 0x00, 0xff, 0x01, 0xff, 0x7f, 0x81, 0x00 }; /* prototypes */ -bool pn53x_reset_settings (nfc_device_t * pnd); -bool pn53x_writeback_register (nfc_device_t * pnd); +bool pn53x_reset_settings (nfc_device * pnd); +bool pn53x_writeback_register (nfc_device * pnd); -nfc_modulation_t pn53x_ptt_to_nm (const pn53x_target_type_t ptt); -pn53x_modulation_t pn53x_nm_to_pm (const nfc_modulation_t nm); -pn53x_target_type_t pn53x_nm_to_ptt (const nfc_modulation_t nm); +nfc_modulation pn53x_ptt_to_nm (const pn53x_target_type ptt); +pn53x_modulation pn53x_nm_to_pm (const nfc_modulation nm); +pn53x_target_type pn53x_nm_to_ptt (const nfc_modulation nm); bool -pn53x_init(nfc_device_t * pnd) +pn53x_init(nfc_device * pnd) { // GetFirmwareVersion command is used to set PN53x chips type (PN531, PN532 or PN533) char abtFirmwareText[22]; @@ -89,7 +89,7 @@ pn53x_init(nfc_device_t * pnd) } bool -pn53x_reset_settings(nfc_device_t * pnd) +pn53x_reset_settings(nfc_device * pnd) { // Reset the ending transmission bits register, it is unknown what the last tranmission used there CHIP_DATA (pnd)->ui8TxBits = 0; @@ -100,7 +100,7 @@ pn53x_reset_settings(nfc_device_t * pnd) } bool -pn53x_transceive (nfc_device_t * pnd, const byte_t * pbtTx, const size_t szTx, byte_t * pbtRx, size_t *pszRx, struct timeval *timeout) +pn53x_transceive (nfc_device * pnd, const byte_t * pbtTx, const size_t szTx, byte_t * pbtRx, size_t *pszRx, struct timeval *timeout) { if (CHIP_DATA (pnd)->wb_trigged) { if (!pn53x_writeback_register (pnd)) { @@ -190,7 +190,7 @@ pn53x_transceive (nfc_device_t * pnd, const byte_t * pbtTx, const size_t szTx, b } bool -pn53x_set_parameters (nfc_device_t * pnd, const uint8_t ui8Parameter, const bool bEnable) +pn53x_set_parameters (nfc_device * pnd, const uint8_t ui8Parameter, const bool bEnable) { uint8_t ui8Value = (bEnable) ? (CHIP_DATA (pnd)->ui8Parameters | ui8Parameter) : (CHIP_DATA (pnd)->ui8Parameters & ~(ui8Parameter)); if (ui8Value != CHIP_DATA (pnd)->ui8Parameters) { @@ -200,7 +200,7 @@ pn53x_set_parameters (nfc_device_t * pnd, const uint8_t ui8Parameter, const bool } bool -pn53x_set_tx_bits (nfc_device_t * pnd, const uint8_t ui8Bits) +pn53x_set_tx_bits (nfc_device * pnd, const uint8_t ui8Bits) { // Test if we need to update the transmission bits register setting if (CHIP_DATA (pnd)->ui8TxBits != ui8Bits) { @@ -318,8 +318,8 @@ pn53x_unwrap_frame (const byte_t * pbtFrame, const size_t szFrameBits, byte_t * } bool -pn53x_decode_target_data (const byte_t * pbtRawData, size_t szRawData, pn53x_type type, nfc_modulation_type_t nmt, - nfc_target_info_t * pnti) +pn53x_decode_target_data (const byte_t * pbtRawData, size_t szRawData, pn53x_type type, nfc_modulationype nmt, + nfc_target_info * pnti) { uint8_t szAttribRes; @@ -462,7 +462,7 @@ pn53x_decode_target_data (const byte_t * pbtRawData, size_t szRawData, pn53x_typ } bool -pn53x_ReadRegister (nfc_device_t * pnd, uint16_t ui16RegisterAddress, uint8_t * ui8Value) +pn53x_ReadRegister (nfc_device * pnd, uint16_t ui16RegisterAddress, uint8_t * ui8Value) { byte_t abtCmd[] = { ReadRegister, ui16RegisterAddress >> 8, ui16RegisterAddress & 0xff }; byte_t abtRegValue[2]; @@ -481,13 +481,13 @@ pn53x_ReadRegister (nfc_device_t * pnd, uint16_t ui16RegisterAddress, uint8_t * return true; } -bool pn53x_read_register (nfc_device_t * pnd, uint16_t ui16RegisterAddress, uint8_t * ui8Value) +bool pn53x_read_register (nfc_device * pnd, uint16_t ui16RegisterAddress, uint8_t * ui8Value) { return pn53x_ReadRegister (pnd, ui16RegisterAddress, ui8Value); } bool -pn53x_WriteRegister (nfc_device_t * pnd, const uint16_t ui16RegisterAddress, const uint8_t ui8Value) +pn53x_WriteRegister (nfc_device * pnd, const uint16_t ui16RegisterAddress, const uint8_t ui8Value) { byte_t abtCmd[] = { WriteRegister, ui16RegisterAddress >> 8, ui16RegisterAddress & 0xff, ui8Value }; PNREG_TRACE (ui16RegisterAddress); @@ -495,7 +495,7 @@ pn53x_WriteRegister (nfc_device_t * pnd, const uint16_t ui16RegisterAddress, con } bool -pn53x_write_register (nfc_device_t * pnd, const uint16_t ui16RegisterAddress, const uint8_t ui8SymbolMask, const uint8_t ui8Value) +pn53x_write_register (nfc_device * pnd, const uint16_t ui16RegisterAddress, const uint8_t ui8SymbolMask, const uint8_t ui8Value) { if ((ui16RegisterAddress < PN53X_CACHE_REGISTER_MIN_ADDRESS) || (ui16RegisterAddress > PN53X_CACHE_REGISTER_MAX_ADDRESS)) { // Direct write @@ -521,7 +521,7 @@ pn53x_write_register (nfc_device_t * pnd, const uint16_t ui16RegisterAddress, co } bool -pn53x_writeback_register (nfc_device_t * pnd) +pn53x_writeback_register (nfc_device * pnd) { // TODO Check at each step (ReadRegister, WriteRegister) if we didn't exceed max supported frame length BUFFER_INIT (abtReadRegisterCmd, PN53x_EXTENDED_FRAME__DATA_MAX_LEN); @@ -589,7 +589,7 @@ pn53x_writeback_register (nfc_device_t * pnd) } bool -pn53x_get_firmware_version (nfc_device_t * pnd, char abtFirmwareText[22]) +pn53x_get_firmware_version (nfc_device * pnd, char abtFirmwareText[22]) { const byte_t abtCmd[] = { GetFirmwareVersion }; byte_t abtFw[4]; @@ -640,7 +640,7 @@ pn53x_get_firmware_version (nfc_device_t * pnd, char abtFirmwareText[22]) } bool -pn53x_configure (nfc_device_t * pnd, const nfc_device_option_t ndo, const bool bEnable) +pn53x_configure (nfc_device * pnd, const nfc_device_option ndo, const bool bEnable) { byte_t btValue; switch (ndo) { @@ -777,7 +777,7 @@ pn53x_configure (nfc_device_t * pnd, const nfc_device_option_t ndo, const bool b } bool -pn53x_idle (nfc_device_t *pnd) +pn53x_idle (nfc_device *pnd) { switch (CHIP_DATA (pnd)->operating_mode) { case TARGET: @@ -825,7 +825,7 @@ pn53x_idle (nfc_device_t *pnd) } bool -pn53x_check_communication (nfc_device_t *pnd) +pn53x_check_communication (nfc_device *pnd) { const byte_t abtCmd[] = { Diagnose, 0x00, 'l', 'i', 'b', 'n', 'f', 'c' }; const byte_t abtExpectedRx[] = { 0x00, 'l', 'i', 'b', 'n', 'f', 'c' }; @@ -843,7 +843,7 @@ pn53x_check_communication (nfc_device_t *pnd) } bool -pn53x_initiator_init (nfc_device_t * pnd) +pn53x_initiator_init (nfc_device * pnd) { pn53x_reset_settings(pnd); @@ -856,10 +856,10 @@ pn53x_initiator_init (nfc_device_t * pnd) } bool -pn53x_initiator_select_passive_target_ext (nfc_device_t * pnd, - const nfc_modulation_t nm, +pn53x_initiator_select_passive_target_ext (nfc_device * pnd, + const nfc_modulation nm, const byte_t * pbtInitData, const size_t szInitData, - nfc_target_t * pnt, + nfc_target * pnt, struct timeval* timeout) { byte_t abtTargetsData[PN53x_EXTENDED_FRAME__DATA_MAX_LEN]; @@ -941,7 +941,7 @@ pn53x_initiator_select_passive_target_ext (nfc_device_t * pnd, return true; } // else: - const pn53x_modulation_t pm = pn53x_nm_to_pm(nm); + const pn53x_modulation pm = pn53x_nm_to_pm(nm); if (PM_UNDEFINED == pm) { pnd->iLastError = EINVALARG; return false; @@ -966,25 +966,25 @@ pn53x_initiator_select_passive_target_ext (nfc_device_t * pnd, } bool -pn53x_initiator_select_passive_target (nfc_device_t * pnd, - const nfc_modulation_t nm, +pn53x_initiator_select_passive_target (nfc_device * pnd, + const nfc_modulation nm, const byte_t * pbtInitData, const size_t szInitData, - nfc_target_t * pnt) + nfc_target * pnt) { return pn53x_initiator_select_passive_target_ext (pnd, nm, pbtInitData, szInitData, pnt, NULL); } bool -pn53x_initiator_poll_target (nfc_device_t * pnd, - const nfc_modulation_t * pnmModulations, const size_t szModulations, +pn53x_initiator_poll_target (nfc_device * pnd, + const nfc_modulation * pnmModulations, const size_t szModulations, const uint8_t uiPollNr, const uint8_t uiPeriod, - nfc_target_t * pnt) + nfc_target * pnt) { if (CHIP_DATA(pnd)->type == PN532) { size_t szTargetTypes = 0; - pn53x_target_type_t apttTargetTypes[32]; + pn53x_target_type apttTargetTypes[32]; for (size_t n=0; niLastError = EINVALARG; return false; @@ -998,7 +998,7 @@ pn53x_initiator_poll_target (nfc_device_t * pnd, szTargetTypes++; } size_t szTargetFound = 0; - nfc_target_t ntTargets[2]; + nfc_target ntTargets[2]; if (!pn53x_InAutoPoll (pnd, apttTargetTypes, szTargetTypes, uiPollNr, uiPeriod, ntTargets, &szTargetFound)) return false; switch (szTargetFound) { @@ -1042,10 +1042,10 @@ pn53x_initiator_poll_target (nfc_device_t * pnd, } bool -pn53x_initiator_select_dep_target(nfc_device_t * pnd, - const nfc_dep_mode_t ndm, const nfc_baud_rate_t nbr, - const nfc_dep_info_t * pndiInitiator, - nfc_target_t * pnt) +pn53x_initiator_select_dep_target(nfc_device * pnd, + const nfc_dep_mode ndm, const nfc_baud_rate nbr, + const nfc_dep_info * pndiInitiator, + nfc_target * pnt) { const byte_t abtPassiveInitiatorData[] = { 0x00, 0xff, 0xff, 0x00, 0x00 }; // Only for 212/424 kpbs: First 4 bytes shall be set like this according to NFCIP-1, last byte is TSN (Time Slot Number) const byte_t * pbtPassiveInitiatorData = NULL; @@ -1070,7 +1070,7 @@ pn53x_initiator_select_dep_target(nfc_device_t * pnd, } bool -pn53x_initiator_transceive_bits (nfc_device_t * pnd, const byte_t * pbtTx, const size_t szTxBits, +pn53x_initiator_transceive_bits (nfc_device * pnd, const byte_t * pbtTx, const size_t szTxBits, const byte_t * pbtTxPar, byte_t * pbtRx, size_t * pszRxBits, byte_t * pbtRxPar) { size_t szFrameBits = 0; @@ -1134,7 +1134,7 @@ pn53x_initiator_transceive_bits (nfc_device_t * pnd, const byte_t * pbtTx, const } bool -pn53x_initiator_transceive_bytes (nfc_device_t * pnd, const byte_t * pbtTx, const size_t szTx, byte_t * pbtRx, +pn53x_initiator_transceive_bytes (nfc_device * pnd, const byte_t * pbtTx, const size_t szTx, byte_t * pbtRx, size_t * pszRx, struct timeval *timeout) { size_t szExtraTxLen; @@ -1180,7 +1180,7 @@ pn53x_initiator_transceive_bytes (nfc_device_t * pnd, const byte_t * pbtTx, cons return true; } -void __pn53x_init_timer(nfc_device_t * pnd, const uint32_t max_cycles) +void __pn53x_init_timer(nfc_device * pnd, const uint32_t max_cycles) { // The prescaler will dictate what will be the precision and // the largest delay to measure before saturation. Some examples: @@ -1201,7 +1201,7 @@ void __pn53x_init_timer(nfc_device_t * pnd, const uint32_t max_cycles) pn53x_write_register (pnd, PN53X_REG_CIU_TReloadVal_lo, 0xFF, reloadval & 0xFF); } -uint32_t __pn53x_get_timer(nfc_device_t * pnd, const uint8_t last_cmd_byte) +uint32_t __pn53x_get_timer(nfc_device * pnd, const uint8_t last_cmd_byte) { uint8_t parity; uint8_t counter_hi, counter_lo; @@ -1262,7 +1262,7 @@ uint32_t __pn53x_get_timer(nfc_device_t * pnd, const uint8_t last_cmd_byte) } bool -pn53x_initiator_transceive_bits_timed (nfc_device_t * pnd, const byte_t * pbtTx, const size_t szTxBits, +pn53x_initiator_transceive_bits_timed (nfc_device * pnd, const byte_t * pbtTx, const size_t szTxBits, const byte_t * pbtTxPar, byte_t * pbtRx, size_t * pszRxBits, byte_t * pbtRxPar, uint32_t * cycles) { // TODO Do something with these bytes... @@ -1364,7 +1364,7 @@ pn53x_initiator_transceive_bits_timed (nfc_device_t * pnd, const byte_t * pbtTx, } bool -pn53x_initiator_transceive_bytes_timed (nfc_device_t * pnd, const byte_t * pbtTx, const size_t szTx, byte_t * pbtRx, +pn53x_initiator_transceive_bytes_timed (nfc_device * pnd, const byte_t * pbtTx, const size_t szTx, byte_t * pbtRx, size_t * pszRx, uint32_t * cycles) { uint16_t i; @@ -1467,7 +1467,7 @@ pn53x_initiator_transceive_bytes_timed (nfc_device_t * pnd, const byte_t * pbtTx } bool -pn53x_initiator_deselect_target (nfc_device_t * pnd) +pn53x_initiator_deselect_target (nfc_device * pnd) { return (pn53x_InDeselect (pnd, 0)); // 0 mean deselect all selected targets } @@ -1475,13 +1475,13 @@ pn53x_initiator_deselect_target (nfc_device_t * pnd) #define SAK_ISO14443_4_COMPLIANT 0x20 #define SAK_ISO18092_COMPLIANT 0x40 bool -pn53x_target_init (nfc_device_t * pnd, nfc_target_t * pnt, byte_t * pbtRx, size_t * pszRx) +pn53x_target_init (nfc_device * pnd, nfc_target * pnt, byte_t * pbtRx, size_t * pszRx) { pn53x_reset_settings(pnd); CHIP_DATA (pnd)->operating_mode = TARGET; - pn53x_target_mode_t ptm = PTM_NORMAL; + pn53x_target_mode ptm = PTM_NORMAL; switch (pnt->nm.nmt) { case NMT_ISO14443A: @@ -1631,11 +1631,11 @@ pn53x_target_init (nfc_device_t * pnd, nfc_target_t * pnt, byte_t * pbtRx, size_ return false; } - nfc_modulation_t nm = { + nfc_modulation nm = { .nmt = NMT_DEP, // Silent compilation warnings .nbr = NBR_UNDEFINED }; - nfc_dep_mode_t ndm = NDM_UNDEFINED; + nfc_dep_mode ndm = NDM_UNDEFINED; // Decode activated "mode" switch(btActivatedMode & 0x70) { // Baud rate case 0x00: // 106kbps @@ -1681,8 +1681,8 @@ pn53x_target_init (nfc_device_t * pnd, nfc_target_t * pnt, byte_t * pbtRx, size_ if (CHIP_DATA (pnd)->current_target) { free (CHIP_DATA (pnd)->current_target); } - CHIP_DATA (pnd)->current_target = malloc (sizeof(nfc_target_t)); - memcpy (CHIP_DATA (pnd)->current_target, pnt, sizeof(nfc_target_t)); + CHIP_DATA (pnd)->current_target = malloc (sizeof(nfc_target)); + memcpy (CHIP_DATA (pnd)->current_target, pnt, sizeof(nfc_target)); if (ptm & PTM_ISO14443_4_PICC_ONLY) { // When PN532 is in PICC target mode, it automatically reply to RATS so @@ -1696,7 +1696,7 @@ pn53x_target_init (nfc_device_t * pnd, nfc_target_t * pnt, byte_t * pbtRx, size_ } bool -pn53x_target_receive_bits (nfc_device_t * pnd, byte_t * pbtRx, size_t * pszRxBits, byte_t * pbtRxPar) +pn53x_target_receive_bits (nfc_device * pnd, byte_t * pbtRx, size_t * pszRxBits, byte_t * pbtRxPar) { byte_t abtCmd[] = { TgGetInitiatorCommand }; @@ -1731,7 +1731,7 @@ pn53x_target_receive_bits (nfc_device_t * pnd, byte_t * pbtRx, size_t * pszRxBit } bool -pn53x_target_receive_bytes (nfc_device_t * pnd, byte_t * pbtRx, size_t * pszRx, struct timeval *timeout) +pn53x_target_receive_bytes (nfc_device * pnd, byte_t * pbtRx, size_t * pszRx, struct timeval *timeout) { byte_t abtCmd[1]; @@ -1780,7 +1780,7 @@ pn53x_target_receive_bytes (nfc_device_t * pnd, byte_t * pbtRx, size_t * pszRx, } bool -pn53x_target_send_bits (nfc_device_t * pnd, const byte_t * pbtTx, const size_t szTxBits, const byte_t * pbtTxPar) +pn53x_target_send_bits (nfc_device * pnd, const byte_t * pbtTx, const size_t szTxBits, const byte_t * pbtTxPar) { size_t szFrameBits = 0; size_t szFrameBytes = 0; @@ -1818,7 +1818,7 @@ pn53x_target_send_bits (nfc_device_t * pnd, const byte_t * pbtTx, const size_t s } bool -pn53x_target_send_bytes (nfc_device_t * pnd, const byte_t * pbtTx, const size_t szTx, struct timeval *timeout) +pn53x_target_send_bytes (nfc_device * pnd, const byte_t * pbtTx, const size_t szTx, struct timeval *timeout) { byte_t abtCmd[PN53x_EXTENDED_FRAME__DATA_MAX_LEN]; @@ -1917,7 +1917,7 @@ static struct sErrorMessage { }; const char * -pn53x_strerror (const nfc_device_t * pnd) +pn53x_strerror (const nfc_device * pnd) { const char *pcRes = "Unknown error"; size_t i; @@ -1933,14 +1933,14 @@ pn53x_strerror (const nfc_device_t * pnd) } bool -pn53x_RFConfiguration__RF_field (nfc_device_t * pnd, bool bEnable) +pn53x_RFConfiguration__RF_field (nfc_device * pnd, bool bEnable) { byte_t abtCmd[] = { RFConfiguration, RFCI_FIELD, (bEnable) ? 0x01 : 0x00 }; return pn53x_transceive (pnd, abtCmd, sizeof (abtCmd), NULL, NULL, NULL); } bool -pn53x_RFConfiguration__Various_timings (nfc_device_t * pnd, const uint8_t fATR_RES_Timeout, const uint8_t fRetryTimeout) +pn53x_RFConfiguration__Various_timings (nfc_device * pnd, const uint8_t fATR_RES_Timeout, const uint8_t fRetryTimeout) { byte_t abtCmd[] = { RFConfiguration, @@ -1953,7 +1953,7 @@ pn53x_RFConfiguration__Various_timings (nfc_device_t * pnd, const uint8_t fATR_R } bool -pn53x_RFConfiguration__MaxRtyCOM (nfc_device_t * pnd, const uint8_t MaxRtyCOM) +pn53x_RFConfiguration__MaxRtyCOM (nfc_device * pnd, const uint8_t MaxRtyCOM) { byte_t abtCmd[] = { RFConfiguration, @@ -1964,7 +1964,7 @@ pn53x_RFConfiguration__MaxRtyCOM (nfc_device_t * pnd, const uint8_t MaxRtyCOM) } bool -pn53x_RFConfiguration__MaxRetries (nfc_device_t * pnd, const uint8_t MxRtyATR, const uint8_t MxRtyPSL, const uint8_t MxRtyPassiveActivation) +pn53x_RFConfiguration__MaxRetries (nfc_device * pnd, const uint8_t MxRtyATR, const uint8_t MxRtyPSL, const uint8_t MxRtyPassiveActivation) { // Retry format: 0x00 means only 1 try, 0xff means infinite byte_t abtCmd[] = { @@ -1978,7 +1978,7 @@ pn53x_RFConfiguration__MaxRetries (nfc_device_t * pnd, const uint8_t MxRtyATR, c } bool -pn53x_SetParameters (nfc_device_t * pnd, const uint8_t ui8Value) +pn53x_SetParameters (nfc_device * pnd, const uint8_t ui8Value) { byte_t abtCmd[] = { SetParameters, ui8Value }; @@ -1991,7 +1991,7 @@ pn53x_SetParameters (nfc_device_t * pnd, const uint8_t ui8Value) } bool -pn53x_SAMConfiguration (nfc_device_t * pnd, const pn532_sam_mode ui8Mode, struct timeval *timeout) +pn53x_SAMConfiguration (nfc_device * pnd, const pn532_sam_mode ui8Mode, struct timeval *timeout) { byte_t abtCmd[] = { SAMConfiguration, ui8Mode, 0x00, 0x00 }; size_t szCmd = sizeof(abtCmd); @@ -2020,7 +2020,7 @@ pn53x_SAMConfiguration (nfc_device_t * pnd, const pn532_sam_mode ui8Mode, struct } bool -pn53x_PowerDown (nfc_device_t * pnd) +pn53x_PowerDown (nfc_device * pnd) { byte_t abtCmd[] = { PowerDown, 0xf0 }; return (pn53x_transceive (pnd, abtCmd, sizeof (abtCmd), NULL, NULL, NULL)); @@ -2030,7 +2030,7 @@ pn53x_PowerDown (nfc_device_t * pnd) * @brief C wrapper to InListPassiveTarget command * @return true if command is successfully sent * - * @param pnd nfc_device_t struct pointer that represent currently used device + * @param pnd nfc_device struct pointer that represent currently used device * @param pmInitModulation Desired modulation * @param pbtInitiatorData Optional initiator data used for Felica, ISO14443B, Topaz Polling or for ISO14443A selecting a specific UID * @param szInitiatorData Length of initiator data \a pbtInitiatorData @@ -2041,8 +2041,8 @@ pn53x_PowerDown (nfc_device_t * pnd) * @note To decode theses TargetData[n], there is @fn pn53x_decode_target_data */ bool -pn53x_InListPassiveTarget (nfc_device_t * pnd, - const pn53x_modulation_t pmInitModulation, const byte_t szMaxTargets, +pn53x_InListPassiveTarget (nfc_device * pnd, + const pn53x_modulation pmInitModulation, const byte_t szMaxTargets, const byte_t * pbtInitiatorData, const size_t szInitiatorData, byte_t * pbtTargetsData, size_t * pszTargetsData, struct timeval* timeout) @@ -2094,7 +2094,7 @@ pn53x_InListPassiveTarget (nfc_device_t * pnd, } bool -pn53x_InDeselect (nfc_device_t * pnd, const uint8_t ui8Target) +pn53x_InDeselect (nfc_device * pnd, const uint8_t ui8Target) { if (CHIP_DATA(pnd)->type == RCS360) { // We should do act here *only* if a target was previously selected @@ -2116,7 +2116,7 @@ pn53x_InDeselect (nfc_device_t * pnd, const uint8_t ui8Target) } bool -pn53x_InRelease (nfc_device_t * pnd, const uint8_t ui8Target) +pn53x_InRelease (nfc_device * pnd, const uint8_t ui8Target) { if (CHIP_DATA(pnd)->type == RCS360) { // We should do act here *only* if a target was previously selected @@ -2138,9 +2138,9 @@ pn53x_InRelease (nfc_device_t * pnd, const uint8_t ui8Target) } bool -pn53x_InAutoPoll (nfc_device_t * pnd, - const pn53x_target_type_t * ppttTargetTypes, const size_t szTargetTypes, - const byte_t btPollNr, const byte_t btPeriod, nfc_target_t * pntTargets, size_t * pszTargetFound) +pn53x_InAutoPoll (nfc_device * pnd, + const pn53x_target_type * ppttTargetTypes, const size_t szTargetTypes, + const byte_t btPollNr, const byte_t btPeriod, nfc_target * pntTargets, size_t * pszTargetFound) { if (CHIP_DATA(pnd)->type != PN532) { // This function is not supported by pn531 neither pn533 @@ -2168,7 +2168,7 @@ pn53x_InAutoPoll (nfc_device_t * pnd, byte_t *pbt = abtRx + 1; /* 1st target */ // Target type - pn53x_target_type_t ptt = *(pbt++); + pn53x_target_type ptt = *(pbt++); pntTargets[0].nm = pn53x_ptt_to_nm(ptt); // AutoPollTargetData length ln = *(pbt++); @@ -2197,16 +2197,16 @@ pn53x_InAutoPoll (nfc_device_t * pnd, * @param pbtNFCID3i NFCID3 of the initiator * @param pbtGBi General Bytes of the initiator * @param szGBi count of General Bytes - * @param[out] pnt \a nfc_target_t which will be filled by this function + * @param[out] pnt \a nfc_target which will be filled by this function */ bool -pn53x_InJumpForDEP (nfc_device_t * pnd, - const nfc_dep_mode_t ndm, - const nfc_baud_rate_t nbr, +pn53x_InJumpForDEP (nfc_device * pnd, + const nfc_dep_mode ndm, + const nfc_baud_rate nbr, const byte_t * pbtPassiveInitiatorData, const byte_t * pbtNFCID3i, const byte_t * pbtGBi, const size_t szGBi, - nfc_target_t * pnt) + nfc_target * pnt) { // Max frame size = 1 (Command) + 1 (ActPass) + 1 (Baud rate) + 1 (Next) + 5 (PassiveInitiatorData) + 10 (NFCID3) + 48 (General bytes) = 67 bytes byte_t abtCmd[67] = { InJumpForDEP, (ndm == NDM_ACTIVE) ? 0x01 : 0x00 }; @@ -2294,7 +2294,7 @@ pn53x_InJumpForDEP (nfc_device_t * pnd, } bool -pn53x_TgInitAsTarget (nfc_device_t * pnd, pn53x_target_mode_t ptm, +pn53x_TgInitAsTarget (nfc_device * pnd, pn53x_target_mode ptm, const byte_t * pbtMifareParams, const byte_t * pbtTkt, size_t szTkt, const byte_t * pbtFeliCaParams, @@ -2365,7 +2365,7 @@ pn53x_TgInitAsTarget (nfc_device_t * pnd, pn53x_target_mode_t ptm, } bool -pn53x_check_ack_frame (nfc_device_t * pnd, const byte_t * pbtRxFrame, const size_t szRxFrameLen) +pn53x_check_ack_frame (nfc_device * pnd, const byte_t * pbtRxFrame, const size_t szRxFrameLen) { if (szRxFrameLen >= sizeof (pn53x_ack_frame)) { if (0 == memcmp (pbtRxFrame, pn53x_ack_frame, sizeof (pn53x_ack_frame))) { @@ -2379,7 +2379,7 @@ pn53x_check_ack_frame (nfc_device_t * pnd, const byte_t * pbtRxFrame, const size } bool -pn53x_check_error_frame (nfc_device_t * pnd, const byte_t * pbtRxFrame, const size_t szRxFrameLen) +pn53x_check_error_frame (nfc_device * pnd, const byte_t * pbtRxFrame, const size_t szRxFrameLen) { if (szRxFrameLen >= sizeof (pn53x_error_frame)) { if (0 == memcmp (pbtRxFrame, pn53x_error_frame, sizeof (pn53x_error_frame))) { @@ -2453,8 +2453,8 @@ pn53x_build_frame (byte_t * pbtFrame, size_t * pszFrame, const byte_t * pbtData, } return true; } -pn53x_modulation_t -pn53x_nm_to_pm(const nfc_modulation_t nm) +pn53x_modulation +pn53x_nm_to_pm(const nfc_modulation nm) { switch(nm.nmt) { case NMT_ISO14443A: @@ -2511,8 +2511,8 @@ pn53x_nm_to_pm(const nfc_modulation_t nm) return PM_UNDEFINED; } -nfc_modulation_t -pn53x_ptt_to_nm( const pn53x_target_type_t ptt ) +nfc_modulation +pn53x_ptt_to_nm( const pn53x_target_type ptt ) { switch (ptt) { case PTT_GENERIC_PASSIVE_106: @@ -2524,44 +2524,44 @@ pn53x_ptt_to_nm( const pn53x_target_type_t ptt ) case PTT_MIFARE: case PTT_ISO14443_4A_106: - return (const nfc_modulation_t){ .nmt = NMT_ISO14443A, .nbr = NBR_106 }; + return (const nfc_modulation){ .nmt = NMT_ISO14443A, .nbr = NBR_106 }; break; case PTT_ISO14443_4B_106: case PTT_ISO14443_4B_TCL_106: - return (const nfc_modulation_t){ .nmt = NMT_ISO14443B, .nbr = NBR_106 }; + return (const nfc_modulation){ .nmt = NMT_ISO14443B, .nbr = NBR_106 }; break; case PTT_JEWEL_106: - return (const nfc_modulation_t){ .nmt = NMT_JEWEL, .nbr = NBR_106 }; + return (const nfc_modulation){ .nmt = NMT_JEWEL, .nbr = NBR_106 }; break; case PTT_FELICA_212: - return (const nfc_modulation_t){ .nmt = NMT_FELICA, .nbr = NBR_212 }; + return (const nfc_modulation){ .nmt = NMT_FELICA, .nbr = NBR_212 }; break; case PTT_FELICA_424: - return (const nfc_modulation_t){ .nmt = NMT_FELICA, .nbr = NBR_424 }; + return (const nfc_modulation){ .nmt = NMT_FELICA, .nbr = NBR_424 }; break; case PTT_DEP_PASSIVE_106: case PTT_DEP_ACTIVE_106: - return (const nfc_modulation_t){ .nmt = NMT_DEP, .nbr = NBR_106 }; + return (const nfc_modulation){ .nmt = NMT_DEP, .nbr = NBR_106 }; break; case PTT_DEP_PASSIVE_212: case PTT_DEP_ACTIVE_212: - return (const nfc_modulation_t){ .nmt = NMT_DEP, .nbr = NBR_212 }; + return (const nfc_modulation){ .nmt = NMT_DEP, .nbr = NBR_212 }; break; case PTT_DEP_PASSIVE_424: case PTT_DEP_ACTIVE_424: - return (const nfc_modulation_t){ .nmt = NMT_DEP, .nbr = NBR_424 }; + return (const nfc_modulation){ .nmt = NMT_DEP, .nbr = NBR_424 }; break; } // We should never be here, this line silent compilation warning - return (const nfc_modulation_t){ .nmt = NMT_ISO14443A, .nbr = NBR_106 }; + return (const nfc_modulation){ .nmt = NMT_ISO14443A, .nbr = NBR_106 }; } -pn53x_target_type_t -pn53x_nm_to_ptt(const nfc_modulation_t nm) +pn53x_target_type +pn53x_nm_to_ptt(const nfc_modulation nm) { switch(nm.nmt) { case NMT_ISO14443A: @@ -2614,7 +2614,7 @@ pn53x_nm_to_ptt(const nfc_modulation_t nm) } void -pn53x_data_new (nfc_device_t * pnd, const struct pn53x_io* io) +pn53x_data_new (nfc_device * pnd, const struct pn53x_io* io) { pnd->chip_data = malloc(sizeof(struct pn53x_data)); @@ -2640,7 +2640,7 @@ pn53x_data_new (nfc_device_t * pnd, const struct pn53x_io* io) } void -pn53x_data_free (nfc_device_t * pnd) +pn53x_data_free (nfc_device * pnd) { if (CHIP_DATA (pnd)->current_target) { free (CHIP_DATA (pnd)->current_target); diff --git a/libnfc/chips/pn53x.h b/libnfc/chips/pn53x.h index 9f32b6f..4e1a53b 100644 --- a/libnfc/chips/pn53x.h +++ b/libnfc/chips/pn53x.h @@ -127,8 +127,8 @@ typedef enum { } pn53x_operating_mode; struct pn53x_io { - bool (*send)(nfc_device_t * pnd, const byte_t * pbtData, const size_t szData, struct timeval *timeout); - int (*receive)(nfc_device_t * pnd, byte_t * pbtData, const size_t szDataLen, struct timeval *timeout); + bool (*send)(nfc_device * pnd, const byte_t * pbtData, const size_t szData, struct timeval *timeout); + int (*receive)(nfc_device * pnd, byte_t * pbtData, const size_t szDataLen, struct timeval *timeout); }; /* defines */ @@ -143,7 +143,7 @@ struct pn53x_data { /** Current operating mode */ pn53x_operating_mode operating_mode; /** Current emulated target */ - nfc_target_t* current_target; + nfc_target* current_target; /** PN53x I/O functions stored in struct */ const struct pn53x_io * io; /** Register cache for REG_CIU_BIT_FRAMING, SYMBOL_TX_LAST_BITS: The last TX bits setting, we need to reset this if it does not apply anymore */ @@ -165,7 +165,7 @@ struct pn53x_data { #define CHIP_DATA(pnd) ((struct pn53x_data*)(pnd->chip_data)) /** - * @enum pn53x_modulation_t + * @enum pn53x_modulation * @brief NFC modulation */ typedef enum { @@ -187,10 +187,10 @@ typedef enum { PM_ISO14443B_424 = 0x07, /** ISO14443-B http://en.wikipedia.org/wiki/ISO/IEC_14443 (Not supported by PN531 nor PN532) */ PM_ISO14443B_847 = 0x08, -} pn53x_modulation_t; +} pn53x_modulation; /** - * @enum pn53x_target_type_t + * @enum pn53x_target_type * @brief NFC target type enumeration */ typedef enum { @@ -228,7 +228,7 @@ typedef enum { PTT_DEP_ACTIVE_212 = 0x81, /** DEP active 424 kbps */ PTT_DEP_ACTIVE_424 = 0x82, -} pn53x_target_type_t; +} pn53x_target_type; typedef enum { PSM_NORMAL = 0x01, @@ -238,7 +238,7 @@ typedef enum { } pn532_sam_mode; /** - * @enum pn53x_target_mode_t + * @enum pn53x_target_mode * @brief PN53x target mode enumeration */ typedef enum { @@ -250,86 +250,86 @@ typedef enum { PTM_DEP_ONLY = 0x02, /** Configure the PN532 to accept to be initialized only as ISO/IEC14443-4 PICC */ PTM_ISO14443_4_PICC_ONLY = 0x04 -} pn53x_target_mode_t; +} pn53x_target_mode; extern const byte_t pn53x_ack_frame[6]; extern const byte_t pn53x_nack_frame[6]; -bool pn53x_init(nfc_device_t * pnd); -bool pn53x_transceive (nfc_device_t * pnd, const byte_t * pbtTx, const size_t szTx, byte_t * pbtRx, size_t *pszRx, struct timeval *timeout); +bool pn53x_init(nfc_device * pnd); +bool pn53x_transceive (nfc_device * pnd, const byte_t * pbtTx, const size_t szTx, byte_t * pbtRx, size_t *pszRx, struct timeval *timeout); -bool pn53x_set_parameters (nfc_device_t * pnd, const uint8_t ui8Value, const bool bEnable); -bool pn53x_set_tx_bits (nfc_device_t * pnd, const uint8_t ui8Bits); +bool pn53x_set_parameters (nfc_device * pnd, const uint8_t ui8Value, const bool bEnable); +bool pn53x_set_tx_bits (nfc_device * pnd, const uint8_t ui8Bits); bool pn53x_wrap_frame (const byte_t * pbtTx, const size_t szTxBits, const byte_t * pbtTxPar, byte_t * pbtFrame, size_t * pszFrameBits); bool pn53x_unwrap_frame (const byte_t * pbtFrame, const size_t szFrameBits, byte_t * pbtRx, size_t * pszRxBits, byte_t * pbtRxPar); bool pn53x_decode_target_data (const byte_t * pbtRawData, size_t szRawData, - pn53x_type chip_type, nfc_modulation_type_t nmt, - nfc_target_info_t * pnti); -bool pn53x_read_register (nfc_device_t * pnd, uint16_t ui16Reg, uint8_t * ui8Value); -bool pn53x_write_register (nfc_device_t * pnd, uint16_t ui16Reg, uint8_t ui8SymbolMask, uint8_t ui8Value); -bool pn53x_get_firmware_version (nfc_device_t * pnd, char abtFirmwareText[22]); -bool pn53x_configure (nfc_device_t * pnd, const nfc_device_option_t ndo, const bool bEnable); -bool pn53x_check_communication (nfc_device_t *pnd); -bool pn53x_idle (nfc_device_t * pnd); + pn53x_type chip_type, nfc_modulationype nmt, + nfc_target_info * pnti); +bool pn53x_read_register (nfc_device * pnd, uint16_t ui16Reg, uint8_t * ui8Value); +bool pn53x_write_register (nfc_device * pnd, uint16_t ui16Reg, uint8_t ui8SymbolMask, uint8_t ui8Value); +bool pn53x_get_firmware_version (nfc_device * pnd, char abtFirmwareText[22]); +bool pn53x_configure (nfc_device * pnd, const nfc_device_option ndo, const bool bEnable); +bool pn53x_check_communication (nfc_device *pnd); +bool pn53x_idle (nfc_device * pnd); // NFC device as Initiator functions -bool pn53x_initiator_init (nfc_device_t * pnd); -bool pn53x_initiator_select_passive_target (nfc_device_t * pnd, - const nfc_modulation_t nm, +bool pn53x_initiator_init (nfc_device * pnd); +bool pn53x_initiator_select_passive_target (nfc_device * pnd, + const nfc_modulation nm, const byte_t * pbtInitData, const size_t szInitData, - nfc_target_t * pnt); -bool pn53x_initiator_poll_target (nfc_device_t * pnd, - const nfc_modulation_t * pnmModulations, const size_t szModulations, + nfc_target * pnt); +bool pn53x_initiator_poll_target (nfc_device * pnd, + const nfc_modulation * pnmModulations, const size_t szModulations, const uint8_t uiPollNr, const uint8_t uiPeriod, - nfc_target_t * pnt); -bool pn53x_initiator_select_dep_target (nfc_device_t * pnd, - const nfc_dep_mode_t ndm, const nfc_baud_rate_t nbr, - const nfc_dep_info_t * pndiInitiator, - nfc_target_t * pnt); -bool pn53x_initiator_transceive_bits (nfc_device_t * pnd, const byte_t * pbtTx, const size_t szTxBits, + nfc_target * pnt); +bool pn53x_initiator_select_dep_target (nfc_device * pnd, + const nfc_dep_mode ndm, const nfc_baud_rate nbr, + const nfc_dep_info * pndiInitiator, + nfc_target * pnt); +bool pn53x_initiator_transceive_bits (nfc_device * pnd, const byte_t * pbtTx, const size_t szTxBits, const byte_t * pbtTxPar, byte_t * pbtRx, size_t * pszRxBits, byte_t * pbtRxPar); -bool pn53x_initiator_transceive_bytes (nfc_device_t * pnd, const byte_t * pbtTx, const size_t szTx, +bool pn53x_initiator_transceive_bytes (nfc_device * pnd, const byte_t * pbtTx, const size_t szTx, byte_t * pbtRx, size_t * pszRx, struct timeval *timeout); -bool pn53x_initiator_transceive_bits_timed (nfc_device_t * pnd, const byte_t * pbtTx, const size_t szTxBits, +bool pn53x_initiator_transceive_bits_timed (nfc_device * pnd, const byte_t * pbtTx, const size_t szTxBits, const byte_t * pbtTxPar, byte_t * pbtRx, size_t * pszRxBits, byte_t * pbtRxPar, uint32_t * cycles); -bool pn53x_initiator_transceive_bytes_timed (nfc_device_t * pnd, const byte_t * pbtTx, const size_t szTx, +bool pn53x_initiator_transceive_bytes_timed (nfc_device * pnd, const byte_t * pbtTx, const size_t szTx, byte_t * pbtRx, size_t * pszRx, uint32_t * cycles); -bool pn53x_initiator_deselect_target (nfc_device_t * pnd); +bool pn53x_initiator_deselect_target (nfc_device * pnd); // NFC device as Target functions -bool pn53x_target_init (nfc_device_t * pnd, nfc_target_t * pnt, byte_t * pbtRx, size_t * pszRx); -bool pn53x_target_receive_bits (nfc_device_t * pnd, byte_t * pbtRx, size_t * pszRxBits, byte_t * pbtRxPar); -bool pn53x_target_receive_bytes (nfc_device_t * pnd, byte_t * pbtRx, size_t * pszRx, struct timeval *timeout); -bool pn53x_target_send_bits (nfc_device_t * pnd, const byte_t * pbtTx, const size_t szTxBits, const byte_t * pbtTxPar); -bool pn53x_target_send_bytes (nfc_device_t * pnd, const byte_t * pbtTx, const size_t szTx, struct timeval *timeout); +bool pn53x_target_init (nfc_device * pnd, nfc_target * pnt, byte_t * pbtRx, size_t * pszRx); +bool pn53x_target_receive_bits (nfc_device * pnd, byte_t * pbtRx, size_t * pszRxBits, byte_t * pbtRxPar); +bool pn53x_target_receive_bytes (nfc_device * pnd, byte_t * pbtRx, size_t * pszRx, struct timeval *timeout); +bool pn53x_target_send_bits (nfc_device * pnd, const byte_t * pbtTx, const size_t szTxBits, const byte_t * pbtTxPar); +bool pn53x_target_send_bytes (nfc_device * pnd, const byte_t * pbtTx, const size_t szTx, struct timeval *timeout); // Error handling functions -const char *pn53x_strerror (const nfc_device_t * pnd); +const char *pn53x_strerror (const nfc_device * pnd); // C wrappers for PN53x commands -bool pn53x_SetParameters (nfc_device_t * pnd, const uint8_t ui8Value); -bool pn53x_SAMConfiguration (nfc_device_t * pnd, const pn532_sam_mode mode, struct timeval *timeout); -bool pn53x_PowerDown (nfc_device_t * pnd); -bool pn53x_InListPassiveTarget (nfc_device_t * pnd, const pn53x_modulation_t pmInitModulation, +bool pn53x_SetParameters (nfc_device * pnd, const uint8_t ui8Value); +bool pn53x_SAMConfiguration (nfc_device * pnd, const pn532_sam_mode mode, struct timeval *timeout); +bool pn53x_PowerDown (nfc_device * pnd); +bool pn53x_InListPassiveTarget (nfc_device * pnd, const pn53x_modulation pmInitModulation, const byte_t szMaxTargets, const byte_t * pbtInitiatorData, const size_t szInitiatorDataLen, byte_t * pbtTargetsData, size_t * pszTargetsData, struct timeval *timeout); -bool pn53x_InDeselect (nfc_device_t * pnd, const uint8_t ui8Target); -bool pn53x_InRelease (nfc_device_t * pnd, const uint8_t ui8Target); -bool pn53x_InAutoPoll (nfc_device_t * pnd, const pn53x_target_type_t * ppttTargetTypes, const size_t szTargetTypes, - const byte_t btPollNr, const byte_t btPeriod, nfc_target_t * pntTargets, +bool pn53x_InDeselect (nfc_device * pnd, const uint8_t ui8Target); +bool pn53x_InRelease (nfc_device * pnd, const uint8_t ui8Target); +bool pn53x_InAutoPoll (nfc_device * pnd, const pn53x_target_type * ppttTargetTypes, const size_t szTargetTypes, + const byte_t btPollNr, const byte_t btPeriod, nfc_target * pntTargets, size_t * pszTargetFound); -bool pn53x_InJumpForDEP (nfc_device_t * pnd, - const nfc_dep_mode_t ndm, const nfc_baud_rate_t nbr, +bool pn53x_InJumpForDEP (nfc_device * pnd, + const nfc_dep_mode ndm, const nfc_baud_rate nbr, const byte_t * pbtPassiveInitiatorData, const byte_t * pbtNFCID3i, const byte_t * pbtGB, const size_t szGB, - nfc_target_t * pnt); -bool pn53x_TgInitAsTarget (nfc_device_t * pnd, pn53x_target_mode_t ptm, + nfc_target * pnt); +bool pn53x_TgInitAsTarget (nfc_device * pnd, pn53x_target_mode ptm, const byte_t * pbtMifareParams, const byte_t * pbtTkt, size_t szTkt, const byte_t * pbtFeliCaParams, @@ -337,17 +337,17 @@ bool pn53x_TgInitAsTarget (nfc_device_t * pnd, pn53x_target_mode_t ptm, byte_t * pbtRx, size_t * pszRx, byte_t * pbtModeByte); // RFConfiguration -bool pn53x_RFConfiguration__RF_field (nfc_device_t * pnd, bool bEnable); -bool pn53x_RFConfiguration__Various_timings (nfc_device_t * pnd, const uint8_t fATR_RES_Timeout, const uint8_t fRetryTimeout); -bool pn53x_RFConfiguration__MaxRtyCOM (nfc_device_t * pnd, const uint8_t MaxRtyCOM); -bool pn53x_RFConfiguration__MaxRetries (nfc_device_t * pnd, const uint8_t MxRtyATR, const uint8_t MxRtyPSL, const uint8_t MxRtyPassiveActivation); +bool pn53x_RFConfiguration__RF_field (nfc_device * pnd, bool bEnable); +bool pn53x_RFConfiguration__Various_timings (nfc_device * pnd, const uint8_t fATR_RES_Timeout, const uint8_t fRetryTimeout); +bool pn53x_RFConfiguration__MaxRtyCOM (nfc_device * pnd, const uint8_t MaxRtyCOM); +bool pn53x_RFConfiguration__MaxRetries (nfc_device * pnd, const uint8_t MxRtyATR, const uint8_t MxRtyPSL, const uint8_t MxRtyPassiveActivation); // Misc -bool pn53x_check_ack_frame (nfc_device_t * pnd, const byte_t * pbtRxFrame, const size_t szRxFrameLen); -bool pn53x_check_error_frame (nfc_device_t * pnd, const byte_t * pbtRxFrame, const size_t szRxFrameLen); +bool pn53x_check_ack_frame (nfc_device * pnd, const byte_t * pbtRxFrame, const size_t szRxFrameLen); +bool pn53x_check_error_frame (nfc_device * pnd, const byte_t * pbtRxFrame, const size_t szRxFrameLen); bool pn53x_build_frame (byte_t * pbtFrame, size_t * pszFrame, const byte_t * pbtData, const size_t szData); -void pn53x_data_new (nfc_device_t * pnd, const struct pn53x_io* io); -void pn53x_data_free (nfc_device_t * pnd); +void pn53x_data_new (nfc_device * pnd, const struct pn53x_io* io); +void pn53x_data_free (nfc_device * pnd); #endif // __NFC_CHIPS_PN53X_H__ diff --git a/libnfc/drivers/acr122.c b/libnfc/drivers/acr122.c index 419f7ad..3c3f349 100644 --- a/libnfc/drivers/acr122.c +++ b/libnfc/drivers/acr122.c @@ -76,7 +76,7 @@ const struct pn53x_io acr122_io; -char *acr122_firmware (nfc_device_t *pnd); +char *acr122_firmware (nfc_device *pnd); const char *supported_devices[] = { "ACS ACR122", // ACR122U & Touchatag, last version @@ -239,7 +239,7 @@ acr122_connstring_decode (const nfc_connstring connstring, struct acr122_descrip return 3; } -nfc_device_t * +nfc_device * acr122_connect (const nfc_connstring connstring) { struct acr122_descriptor ndd; @@ -251,7 +251,7 @@ acr122_connect (const nfc_connstring connstring) // FIXME: acr122_connect() does not take care about bus index char *pcFirmware; - nfc_device_t *pnd = nfc_device_new (); + nfc_device *pnd = nfc_device_new (); pnd->driver_data = malloc (sizeof (struct acr122_data)); // Alloc and init chip's data @@ -300,7 +300,7 @@ error: } void -acr122_disconnect (nfc_device_t * pnd) +acr122_disconnect (nfc_device * pnd) { SCardDisconnect (DRIVER_DATA (pnd)->hCard, SCARD_LEAVE_CARD); acr122_free_scardcontext (); @@ -310,7 +310,7 @@ acr122_disconnect (nfc_device_t * pnd) } bool -acr122_send (nfc_device_t * pnd, const byte_t * pbtData, const size_t szData, struct timeval *timeout) +acr122_send (nfc_device * pnd, const byte_t * pbtData, const size_t szData, struct timeval *timeout) { // FIXME: timeout is not handled (void) timeout; @@ -381,7 +381,7 @@ acr122_send (nfc_device_t * pnd, const byte_t * pbtData, const size_t szData, st } int -acr122_receive (nfc_device_t * pnd, byte_t * pbtData, const size_t szData, struct timeval *timeout) +acr122_receive (nfc_device * pnd, byte_t * pbtData, const size_t szData, struct timeval *timeout) { // FIXME: timeout is not handled (void) timeout; @@ -422,7 +422,7 @@ acr122_receive (nfc_device_t * pnd, byte_t * pbtData, const size_t szData, struc } char * -acr122_firmware (nfc_device_t *pnd) +acr122_firmware (nfc_device *pnd) { byte_t abtGetFw[5] = { 0xFF, 0x00, 0x48, 0x00, 0x00 }; uint32_t uiResult; @@ -445,7 +445,7 @@ acr122_firmware (nfc_device_t *pnd) #if 0 bool -acr122_led_red (nfc_device_t *pnd, bool bOn) +acr122_led_red (nfc_device *pnd, bool bOn) { byte_t abtLed[9] = { 0xFF, 0x00, 0x40, 0x05, 0x04, 0x00, 0x00, 0x00, 0x00 }; byte_t abtBuf[2]; diff --git a/libnfc/drivers/acr122.h b/libnfc/drivers/acr122.h index 0a2c087..a139cb2 100644 --- a/libnfc/drivers/acr122.h +++ b/libnfc/drivers/acr122.h @@ -29,10 +29,10 @@ bool acr122_probe (nfc_connstring connstrings[], size_t connstrings_len, size_t * pszDeviceFound); // Functions used by developer to handle connection to this device -nfc_device_t *acr122_connect (const nfc_connstring connstring); -bool acr122_send (nfc_device_t * pnd, const byte_t * pbtData, const size_t szData, struct timeval *timeout); -int acr122_receive (nfc_device_t * pnd, byte_t * pbtData, const size_t szData, struct timeval *timeout); -void acr122_disconnect (nfc_device_t * pnd); +nfc_device *acr122_connect (const nfc_connstring connstring); +bool acr122_send (nfc_device * pnd, const byte_t * pbtData, const size_t szData, struct timeval *timeout); +int acr122_receive (nfc_device * pnd, byte_t * pbtData, const size_t szData, struct timeval *timeout); +void acr122_disconnect (nfc_device * pnd); extern const struct nfc_driver_t acr122_driver; diff --git a/libnfc/drivers/arygon.c b/libnfc/drivers/arygon.c index 2ff78c6..d39cd07 100644 --- a/libnfc/drivers/arygon.c +++ b/libnfc/drivers/arygon.c @@ -88,8 +88,8 @@ static const byte_t arygon_error_none[] = "FF000000\x0d\x0a"; static const byte_t arygon_error_incomplete_command[] = "FF0C0000\x0d\x0a"; static const byte_t arygon_error_unknown_mode[] = "FF060000\x0d\x0a"; -bool arygon_reset_tama (nfc_device_t * pnd); -void arygon_firmware (nfc_device_t * pnd, char * str); +bool arygon_reset_tama (nfc_device * pnd); +void arygon_firmware (nfc_device * pnd, char * str); bool arygon_probe (nfc_connstring connstrings[], size_t connstrings_len, size_t * pszDeviceFound) @@ -120,7 +120,7 @@ arygon_probe (nfc_connstring connstrings[], size_t connstrings_len, size_t * psz uart_flush_input (sp); uart_set_speed (sp, ARYGON_DEFAULT_SPEED); - nfc_device_t *pnd = nfc_device_new (); + nfc_device *pnd = nfc_device_new (); pnd->driver = &arygon_driver; pnd->driver_data = malloc(sizeof(struct arygon_data)); DRIVER_DATA (pnd)->port = sp; @@ -215,7 +215,7 @@ arygon_connstring_decode (const nfc_connstring connstring, struct arygon_descrip return 3; } -nfc_device_t * +nfc_device * arygon_connect (const nfc_connstring connstring) { struct arygon_descriptor ndd; @@ -228,7 +228,7 @@ arygon_connect (const nfc_connstring connstring) ndd.speed = ARYGON_DEFAULT_SPEED; } serial_port sp; - nfc_device_t *pnd = NULL; + nfc_device *pnd = NULL; log_put (LOG_CATEGORY, NFC_PRIORITY_TRACE, "Attempt to connect to: %s at %d bauds.", ndd.port, ndd.speed); sp = uart_open (ndd.port); @@ -286,7 +286,7 @@ arygon_connect (const nfc_connstring connstring) } void -arygon_disconnect (nfc_device_t * pnd) +arygon_disconnect (nfc_device * pnd) { // Release UART port uart_close (DRIVER_DATA (pnd)->port); @@ -304,7 +304,7 @@ arygon_disconnect (nfc_device_t * pnd) #define ARYGON_TX_BUFFER_LEN (PN53x_NORMAL_FRAME__DATA_MAX_LEN + PN53x_NORMAL_FRAME__OVERHEAD + 1) #define ARYGON_RX_BUFFER_LEN (PN53x_EXTENDED_FRAME__DATA_MAX_LEN + PN53x_EXTENDED_FRAME__OVERHEAD) bool -arygon_tama_send (nfc_device_t * pnd, const byte_t * pbtData, const size_t szData, struct timeval *timeout) +arygon_tama_send (nfc_device * pnd, const byte_t * pbtData, const size_t szData, struct timeval *timeout) { // Before sending anything, we need to discard from any junk bytes uart_flush_input (DRIVER_DATA(pnd)->port); @@ -354,7 +354,7 @@ arygon_tama_send (nfc_device_t * pnd, const byte_t * pbtData, const size_t szDat } int -arygon_abort (nfc_device_t *pnd) +arygon_abort (nfc_device *pnd) { // Send a valid TAMA packet to wakup the PN53x (we will not have an answer, according to Arygon manual) byte_t dummy[] = { 0x32, 0x00, 0x00, 0xff, 0x09, 0xf7, 0xd4, 0x00, 0x00, 0x6c, 0x69, 0x62, 0x6e, 0x66, 0x63, 0xbe, 0x00 }; @@ -366,7 +366,7 @@ arygon_abort (nfc_device_t *pnd) } int -arygon_tama_receive (nfc_device_t * pnd, byte_t * pbtData, const size_t szDataLen, struct timeval *timeout) +arygon_tama_receive (nfc_device * pnd, byte_t * pbtData, const size_t szDataLen, struct timeval *timeout) { byte_t abtRxBuf[5]; size_t len; @@ -484,7 +484,7 @@ arygon_tama_receive (nfc_device_t * pnd, byte_t * pbtData, const size_t szDataLe } void -arygon_firmware (nfc_device_t * pnd, char * str) +arygon_firmware (nfc_device * pnd, char * str) { const byte_t arygon_firmware_version_cmd[] = { DEV_ARYGON_PROTOCOL_ARYGON_ASCII, 'a', 'v' }; byte_t abtRx[16]; @@ -512,7 +512,7 @@ arygon_firmware (nfc_device_t * pnd, char * str) } bool -arygon_reset_tama (nfc_device_t * pnd) +arygon_reset_tama (nfc_device * pnd) { const byte_t arygon_reset_tama_cmd[] = { DEV_ARYGON_PROTOCOL_ARYGON_ASCII, 'a', 'r' }; byte_t abtRx[10]; // Attempted response is 10 bytes long @@ -541,7 +541,7 @@ arygon_reset_tama (nfc_device_t * pnd) } bool -arygon_abort_command (nfc_device_t * pnd) +arygon_abort_command (nfc_device * pnd) { if (pnd) { #ifndef WIN32 diff --git a/libnfc/drivers/arygon.h b/libnfc/drivers/arygon.h index 6ef6ff3..482a97b 100644 --- a/libnfc/drivers/arygon.h +++ b/libnfc/drivers/arygon.h @@ -32,11 +32,11 @@ bool arygon_probe (nfc_connstring connstrings[], size_t connstrings_len, size_t * pszDeviceFound); -nfc_device_t *arygon_connect (const nfc_connstring connstring); -void arygon_disconnect (nfc_device_t * pnd); +nfc_device *arygon_connect (const nfc_connstring connstring); +void arygon_disconnect (nfc_device * pnd); -bool arygon_tama_send (nfc_device_t * pnd, const byte_t * pbtData, const size_t szData, struct timeval *timeout); -int arygon_tama_receive (nfc_device_t * pnd, byte_t * pbtData, const size_t szDat, struct timeval *timeouta); +bool arygon_tama_send (nfc_device * pnd, const byte_t * pbtData, const size_t szData, struct timeval *timeout); +int arygon_tama_receive (nfc_device * pnd, byte_t * pbtData, const size_t szDat, struct timeval *timeouta); extern const struct nfc_driver_t arygon_driver; diff --git a/libnfc/drivers/pn532_uart.c b/libnfc/drivers/pn532_uart.c index 688323d..45ec262 100644 --- a/libnfc/drivers/pn532_uart.c +++ b/libnfc/drivers/pn532_uart.c @@ -48,8 +48,8 @@ #define PN532_UART_DRIVER_NAME "pn532_uart" #define LOG_CATEGORY "libnfc.driver.pn532_uart" -int pn532_uart_ack (nfc_device_t * pnd); -int pn532_uart_wakeup (nfc_device_t * pnd); +int pn532_uart_ack (nfc_device * pnd); +int pn532_uart_wakeup (nfc_device * pnd); const struct pn53x_io pn532_uart_io; @@ -94,7 +94,7 @@ pn532_uart_probe (nfc_connstring connstrings[], size_t connstrings_len, size_t * // Serial port claimed but we need to check if a PN532_UART is connected. uart_set_speed (sp, PN532_UART_DEFAULT_SPEED); - nfc_device_t *pnd = nfc_device_new (); + nfc_device *pnd = nfc_device_new (); pnd->driver = &pn532_uart_driver; pnd->driver_data = malloc(sizeof(struct pn532_uart_data)); DRIVER_DATA (pnd)->port = sp; @@ -196,7 +196,7 @@ pn532_connstring_decode (const nfc_connstring connstring, struct pn532_uart_desc return 3; } -nfc_device_t * +nfc_device * pn532_uart_connect (const nfc_connstring connstring) { struct pn532_uart_descriptor ndd; @@ -209,7 +209,7 @@ pn532_uart_connect (const nfc_connstring connstring) ndd.speed = PN532_UART_DEFAULT_SPEED; } serial_port sp; - nfc_device_t *pnd = NULL; + nfc_device *pnd = NULL; log_put (LOG_CATEGORY, NFC_PRIORITY_TRACE, "Attempt to connect to: %s at %d bauds.", ndd.port, ndd.speed); sp = uart_open (ndd.port); @@ -262,7 +262,7 @@ pn532_uart_connect (const nfc_connstring connstring) } void -pn532_uart_disconnect (nfc_device_t * pnd) +pn532_uart_disconnect (nfc_device * pnd) { // Release UART port uart_close (DRIVER_DATA(pnd)->port); @@ -278,7 +278,7 @@ pn532_uart_disconnect (nfc_device_t * pnd) } int -pn532_uart_wakeup (nfc_device_t * pnd) +pn532_uart_wakeup (nfc_device * pnd) { /* High Speed Unit (HSU) wake up consist to send 0x55 and wait a "long" delay for PN532 being wakeup. */ const byte_t pn532_wakeup_preamble[] = { 0x55, 0x55, 0x00, 0x00, 0x00 }; @@ -289,7 +289,7 @@ pn532_uart_wakeup (nfc_device_t * pnd) #define PN532_BUFFER_LEN (PN53x_EXTENDED_FRAME__DATA_MAX_LEN + PN53x_EXTENDED_FRAME__OVERHEAD) bool -pn532_uart_send (nfc_device_t * pnd, const byte_t * pbtData, const size_t szData, struct timeval *timeout) +pn532_uart_send (nfc_device * pnd, const byte_t * pbtData, const size_t szData, struct timeval *timeout) { // Before sending anything, we need to discard from any junk bytes uart_flush_input (DRIVER_DATA(pnd)->port); @@ -352,7 +352,7 @@ pn532_uart_send (nfc_device_t * pnd, const byte_t * pbtData, const size_t szData } int -pn532_uart_receive (nfc_device_t * pnd, byte_t * pbtData, const size_t szDataLen, struct timeval *timeout) +pn532_uart_receive (nfc_device * pnd, byte_t * pbtData, const size_t szDataLen, struct timeval *timeout) { byte_t abtRxBuf[5]; size_t len; @@ -476,7 +476,7 @@ pn532_uart_receive (nfc_device_t * pnd, byte_t * pbtData, const size_t szDataLen } int -pn532_uart_ack (nfc_device_t * pnd) +pn532_uart_ack (nfc_device * pnd) { if (POWERDOWN == CHIP_DATA(pnd)->power_mode) { if (-1 == pn532_uart_wakeup(pnd)) { @@ -487,7 +487,7 @@ pn532_uart_ack (nfc_device_t * pnd) } bool -pn532_uart_abort_command (nfc_device_t * pnd) +pn532_uart_abort_command (nfc_device * pnd) { if (pnd) { #ifndef WIN32 diff --git a/libnfc/drivers/pn532_uart.h b/libnfc/drivers/pn532_uart.h index 5f8f344..20e0ca6 100644 --- a/libnfc/drivers/pn532_uart.h +++ b/libnfc/drivers/pn532_uart.h @@ -31,10 +31,10 @@ bool pn532_uart_probe (nfc_connstring connstrings[], size_t connstrings_len, size_t * pszDeviceFound); -nfc_device_t *pn532_uart_connect (const nfc_connstring connstring); -void pn532_uart_disconnect (nfc_device_t * pnd); -bool pn532_uart_send (nfc_device_t * pnd, const byte_t * pbtData, const size_t szData, struct timeval *timeout); -int pn532_uart_receive (nfc_device_t * pnd, byte_t * pbtData, const size_t szData, struct timeval *timeout); +nfc_device *pn532_uart_connect (const nfc_connstring connstring); +void pn532_uart_disconnect (nfc_device * pnd); +bool pn532_uart_send (nfc_device * pnd, const byte_t * pbtData, const size_t szData, struct timeval *timeout); +int pn532_uart_receive (nfc_device * pnd, byte_t * pbtData, const size_t szData, struct timeval *timeout); extern const struct nfc_driver_t pn532_uart_driver; diff --git a/libnfc/drivers/pn53x_usb.c b/libnfc/drivers/pn53x_usb.c index aae9203..41e92ca 100644 --- a/libnfc/drivers/pn53x_usb.c +++ b/libnfc/drivers/pn53x_usb.c @@ -118,7 +118,7 @@ struct pn53x_usb_data { const struct pn53x_io pn53x_usb_io; bool pn53x_usb_get_usb_device_name (struct usb_device *dev, usb_dev_handle *udev, char *buffer, size_t len); -bool pn53x_usb_init (nfc_device_t *pnd); +bool pn53x_usb_init (nfc_device *pnd); int pn53x_usb_bulk_read (struct pn53x_usb_data *data, byte_t abtRx[], const size_t szRx, struct timeval *timeout) @@ -190,7 +190,7 @@ pn53x_usb_get_device_model (uint16_t vendor_id, uint16_t product_id) return UNKNOWN; } -int pn53x_usb_ack (nfc_device_t * pnd); +int pn53x_usb_ack (nfc_device * pnd); // Find transfer endpoints for bulk transfers void @@ -378,7 +378,7 @@ pn53x_usb_get_usb_device_name (struct usb_device *dev, usb_dev_handle *udev, cha return false; } -nfc_device_t * +nfc_device * pn53x_usb_connect (const nfc_connstring connstring) { struct pn53x_usb_descriptor desc; @@ -388,7 +388,7 @@ pn53x_usb_connect (const nfc_connstring connstring) return NULL; } - nfc_device_t *pnd = NULL; + nfc_device *pnd = NULL; struct pn53x_usb_data data = { .pudh = NULL, .uiEndPointIn = 0, @@ -492,7 +492,7 @@ error: } void -pn53x_usb_disconnect (nfc_device_t * pnd) +pn53x_usb_disconnect (nfc_device * pnd) { pn53x_usb_ack (pnd); @@ -519,7 +519,7 @@ pn53x_usb_disconnect (nfc_device_t * pnd) #define PN53X_USB_BUFFER_LEN (PN53x_EXTENDED_FRAME__DATA_MAX_LEN + PN53x_EXTENDED_FRAME__OVERHEAD) bool -pn53x_usb_send (nfc_device_t * pnd, const byte_t * pbtData, const size_t szData, struct timeval *timeout) +pn53x_usb_send (nfc_device * pnd, const byte_t * pbtData, const size_t szData, struct timeval *timeout) { byte_t abtFrame[PN53X_USB_BUFFER_LEN] = { 0x00, 0x00, 0xff }; // Every packet must start with "00 00 ff" size_t szFrame = 0; @@ -565,7 +565,7 @@ pn53x_usb_send (nfc_device_t * pnd, const byte_t * pbtData, const size_t szData, } int -pn53x_usb_receive (nfc_device_t * pnd, byte_t * pbtData, const size_t szDataLen, struct timeval *timeout) +pn53x_usb_receive (nfc_device * pnd, byte_t * pbtData, const size_t szDataLen, struct timeval *timeout) { size_t len; off_t offset = 0; @@ -715,13 +715,13 @@ read: } int -pn53x_usb_ack (nfc_device_t * pnd) +pn53x_usb_ack (nfc_device * pnd) { return pn53x_usb_bulk_write (DRIVER_DATA (pnd), (byte_t *) pn53x_ack_frame, sizeof (pn53x_ack_frame), NULL); } bool -pn53x_usb_init (nfc_device_t *pnd) +pn53x_usb_init (nfc_device *pnd) { // Sometimes PN53x USB doesn't reply ACK one the first frame, so we need to send a dummy one... //pn53x_check_communication (pnd); // Sony RC-S360 doesn't support this command for now so let's use a get_firmware_version instead: @@ -778,7 +778,7 @@ On ASK LoGO hardware: } bool -pn53x_usb_configure (nfc_device_t * pnd, const nfc_device_option_t ndo, const bool bEnable) +pn53x_usb_configure (nfc_device * pnd, const nfc_device_option ndo, const bool bEnable) { if (!pn53x_configure (pnd, ndo, bEnable)) return false; @@ -806,7 +806,7 @@ pn53x_usb_configure (nfc_device_t * pnd, const nfc_device_option_t ndo, const bo } bool -pn53x_usb_abort_command (nfc_device_t * pnd) +pn53x_usb_abort_command (nfc_device * pnd) { DRIVER_DATA (pnd)->abort_flag = true; return true; diff --git a/libnfc/drivers/pn53x_usb.h b/libnfc/drivers/pn53x_usb.h index 2450ea5..9d8b637 100644 --- a/libnfc/drivers/pn53x_usb.h +++ b/libnfc/drivers/pn53x_usb.h @@ -30,10 +30,10 @@ # include bool pn53x_usb_probe (nfc_connstring connstrings[], size_t connstrings_len, size_t * pszDeviceFound); -nfc_device_t *pn53x_usb_connect (const nfc_connstring connstring); -bool pn53x_usb_send (nfc_device_t * pnd, const byte_t * pbtData, const size_t szData, struct timeval *timeout); -int pn53x_usb_receive (nfc_device_t * pnd, byte_t * pbtData, const size_t szData, struct timeval *timeout); -void pn53x_usb_disconnect (nfc_device_t * pnd); +nfc_device *pn53x_usb_connect (const nfc_connstring connstring); +bool pn53x_usb_send (nfc_device * pnd, const byte_t * pbtData, const size_t szData, struct timeval *timeout); +int pn53x_usb_receive (nfc_device * pnd, byte_t * pbtData, const size_t szData, struct timeval *timeout); +void pn53x_usb_disconnect (nfc_device * pnd); extern const struct nfc_driver_t pn53x_usb_driver; diff --git a/libnfc/nfc-device.c b/libnfc/nfc-device.c index 7cd5cb6..edf3a17 100644 --- a/libnfc/nfc-device.c +++ b/libnfc/nfc-device.c @@ -19,7 +19,7 @@ /** * @file nfc-device.c - * @brief Provide internal function to manipulate nfc_device_t type + * @brief Provide internal function to manipulate nfc_device type */ /* vim:set et sw=2 ts=2: */ @@ -32,10 +32,10 @@ #include "nfc-internal.h" -nfc_device_t * +nfc_device * nfc_device_new (void) { - nfc_device_t *res = malloc (sizeof (*res)); + nfc_device *res = malloc (sizeof (*res)); if (!res) { err (EXIT_FAILURE, "nfc_device_new: malloc"); @@ -57,7 +57,7 @@ nfc_device_new (void) } void -nfc_device_free (nfc_device_t *nfc_device) +nfc_device_free (nfc_device *nfc_device) { if (nfc_device) { free (nfc_device->driver_data); diff --git a/libnfc/nfc-emulation.c b/libnfc/nfc-emulation.c index 264f651..d9b622c 100644 --- a/libnfc/nfc-emulation.c +++ b/libnfc/nfc-emulation.c @@ -28,7 +28,7 @@ #include "iso7816.h" int -nfc_emulate_target (nfc_device_t* pnd, struct nfc_emulator *emulator) +nfc_emulate_target (nfc_device* pnd, struct nfc_emulator *emulator) { byte_t abtRx[ISO7816_SHORT_R_APDU_MAX_LEN]; size_t szRx = sizeof(abtRx); diff --git a/libnfc/nfc-internal.c b/libnfc/nfc-internal.c index 36da205..bd74b30 100644 --- a/libnfc/nfc-internal.c +++ b/libnfc/nfc-internal.c @@ -23,10 +23,9 @@ */ #include -#include void -prepare_initiator_data (const nfc_modulation_t nm, byte_t **ppbtInitiatorData, size_t * pszInitiatorData) +prepare_initiator_data (const nfc_modulation nm, byte_t **ppbtInitiatorData, size_t * pszInitiatorData) { switch (nm.nmt) { case NMT_ISO14443B: { diff --git a/libnfc/nfc-internal.h b/libnfc/nfc-internal.h index 425c341..1617286 100644 --- a/libnfc/nfc-internal.h +++ b/libnfc/nfc-internal.h @@ -127,37 +127,37 @@ struct nfc_driver_t { const char *name; bool (*probe)(nfc_connstring connstrings[], size_t connstrings_len, size_t * pszDeviceFound); - nfc_device_t * (*connect) (const nfc_connstring connstring); - void (*disconnect) (nfc_device_t * pnd); - const char *(*strerror) (const nfc_device_t * pnd); + nfc_device * (*connect) (const nfc_connstring connstring); + void (*disconnect) (nfc_device * pnd); + const char *(*strerror) (const nfc_device * pnd); - bool (*initiator_init) (nfc_device_t * pnd); - bool (*initiator_select_passive_target) (nfc_device_t * pnd, const nfc_modulation_t nm, const byte_t * pbtInitData, const size_t szInitData, nfc_target_t * pnt); - bool (*initiator_poll_target) (nfc_device_t * pnd, const nfc_modulation_t * pnmModulations, const size_t szModulations, const uint8_t uiPollNr, const uint8_t btPeriod, nfc_target_t * pnt); - bool (*initiator_select_dep_target) (nfc_device_t * pnd, const nfc_dep_mode_t ndm, const nfc_baud_rate_t nbr, const nfc_dep_info_t * pndiInitiator, nfc_target_t * pnt); - bool (*initiator_deselect_target) (nfc_device_t * pnd); - bool (*initiator_transceive_bytes) (nfc_device_t * pnd, const byte_t * pbtTx, const size_t szTx, byte_t * pbtRx, size_t * pszRx, struct timeval *timeout); - bool (*initiator_transceive_bits) (nfc_device_t * pnd, const byte_t * pbtTx, const size_t szTxBits, const byte_t * pbtTxPar, byte_t * pbtRx, size_t * pszRxBits, byte_t * pbtRxPar); - bool (*initiator_transceive_bytes_timed) (nfc_device_t * pnd, const byte_t * pbtTx, const size_t szTx, byte_t * pbtRx, size_t * pszRx, uint32_t * cycles); - bool (*initiator_transceive_bits_timed) (nfc_device_t * pnd, const byte_t * pbtTx, const size_t szTxBits, const byte_t * pbtTxPar, byte_t * pbtRx, size_t * pszRxBits, byte_t * pbtRxPar, uint32_t * cycles); + bool (*initiator_init) (nfc_device * pnd); + bool (*initiator_select_passive_target) (nfc_device * pnd, const nfc_modulation nm, const byte_t * pbtInitData, const size_t szInitData, nfc_target * pnt); + bool (*initiator_poll_target) (nfc_device * pnd, const nfc_modulation * pnmModulations, const size_t szModulations, const uint8_t uiPollNr, const uint8_t btPeriod, nfc_target * pnt); + bool (*initiator_select_dep_target) (nfc_device * pnd, const nfc_dep_mode ndm, const nfc_baud_rate nbr, const nfc_dep_info * pndiInitiator, nfc_target * pnt); + bool (*initiator_deselect_target) (nfc_device * pnd); + bool (*initiator_transceive_bytes) (nfc_device * pnd, const byte_t * pbtTx, const size_t szTx, byte_t * pbtRx, size_t * pszRx, struct timeval *timeout); + bool (*initiator_transceive_bits) (nfc_device * pnd, const byte_t * pbtTx, const size_t szTxBits, const byte_t * pbtTxPar, byte_t * pbtRx, size_t * pszRxBits, byte_t * pbtRxPar); + bool (*initiator_transceive_bytes_timed) (nfc_device * pnd, const byte_t * pbtTx, const size_t szTx, byte_t * pbtRx, size_t * pszRx, uint32_t * cycles); + bool (*initiator_transceive_bits_timed) (nfc_device * pnd, const byte_t * pbtTx, const size_t szTxBits, const byte_t * pbtTxPar, byte_t * pbtRx, size_t * pszRxBits, byte_t * pbtRxPar, uint32_t * cycles); - bool (*target_init) (nfc_device_t * pnd, nfc_target_t * pnt, byte_t * pbtRx, size_t * pszRx); - bool (*target_send_bytes) (nfc_device_t * pnd, const byte_t * pbtTx, const size_t szTx, struct timeval *timeout); - bool (*target_receive_bytes) (nfc_device_t * pnd, byte_t * pbtRx, size_t * pszRx, struct timeval *timeout); - bool (*target_send_bits) (nfc_device_t * pnd, const byte_t * pbtTx, const size_t szTxBits, const byte_t * pbtTxPar); - bool (*target_receive_bits) (nfc_device_t * pnd, byte_t * pbtRx, size_t * pszRxBits, byte_t * pbtRxPar); + bool (*target_init) (nfc_device * pnd, nfc_target * pnt, byte_t * pbtRx, size_t * pszRx); + bool (*target_send_bytes) (nfc_device * pnd, const byte_t * pbtTx, const size_t szTx, struct timeval *timeout); + bool (*target_receive_bytes) (nfc_device * pnd, byte_t * pbtRx, size_t * pszRx, struct timeval *timeout); + bool (*target_send_bits) (nfc_device * pnd, const byte_t * pbtTx, const size_t szTxBits, const byte_t * pbtTxPar); + bool (*target_receive_bits) (nfc_device * pnd, byte_t * pbtRx, size_t * pszRxBits, byte_t * pbtRxPar); - bool (*configure) (nfc_device_t * pnd, const nfc_device_option_t ndo, const bool bEnable); + bool (*configure) (nfc_device * pnd, const nfc_device_option ndo, const bool bEnable); - bool (*abort_command) (nfc_device_t * pnd); - bool (*idle) (nfc_device_t * pnd); + bool (*abort_command) (nfc_device * pnd); + bool (*idle) (nfc_device * pnd); }; -nfc_device_t *nfc_device_new (void); -void nfc_device_free (nfc_device_t *nfc_device); +nfc_device *nfc_device_new (void); +void nfc_device_free (nfc_device *nfc_device); void iso14443_cascade_uid (const byte_t abtUID[], const size_t szUID, byte_t * pbtCascadedUID, size_t * pszCascadedUID); -void prepare_initiator_data (const nfc_modulation_t nm, byte_t **ppbtInitiatorData, size_t * pszInitiatorData); +void prepare_initiator_data (const nfc_modulation nm, byte_t **ppbtInitiatorData, size_t * pszInitiatorData); #endif // __NFC_INTERNAL_H__ diff --git a/libnfc/nfc.c b/libnfc/nfc.c index 5f1810f..aeea3da 100644 --- a/libnfc/nfc.c +++ b/libnfc/nfc.c @@ -98,24 +98,24 @@ nfc_get_default_device (nfc_connstring *connstring) /** * @brief Connect to a NFC device * @param connstring The device connection string if specific device is wanted, \c NULL otherwise - * @return Returns pointer to a \a nfc_device_t struct if successfull; otherwise returns \c NULL value. + * @return Returns pointer to a \a nfc_device struct if successfull; otherwise returns \c NULL value. * * If \e connstring is \c NULL, the \a nfc_get_default_device() function is used. * * If \e connstring is set, this function will try to claim the right device using information provided by \e connstring. * * When it has successfully claimed a NFC device, memory is allocated to save the device information. - * It will return a pointer to a \a nfc_device_t struct. + * It will return a pointer to a \a nfc_device struct. * This pointer should be supplied by every next functions of libnfc that should perform an action with this device. * * @note Depending on the desired operation mode, the device needs to be configured by using nfc_initiator_init() or nfc_target_init(), * optionally followed by manual tuning of the parameters if the default parameters are not suiting your goals. */ -nfc_device_t * +nfc_device * nfc_connect (const nfc_connstring connstring) { log_init (); - nfc_device_t *pnd = NULL; + nfc_device *pnd = NULL; nfc_connstring ncs; if (connstring == NULL) { @@ -158,12 +158,12 @@ nfc_connect (const nfc_connstring connstring) /** * @brief Disconnect from a NFC device - * @param pnd \a nfc_device_t struct pointer that represent currently used device + * @param pnd \a nfc_device struct pointer that represent currently used device * - * Initiator's selected tag is disconnected and the device, including allocated \a nfc_device_t struct, is released. + * Initiator's selected tag is disconnected and the device, including allocated \a nfc_device struct, is released. */ void -nfc_disconnect (nfc_device_t * pnd) +nfc_disconnect (nfc_device * pnd) { if (pnd) { // Go in idle mode @@ -206,8 +206,8 @@ nfc_list_devices (nfc_connstring connstrings[] , size_t szDevices, size_t * pszD /** * @brief Configure advanced NFC device settings * @return Returns \c true if action was successfully performed; otherwise returns \c false. - * @param pnd \a nfc_device_t struct pointer that represent currently used device - * @param ndo \a nfc_device_option_t struct that contains option to set to device + * @param pnd \a nfc_device struct pointer that represent currently used device + * @param ndo \a nfc_device_option struct that contains option to set to device * @param bEnable boolean to activate/disactivate the option * * Configures parameters and registers that control for example timing, @@ -216,7 +216,7 @@ nfc_list_devices (nfc_connstring connstrings[] , size_t szDevices, size_t * pszD * accept). */ bool -nfc_configure (nfc_device_t * pnd, const nfc_device_option_t ndo, const bool bEnable) +nfc_configure (nfc_device * pnd, const nfc_device_option ndo, const bool bEnable) { HAL (configure, pnd, ndo, bEnable); } @@ -224,7 +224,7 @@ nfc_configure (nfc_device_t * pnd, const nfc_device_option_t ndo, const bool bEn /** * @brief Initialize NFC device as initiator (reader) * @return Returns \c true if action was successfully performed; otherwise returns \c false. - * @param pnd \a nfc_device_t struct pointer that represent currently used device + * @param pnd \a nfc_device struct pointer that represent currently used device * * The NFC device is configured to function as RFID reader. * After initialization it can be used to communicate to passive RFID tags and active NFC devices. @@ -242,7 +242,7 @@ nfc_configure (nfc_device_t * pnd, const nfc_device_option_t ndo, const bool bEn * - RF field is shortly dropped (if it was enabled) then activated again */ bool -nfc_initiator_init (nfc_device_t * pnd) +nfc_initiator_init (nfc_device * pnd) { // Drop the field for a while if (!nfc_configure (pnd, NDO_ACTIVATE_FIELD, false)) @@ -287,7 +287,7 @@ nfc_initiator_init (nfc_device_t * pnd) * @brief Select a passive or emulated tag * @return Returns \c true if action was successfully performed; otherwise returns \c false. * - * @param pnd \a nfc_device_t struct pointer that represent currently used device + * @param pnd \a nfc_device struct pointer that represent currently used device * @param nm desired modulation * @param pbtInitData optional initiator data used for Felica, ISO14443B, Topaz polling or to select a specific UID in ISO14443A. * @param szInitData length of initiator data \a pbtInitData. @@ -296,7 +296,7 @@ nfc_initiator_init (nfc_device_t * pnd) * - for an ISO/IEC 14443 type B modulation, pbbInitData contains Application Family Identifier (AFI) (see ISO/IEC 14443-3); * - for a FeliCa modulation, pbbInitData contains polling payload (see ISO/IEC 18092 11.2.2.5). * - * @param[out] pnt \a nfc_target_t struct pointer which will filled if available + * @param[out] pnt \a nfc_target struct pointer which will filled if available * * The NFC device will try to find one available passive tag or emulated tag. * @@ -304,10 +304,10 @@ nfc_initiator_init (nfc_device_t * pnd) * the initial modulation and speed (106, 212 or 424 kbps) should be supplied. */ bool -nfc_initiator_select_passive_target (nfc_device_t * pnd, - const nfc_modulation_t nm, +nfc_initiator_select_passive_target (nfc_device * pnd, + const nfc_modulation nm, const byte_t * pbtInitData, const size_t szInitData, - nfc_target_t * pnt) + nfc_target * pnt) { byte_t abtInit[MAX(12, szInitData)]; size_t szInit; @@ -330,9 +330,9 @@ nfc_initiator_select_passive_target (nfc_device_t * pnd, * @brief List passive or emulated tags * @return Returns \c true if action was successfully performed; otherwise returns \c false. * - * @param pnd \a nfc_device_t struct pointer that represent currently used device + * @param pnd \a nfc_device struct pointer that represent currently used device * @param nm desired modulation - * @param[out] ant array of \a nfc_target_t that will be filled with targets info + * @param[out] ant array of \a nfc_target that will be filled with targets info * @param szTargets size of \a ant (will be the max targets listed) * @param[out] pszTargetFound pointer where target found counter will be stored * @@ -344,11 +344,11 @@ nfc_initiator_select_passive_target (nfc_device_t * pnd, * should be supplied. */ bool -nfc_initiator_list_passive_targets (nfc_device_t * pnd, - const nfc_modulation_t nm, - nfc_target_t ant[], const size_t szTargets, size_t * pszTargetFound) +nfc_initiator_list_passive_targets (nfc_device * pnd, + const nfc_modulation nm, + nfc_target ant[], const size_t szTargets, size_t * pszTargetFound) { - nfc_target_t nt; + nfc_target nt; size_t szTargetFound = 0; byte_t *pbtInitData = NULL; size_t szInitDataLen = 0; @@ -371,14 +371,14 @@ nfc_initiator_list_passive_targets (nfc_device_t * pnd, bool seen = false; // Check if we've already seen this tag for (i = 0; i < szTargetFound; i++) { - if (memcmp(&(ant[i]), &nt, sizeof (nfc_target_t)) == 0) { + if (memcmp(&(ant[i]), &nt, sizeof (nfc_target)) == 0) { seen = true; } } if (seen) { break; } - memcpy (&(ant[szTargetFound]), &nt, sizeof (nfc_target_t)); + memcpy (&(ant[szTargetFound]), &nt, sizeof (nfc_target)); szTargetFound++; // deselect has no effect on FeliCa and Jewel cards so we'll stop after one... // ISO/IEC 14443 B' cards are polled at 100% probability so it's not possible to detect correctly two cards at the same time @@ -395,20 +395,20 @@ nfc_initiator_list_passive_targets (nfc_device_t * pnd, * @brief Polling for NFC targets * @return Returns \c true if action was successfully performed; otherwise returns \c false. * - * @param pnd \a nfc_device_t struct pointer that represent currently used device + * @param pnd \a nfc_device struct pointer that represent currently used device * @param ppttTargetTypes array of desired target types * @param szTargetTypes \e ppttTargetTypes count * @param uiPollNr specifies the number of polling (0x01 – 0xFE: 1 up to 254 polling, 0xFF: Endless polling) * @note one polling is a polling for each desired target type * @param uiPeriod indicates the polling period in units of 150 ms (0x01 – 0x0F: 150ms – 2.25s) * @note e.g. if uiPeriod=10, it will poll each desired target type during 1.5s - * @param[out] pnt pointer on \a nfc_target_t (over)writable struct + * @param[out] pnt pointer on \a nfc_target (over)writable struct */ bool -nfc_initiator_poll_target (nfc_device_t * pnd, - const nfc_modulation_t * pnmModulations, const size_t szModulations, +nfc_initiator_poll_target (nfc_device * pnd, + const nfc_modulation * pnmModulations, const size_t szModulations, const uint8_t uiPollNr, const uint8_t uiPeriod, - nfc_target_t * pnt) + nfc_target * pnt) { HAL (initiator_poll_target, pnd, pnmModulations, szModulations, uiPollNr, uiPeriod, pnt); } @@ -418,21 +418,21 @@ nfc_initiator_poll_target (nfc_device_t * pnd, * @brief Select a target and request active or passive mode for D.E.P. (Data Exchange Protocol) * @return Returns \c true if action was successfully performed; otherwise returns \c false. * - * @param pnd \a nfc_device_t struct pointer that represent currently used device + * @param pnd \a nfc_device struct pointer that represent currently used device * @param ndm desired D.E.P. mode (\a NDM_ACTIVE or \a NDM_PASSIVE for active, respectively passive mode) - * @param ndiInitiator pointer \a nfc_dep_info_t struct that contains \e NFCID3 and \e General \e Bytes to set to the initiator device (optionnal, can be \e NULL) - * @param[out] pnt is a \a nfc_target_t struct pointer where target information will be put. + * @param ndiInitiator pointer \a nfc_dep_info struct that contains \e NFCID3 and \e General \e Bytes to set to the initiator device (optionnal, can be \e NULL) + * @param[out] pnt is a \a nfc_target struct pointer where target information will be put. * * The NFC device will try to find an available D.E.P. target. The standards * (ISO18092 and ECMA-340) describe the modulation that can be used for reader * to passive communications. * - * @note \a nfc_dep_info_t will be returned when the target was acquired successfully. + * @note \a nfc_dep_info will be returned when the target was acquired successfully. */ bool -nfc_initiator_select_dep_target (nfc_device_t * pnd, - const nfc_dep_mode_t ndm, const nfc_baud_rate_t nbr, - const nfc_dep_info_t * pndiInitiator, nfc_target_t * pnt) +nfc_initiator_select_dep_target (nfc_device * pnd, + const nfc_dep_mode ndm, const nfc_baud_rate nbr, + const nfc_dep_info * pndiInitiator, nfc_target * pnt) { HAL (initiator_select_dep_target, pnd, ndm, nbr, pndiInitiator, pnt); } @@ -440,7 +440,7 @@ nfc_initiator_select_dep_target (nfc_device_t * pnd, /** * @brief Deselect a selected passive or emulated tag * @return Returns \c true if action was successfully performed; otherwise returns \c false. - * @param pnd \a nfc_device_t struct pointer that represents currently used device + * @param pnd \a nfc_device struct pointer that represents currently used device * * After selecting and communicating with a passive tag, this function could be * used to deactivate and release the tag. This is very useful when there are @@ -450,7 +450,7 @@ nfc_initiator_select_dep_target (nfc_device_t * pnd, * the next tag until the correct tag is found. */ bool -nfc_initiator_deselect_target (nfc_device_t * pnd) +nfc_initiator_deselect_target (nfc_device * pnd) { HAL (initiator_deselect_target, pnd); } @@ -479,7 +479,7 @@ nfc_initiator_deselect_target (nfc_device_t * pnd) * @warning The configuration option \a NDO_HANDLE_PARITY must be set to \c true (the default value). */ bool -nfc_initiator_transceive_bytes (nfc_device_t * pnd, const byte_t * pbtTx, const size_t szTx, byte_t * pbtRx, +nfc_initiator_transceive_bytes (nfc_device * pnd, const byte_t * pbtTx, const size_t szTx, byte_t * pbtRx, size_t * pszRx, struct timeval *timeout) { HAL (initiator_transceive_bytes, pnd, pbtTx, szTx, pbtRx, pszRx, timeout) @@ -521,7 +521,7 @@ nfc_initiator_transceive_bytes (nfc_device_t * pnd, const byte_t * pbtTx, const * CRC bytes. Using this feature you are able to simulate these frames. */ bool -nfc_initiator_transceive_bits (nfc_device_t * pnd, const byte_t * pbtTx, const size_t szTxBits, const byte_t * pbtTxPar, +nfc_initiator_transceive_bits (nfc_device * pnd, const byte_t * pbtTx, const size_t szTxBits, const byte_t * pbtTxPar, byte_t * pbtRx, size_t * pszRxBits, byte_t * pbtRxPar) { HAL (initiator_transceive_bits, pnd, pbtTx, szTxBits, pbtTxPar, pbtRx, pszRxBits, pbtRxPar); @@ -548,7 +548,7 @@ nfc_initiator_transceive_bits (nfc_device_t * pnd, const byte_t * pbtTx, const s * @warning The configuration option \a NDO_HANDLE_PARITY must be set to \c true (the default value). */ bool -nfc_initiator_transceive_bytes_timed (nfc_device_t * pnd, const byte_t * pbtTx, const size_t szTx, byte_t * pbtRx, +nfc_initiator_transceive_bytes_timed (nfc_device * pnd, const byte_t * pbtTx, const size_t szTx, byte_t * pbtRx, size_t * pszRx, uint32_t * cycles) { HAL (initiator_transceive_bytes_timed, pnd, pbtTx, szTx, pbtRx, pszRx, cycles) @@ -576,7 +576,7 @@ nfc_initiator_transceive_bytes_timed (nfc_device_t * pnd, const byte_t * pbtTx, * @warning The configuration option \a NDO_HANDLE_PARITY must be set to \c true (the default value). */ bool -nfc_initiator_transceive_bits_timed (nfc_device_t * pnd, const byte_t * pbtTx, const size_t szTxBits, const byte_t * pbtTxPar, +nfc_initiator_transceive_bits_timed (nfc_device * pnd, const byte_t * pbtTx, const size_t szTxBits, const byte_t * pbtTxPar, byte_t * pbtRx, size_t * pszRxBits, byte_t * pbtRxPar, uint32_t * cycles) { HAL (initiator_transceive_bits_timed, pnd, pbtTx, szTxBits, pbtTxPar, pbtRx, pszRxBits, pbtRxPar, cycles); @@ -586,9 +586,9 @@ nfc_initiator_transceive_bits_timed (nfc_device_t * pnd, const byte_t * pbtTx, c * @brief Initialize NFC device as an emulated tag * @return Returns \c true if action was successfully performed; otherwise returns \c false. * - * @param pnd \a nfc_device_t struct pointer that represent currently used device + * @param pnd \a nfc_device struct pointer that represent currently used device * @param ntm target mode restriction that you want to emulate (eg. NTM_PASSIVE_ONLY) - * @param pnt pointer to \a nfc_target_t struct that represents the wanted emulated target + * @param pnt pointer to \a nfc_target struct that represents the wanted emulated target * * @note \a pnt can be updated by this function: if you set NBR_UNDEFINED * and/or NDM_UNDEFINED (ie. for DEP mode), these fields will be updated. @@ -613,7 +613,7 @@ nfc_initiator_transceive_bits_timed (nfc_device_t * pnd, const byte_t * pbtTx, c * receive functions can be used. */ bool -nfc_target_init (nfc_device_t * pnd, nfc_target_t * pnt, byte_t * pbtRx, size_t * pszRx) +nfc_target_init (nfc_device * pnd, nfc_target * pnt, byte_t * pbtRx, size_t * pszRx) { // Disallow invalid frame if (!nfc_configure (pnd, NDO_ACCEPT_INVALID_FRAMES, false)) @@ -646,14 +646,14 @@ nfc_target_init (nfc_device_t * pnd, nfc_target_t * pnt, byte_t * pbtRx, size_t * @brief Turn NFC device in idle mode * @return Returns \c true if action was successfully performed; otherwise returns \c false. * - * @param pnd \a nfc_device_t struct pointer that represent currently used device + * @param pnd \a nfc_device struct pointer that represent currently used device * * This function switch the device in idle mode. * In initiator mode, the RF field is turned off and the device is set to low power mode (if avaible); * In target mode, the emulation is stoped (no target available from external initiator) and the device is set to low power mode (if avaible). */ bool -nfc_idle (nfc_device_t * pnd) +nfc_idle (nfc_device * pnd) { HAL (idle, pnd); } @@ -662,7 +662,7 @@ nfc_idle (nfc_device_t * pnd) * @brief Abort current running command * @return Returns \c true if action was successfully performed; otherwise returns \c false. * - * @param pnd \a nfc_device_t struct pointer that represent currently used device + * @param pnd \a nfc_device struct pointer that represent currently used device * * Some commands (ie. nfc_target_init()) are blocking functions and will return only in particular conditions (ie. external initiator request). * This function attempt to abort the current running command. @@ -670,7 +670,7 @@ nfc_idle (nfc_device_t * pnd) * @note The blocking function (ie. nfc_target_init()) will failed with DEABORT error. */ bool -nfc_abort_command (nfc_device_t * pnd) +nfc_abort_command (nfc_device * pnd) { HAL (abort_command, pnd); } @@ -679,7 +679,7 @@ nfc_abort_command (nfc_device_t * pnd) * @brief Send bytes and APDU frames * @return Returns \c true if action was successfully performed; otherwise returns \c false. * - * @param pnd \a nfc_device_t struct pointer that represent currently used device + * @param pnd \a nfc_device struct pointer that represent currently used device * @param pbtTx pointer to Tx buffer * @param szTx size of Tx buffer * @param timeout timeval struct pointer (NULL means infinite) @@ -691,7 +691,7 @@ nfc_abort_command (nfc_device_t * pnd) * If timeout is a null pointer, the function blocks indefinitely (until an error is raised or function is completed). */ bool -nfc_target_send_bytes (nfc_device_t * pnd, const byte_t * pbtTx, const size_t szTx, struct timeval *timeout) +nfc_target_send_bytes (nfc_device * pnd, const byte_t * pbtTx, const size_t szTx, struct timeval *timeout) { HAL (target_send_bytes, pnd, pbtTx, szTx, timeout); } @@ -700,7 +700,7 @@ nfc_target_send_bytes (nfc_device_t * pnd, const byte_t * pbtTx, const size_t sz * @brief Receive bytes and APDU frames * @return Returns \c true if action was successfully performed; otherwise returns \c false. * - * @param pnd \a nfc_device_t struct pointer that represent currently used device + * @param pnd \a nfc_device struct pointer that represent currently used device * @param[out] pbtRx pointer to Rx buffer * @param[out] pszRx received byte count * @param timeout timeval struct pointer (NULL means infinite) @@ -711,7 +711,7 @@ nfc_target_send_bytes (nfc_device_t * pnd, const byte_t * pbtTx, const size_t sz * If timeout is a null pointer, the function blocks indefinitely (until an error is raised or function is completed). */ bool -nfc_target_receive_bytes (nfc_device_t * pnd, byte_t * pbtRx, size_t * pszRx, struct timeval *timeout) +nfc_target_receive_bytes (nfc_device * pnd, byte_t * pbtRx, size_t * pszRx, struct timeval *timeout) { HAL (target_receive_bytes, pnd, pbtRx, pszRx, timeout); } @@ -724,7 +724,7 @@ nfc_target_receive_bytes (nfc_device_t * pnd, byte_t * pbtRx, size_t * pszRx, st * using the specified NFC device (configured as \e target). */ bool -nfc_target_send_bits (nfc_device_t * pnd, const byte_t * pbtTx, const size_t szTxBits, const byte_t * pbtTxPar) +nfc_target_send_bits (nfc_device * pnd, const byte_t * pbtTx, const size_t szTxBits, const byte_t * pbtTxPar) { HAL (target_send_bits, pnd, pbtTx, szTxBits, pbtTxPar); } @@ -741,7 +741,7 @@ nfc_target_send_bits (nfc_device_t * pnd, const byte_t * pbtTx, const size_t szT * frames. */ bool -nfc_target_receive_bits (nfc_device_t * pnd, byte_t * pbtRx, size_t * pszRxBits, byte_t * pbtRxPar) +nfc_target_receive_bits (nfc_device * pnd, byte_t * pbtRx, size_t * pszRxBits, byte_t * pbtRxPar) { HAL (target_receive_bits, pnd, pbtRx, pszRxBits, pbtRxPar); } @@ -751,7 +751,7 @@ nfc_target_receive_bits (nfc_device_t * pnd, byte_t * pbtRx, size_t * pszRxBits, * @return Returns a string */ const char * -nfc_strerror (const nfc_device_t * pnd) +nfc_strerror (const nfc_device * pnd) { return pnd->driver->strerror (pnd); } @@ -761,7 +761,7 @@ nfc_strerror (const nfc_device_t * pnd) * @return Returns 0 upon success */ int -nfc_strerror_r (const nfc_device_t * pnd, char *pcStrErrBuf, size_t szBufLen) +nfc_strerror_r (const nfc_device * pnd, char *pcStrErrBuf, size_t szBufLen) { return (snprintf (pcStrErrBuf, szBufLen, "%s", nfc_strerror (pnd)) < 0) ? -1 : 0; } @@ -770,7 +770,7 @@ nfc_strerror_r (const nfc_device_t * pnd, char *pcStrErrBuf, size_t szBufLen) * @brief Display the PCD error a-la perror */ void -nfc_perror (const nfc_device_t * pnd, const char *pcString) +nfc_perror (const nfc_device * pnd, const char *pcString) { fprintf (stderr, "%s: %s\n", pcString, nfc_strerror (pnd)); } @@ -782,7 +782,7 @@ nfc_perror (const nfc_device_t * pnd, const char *pcString) * @return Returns a string with the device name */ const char * -nfc_device_name (nfc_device_t * pnd) +nfc_device_name (nfc_device * pnd) { return pnd->acName; } diff --git a/test/test_access_storm.c b/test/test_access_storm.c index 7e8408b..ead2190 100644 --- a/test/test_access_storm.c +++ b/test/test_access_storm.c @@ -14,31 +14,31 @@ void test_access_storm (void) { int n = NTESTS; - nfc_device_desc_t devices[MAX_DEVICE_COUNT]; + nfc_connstring connstrings[MAX_DEVICE_COUNT]; size_t device_count, ref_device_count, target_count; bool res; - nfc_list_devices (devices, MAX_DEVICE_COUNT, &ref_device_count); + nfc_list_devices (connstrings, MAX_DEVICE_COUNT, &ref_device_count); if (!ref_device_count) cut_omit ("No NFC device found"); while (n) { size_t i; - nfc_list_devices (devices, MAX_DEVICE_COUNT, &device_count); + nfc_list_devices (connstrings, MAX_DEVICE_COUNT, &device_count); cut_assert_equal_int (ref_device_count, device_count, cut_message ("device count")); for (i = 0; i < device_count; i++) { - nfc_device_t *device; - nfc_target_t ant[MAX_TARGET_COUNT]; + nfc_device *device; + nfc_target ant[MAX_TARGET_COUNT]; - device = nfc_connect (&(devices[i])); + device = nfc_connect (&(connstrings[i])); cut_assert_not_null (device, cut_message ("nfc_connect")); res = nfc_initiator_init(device); cut_assert_true (res, cut_message ("nfc_initiator_init")); - const nfc_modulation_t nm = { + const nfc_modulation nm = { .nmt = NMT_ISO14443A, .nbr = NBR_106, }; diff --git a/test/test_dep.c b/test/test_dep.c index d7b96b3..0c84678 100644 --- a/test/test_dep.c +++ b/test/test_dep.c @@ -9,8 +9,8 @@ #define TARGET 1 pthread_t threads[2]; -nfc_device_desc_t device_descriptions[2]; -nfc_device_t *devices[2]; +nfc_connstring connstrings[2]; +nfc_device *devices[2]; intptr_t result[2]; void @@ -28,13 +28,13 @@ cut_setup (void) { size_t n; - nfc_list_devices (device_descriptions, 2, &n); + nfc_list_devices (connstrings, 2, &n); if (n < 2) { cut_omit ("At least two NFC devices must be plugged-in to run this test"); } - devices[TARGET] = nfc_connect (&device_descriptions[TARGET]); - devices[INITIATOR] = nfc_connect (&device_descriptions[INITIATOR]); + devices[TARGET] = nfc_connect (&connstrings[TARGET]); + devices[INITIATOR] = nfc_connect (&connstrings[INITIATOR]); signal (SIGINT, abort_test_by_keypress); } @@ -47,7 +47,7 @@ cut_teardown (void) } struct thread_data { - nfc_device_t *device; + nfc_device *device; void *cut_test_context; }; @@ -55,11 +55,11 @@ void * target_thread (void *arg) { intptr_t thread_res = 0; -// nfc_device_t *device = ((struct thread_data *) arg)->device; +// nfc_device *device = ((struct thread_data *) arg)->device; cut_set_current_test_context (((struct thread_data *) arg)->cut_test_context); #if 0 - nfc_target_t nt = { + nfc_target nt = { .nm = { .nmt = NMT_DEP, .nbr = NBR_UNDEFINED @@ -106,7 +106,7 @@ void * initiator_thread (void *arg) { intptr_t thread_res = 0; -// nfc_device_t *device = ((struct thread_data *) arg)->device; +// nfc_device *device = ((struct thread_data *) arg)->device; cut_set_current_test_context (((struct thread_data *) arg)->cut_test_context); cut_fail("plop"); @@ -122,7 +122,7 @@ initiator_thread (void *arg) bool res = nfc_initiator_init (device); // cut_assert_true (res, cut_message ("Can't initialize NFC device as initiator")); - nfc_target_t nt; + nfc_target nt; // Passive mode / 212Kbps res = nfc_initiator_select_dep_target (device, NDM_PASSIVE, NBR_212, NULL, &nt); diff --git a/test/test_register_access.c b/test/test_register_access.c index a713081..9363f9f 100644 --- a/test/test_register_access.c +++ b/test/test_register_access.c @@ -9,17 +9,17 @@ void test_register_endianness (void) { - nfc_device_desc_t devices[MAX_DEVICE_COUNT]; + nfc_connstring connstrings[MAX_DEVICE_COUNT]; size_t device_count; bool res; - nfc_list_devices (devices, MAX_DEVICE_COUNT, &device_count); + nfc_list_devices (connstrings, MAX_DEVICE_COUNT, &device_count); if (!device_count) cut_omit ("No NFC device found"); - nfc_device_t *device; + nfc_device *device; - device = nfc_connect (&(devices[0])); + device = nfc_connect (&(connstrings[0])); cut_assert_not_null (device, cut_message ("nfc_connect")); uint8_t value; diff --git a/test/test_register_endianness.c b/test/test_register_endianness.c index 19372a3..2f7e6e5 100644 --- a/test/test_register_endianness.c +++ b/test/test_register_endianness.c @@ -10,17 +10,17 @@ void test_register_endianness (void) { - nfc_device_desc_t devices[MAX_DEVICE_COUNT]; + nfc_connstring connstrings[MAX_DEVICE_COUNT]; size_t device_count; bool res; - nfc_list_devices (devices, MAX_DEVICE_COUNT, &device_count); + nfc_list_devices (connstrings, MAX_DEVICE_COUNT, &device_count); if (!device_count) cut_omit ("No NFC device found"); - nfc_device_t *device; + nfc_device *device; - device = nfc_connect (&(devices[0])); + device = nfc_connect (&(connstrings[0])); cut_assert_not_null (device, cut_message ("nfc_connect")); uint8_t value; diff --git a/utils/mifare.c b/utils/mifare.c index ef9d147..32679c1 100644 --- a/utils/mifare.c +++ b/utils/mifare.c @@ -48,7 +48,7 @@ * The MIFARE Classic Specification (http://www.nxp.com/acrobat/other/identification/M001053_MF1ICS50_rev5_3.pdf) explains more about this process. */ bool -nfc_initiator_mifare_cmd (nfc_device_t * pnd, const mifare_cmd mc, const uint8_t ui8Block, mifare_param * pmp) +nfc_initiator_mifare_cmd (nfc_device * pnd, const mifare_cmd mc, const uint8_t ui8Block, mifare_param * pmp) { byte_t abtRx[265]; size_t szRx = sizeof(abtRx); @@ -69,19 +69,19 @@ nfc_initiator_mifare_cmd (nfc_device_t * pnd, const mifare_cmd mc, const uint8_t // Authenticate command case MC_AUTH_A: case MC_AUTH_B: - szParamLen = sizeof (mifare_param_auth); + szParamLen = sizeof (struct mifare_param_auth); break; // Data command case MC_WRITE: - szParamLen = sizeof (mifare_param_data); + szParamLen = sizeof (struct mifare_param_data); break; // Value command case MC_DECREMENT: case MC_INCREMENT: case MC_TRANSFER: - szParamLen = sizeof (mifare_param_value); + szParamLen = sizeof (struct mifare_param_value); break; // Please fix your code, you never should reach this statement diff --git a/utils/mifare.h b/utils/mifare.h index df357df..2b44f9e 100644 --- a/utils/mifare.h +++ b/utils/mifare.h @@ -53,29 +53,29 @@ typedef enum { } mifare_cmd; // MIFARE command params -typedef struct { +struct mifare_param_auth { byte_t abtKey[6]; byte_t abtUid[4]; -} mifare_param_auth; +}; -typedef struct { +struct mifare_param_data { byte_t abtData[16]; -} mifare_param_data; +}; -typedef struct { +struct mifare_param_value { byte_t abtValue[4]; -} mifare_param_value; +}; typedef union { - mifare_param_auth mpa; - mifare_param_data mpd; - mifare_param_value mpv; + struct mifare_param_auth mpa; + struct mifare_param_data mpd; + struct mifare_param_value mpv; } mifare_param; // Reset struct alignment to default # pragma pack() -bool nfc_initiator_mifare_cmd (nfc_device_t * pnd, const mifare_cmd mc, const uint8_t ui8Block, mifare_param * pmp); +bool nfc_initiator_mifare_cmd (nfc_device * pnd, const mifare_cmd mc, const uint8_t ui8Block, mifare_param * pmp); // Compiler directive, set struct alignment to 1 byte_t for compatibility # pragma pack(1) diff --git a/utils/nfc-emulate-forum-tag2.c b/utils/nfc-emulate-forum-tag2.c index 0159bae..04e274c 100644 --- a/utils/nfc-emulate-forum-tag2.c +++ b/utils/nfc-emulate-forum-tag2.c @@ -71,7 +71,7 @@ #include "nfc-utils.h" -static nfc_device_t *pnd; +static nfc_device *pnd; void stop_emulation (int sig) @@ -155,7 +155,7 @@ main(int argc, char *argv[]) (void)argc; (void)argv; - nfc_target_t nt = { + nfc_target nt = { .nm = { .nmt = NMT_ISO14443A, .nbr = NBR_UNDEFINED, // Will be updated by nfc_target_init() diff --git a/utils/nfc-emulate-forum-tag4.c b/utils/nfc-emulate-forum-tag4.c index b942b96..51ea535 100644 --- a/utils/nfc-emulate-forum-tag4.c +++ b/utils/nfc-emulate-forum-tag4.c @@ -68,7 +68,7 @@ #include "nfc-utils.h" -static nfc_device_t *pnd; +static nfc_device *pnd; static bool quiet_output = false; #define SYMBOL_PARAM_fISO14443_4_PICC 0x20 @@ -289,7 +289,7 @@ usage (char *progname) int main (int argc, char *argv[]) { - nfc_target_t nt = { + nfc_target nt = { .nm = { .nmt = NMT_ISO14443A, .nbr = NBR_UNDEFINED, // Will be updated by nfc_target_init() diff --git a/utils/nfc-list.c b/utils/nfc-list.c index 342b5f2..9c7f62e 100644 --- a/utils/nfc-list.c +++ b/utils/nfc-list.c @@ -57,7 +57,7 @@ #define MAX_DEVICE_COUNT 16 #define MAX_TARGET_COUNT 16 -static nfc_device_t *pnd; +static nfc_device *pnd; int main (int argc, const char *argv[]) @@ -108,7 +108,7 @@ main (int argc, const char *argv[]) } for (i = 0; i < szDeviceFound; i++) { - nfc_target_t ant[MAX_TARGET_COUNT]; + nfc_target ant[MAX_TARGET_COUNT]; pnd = nfc_connect (connstrings[i]); if (pnd == NULL) { @@ -119,7 +119,7 @@ main (int argc, const char *argv[]) printf ("Connected to NFC device: %s\n", pnd->acName); - nfc_modulation_t nm; + nfc_modulation nm; nm.nmt = NMT_ISO14443A; nm.nbr = NBR_106; diff --git a/utils/nfc-mfclassic.c b/utils/nfc-mfclassic.c index c3d7d95..440e6f2 100644 --- a/utils/nfc-mfclassic.c +++ b/utils/nfc-mfclassic.c @@ -52,8 +52,8 @@ #include "mifare.h" #include "nfc-utils.h" -static nfc_device_t *pnd; -static nfc_target_t nt; +static nfc_device *pnd; +static nfc_target nt; static mifare_param mp; static mifare_classic_tag mtKeys; static mifare_classic_tag mtDump; @@ -72,7 +72,7 @@ static byte_t keys[] = { 0xab, 0xcd, 0xef, 0x12, 0x34, 0x56 }; -static const nfc_modulation_t nmMifare = { +static const nfc_modulation nmMifare = { .nmt = NMT_ISO14443A, .nbr = NBR_106, }; diff --git a/utils/nfc-mfsetuid.c b/utils/nfc-mfsetuid.c index 4134acc..79f398f 100644 --- a/utils/nfc-mfsetuid.c +++ b/utils/nfc-mfsetuid.c @@ -65,7 +65,7 @@ static byte_t abtSak; static byte_t abtAts[MAX_FRAME_LEN]; static byte_t szAts = 0; static size_t szCL = 1;//Always start with Cascade Level 1 (CL1) -static nfc_device_t *pnd; +static nfc_device *pnd; bool quiet_output = false; bool iso_ats_supported = false; diff --git a/utils/nfc-mfultralight.c b/utils/nfc-mfultralight.c index 4ae8ccf..a9396c7 100644 --- a/utils/nfc-mfultralight.c +++ b/utils/nfc-mfultralight.c @@ -51,13 +51,13 @@ #include "nfc-utils.h" #include "mifare.h" -static nfc_device_t *pnd; -static nfc_target_t nt; +static nfc_device *pnd; +static nfc_target nt; static mifare_param mp; static mifareul_tag mtDump; static uint32_t uiBlocks = 0xF; -static const nfc_modulation_t nmMifare = { +static const nfc_modulation nmMifare = { .nmt = NMT_ISO14443A, .nbr = NBR_106, }; diff --git a/utils/nfc-read-forum-tag3.c b/utils/nfc-read-forum-tag3.c index cf68e23..c634ea5 100644 --- a/utils/nfc-read-forum-tag3.c +++ b/utils/nfc-read-forum-tag3.c @@ -57,7 +57,7 @@ #include "nfc-utils.h" -static nfc_device_t *pnd; +static nfc_device *pnd; void print_usage(char *progname) @@ -77,7 +77,7 @@ void stop_select (int sig) } void -build_felica_frame(const nfc_felica_info_t nfi, const byte_t command, const byte_t* payload, const size_t payload_len, byte_t * frame, size_t * frame_len) +build_felica_frame(const nfc_felica_info nfi, const byte_t command, const byte_t* payload, const size_t payload_len, byte_t * frame, size_t * frame_len) { frame[0] = 1 + 1 + 8 + payload_len; *frame_len = frame[0]; @@ -88,7 +88,7 @@ build_felica_frame(const nfc_felica_info_t nfi, const byte_t command, const byte #define CHECK 0x06 int -nfc_forum_tag_type3_check (nfc_device_t *pnd, const nfc_target_t nt, const uint16_t block, const uint8_t block_count, byte_t * data, size_t * data_len) +nfc_forum_tag_type3_check (nfc_device *pnd, const nfc_target nt, const uint16_t block, const uint8_t block_count, byte_t * data, size_t * data_len) { byte_t payload[1024] = { 1, // Services @@ -204,14 +204,14 @@ main(int argc, char *argv[]) fprintf (message_stream, "Connected to NFC device: %s\n", pnd->acName); - nfc_modulation_t nm = { + nfc_modulation nm = { .nmt = NMT_FELICA, .nbr = NBR_212, }; signal (SIGINT, stop_select); - nfc_target_t nt; + nfc_target nt; nfc_initiator_init(pnd); fprintf (message_stream, "Place your NFC Forum Tag Type 3 in the field...\n"); diff --git a/utils/nfc-relay-picc.c b/utils/nfc-relay-picc.c index cbf27b7..82b26a0 100644 --- a/utils/nfc-relay-picc.c +++ b/utils/nfc-relay-picc.c @@ -61,8 +61,8 @@ static byte_t abtCapdu[MAX_FRAME_LEN]; static size_t szCapduLen; static byte_t abtRapdu[MAX_FRAME_LEN]; static size_t szRapduLen; -static nfc_device_t *pndInitiator; -static nfc_device_t *pndTarget; +static nfc_device *pndInitiator; +static nfc_device *pndTarget; static bool quitting = false; static bool quiet_output = false; static bool initiator_only_mode = false; @@ -151,7 +151,7 @@ main (int argc, char *argv[]) int arg; size_t szFound; const char *acLibnfcVersion = nfc_version (); - nfc_target_t ntRealTarget; + nfc_target ntRealTarget; // Get commandline options for (arg = 1; arg < argc; arg++) { @@ -236,7 +236,7 @@ main (int argc, char *argv[]) } // Try to find a ISO 14443-4A tag - nfc_modulation_t nm = { + nfc_modulation nm = { .nmt = NMT_ISO14443A, .nbr = NBR_106, }; @@ -279,7 +279,7 @@ main (int argc, char *argv[]) printf ("Hint: tag <---> initiator (relay) <---> target (relay) <---> original reader\n\n"); } if (!initiator_only_mode) { - nfc_target_t ntEmulatedTarget = { + nfc_target ntEmulatedTarget = { .nm = { .nmt = NMT_ISO14443A, .nbr = NBR_106, diff --git a/utils/nfc-utils.c b/utils/nfc-utils.c index 4321169..9b87844 100644 --- a/utils/nfc-utils.c +++ b/utils/nfc-utils.c @@ -133,7 +133,7 @@ print_hex_par (const byte_t * pbtData, const size_t szBits, const byte_t * pbtDa #define SAK_ISO18092_COMPLIANT 0x40 void -print_nfc_iso14443a_info (const nfc_iso14443a_info_t nai, bool verbose) +print_nfc_iso14443a_info (const nfc_iso14443a_info nai, bool verbose) { printf (" ATQA (SENS_RES): "); print_hex (nai.abtAtqa, 2); @@ -530,7 +530,7 @@ print_nfc_iso14443a_info (const nfc_iso14443a_info_t nai, bool verbose) } void -print_nfc_felica_info (const nfc_felica_info_t nfi, bool verbose) +print_nfc_felica_info (const nfc_felica_info nfi, bool verbose) { (void) verbose; printf (" ID (NFCID2): "); @@ -542,7 +542,7 @@ print_nfc_felica_info (const nfc_felica_info_t nfi, bool verbose) } void -print_nfc_jewel_info (const nfc_jewel_info_t nji, bool verbose) +print_nfc_jewel_info (const nfc_jewel_info nji, bool verbose) { (void) verbose; printf (" ATQA (SENS_RES): "); @@ -555,7 +555,7 @@ print_nfc_jewel_info (const nfc_jewel_info_t nji, bool verbose) #define PI_NAD_SUPPORTED 0x01 #define PI_CID_SUPPORTED 0x02 void -print_nfc_iso14443b_info (const nfc_iso14443b_info_t nbi, bool verbose) +print_nfc_iso14443b_info (const nfc_iso14443b_info nbi, bool verbose) { const int iMaxFrameSizes[] = { 16, 24, 32, 40, 48, 64, 96, 128, 256 }; printf (" PUPI: "); @@ -610,7 +610,7 @@ print_nfc_iso14443b_info (const nfc_iso14443b_info_t nbi, bool verbose) } void -print_nfc_iso14443bi_info (const nfc_iso14443bi_info_t nii, bool verbose) +print_nfc_iso14443bi_info (const nfc_iso14443bi_info nii, bool verbose) { printf (" DIV: "); print_hex (nii.abtDIV, 4); @@ -634,7 +634,7 @@ print_nfc_iso14443bi_info (const nfc_iso14443bi_info_t nii, bool verbose) } void -print_nfc_iso14443b2sr_info (const nfc_iso14443b2sr_info_t nsi, bool verbose) +print_nfc_iso14443b2sr_info (const nfc_iso14443b2sr_info nsi, bool verbose) { (void) verbose; printf (" UID: "); @@ -642,7 +642,7 @@ print_nfc_iso14443b2sr_info (const nfc_iso14443b2sr_info_t nsi, bool verbose) } void -print_nfc_iso14443b2ct_info (const nfc_iso14443b2ct_info_t nci, bool verbose) +print_nfc_iso14443b2ct_info (const nfc_iso14443b2ct_info nci, bool verbose) { (void) verbose; uint32_t uid; @@ -655,7 +655,7 @@ print_nfc_iso14443b2ct_info (const nfc_iso14443b2ct_info_t nci, bool verbose) } void -print_nfc_dep_info (const nfc_dep_info_t ndi, bool verbose) +print_nfc_dep_info (const nfc_dep_info ndi, bool verbose) { (void) verbose; printf (" NFCID3: "); @@ -671,7 +671,7 @@ print_nfc_dep_info (const nfc_dep_info_t ndi, bool verbose) } const char * -str_nfc_baud_rate (const nfc_baud_rate_t nbr) +str_nfc_baud_rate (const nfc_baud_rate nbr) { switch(nbr) { case NBR_UNDEFINED: @@ -694,7 +694,7 @@ str_nfc_baud_rate (const nfc_baud_rate_t nbr) } void -print_nfc_target (const nfc_target_t nt, bool verbose) +print_nfc_target (const nfc_target nt, bool verbose) { switch(nt.nm.nmt) { case NMT_ISO14443A: diff --git a/utils/nfc-utils.h b/utils/nfc-utils.h index ace6473..f0ae97f 100644 --- a/utils/nfc-utils.h +++ b/utils/nfc-utils.h @@ -86,15 +86,15 @@ void print_hex (const byte_t * pbtData, const size_t szLen); void print_hex_bits (const byte_t * pbtData, const size_t szBits); void print_hex_par (const byte_t * pbtData, const size_t szBits, const byte_t * pbtDataPar); -void print_nfc_iso14443a_info (const nfc_iso14443a_info_t nai, bool verbose); -void print_nfc_iso14443b_info (const nfc_iso14443b_info_t nbi, bool verbose); -void print_nfc_iso14443bi_info (const nfc_iso14443bi_info_t nii, bool verbose); -void print_nfc_iso14443b2sr_info (const nfc_iso14443b2sr_info_t nsi, bool verbose); -void print_nfc_iso14443b2ct_info (const nfc_iso14443b2ct_info_t nci, bool verbose); -void print_nfc_felica_info (const nfc_felica_info_t nfi, bool verbose); -void print_nfc_jewel_info (const nfc_jewel_info_t nji, bool verbose); -void print_nfc_dep_info (const nfc_dep_info_t ndi, bool verbose); +void print_nfc_iso14443a_info (const nfc_iso14443a_info nai, bool verbose); +void print_nfc_iso14443b_info (const nfc_iso14443b_info nbi, bool verbose); +void print_nfc_iso14443bi_info (const nfc_iso14443bi_info nii, bool verbose); +void print_nfc_iso14443b2sr_info (const nfc_iso14443b2sr_info nsi, bool verbose); +void print_nfc_iso14443b2ct_info (const nfc_iso14443b2ct_info nci, bool verbose); +void print_nfc_felica_info (const nfc_felica_info nfi, bool verbose); +void print_nfc_jewel_info (const nfc_jewel_info nji, bool verbose); +void print_nfc_dep_info (const nfc_dep_info ndi, bool verbose); -void print_nfc_target (const nfc_target_t nt, bool verbose); +void print_nfc_target (const nfc_target nt, bool verbose); #endif From 322eec6fb1f09b58da6dab5911a052e05192a82d Mon Sep 17 00:00:00 2001 From: Audrey Diacre Date: Thu, 24 Nov 2011 10:20:36 +0000 Subject: [PATCH 003/113] Define macro to cast to serial_port_unix. --- libnfc/buses/uart_posix.c | 36 +++++++++++++++++------------------- 1 file changed, 17 insertions(+), 19 deletions(-) diff --git a/libnfc/buses/uart_posix.c b/libnfc/buses/uart_posix.c index 2fc3b47..9af33e4 100644 --- a/libnfc/buses/uart_posix.c +++ b/libnfc/buses/uart_posix.c @@ -67,7 +67,7 @@ struct serial_port_unix{ struct termios termios_new; // Terminal info during the transaction }; -// TODO: #define UART_FD( X ) (((struct serial_port_unix *) X)->fd) +#define UART_DATA( X ) ((struct serial_port_unix *) X) void uart_close_ext (const serial_port sp, const bool restore_termios); @@ -116,12 +116,12 @@ void uart_flush_input (serial_port sp) { // This line seems to produce absolutely no effect on my system (GNU/Linux 2.6.35) - tcflush (((struct serial_port_unix *) sp)->fd, TCIFLUSH); + tcflush (UART_DATA(sp)->fd, TCIFLUSH); // So, I wrote this byte-eater // Retrieve the count of the incoming bytes int available_bytes_count = 0; int res; - res = ioctl (((struct serial_port_unix *) sp)->fd, FIONREAD, &available_bytes_count); + res = ioctl (UART_DATA(sp)->fd, FIONREAD, &available_bytes_count); if (res != 0) { return; } @@ -130,7 +130,7 @@ uart_flush_input (serial_port sp) } char* rx = malloc (available_bytes_count); // There is something available, read the data - res = read (((struct serial_port_unix *) sp)->fd, rx, available_bytes_count); + res = read (UART_DATA(sp)->fd, rx, available_bytes_count); log_put (LOG_CATEGORY, NFC_PRIORITY_TRACE, "%d bytes have eatten.", available_bytes_count); free (rx); } @@ -139,8 +139,7 @@ void uart_set_speed (serial_port sp, const uint32_t uiPortSpeed) { log_put (LOG_CATEGORY, NFC_PRIORITY_TRACE, "Serial port speed requested to be set to %d bauds.", uiPortSpeed); - struct serial_port_unix *spu = (struct serial_port_unix *) sp; - + // Portability note: on some systems, B9600 != 9600 so we have to do // uint32_t <=> speed_t associations by hand. speed_t stPortSpeed = B9600; @@ -181,9 +180,9 @@ uart_set_speed (serial_port sp, const uint32_t uiPortSpeed) }; // Set port speed (Input and Output) - cfsetispeed (&(spu->termios_new), stPortSpeed); - cfsetospeed (&(spu->termios_new), stPortSpeed); - if (tcsetattr (spu->fd, TCSADRAIN, &(spu->termios_new)) == -1) { + cfsetispeed (&(UART_DATA(sp)->termios_new), stPortSpeed); + cfsetospeed (&(UART_DATA(sp)->termios_new), stPortSpeed); + if (tcsetattr (UART_DATA(sp)->fd, TCSADRAIN, &(UART_DATA(sp)->termios_new)) == -1) { log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Unable to apply new speed settings."); } } @@ -192,8 +191,7 @@ uint32_t uart_get_speed (serial_port sp) { uint32_t uiPortSpeed = 0; - const struct serial_port_unix *spu = (struct serial_port_unix *) sp; - switch (cfgetispeed (&spu->termios_new)) { + switch (cfgetispeed (&UART_DATA(sp)->termios_new)) { case B9600: uiPortSpeed = 9600; break; @@ -231,10 +229,10 @@ uart_get_speed (serial_port sp) void uart_close_ext (const serial_port sp, const bool restore_termios) { - if (((struct serial_port_unix *) sp)->fd >= 0) { + if (UART_DATA(sp)->fd >= 0) { if (restore_termios) - tcsetattr (((struct serial_port_unix *) sp)->fd, TCSANOW, &((struct serial_port_unix *) sp)->termios_backup); - close (((struct serial_port_unix *) sp)->fd); + tcsetattr (UART_DATA(sp)->fd, TCSANOW, &UART_DATA(sp)->termios_backup); + close (UART_DATA(sp)->fd); } free (sp); } @@ -263,7 +261,7 @@ uart_receive (serial_port sp, byte_t * pbtRx, const size_t szRx, void * abort_p, select: // Reset file descriptor FD_ZERO (&rfds); - FD_SET (((struct serial_port_unix *) sp)->fd, &rfds); + FD_SET (UART_DATA(sp)->fd, &rfds); if (iAbortFd) { FD_SET (iAbortFd, &rfds); @@ -279,7 +277,7 @@ select: timeout = &fixed_timeout; } - res = select (MAX(((struct serial_port_unix *) sp)->fd, iAbortFd) + 1, &rfds, NULL, NULL, timeout); + res = select (MAX(UART_DATA(sp)->fd, iAbortFd) + 1, &rfds, NULL, NULL, timeout); if ((res < 0) && (EINTR == errno)) { // The system call was interupted by a signal and a signal handler was @@ -306,12 +304,12 @@ select: } // Retrieve the count of the incoming bytes - res = ioctl (((struct serial_port_unix *) sp)->fd, FIONREAD, &available_bytes_count); + res = ioctl (UART_DATA(sp)->fd, FIONREAD, &available_bytes_count); if (res != 0) { return ECOMIO; } // There is something available, read the data - res = read (((struct serial_port_unix *) sp)->fd, pbtRx + received_bytes_count, MIN(available_bytes_count, (expected_bytes_count - received_bytes_count))); + res = read (UART_DATA(sp)->fd, pbtRx + received_bytes_count, MIN(available_bytes_count, (expected_bytes_count - received_bytes_count))); // Stop if the OS has some troubles reading the data if (res <= 0) { return ECOMIO; @@ -333,7 +331,7 @@ uart_send (serial_port sp, const byte_t * pbtTx, const size_t szTx, struct timev { (void) timeout; LOG_HEX ("TX", pbtTx, szTx); - if ((int) szTx == write (((struct serial_port_unix *) sp)->fd, pbtTx, szTx)) + if ((int) szTx == write (UART_DATA(sp)->fd, pbtTx, szTx)) return 0; else return ECOMIO; From ce846931bc87909a398d280e1017b206dcefcee7 Mon Sep 17 00:00:00 2001 From: Audrey Diacre Date: Thu, 24 Nov 2011 10:27:02 +0000 Subject: [PATCH 004/113] Move nfc-emulate-forum-tag2 from utils to examples. --- examples/CMakeLists.txt | 2 +- examples/Makefile.am | 5 +++++ {utils => examples}/nfc-emulate-forum-tag2.c | 2 +- utils/CMakeLists.txt | 2 +- utils/Makefile.am | 5 ----- 5 files changed, 8 insertions(+), 8 deletions(-) rename {utils => examples}/nfc-emulate-forum-tag2.c (99%) diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index f4571d1..d3fd23b 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -1,4 +1,4 @@ -SET(EXAMPLES-SOURCES nfc-anticol nfc-dep-initiator nfc-dep-target nfc-emulate-tag nfc-emulate-uid nfc-poll nfc-relay) +SET(EXAMPLES-SOURCES nfc-anticol nfc-dep-initiator nfc-dep-target nfc-emulate-forum-tag2 nfc-emulate-tag nfc-emulate-uid nfc-poll nfc-relay) INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}/../libnfc) diff --git a/examples/Makefile.am b/examples/Makefile.am index 627ed4d..db402e5 100644 --- a/examples/Makefile.am +++ b/examples/Makefile.am @@ -4,6 +4,7 @@ bin_PROGRAMS = \ nfc-anticol \ nfc-dep-initiator \ nfc-dep-target \ + nfc-emulate-forum-tag2 \ nfc-emulate-tag \ nfc-emulate-uid \ nfc-poll \ @@ -33,6 +34,10 @@ nfc_relay_SOURCES = nfc-relay.c nfc_relay_LDADD = $(top_builddir)/libnfc/libnfc.la \ $(top_builddir)/utils/libnfcutils.la +nfc_emulate_forum_tag2_SOURCES = nfc-emulate-forum-tag2.c +nfc_emulate_forum_tag2_LDADD = $(top_builddir)/libnfc/libnfc.la \ + $(top_builddir)/utils/libnfcutils.la + nfc_emulate_tag_SOURCES = nfc-emulate-tag.c nfc_emulate_tag_LDADD = $(top_builddir)/libnfc/libnfc.la \ $(top_builddir)/utils/libnfcutils.la diff --git a/utils/nfc-emulate-forum-tag2.c b/examples/nfc-emulate-forum-tag2.c similarity index 99% rename from utils/nfc-emulate-forum-tag2.c rename to examples/nfc-emulate-forum-tag2.c index 04e274c..c514152 100644 --- a/utils/nfc-emulate-forum-tag2.c +++ b/examples/nfc-emulate-forum-tag2.c @@ -69,7 +69,7 @@ #include #include -#include "nfc-utils.h" +#include "utils/nfc-utils.h" static nfc_device *pnd; diff --git a/utils/CMakeLists.txt b/utils/CMakeLists.txt index c96d2e7..30ea767 100644 --- a/utils/CMakeLists.txt +++ b/utils/CMakeLists.txt @@ -1,4 +1,4 @@ -SET(UTILS-SOURCES nfc-emulate-forum-tag2 nfc-emulate-forum-tag4 nfc-list nfc-relay-picc nfc-mfclassic nfc-mfultralight) +SET(UTILS-SOURCES nfc-emulate-forum-tag4 nfc-list nfc-relay-picc nfc-mfclassic nfc-mfultralight) ADD_LIBRARY(nfcutils STATIC nfc-utils.c diff --git a/utils/Makefile.am b/utils/Makefile.am index b4b1c3e..49ec624 100644 --- a/utils/Makefile.am +++ b/utils/Makefile.am @@ -1,5 +1,4 @@ bin_PROGRAMS = \ - nfc-emulate-forum-tag2 \ nfc-emulate-forum-tag4 \ nfc-list \ nfc-mfclassic \ @@ -18,10 +17,6 @@ noinst_LTLIBRARIES = libnfcutils.la libnfcutils_la_SOURCES = nfc-utils.c -nfc_emulate_forum_tag2_SOURCES = nfc-emulate-forum-tag2.c -nfc_emulate_forum_tag2_LDADD = $(top_builddir)/libnfc/libnfc.la \ - libnfcutils.la - nfc_emulate_forum_tag4_SOURCES = nfc-emulate-forum-tag4.c nfc_emulate_forum_tag4_LDADD = $(top_builddir)/libnfc/libnfc.la \ libnfcutils.la From 784a2f86a212afba5f6625a4fa8bfd19d26d48b6 Mon Sep 17 00:00:00 2001 From: Audrey Diacre Date: Thu, 24 Nov 2011 10:54:42 +0000 Subject: [PATCH 005/113] Change byte_t type to uint8_t (Fixes Issue 147) --- examples/nfc-anticol.c | 26 ++-- examples/nfc-dep-initiator.c | 4 +- examples/nfc-dep-target.c | 4 +- examples/nfc-emulate-forum-tag2.c | 2 +- examples/nfc-emulate-tag.c | 6 +- examples/nfc-emulate-uid.c | 14 +- examples/nfc-relay.c | 8 +- examples/pn53x-diagnose.c | 8 +- examples/pn53x-sam.c | 2 +- examples/pn53x-tamashell.c | 8 +- include/nfc/nfc-emulation.h | 2 +- include/nfc/nfc-types.h | 62 +++++---- include/nfc/nfc.h | 26 ++-- libnfc/buses/uart.h | 4 +- libnfc/buses/uart_posix.c | 4 +- libnfc/buses/uart_win32.c | 4 +- libnfc/chips/pn53x.c | 214 +++++++++++++++--------------- libnfc/chips/pn53x.h | 78 +++++------ libnfc/drivers/acr122.c | 20 +-- libnfc/drivers/acr122.h | 4 +- libnfc/drivers/arygon.c | 32 ++--- libnfc/drivers/arygon.h | 4 +- libnfc/drivers/pn532_uart.c | 16 +-- libnfc/drivers/pn532_uart.h | 4 +- libnfc/drivers/pn53x_usb.c | 26 ++-- libnfc/drivers/pn53x_usb.h | 4 +- libnfc/iso14443-subr.c | 18 +-- libnfc/mirror-subr.c | 12 +- libnfc/mirror-subr.h | 4 +- libnfc/nfc-emulation.c | 4 +- libnfc/nfc-internal.c | 12 +- libnfc/nfc-internal.h | 24 ++-- libnfc/nfc.c | 28 ++-- test/test_dep.c | 14 +- utils/mifare.c | 6 +- utils/mifare.h | 46 +++---- utils/nfc-emulate-forum-tag4.c | 2 +- utils/nfc-mfclassic.c | 18 +-- utils/nfc-mfsetuid.c | 38 +++--- utils/nfc-read-forum-tag3.c | 22 +-- utils/nfc-relay-picc.c | 12 +- utils/nfc-utils.c | 30 ++--- utils/nfc-utils.h | 10 +- 43 files changed, 442 insertions(+), 444 deletions(-) diff --git a/examples/nfc-anticol.c b/examples/nfc-anticol.c index 4566c98..f6b4ca4 100644 --- a/examples/nfc-anticol.c +++ b/examples/nfc-anticol.c @@ -51,14 +51,14 @@ #define MAX_FRAME_LEN 264 -static byte_t abtRx[MAX_FRAME_LEN]; +static uint8_t abtRx[MAX_FRAME_LEN]; static size_t szRxBits; static size_t szRx = sizeof(abtRx); -static byte_t abtRawUid[12]; -static byte_t abtAtqa[2]; -static byte_t abtSak; -static byte_t abtAts[MAX_FRAME_LEN]; -static byte_t szAts = 0; +static uint8_t abtRawUid[12]; +static uint8_t abtAtqa[2]; +static uint8_t abtSak; +static uint8_t abtAts[MAX_FRAME_LEN]; +static uint8_t szAts = 0; static size_t szCL = 1;//Always start with Cascade Level 1 (CL1) static nfc_device *pnd; @@ -67,15 +67,15 @@ bool force_rats = false; bool iso_ats_supported = false; // ISO14443A Anti-Collision Commands -byte_t abtReqa[1] = { 0x26 }; -byte_t abtSelectAll[2] = { 0x93, 0x20 }; -byte_t abtSelectTag[9] = { 0x93, 0x70, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; -byte_t abtRats[4] = { 0xe0, 0x50, 0x00, 0x00 }; -byte_t abtHalt[4] = { 0x50, 0x00, 0x00, 0x00 }; +uint8_t abtReqa[1] = { 0x26 }; +uint8_t abtSelectAll[2] = { 0x93, 0x20 }; +uint8_t abtSelectTag[9] = { 0x93, 0x70, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; +uint8_t abtRats[4] = { 0xe0, 0x50, 0x00, 0x00 }; +uint8_t abtHalt[4] = { 0x50, 0x00, 0x00, 0x00 }; #define CASCADE_BIT 0x04 static bool -transmit_bits (const byte_t * pbtTx, const size_t szTxBits) +transmit_bits (const uint8_t * pbtTx, const size_t szTxBits) { // Show transmitted command if (!quiet_output) { @@ -97,7 +97,7 @@ transmit_bits (const byte_t * pbtTx, const size_t szTxBits) static bool -transmit_bytes (const byte_t * pbtTx, const size_t szTx) +transmit_bytes (const uint8_t * pbtTx, const size_t szTx) { // Show transmitted command if (!quiet_output) { diff --git a/examples/nfc-dep-initiator.c b/examples/nfc-dep-initiator.c index 37cf78c..d3ca65e 100644 --- a/examples/nfc-dep-initiator.c +++ b/examples/nfc-dep-initiator.c @@ -63,9 +63,9 @@ int main (int argc, const char *argv[]) { nfc_target nt; - byte_t abtRx[MAX_FRAME_LEN]; + uint8_t abtRx[MAX_FRAME_LEN]; size_t szRx = sizeof(abtRx); - byte_t abtTx[] = "Hello World!"; + uint8_t abtTx[] = "Hello World!"; if (argc > 1) { printf ("Usage: %s\n", argv[0]); diff --git a/examples/nfc-dep-target.c b/examples/nfc-dep-target.c index a46bf45..2195ea8 100644 --- a/examples/nfc-dep-target.c +++ b/examples/nfc-dep-target.c @@ -61,10 +61,10 @@ void stop_dep_communication (int sig) int main (int argc, const char *argv[]) { - byte_t abtRx[MAX_FRAME_LEN]; + uint8_t abtRx[MAX_FRAME_LEN]; size_t szRx = sizeof(abtRx); size_t szDeviceFound; - byte_t abtTx[] = "Hello Mars!"; + uint8_t abtTx[] = "Hello Mars!"; #define MAX_DEVICE_COUNT 2 nfc_connstring connstrings[MAX_DEVICE_COUNT]; nfc_list_devices (connstrings, MAX_DEVICE_COUNT, &szDeviceFound); diff --git a/examples/nfc-emulate-forum-tag2.c b/examples/nfc-emulate-forum-tag2.c index c514152..5289269 100644 --- a/examples/nfc-emulate-forum-tag2.c +++ b/examples/nfc-emulate-forum-tag2.c @@ -112,7 +112,7 @@ static uint8_t __nfcforum_tag2_memory_area[] = { #define HALT 0x50 int -nfcforum_tag2_io (struct nfc_emulator *emulator, const byte_t *data_in, const size_t data_in_len, byte_t *data_out, const size_t data_out_len) +nfcforum_tag2_io (struct nfc_emulator *emulator, const uint8_t *data_in, const size_t data_in_len, uint8_t *data_out, const size_t data_out_len) { int res = 0; diff --git a/examples/nfc-emulate-tag.c b/examples/nfc-emulate-tag.c index 133701d..e75b569 100644 --- a/examples/nfc-emulate-tag.c +++ b/examples/nfc-emulate-tag.c @@ -54,7 +54,7 @@ #define MAX_FRAME_LEN (264) #define SAK_ISO14443_4_COMPLIANT 0x20 -static byte_t abtRx[MAX_FRAME_LEN]; +static uint8_t abtRx[MAX_FRAME_LEN]; static size_t szRx = sizeof(abtRx); static nfc_device *pnd; static bool quiet_output = false; @@ -71,7 +71,7 @@ intr_hdlr (void) } bool -target_io( nfc_target * pnt, const byte_t * pbtInput, const size_t szInput, byte_t * pbtOutput, size_t *pszOutput ) +target_io( nfc_target * pnt, const uint8_t * pbtInput, const size_t szInput, uint8_t * pbtOutput, size_t *pszOutput ) { bool loop = true; *pszOutput = 0; @@ -137,7 +137,7 @@ bool nfc_target_emulate_tag(nfc_device* pnd, nfc_target * pnt) { size_t szTx; - byte_t abtTx[MAX_FRAME_LEN]; + uint8_t abtTx[MAX_FRAME_LEN]; bool loop = true; if (!nfc_target_init (pnd, pnt, abtRx, &szRx)) { diff --git a/examples/nfc-emulate-uid.c b/examples/nfc-emulate-uid.c index a6f8387..4100ee8 100644 --- a/examples/nfc-emulate-uid.c +++ b/examples/nfc-emulate-uid.c @@ -56,14 +56,14 @@ #define MAX_FRAME_LEN 264 -static byte_t abtRecv[MAX_FRAME_LEN]; +static uint8_t abtRecv[MAX_FRAME_LEN]; static size_t szRecvBits; static nfc_device *pnd; // ISO14443A Anti-Collision response -byte_t abtAtqa[2] = { 0x04, 0x00 }; -byte_t abtUidBcc[5] = { 0xDE, 0xAD, 0xBE, 0xEF, 0x62 }; -byte_t abtSak[9] = { 0x08, 0xb6, 0xdd }; +uint8_t abtAtqa[2] = { 0x04, 0x00 }; +uint8_t abtUidBcc[5] = { 0xDE, 0xAD, 0xBE, 0xEF, 0x62 }; +uint8_t abtSak[9] = { 0x08, 0xb6, 0xdd }; void intr_hdlr (void) @@ -88,7 +88,7 @@ print_usage (char *argv[]) int main (int argc, char *argv[]) { - byte_t *pbtTx = NULL; + uint8_t *pbtTx = NULL; size_t szTxBits; bool quiet_output = false; @@ -104,12 +104,12 @@ main (int argc, char *argv[]) printf ("Quiet mode.\n"); quiet_output = true; } else if ((arg == argc - 1) && (strlen (argv[arg]) == 8)) { // See if UID was specified as HEX string - byte_t abtTmp[3] = { 0x00, 0x00, 0x00 }; + uint8_t abtTmp[3] = { 0x00, 0x00, 0x00 }; printf ("[+] Using UID: %s\n", argv[arg]); abtUidBcc[4] = 0x00; for (i = 0; i < 4; ++i) { memcpy (abtTmp, argv[arg] + i * 2, 2); - abtUidBcc[i] = (byte_t) strtol ((char *) abtTmp, NULL, 16); + abtUidBcc[i] = (uint8_t) strtol ((char *) abtTmp, NULL, 16); abtUidBcc[4] ^= abtUidBcc[i]; } } else { diff --git a/examples/nfc-relay.c b/examples/nfc-relay.c index 631a63a..d94da4b 100644 --- a/examples/nfc-relay.c +++ b/examples/nfc-relay.c @@ -50,11 +50,11 @@ #define MAX_FRAME_LEN 264 #define MAX_DEVICE_COUNT 2 -static byte_t abtReaderRx[MAX_FRAME_LEN]; -static byte_t abtReaderRxPar[MAX_FRAME_LEN]; +static uint8_t abtReaderRx[MAX_FRAME_LEN]; +static uint8_t abtReaderRxPar[MAX_FRAME_LEN]; static size_t szReaderRxBits; -static byte_t abtTagRx[MAX_FRAME_LEN]; -static byte_t abtTagRxPar[MAX_FRAME_LEN]; +static uint8_t abtTagRx[MAX_FRAME_LEN]; +static uint8_t abtTagRxPar[MAX_FRAME_LEN]; static size_t szTagRxBits; static nfc_device *pndReader; static nfc_device *pndTag; diff --git a/examples/pn53x-diagnose.c b/examples/pn53x-diagnose.c index 0bf53c6..5ff4bc1 100644 --- a/examples/pn53x-diagnose.c +++ b/examples/pn53x-diagnose.c @@ -58,11 +58,11 @@ main (int argc, const char *argv[]) const char *acLibnfcVersion; bool result; - byte_t abtRx[PN53x_EXTENDED_FRAME__DATA_MAX_LEN]; + uint8_t abtRx[PN53x_EXTENDED_FRAME__DATA_MAX_LEN]; size_t szRx = sizeof(abtRx); - const byte_t pncmd_diagnose_communication_line_test[] = { Diagnose, 0x00, 0x06, 'l', 'i', 'b', 'n', 'f', 'c' }; - const byte_t pncmd_diagnose_rom_test[] = { Diagnose, 0x01 }; - const byte_t pncmd_diagnose_ram_test[] = { Diagnose, 0x02 }; + const uint8_t pncmd_diagnose_communication_line_test[] = { Diagnose, 0x00, 0x06, 'l', 'i', 'b', 'n', 'f', 'c' }; + const uint8_t pncmd_diagnose_rom_test[] = { Diagnose, 0x01 }; + const uint8_t pncmd_diagnose_ram_test[] = { Diagnose, 0x02 }; if (argc > 1) { errx (1, "usage: %s", argv[0]); diff --git a/examples/pn53x-sam.c b/examples/pn53x-sam.c index 113cac0..11a4c29 100644 --- a/examples/pn53x-sam.c +++ b/examples/pn53x-sam.c @@ -153,7 +153,7 @@ main (int argc, const char *argv[]) case PSM_DUAL_CARD: { - byte_t abtRx[MAX_FRAME_LEN]; + uint8_t abtRx[MAX_FRAME_LEN]; size_t szRx = sizeof(abtRx); nfc_target nt = { diff --git a/examples/pn53x-tamashell.c b/examples/pn53x-tamashell.c index 1929697..3621d2c 100644 --- a/examples/pn53x-tamashell.c +++ b/examples/pn53x-tamashell.c @@ -72,8 +72,8 @@ int main(int argc, const char* argv[]) { nfc_device* pnd; - byte_t abtRx[MAX_FRAME_LEN]; - byte_t abtTx[MAX_FRAME_LEN]; + uint8_t abtRx[MAX_FRAME_LEN]; + uint8_t abtTx[MAX_FRAME_LEN]; size_t szRx = sizeof(abtRx); size_t szTx; extern FILE* stdin; @@ -154,7 +154,7 @@ int main(int argc, const char* argv[]) szTx = 0; for(int i = 0; i # include -typedef uint8_t byte_t; - # define DEVICE_NAME_LENGTH 256 # define DEVICE_PORT_LENGTH 64 @@ -59,7 +57,7 @@ typedef struct { selecting tags supporting it? */ bool bAutoIso14443_4; /** Supported modulation encoded in a byte */ - byte_t btSupportByte; + uint8_t btSupportByte; /** Last error reported by the PCD / encountered by the PCD driver * MSB LSB * | 00 | 00 | @@ -73,7 +71,7 @@ typedef struct { typedef char nfc_connstring[1024]; -// Compiler directive, set struct alignment to 1 byte_t for compatibility +// Compiler directive, set struct alignment to 1 uint8_t for compatibility # pragma pack(1) /** @@ -155,19 +153,19 @@ typedef enum { */ typedef struct { /** NFCID3 */ - byte_t abtNFCID3[10]; + uint8_t abtNFCID3[10]; /** DID */ - byte_t btDID; + uint8_t btDID; /** Supported send-bit rate */ - byte_t btBS; + uint8_t btBS; /** Supported receive-bit rate */ - byte_t btBR; + uint8_t btBR; /** Timeout value */ - byte_t btTO; + uint8_t btTO; /** PP Parameters */ - byte_t btPP; + uint8_t btPP; /** General Bytes */ - byte_t abtGB[48]; + uint8_t abtGB[48]; size_t szGB; /** DEP mode */ nfc_dep_mode ndm; @@ -178,12 +176,12 @@ typedef struct { * @brief NFC ISO14443A tag (MIFARE) information */ typedef struct { - byte_t abtAtqa[2]; - byte_t btSak; + uint8_t abtAtqa[2]; + uint8_t btSak; size_t szUidLen; - byte_t abtUid[10]; + uint8_t abtUid[10]; size_t szAtsLen; - byte_t abtAts[254]; // Maximal theoretical ATS is FSD-2, FSD=256 for FSDI=8 in RATS + uint8_t abtAts[254]; // Maximal theoretical ATS is FSD-2, FSD=256 for FSDI=8 in RATS } nfc_iso14443a_info; /** @@ -192,10 +190,10 @@ typedef struct { */ typedef struct { size_t szLen; - byte_t btResCode; - byte_t abtId[8]; - byte_t abtPad[8]; - byte_t abtSysCode[2]; + uint8_t btResCode; + uint8_t abtId[8]; + uint8_t abtPad[8]; + uint8_t abtSysCode[2]; } nfc_felica_info; /** @@ -204,11 +202,11 @@ typedef struct { */ typedef struct { /** abtPupi store PUPI contained in ATQB (Answer To reQuest of type B) (see ISO14443-3) */ - byte_t abtPupi[4]; + uint8_t abtPupi[4]; /** abtApplicationData store Application Data contained in ATQB (see ISO14443-3) */ - byte_t abtApplicationData[4]; + uint8_t abtApplicationData[4]; /** abtProtocolInfo store Protocol Info contained in ATQB (see ISO14443-3) */ - byte_t abtProtocolInfo[3]; + uint8_t abtProtocolInfo[3]; /** ui8CardIdentifier store CID (Card Identifier) attributted by PCD to the PICC */ uint8_t ui8CardIdentifier; } nfc_iso14443b_info; @@ -219,14 +217,14 @@ typedef struct { */ typedef struct { /** DIV: 4 LSBytes of tag serial number */ - byte_t abtDIV[4]; + uint8_t abtDIV[4]; /** Software version & type of REPGEN */ - byte_t btVerLog; + uint8_t btVerLog; /** Config Byte, present if long REPGEN */ - byte_t btConfig; + uint8_t btConfig; /** ATR, if any */ size_t szAtrLen; - byte_t abtAtr[33]; + uint8_t abtAtr[33]; } nfc_iso14443bi_info; /** @@ -234,7 +232,7 @@ typedef struct { * @brief NFC ISO14443-2B ST SRx tag information */ typedef struct { - byte_t abtUID[8]; + uint8_t abtUID[8]; } nfc_iso14443b2sr_info; /** @@ -242,9 +240,9 @@ typedef struct { * @brief NFC ISO14443-2B ASK CTx tag information */ typedef struct { - byte_t abtUID[4]; - byte_t btProdCode; - byte_t btFabCode; + uint8_t abtUID[4]; + uint8_t btProdCode; + uint8_t btFabCode; } nfc_iso14443b2ct_info; /** @@ -252,8 +250,8 @@ typedef struct { * @brief NFC Jewel tag information */ typedef struct { - byte_t btSensRes[2]; - byte_t btId[4]; + uint8_t btSensRes[2]; + uint8_t btId[4]; } nfc_jewel_info; /** diff --git a/include/nfc/nfc.h b/include/nfc/nfc.h index c90fa17..3babb1b 100644 --- a/include/nfc/nfc.h +++ b/include/nfc/nfc.h @@ -73,22 +73,22 @@ extern "C" { /* NFC initiator: act as "reader" */ NFC_EXPORT bool nfc_initiator_init (nfc_device * pnd); - NFC_EXPORT bool nfc_initiator_select_passive_target (nfc_device * pnd, const nfc_modulation nm, const byte_t * pbtInitData, const size_t szInitData, nfc_target * pnt); + NFC_EXPORT bool nfc_initiator_select_passive_target (nfc_device * pnd, const nfc_modulation nm, const uint8_t * pbtInitData, const size_t szInitData, nfc_target * pnt); NFC_EXPORT bool nfc_initiator_list_passive_targets (nfc_device * pnd, const nfc_modulation nm, nfc_target ant[], const size_t szTargets, size_t * pszTargetFound); NFC_EXPORT bool nfc_initiator_poll_target (nfc_device * pnd, const nfc_modulation * pnmTargetTypes, const size_t szTargetTypes, const uint8_t uiPollNr, const uint8_t uiPeriod, nfc_target * pnt); NFC_EXPORT bool nfc_initiator_select_dep_target (nfc_device * pnd, const nfc_dep_mode ndm, const nfc_baud_rate nbr, const nfc_dep_info * pndiInitiator, nfc_target * pnt); NFC_EXPORT bool nfc_initiator_deselect_target (nfc_device * pnd); - NFC_EXPORT bool nfc_initiator_transceive_bytes (nfc_device * pnd, const byte_t * pbtTx, const size_t szTx, byte_t * pbtRx, size_t * pszRx, struct timeval *timeout); - NFC_EXPORT bool nfc_initiator_transceive_bits (nfc_device * pnd, const byte_t * pbtTx, const size_t szTxBits, const byte_t * pbtTxPar, byte_t * pbtRx, size_t * pszRxBits, byte_t * pbtRxPar); - NFC_EXPORT bool nfc_initiator_transceive_bytes_timed (nfc_device * pnd, const byte_t * pbtTx, const size_t szTx, byte_t * pbtRx, size_t * pszRx, uint32_t * cycles); - NFC_EXPORT bool nfc_initiator_transceive_bits_timed (nfc_device * pnd, const byte_t * pbtTx, const size_t szTxBits, const byte_t * pbtTxPar, byte_t * pbtRx, size_t * pszRxBits, byte_t * pbtRxPar, uint32_t * cycles); + NFC_EXPORT bool nfc_initiator_transceive_bytes (nfc_device * pnd, const uint8_t * pbtTx, const size_t szTx, uint8_t * pbtRx, size_t * pszRx, struct timeval *timeout); + NFC_EXPORT bool nfc_initiator_transceive_bits (nfc_device * pnd, const uint8_t * pbtTx, const size_t szTxBits, const uint8_t * pbtTxPar, uint8_t * pbtRx, size_t * pszRxBits, uint8_t * pbtRxPar); + NFC_EXPORT bool nfc_initiator_transceive_bytes_timed (nfc_device * pnd, const uint8_t * pbtTx, const size_t szTx, uint8_t * pbtRx, size_t * pszRx, uint32_t * cycles); + NFC_EXPORT bool nfc_initiator_transceive_bits_timed (nfc_device * pnd, const uint8_t * pbtTx, const size_t szTxBits, const uint8_t * pbtTxPar, uint8_t * pbtRx, size_t * pszRxBits, uint8_t * pbtRxPar, uint32_t * cycles); /* NFC target: act as tag (i.e. MIFARE Classic) or NFC target device. */ - NFC_EXPORT bool nfc_target_init (nfc_device * pnd, nfc_target * pnt, byte_t * pbtRx, size_t * pszRx); - NFC_EXPORT bool nfc_target_send_bytes (nfc_device * pnd, const byte_t * pbtTx, const size_t szTx, struct timeval *timout); - NFC_EXPORT bool nfc_target_receive_bytes (nfc_device * pnd, byte_t * pbtRx, size_t * pszRx, struct timeval *timout); - NFC_EXPORT bool nfc_target_send_bits (nfc_device * pnd, const byte_t * pbtTx, const size_t szTxBits, const byte_t * pbtTxPar); - NFC_EXPORT bool nfc_target_receive_bits (nfc_device * pnd, byte_t * pbtRx, size_t * pszRxBits, byte_t * pbtRxPar); + NFC_EXPORT bool nfc_target_init (nfc_device * pnd, nfc_target * pnt, uint8_t * pbtRx, size_t * pszRx); + NFC_EXPORT bool nfc_target_send_bytes (nfc_device * pnd, const uint8_t * pbtTx, const size_t szTx, struct timeval *timout); + NFC_EXPORT bool nfc_target_receive_bytes (nfc_device * pnd, uint8_t * pbtRx, size_t * pszRx, struct timeval *timout); + NFC_EXPORT bool nfc_target_send_bits (nfc_device * pnd, const uint8_t * pbtTx, const size_t szTxBits, const uint8_t * pbtTxPar); + NFC_EXPORT bool nfc_target_receive_bits (nfc_device * pnd, uint8_t * pbtRx, size_t * pszRxBits, uint8_t * pbtRxPar); /* Error reporting */ NFC_EXPORT const char *nfc_strerror (const nfc_device * pnd); @@ -99,9 +99,9 @@ extern "C" { NFC_EXPORT const char *nfc_device_name (nfc_device * pnd); /* Misc. functions */ - NFC_EXPORT void iso14443a_crc (byte_t * pbtData, size_t szLen, byte_t * pbtCrc); - NFC_EXPORT void iso14443a_crc_append (byte_t * pbtData, size_t szLen); - NFC_EXPORT byte_t * iso14443a_locate_historical_bytes (byte_t * pbtAts, size_t szAts, size_t * pszTk); + NFC_EXPORT void iso14443a_crc (uint8_t * pbtData, size_t szLen, uint8_t * pbtCrc); + NFC_EXPORT void iso14443a_crc_append (uint8_t * pbtData, size_t szLen); + NFC_EXPORT uint8_t * iso14443a_locate_historical_bytes (uint8_t * pbtAts, size_t szAts, size_t * pszTk); NFC_EXPORT const char *nfc_version (void); /* PN53x specific errors */ diff --git a/libnfc/buses/uart.h b/libnfc/buses/uart.h index a2fbd96..24b4924 100644 --- a/libnfc/buses/uart.h +++ b/libnfc/buses/uart.h @@ -49,8 +49,8 @@ void uart_flush_input (const serial_port sp); void uart_set_speed (serial_port sp, const uint32_t uiPortSpeed); uint32_t uart_get_speed (const serial_port sp); -int uart_receive (serial_port sp, byte_t * pbtRx, const size_t szRx, void * abort_p, struct timeval *timeout); -int uart_send (serial_port sp, const byte_t * pbtTx, const size_t szTx, struct timeval *timeout); +int uart_receive (serial_port sp, uint8_t * pbtRx, const size_t szRx, void * abort_p, struct timeval *timeout); +int uart_send (serial_port sp, const uint8_t * pbtTx, const size_t szTx, struct timeval *timeout); char **uart_list_ports (void); diff --git a/libnfc/buses/uart_posix.c b/libnfc/buses/uart_posix.c index 9af33e4..e74b9b3 100644 --- a/libnfc/buses/uart_posix.c +++ b/libnfc/buses/uart_posix.c @@ -249,7 +249,7 @@ uart_close (const serial_port sp) * @return 0 on success, otherwise driver error code */ int -uart_receive (serial_port sp, byte_t * pbtRx, const size_t szRx, void * abort_p, struct timeval *timeout) +uart_receive (serial_port sp, uint8_t * pbtRx, const size_t szRx, void * abort_p, struct timeval *timeout) { int iAbortFd = abort_p ? *((int*)abort_p) : 0; int received_bytes_count = 0; @@ -327,7 +327,7 @@ select: * @return 0 on success, otherwise a driver error is returned */ int -uart_send (serial_port sp, const byte_t * pbtTx, const size_t szTx, struct timeval *timeout) +uart_send (serial_port sp, const uint8_t * pbtTx, const size_t szTx, struct timeval *timeout) { (void) timeout; LOG_HEX ("TX", pbtTx, szTx); diff --git a/libnfc/buses/uart_win32.c b/libnfc/buses/uart_win32.c index 339be57..4a886bc 100644 --- a/libnfc/buses/uart_win32.c +++ b/libnfc/buses/uart_win32.c @@ -139,7 +139,7 @@ uart_get_speed (const serial_port sp) } int -uart_receive (serial_port sp, byte_t * pbtRx, const size_t szRx, void * abort_p, struct timeval *timeout) +uart_receive (serial_port sp, uint8_t * pbtRx, const size_t szRx, void * abort_p, struct timeval *timeout) { DWORD dwBytesToGet = (DWORD)szRx; DWORD dwBytesReceived = 0; @@ -194,7 +194,7 @@ uart_receive (serial_port sp, byte_t * pbtRx, const size_t szRx, void * abort_p, } int -uart_send (serial_port sp, const byte_t * pbtTx, const size_t szTx, struct timeval *timeout) +uart_send (serial_port sp, const uint8_t * pbtTx, const size_t szTx, struct timeval *timeout) { DWORD dwTxLen = 0; diff --git a/libnfc/chips/pn53x.c b/libnfc/chips/pn53x.c index 7de9520..4e4b058 100644 --- a/libnfc/chips/pn53x.c +++ b/libnfc/chips/pn53x.c @@ -46,9 +46,9 @@ #define LOG_CATEGORY "libnfc.chip.pn53x" -const byte_t pn53x_ack_frame[] = { 0x00, 0x00, 0xff, 0x00, 0xff, 0x00 }; -const byte_t pn53x_nack_frame[] = { 0x00, 0x00, 0xff, 0xff, 0x00, 0x00 }; -static const byte_t pn53x_error_frame[] = { 0x00, 0x00, 0xff, 0x01, 0xff, 0x7f, 0x81, 0x00 }; +const uint8_t pn53x_ack_frame[] = { 0x00, 0x00, 0xff, 0x00, 0xff, 0x00 }; +const uint8_t pn53x_nack_frame[] = { 0x00, 0x00, 0xff, 0xff, 0x00, 0x00 }; +static const uint8_t pn53x_error_frame[] = { 0x00, 0x00, 0xff, 0x01, 0xff, 0x7f, 0x81, 0x00 }; /* prototypes */ bool pn53x_reset_settings (nfc_device * pnd); @@ -100,7 +100,7 @@ pn53x_reset_settings(nfc_device * pnd) } bool -pn53x_transceive (nfc_device * pnd, const byte_t * pbtTx, const size_t szTx, byte_t * pbtRx, size_t *pszRx, struct timeval *timeout) +pn53x_transceive (nfc_device * pnd, const uint8_t * pbtTx, const size_t szTx, uint8_t * pbtRx, size_t *pszRx, struct timeval *timeout) { if (CHIP_DATA (pnd)->wb_trigged) { if (!pn53x_writeback_register (pnd)) { @@ -112,7 +112,7 @@ pn53x_transceive (nfc_device * pnd, const byte_t * pbtTx, const size_t szTx, byt if (timeout) log_put (LOG_CATEGORY, NFC_PRIORITY_TRACE, "Timeout values: %li s, %li us", timeout->tv_sec, timeout->tv_usec); - byte_t abtRx[PN53x_EXTENDED_FRAME__DATA_MAX_LEN]; + uint8_t abtRx[PN53x_EXTENDED_FRAME__DATA_MAX_LEN]; size_t szRx = sizeof(abtRx); // Check if receiving buffers are available, if not, replace them @@ -215,11 +215,11 @@ pn53x_set_tx_bits (nfc_device * pnd, const uint8_t ui8Bits) } bool -pn53x_wrap_frame (const byte_t * pbtTx, const size_t szTxBits, const byte_t * pbtTxPar, - byte_t * pbtFrame, size_t * pszFrameBits) +pn53x_wrap_frame (const uint8_t * pbtTx, const size_t szTxBits, const uint8_t * pbtTxPar, + uint8_t * pbtFrame, size_t * pszFrameBits) { - byte_t btFrame; - byte_t btData; + uint8_t btFrame; + uint8_t btData; uint32_t uiBitPos; uint32_t uiDataPos = 0; size_t szBitsLeft = szTxBits; @@ -271,14 +271,14 @@ pn53x_wrap_frame (const byte_t * pbtTx, const size_t szTxBits, const byte_t * pb } bool -pn53x_unwrap_frame (const byte_t * pbtFrame, const size_t szFrameBits, byte_t * pbtRx, size_t * pszRxBits, - byte_t * pbtRxPar) +pn53x_unwrap_frame (const uint8_t * pbtFrame, const size_t szFrameBits, uint8_t * pbtRx, size_t * pszRxBits, + uint8_t * pbtRxPar) { - byte_t btFrame; - byte_t btData; + uint8_t btFrame; + uint8_t btData; uint8_t uiBitPos; uint32_t uiDataPos = 0; - byte_t *pbtFramePos = (byte_t *) pbtFrame; + uint8_t *pbtFramePos = (uint8_t *) pbtFrame; size_t szBitsLeft = szFrameBits; // Make sure we should frame at least something @@ -318,7 +318,7 @@ pn53x_unwrap_frame (const byte_t * pbtFrame, const size_t szFrameBits, byte_t * } bool -pn53x_decode_target_data (const byte_t * pbtRawData, size_t szRawData, pn53x_type type, nfc_modulationype nmt, +pn53x_decode_target_data (const uint8_t * pbtRawData, size_t szRawData, pn53x_type type, nfc_modulationype nmt, nfc_target_info * pnti) { uint8_t szAttribRes; @@ -464,8 +464,8 @@ pn53x_decode_target_data (const byte_t * pbtRawData, size_t szRawData, pn53x_typ bool pn53x_ReadRegister (nfc_device * pnd, uint16_t ui16RegisterAddress, uint8_t * ui8Value) { - byte_t abtCmd[] = { ReadRegister, ui16RegisterAddress >> 8, ui16RegisterAddress & 0xff }; - byte_t abtRegValue[2]; + uint8_t abtCmd[] = { ReadRegister, ui16RegisterAddress >> 8, ui16RegisterAddress & 0xff }; + uint8_t abtRegValue[2]; size_t szRegValue = sizeof (abtRegValue); PNREG_TRACE (ui16RegisterAddress); @@ -489,7 +489,7 @@ bool pn53x_read_register (nfc_device * pnd, uint16_t ui16RegisterAddress, uint8_ bool pn53x_WriteRegister (nfc_device * pnd, const uint16_t ui16RegisterAddress, const uint8_t ui8Value) { - byte_t abtCmd[] = { WriteRegister, ui16RegisterAddress >> 8, ui16RegisterAddress & 0xff, ui8Value }; + uint8_t abtCmd[] = { WriteRegister, ui16RegisterAddress >> 8, ui16RegisterAddress & 0xff, ui8Value }; PNREG_TRACE (ui16RegisterAddress); return pn53x_transceive (pnd, abtCmd, sizeof (abtCmd), NULL, NULL, NULL); } @@ -591,8 +591,8 @@ pn53x_writeback_register (nfc_device * pnd) bool pn53x_get_firmware_version (nfc_device * pnd, char abtFirmwareText[22]) { - const byte_t abtCmd[] = { GetFirmwareVersion }; - byte_t abtFw[4]; + const uint8_t abtCmd[] = { GetFirmwareVersion }; + uint8_t abtFw[4]; size_t szFwLen = sizeof (abtFw); if (!pn53x_transceive (pnd, abtCmd, sizeof (abtCmd), abtFw, &szFwLen, NULL)) { return false; @@ -642,7 +642,7 @@ pn53x_get_firmware_version (nfc_device * pnd, char abtFirmwareText[22]) bool pn53x_configure (nfc_device * pnd, const nfc_device_option ndo, const bool bEnable) { - byte_t btValue; + uint8_t btValue; switch (ndo) { case NDO_HANDLE_CRC: // Enable or disable automatic receiving/sending of CRC bytes @@ -827,9 +827,9 @@ pn53x_idle (nfc_device *pnd) bool pn53x_check_communication (nfc_device *pnd) { - const byte_t abtCmd[] = { Diagnose, 0x00, 'l', 'i', 'b', 'n', 'f', 'c' }; - const byte_t abtExpectedRx[] = { 0x00, 'l', 'i', 'b', 'n', 'f', 'c' }; - byte_t abtRx[sizeof(abtExpectedRx)]; + const uint8_t abtCmd[] = { Diagnose, 0x00, 'l', 'i', 'b', 'n', 'f', 'c' }; + const uint8_t abtExpectedRx[] = { 0x00, 'l', 'i', 'b', 'n', 'f', 'c' }; + uint8_t abtRx[sizeof(abtExpectedRx)]; size_t szRx = sizeof (abtRx); struct timeval timeout; @@ -858,11 +858,11 @@ pn53x_initiator_init (nfc_device * pnd) bool pn53x_initiator_select_passive_target_ext (nfc_device * pnd, const nfc_modulation nm, - const byte_t * pbtInitData, const size_t szInitData, + const uint8_t * pbtInitData, const size_t szInitData, nfc_target * pnt, struct timeval* timeout) { - byte_t abtTargetsData[PN53x_EXTENDED_FRAME__DATA_MAX_LEN]; + uint8_t abtTargetsData[PN53x_EXTENDED_FRAME__DATA_MAX_LEN]; size_t szTargetsData = sizeof(abtTargetsData); if (nm.nmt == NMT_ISO14443BI || nm.nmt == NMT_ISO14443B2SR || nm.nmt == NMT_ISO14443B2CT) { @@ -884,11 +884,11 @@ pn53x_initiator_select_passive_target_ext (nfc_device * pnd, pnd->bEasyFraming = false; if (nm.nmt == NMT_ISO14443B2SR) { // Some work to do before getting the UID... - byte_t abtInitiate[]="\x06\x00"; + uint8_t abtInitiate[]="\x06\x00"; size_t szInitiateLen = 2; - byte_t abtSelect[]="\x0e\x00"; + uint8_t abtSelect[]="\x0e\x00"; size_t szSelectLen = 2; - byte_t abtRx[1]; + uint8_t abtRx[1]; size_t szRxLen = 1; // Getting random Chip_ID if (!pn53x_initiator_transceive_bytes (pnd, abtInitiate, szInitiateLen, abtRx, &szRxLen, timeout)) { @@ -901,7 +901,7 @@ pn53x_initiator_select_passive_target_ext (nfc_device * pnd, } else if (nm.nmt == NMT_ISO14443B2CT) { // Some work to do before getting the UID... - byte_t abtReqt[]="\x10"; + uint8_t abtReqt[]="\x10"; size_t szReqtLen = 1; // Getting product code / fab code & store it in output buffer after the serial nr we'll obtain later if (!pn53x_initiator_transceive_bytes (pnd, abtReqt, szReqtLen, abtTargetsData+2, &szTargetsData, timeout) || szTargetsData != 2) { @@ -914,7 +914,7 @@ pn53x_initiator_select_passive_target_ext (nfc_device * pnd, if (nm.nmt == NMT_ISO14443B2CT) { if (szTargetsData != 2) return false; - byte_t abtRead[]="\xC4"; // Reading UID_MSB (Read address 4) + uint8_t abtRead[]="\xC4"; // Reading UID_MSB (Read address 4) size_t szReadLen = 1; if (!pn53x_initiator_transceive_bytes (pnd, abtRead, szReadLen, abtTargetsData+4, &szTargetsData, timeout) || szTargetsData != 2) { return false; @@ -930,7 +930,7 @@ pn53x_initiator_select_passive_target_ext (nfc_device * pnd, } if (nm.nmt == NMT_ISO14443BI) { // Select tag - byte_t abtAttrib[6]; + uint8_t abtAttrib[6]; size_t szAttribLen = sizeof(abtAttrib); memcpy(abtAttrib, abtTargetsData, szAttribLen); abtAttrib[1] = 0x0f; // ATTRIB @@ -968,7 +968,7 @@ pn53x_initiator_select_passive_target_ext (nfc_device * pnd, bool pn53x_initiator_select_passive_target (nfc_device * pnd, const nfc_modulation nm, - const byte_t * pbtInitData, const size_t szInitData, + const uint8_t * pbtInitData, const size_t szInitData, nfc_target * pnt) { return pn53x_initiator_select_passive_target_ext (pnd, nm, pbtInitData, szInitData, pnt, NULL); @@ -1020,7 +1020,7 @@ pn53x_initiator_poll_target (nfc_device * pnd, do { for (size_t p=0; pbPar) { @@ -1103,7 +1103,7 @@ pn53x_initiator_transceive_bits (nfc_device * pnd, const byte_t * pbtTx, const s // Send the frame to the PN53X chip and get the answer // We have to give the amount of bytes + (the command byte 0x42) - byte_t abtRx[PN53x_EXTENDED_FRAME__DATA_MAX_LEN]; + uint8_t abtRx[PN53x_EXTENDED_FRAME__DATA_MAX_LEN]; size_t szRx = sizeof(abtRx); if (!pn53x_transceive (pnd, abtCmd, szFrameBytes + 1, abtRx, &szRx, NULL)) return false; @@ -1134,11 +1134,11 @@ pn53x_initiator_transceive_bits (nfc_device * pnd, const byte_t * pbtTx, const s } bool -pn53x_initiator_transceive_bytes (nfc_device * pnd, const byte_t * pbtTx, const size_t szTx, byte_t * pbtRx, +pn53x_initiator_transceive_bytes (nfc_device * pnd, const uint8_t * pbtTx, const size_t szTx, uint8_t * pbtRx, size_t * pszRx, struct timeval *timeout) { size_t szExtraTxLen; - byte_t abtCmd[PN53x_EXTENDED_FRAME__DATA_MAX_LEN]; + uint8_t abtCmd[PN53x_EXTENDED_FRAME__DATA_MAX_LEN]; // We can not just send bytes without parity if while the PN53X expects we handled them if (!pnd->bPar) { @@ -1164,7 +1164,7 @@ pn53x_initiator_transceive_bytes (nfc_device * pnd, const byte_t * pbtTx, const // Send the frame to the PN53X chip and get the answer // We have to give the amount of bytes + (the two command bytes 0xD4, 0x42) - byte_t abtRx[PN53x_EXTENDED_FRAME__DATA_MAX_LEN]; + uint8_t abtRx[PN53x_EXTENDED_FRAME__DATA_MAX_LEN]; size_t szRx = sizeof(abtRx); if (!pn53x_transceive (pnd, abtCmd, szTx + szExtraTxLen, abtRx, &szRx, timeout)) return false; @@ -1262,8 +1262,8 @@ uint32_t __pn53x_get_timer(nfc_device * pnd, const uint8_t last_cmd_byte) } bool -pn53x_initiator_transceive_bits_timed (nfc_device * pnd, const byte_t * pbtTx, const size_t szTxBits, - const byte_t * pbtTxPar, byte_t * pbtRx, size_t * pszRxBits, byte_t * pbtRxPar, uint32_t * cycles) +pn53x_initiator_transceive_bits_timed (nfc_device * pnd, const uint8_t * pbtTx, const size_t szTxBits, + const uint8_t * pbtTxPar, uint8_t * pbtRx, size_t * pszRxBits, uint8_t * pbtRxPar, uint32_t * cycles) { // TODO Do something with these bytes... (void) pbtTxPar; @@ -1364,7 +1364,7 @@ pn53x_initiator_transceive_bits_timed (nfc_device * pnd, const byte_t * pbtTx, c } bool -pn53x_initiator_transceive_bytes_timed (nfc_device * pnd, const byte_t * pbtTx, const size_t szTx, byte_t * pbtRx, +pn53x_initiator_transceive_bytes_timed (nfc_device * pnd, const uint8_t * pbtTx, const size_t szTx, uint8_t * pbtRx, size_t * pszRx, uint32_t * cycles) { uint16_t i; @@ -1475,7 +1475,7 @@ pn53x_initiator_deselect_target (nfc_device * pnd) #define SAK_ISO14443_4_COMPLIANT 0x20 #define SAK_ISO18092_COMPLIANT 0x40 bool -pn53x_target_init (nfc_device * pnd, nfc_target * pnt, byte_t * pbtRx, size_t * pszRx) +pn53x_target_init (nfc_device * pnd, nfc_target * pnt, uint8_t * pbtRx, size_t * pszRx) { pn53x_reset_settings(pnd); @@ -1525,16 +1525,16 @@ pn53x_target_init (nfc_device * pnd, nfc_target * pnt, byte_t * pbtRx, size_t * if (!pn53x_write_register (pnd, PN53X_REG_CIU_TxAuto, SYMBOL_INITIAL_RF_ON, 0x04)) return false; - byte_t abtMifareParams[6]; - byte_t * pbtMifareParams = NULL; - byte_t * pbtTkt = NULL; + uint8_t abtMifareParams[6]; + uint8_t * pbtMifareParams = NULL; + uint8_t * pbtTkt = NULL; size_t szTkt = 0; - byte_t abtFeliCaParams[18]; - byte_t * pbtFeliCaParams = NULL; + uint8_t abtFeliCaParams[18]; + uint8_t * pbtFeliCaParams = NULL; - const byte_t * pbtNFCID3t = NULL; - const byte_t * pbtGBt = NULL; + const uint8_t * pbtNFCID3t = NULL; + const uint8_t * pbtGBt = NULL; size_t szGBt = 0; switch(pnt->nm.nmt) { @@ -1625,7 +1625,7 @@ pn53x_target_init (nfc_device * pnd, nfc_target * pnt, byte_t * pbtRx, size_t * bool targetActivated = false; while (!targetActivated) { - byte_t btActivatedMode; + uint8_t btActivatedMode; if(!pn53x_TgInitAsTarget(pnd, ptm, pbtMifareParams, pbtTkt, szTkt, pbtFeliCaParams, pbtNFCID3t, pbtGBt, szGBt, pbtRx, pszRx, &btActivatedMode)) { return false; @@ -1696,11 +1696,11 @@ pn53x_target_init (nfc_device * pnd, nfc_target * pnt, byte_t * pbtRx, size_t * } bool -pn53x_target_receive_bits (nfc_device * pnd, byte_t * pbtRx, size_t * pszRxBits, byte_t * pbtRxPar) +pn53x_target_receive_bits (nfc_device * pnd, uint8_t * pbtRx, size_t * pszRxBits, uint8_t * pbtRxPar) { - byte_t abtCmd[] = { TgGetInitiatorCommand }; + uint8_t abtCmd[] = { TgGetInitiatorCommand }; - byte_t abtRx[PN53x_EXTENDED_FRAME__DATA_MAX_LEN]; + uint8_t abtRx[PN53x_EXTENDED_FRAME__DATA_MAX_LEN]; size_t szRx = sizeof (abtRx); // Try to gather a received frame from the reader if (!pn53x_transceive (pnd, abtCmd, sizeof (abtCmd), abtRx, &szRx, NULL)) @@ -1731,9 +1731,9 @@ pn53x_target_receive_bits (nfc_device * pnd, byte_t * pbtRx, size_t * pszRxBits, } bool -pn53x_target_receive_bytes (nfc_device * pnd, byte_t * pbtRx, size_t * pszRx, struct timeval *timeout) +pn53x_target_receive_bytes (nfc_device * pnd, uint8_t * pbtRx, size_t * pszRx, struct timeval *timeout) { - byte_t abtCmd[1]; + uint8_t abtCmd[1]; // XXX I think this is not a clean way to provide some kind of "EasyFraming" // but at the moment I have no more better than this @@ -1764,7 +1764,7 @@ pn53x_target_receive_bytes (nfc_device * pnd, byte_t * pbtRx, size_t * pszRx, st } // Try to gather a received frame from the reader - byte_t abtRx[PN53x_EXTENDED_FRAME__DATA_MAX_LEN]; + uint8_t abtRx[PN53x_EXTENDED_FRAME__DATA_MAX_LEN]; size_t szRx = sizeof (abtRx); if (!pn53x_transceive (pnd, abtCmd, sizeof (abtCmd), abtRx, &szRx, timeout)) return false; @@ -1780,12 +1780,12 @@ pn53x_target_receive_bytes (nfc_device * pnd, byte_t * pbtRx, size_t * pszRx, st } bool -pn53x_target_send_bits (nfc_device * pnd, const byte_t * pbtTx, const size_t szTxBits, const byte_t * pbtTxPar) +pn53x_target_send_bits (nfc_device * pnd, const uint8_t * pbtTx, const size_t szTxBits, const uint8_t * pbtTxPar) { size_t szFrameBits = 0; size_t szFrameBytes = 0; uint8_t ui8Bits = 0; - byte_t abtCmd[PN53x_EXTENDED_FRAME__DATA_MAX_LEN] = { TgResponseToInitiator }; + uint8_t abtCmd[PN53x_EXTENDED_FRAME__DATA_MAX_LEN] = { TgResponseToInitiator }; // Check if we should prepare the parity bits ourself if (!pnd->bPar) { @@ -1818,9 +1818,9 @@ pn53x_target_send_bits (nfc_device * pnd, const byte_t * pbtTx, const size_t szT } bool -pn53x_target_send_bytes (nfc_device * pnd, const byte_t * pbtTx, const size_t szTx, struct timeval *timeout) +pn53x_target_send_bytes (nfc_device * pnd, const uint8_t * pbtTx, const size_t szTx, struct timeval *timeout) { - byte_t abtCmd[PN53x_EXTENDED_FRAME__DATA_MAX_LEN]; + uint8_t abtCmd[PN53x_EXTENDED_FRAME__DATA_MAX_LEN]; // We can not just send bytes without parity if while the PN53X expects we handled them if (!pnd->bPar) @@ -1935,14 +1935,14 @@ pn53x_strerror (const nfc_device * pnd) bool pn53x_RFConfiguration__RF_field (nfc_device * pnd, bool bEnable) { - byte_t abtCmd[] = { RFConfiguration, RFCI_FIELD, (bEnable) ? 0x01 : 0x00 }; + uint8_t abtCmd[] = { RFConfiguration, RFCI_FIELD, (bEnable) ? 0x01 : 0x00 }; return pn53x_transceive (pnd, abtCmd, sizeof (abtCmd), NULL, NULL, NULL); } bool pn53x_RFConfiguration__Various_timings (nfc_device * pnd, const uint8_t fATR_RES_Timeout, const uint8_t fRetryTimeout) { - byte_t abtCmd[] = { + uint8_t abtCmd[] = { RFConfiguration, RFCI_TIMING, 0x00, // RFU @@ -1955,7 +1955,7 @@ pn53x_RFConfiguration__Various_timings (nfc_device * pnd, const uint8_t fATR_RES bool pn53x_RFConfiguration__MaxRtyCOM (nfc_device * pnd, const uint8_t MaxRtyCOM) { - byte_t abtCmd[] = { + uint8_t abtCmd[] = { RFConfiguration, RFCI_RETRY_DATA, MaxRtyCOM // MaxRtyCOM, default: 0x00 (no retry, only one try), inifite: 0xff @@ -1967,7 +1967,7 @@ bool pn53x_RFConfiguration__MaxRetries (nfc_device * pnd, const uint8_t MxRtyATR, const uint8_t MxRtyPSL, const uint8_t MxRtyPassiveActivation) { // Retry format: 0x00 means only 1 try, 0xff means infinite - byte_t abtCmd[] = { + uint8_t abtCmd[] = { RFConfiguration, RFCI_RETRY_SELECT, MxRtyATR, // MxRtyATR, default: active = 0xff, passive = 0x02 @@ -1980,7 +1980,7 @@ pn53x_RFConfiguration__MaxRetries (nfc_device * pnd, const uint8_t MxRtyATR, con bool pn53x_SetParameters (nfc_device * pnd, const uint8_t ui8Value) { - byte_t abtCmd[] = { SetParameters, ui8Value }; + uint8_t abtCmd[] = { SetParameters, ui8Value }; if(!pn53x_transceive (pnd, abtCmd, sizeof (abtCmd), NULL, NULL, NULL)) { return false; @@ -1993,7 +1993,7 @@ pn53x_SetParameters (nfc_device * pnd, const uint8_t ui8Value) bool pn53x_SAMConfiguration (nfc_device * pnd, const pn532_sam_mode ui8Mode, struct timeval *timeout) { - byte_t abtCmd[] = { SAMConfiguration, ui8Mode, 0x00, 0x00 }; + uint8_t abtCmd[] = { SAMConfiguration, ui8Mode, 0x00, 0x00 }; size_t szCmd = sizeof(abtCmd); if (CHIP_DATA(pnd)->type != PN532) { @@ -2022,7 +2022,7 @@ pn53x_SAMConfiguration (nfc_device * pnd, const pn532_sam_mode ui8Mode, struct t bool pn53x_PowerDown (nfc_device * pnd) { - byte_t abtCmd[] = { PowerDown, 0xf0 }; + uint8_t abtCmd[] = { PowerDown, 0xf0 }; return (pn53x_transceive (pnd, abtCmd, sizeof (abtCmd), NULL, NULL, NULL)); } @@ -2042,12 +2042,12 @@ pn53x_PowerDown (nfc_device * pnd) */ bool pn53x_InListPassiveTarget (nfc_device * pnd, - const pn53x_modulation pmInitModulation, const byte_t szMaxTargets, - const byte_t * pbtInitiatorData, const size_t szInitiatorData, - byte_t * pbtTargetsData, size_t * pszTargetsData, + const pn53x_modulation pmInitModulation, const uint8_t szMaxTargets, + const uint8_t * pbtInitiatorData, const size_t szInitiatorData, + uint8_t * pbtTargetsData, size_t * pszTargetsData, struct timeval* timeout) { - byte_t abtCmd[15] = { InListPassiveTarget }; + uint8_t abtCmd[15] = { InListPassiveTarget }; abtCmd[1] = szMaxTargets; // MaxTg @@ -2098,9 +2098,9 @@ pn53x_InDeselect (nfc_device * pnd, const uint8_t ui8Target) { if (CHIP_DATA(pnd)->type == RCS360) { // We should do act here *only* if a target was previously selected - byte_t abtStatus[PN53x_EXTENDED_FRAME__DATA_MAX_LEN]; + uint8_t abtStatus[PN53x_EXTENDED_FRAME__DATA_MAX_LEN]; size_t szStatus = sizeof(abtStatus); - byte_t abtCmdGetStatus[] = { GetGeneralStatus }; + uint8_t abtCmdGetStatus[] = { GetGeneralStatus }; if (!pn53x_transceive (pnd, abtCmdGetStatus, sizeof (abtCmdGetStatus), abtStatus, &szStatus, NULL)) { return false; } @@ -2108,10 +2108,10 @@ pn53x_InDeselect (nfc_device * pnd, const uint8_t ui8Target) return true; } // No much choice what to deselect actually... - byte_t abtCmdRcs360[] = { InDeselect, 0x01, 0x01 }; + uint8_t abtCmdRcs360[] = { InDeselect, 0x01, 0x01 }; return (pn53x_transceive (pnd, abtCmdRcs360, sizeof (abtCmdRcs360), NULL, NULL, NULL)); } - byte_t abtCmd[] = { InDeselect, ui8Target }; + uint8_t abtCmd[] = { InDeselect, ui8Target }; return (pn53x_transceive (pnd, abtCmd, sizeof (abtCmd), NULL, NULL, NULL)); } @@ -2120,9 +2120,9 @@ pn53x_InRelease (nfc_device * pnd, const uint8_t ui8Target) { if (CHIP_DATA(pnd)->type == RCS360) { // We should do act here *only* if a target was previously selected - byte_t abtStatus[PN53x_EXTENDED_FRAME__DATA_MAX_LEN]; + uint8_t abtStatus[PN53x_EXTENDED_FRAME__DATA_MAX_LEN]; size_t szStatus = sizeof(abtStatus); - byte_t abtCmdGetStatus[] = { GetGeneralStatus }; + uint8_t abtCmdGetStatus[] = { GetGeneralStatus }; if (!pn53x_transceive (pnd, abtCmdGetStatus, sizeof (abtCmdGetStatus), abtStatus, &szStatus, NULL)) { return false; } @@ -2130,17 +2130,17 @@ pn53x_InRelease (nfc_device * pnd, const uint8_t ui8Target) return true; } // No much choice what to release actually... - byte_t abtCmdRcs360[] = { InRelease, 0x01, 0x01 }; + uint8_t abtCmdRcs360[] = { InRelease, 0x01, 0x01 }; return (pn53x_transceive (pnd, abtCmdRcs360, sizeof (abtCmdRcs360), NULL, NULL, NULL)); } - byte_t abtCmd[] = { InRelease, ui8Target }; + uint8_t abtCmd[] = { InRelease, ui8Target }; return (pn53x_transceive (pnd, abtCmd, sizeof (abtCmd), NULL, NULL, NULL)); } bool pn53x_InAutoPoll (nfc_device * pnd, const pn53x_target_type * ppttTargetTypes, const size_t szTargetTypes, - const byte_t btPollNr, const byte_t btPeriod, nfc_target * pntTargets, size_t * pszTargetFound) + const uint8_t btPollNr, const uint8_t btPeriod, nfc_target * pntTargets, size_t * pszTargetFound) { if (CHIP_DATA(pnd)->type != PN532) { // This function is not supported by pn531 neither pn533 @@ -2150,12 +2150,12 @@ pn53x_InAutoPoll (nfc_device * pnd, // InAutoPoll frame looks like this { 0xd4, 0x60, 0x0f, 0x01, 0x00 } => { direction, command, pollnr, period, types... } size_t szTxInAutoPoll = 3 + szTargetTypes; - byte_t abtCmd[3+15] = { InAutoPoll, btPollNr, btPeriod }; + uint8_t abtCmd[3+15] = { InAutoPoll, btPollNr, btPeriod }; for (size_t n = 0; n < szTargetTypes; n++) { abtCmd[3 + n] = ppttTargetTypes[n]; } - byte_t abtRx[PN53x_EXTENDED_FRAME__DATA_MAX_LEN]; + uint8_t abtRx[PN53x_EXTENDED_FRAME__DATA_MAX_LEN]; size_t szRx = sizeof(abtRx); bool res = pn53x_transceive (pnd, abtCmd, szTxInAutoPoll, abtRx, &szRx, NULL); @@ -2165,7 +2165,7 @@ pn53x_InAutoPoll (nfc_device * pnd, *pszTargetFound = abtRx[0]; if (*pszTargetFound) { uint8_t ln; - byte_t *pbt = abtRx + 1; + uint8_t *pbt = abtRx + 1; /* 1st target */ // Target type pn53x_target_type ptt = *(pbt++); @@ -2203,13 +2203,13 @@ bool pn53x_InJumpForDEP (nfc_device * pnd, const nfc_dep_mode ndm, const nfc_baud_rate nbr, - const byte_t * pbtPassiveInitiatorData, - const byte_t * pbtNFCID3i, - const byte_t * pbtGBi, const size_t szGBi, + const uint8_t * pbtPassiveInitiatorData, + const uint8_t * pbtNFCID3i, + const uint8_t * pbtGBi, const size_t szGBi, nfc_target * pnt) { // Max frame size = 1 (Command) + 1 (ActPass) + 1 (Baud rate) + 1 (Next) + 5 (PassiveInitiatorData) + 10 (NFCID3) + 48 (General bytes) = 67 bytes - byte_t abtCmd[67] = { InJumpForDEP, (ndm == NDM_ACTIVE) ? 0x01 : 0x00 }; + uint8_t abtCmd[67] = { InJumpForDEP, (ndm == NDM_ACTIVE) ? 0x01 : 0x00 }; size_t offset = 4; // 1 byte for command, 1 byte for DEP mode (Active/Passive), 1 byte for baud rate, 1 byte for following parameters flag @@ -2263,7 +2263,7 @@ pn53x_InJumpForDEP (nfc_device * pnd, offset += szGBi; } - byte_t abtRx[PN53x_EXTENDED_FRAME__DATA_MAX_LEN]; + uint8_t abtRx[PN53x_EXTENDED_FRAME__DATA_MAX_LEN]; size_t szRx = sizeof (abtRx); // Try to find a target, call the transceive callback function of the current device if (!pn53x_transceive (pnd, abtCmd, offset, abtRx, &szRx, NULL)) @@ -2295,13 +2295,13 @@ pn53x_InJumpForDEP (nfc_device * pnd, bool pn53x_TgInitAsTarget (nfc_device * pnd, pn53x_target_mode ptm, - const byte_t * pbtMifareParams, - const byte_t * pbtTkt, size_t szTkt, - const byte_t * pbtFeliCaParams, - const byte_t * pbtNFCID3t, const byte_t * pbtGBt, const size_t szGBt, - byte_t * pbtRx, size_t * pszRx, byte_t * pbtModeByte) + const uint8_t * pbtMifareParams, + const uint8_t * pbtTkt, size_t szTkt, + const uint8_t * pbtFeliCaParams, + const uint8_t * pbtNFCID3t, const uint8_t * pbtGBt, const size_t szGBt, + uint8_t * pbtRx, size_t * pszRx, uint8_t * pbtModeByte) { - byte_t abtCmd[39 + 47 + 48] = { TgInitAsTarget }; // Worst case: 39-byte base, 47 bytes max. for General Bytes, 48 bytes max. for Historical Bytes + uint8_t abtCmd[39 + 47 + 48] = { TgInitAsTarget }; // Worst case: 39-byte base, 47 bytes max. for General Bytes, 48 bytes max. for Historical Bytes size_t szOptionalBytes = 0; // Clear the target init struct, reset to all zeros @@ -2329,7 +2329,7 @@ pn53x_TgInitAsTarget (nfc_device * pnd, pn53x_target_mode ptm, szOptionalBytes = szGBt; } } else { - abtCmd[36] = (byte_t)(szGBt); + abtCmd[36] = (uint8_t)(szGBt); if (szGBt) { memcpy (abtCmd+37, pbtGBt, szGBt); } @@ -2337,7 +2337,7 @@ pn53x_TgInitAsTarget (nfc_device * pnd, pn53x_target_mode ptm, } // Historical bytes (ISO/IEC 14443-4) if ((CHIP_DATA(pnd)->type != PN531) && (CHIP_DATA(pnd)->type != RCS360)) { // PN531 does not handle Historical Bytes - abtCmd[36+szOptionalBytes] = (byte_t)(szTkt); + abtCmd[36+szOptionalBytes] = (uint8_t)(szTkt); if (szTkt) { memcpy (abtCmd+37+szOptionalBytes, pbtTkt, szTkt); } @@ -2345,7 +2345,7 @@ pn53x_TgInitAsTarget (nfc_device * pnd, pn53x_target_mode ptm, } // Request the initialization as a target - byte_t abtRx[PN53x_EXTENDED_FRAME__DATA_MAX_LEN]; + uint8_t abtRx[PN53x_EXTENDED_FRAME__DATA_MAX_LEN]; size_t szRx = sizeof (abtRx); if (!pn53x_transceive (pnd, abtCmd, 36 + szOptionalBytes, abtRx, &szRx, NULL)) return false; @@ -2365,7 +2365,7 @@ pn53x_TgInitAsTarget (nfc_device * pnd, pn53x_target_mode ptm, } bool -pn53x_check_ack_frame (nfc_device * pnd, const byte_t * pbtRxFrame, const size_t szRxFrameLen) +pn53x_check_ack_frame (nfc_device * pnd, const uint8_t * pbtRxFrame, const size_t szRxFrameLen) { if (szRxFrameLen >= sizeof (pn53x_ack_frame)) { if (0 == memcmp (pbtRxFrame, pn53x_ack_frame, sizeof (pn53x_ack_frame))) { @@ -2379,7 +2379,7 @@ pn53x_check_ack_frame (nfc_device * pnd, const byte_t * pbtRxFrame, const size_t } bool -pn53x_check_error_frame (nfc_device * pnd, const byte_t * pbtRxFrame, const size_t szRxFrameLen) +pn53x_check_error_frame (nfc_device * pnd, const uint8_t * pbtRxFrame, const size_t szRxFrameLen) { if (szRxFrameLen >= sizeof (pn53x_error_frame)) { if (0 == memcmp (pbtRxFrame, pn53x_error_frame, sizeof (pn53x_error_frame))) { @@ -2398,7 +2398,7 @@ pn53x_check_error_frame (nfc_device * pnd, const byte_t * pbtRxFrame, const size * @note The first byte of pbtData is the Command Code (CC) */ bool -pn53x_build_frame (byte_t * pbtFrame, size_t * pszFrame, const byte_t * pbtData, const size_t szData) +pn53x_build_frame (uint8_t * pbtFrame, size_t * pszFrame, const uint8_t * pbtData, const size_t szData) { if (szData <= PN53x_NORMAL_FRAME__DATA_MAX_LEN) { // LEN - Packet length = data length (len) + checksum (1) + end of stream marker (1) @@ -2411,7 +2411,7 @@ pn53x_build_frame (byte_t * pbtFrame, size_t * pszFrame, const byte_t * pbtData, memcpy (pbtFrame + 6, pbtData, szData); // DCS - Calculate data payload checksum - byte_t btDCS = (256 - 0xD4); + uint8_t btDCS = (256 - 0xD4); for (size_t szPos = 0; szPos < szData; szPos++) { btDCS -= pbtData[szPos]; } @@ -2437,7 +2437,7 @@ pn53x_build_frame (byte_t * pbtFrame, size_t * pszFrame, const byte_t * pbtData, memcpy (pbtFrame + 9, pbtData, szData); // DCS - Calculate data payload checksum - byte_t btDCS = (256 - 0xD4); + uint8_t btDCS = (256 - 0xD4); for (size_t szPos = 0; szPos < szData; szPos++) { btDCS -= pbtData[szPos]; } diff --git a/libnfc/chips/pn53x.h b/libnfc/chips/pn53x.h index 4e1a53b..6a1d229 100644 --- a/libnfc/chips/pn53x.h +++ b/libnfc/chips/pn53x.h @@ -127,8 +127,8 @@ typedef enum { } pn53x_operating_mode; struct pn53x_io { - bool (*send)(nfc_device * pnd, const byte_t * pbtData, const size_t szData, struct timeval *timeout); - int (*receive)(nfc_device * pnd, byte_t * pbtData, const size_t szDataLen, struct timeval *timeout); + bool (*send)(nfc_device * pnd, const uint8_t * pbtData, const size_t szData, struct timeval *timeout); + int (*receive)(nfc_device * pnd, uint8_t * pbtData, const size_t szDataLen, struct timeval *timeout); }; /* defines */ @@ -252,19 +252,19 @@ typedef enum { PTM_ISO14443_4_PICC_ONLY = 0x04 } pn53x_target_mode; -extern const byte_t pn53x_ack_frame[6]; -extern const byte_t pn53x_nack_frame[6]; +extern const uint8_t pn53x_ack_frame[6]; +extern const uint8_t pn53x_nack_frame[6]; bool pn53x_init(nfc_device * pnd); -bool pn53x_transceive (nfc_device * pnd, const byte_t * pbtTx, const size_t szTx, byte_t * pbtRx, size_t *pszRx, struct timeval *timeout); +bool pn53x_transceive (nfc_device * pnd, const uint8_t * pbtTx, const size_t szTx, uint8_t * pbtRx, size_t *pszRx, struct timeval *timeout); bool pn53x_set_parameters (nfc_device * pnd, const uint8_t ui8Value, const bool bEnable); bool pn53x_set_tx_bits (nfc_device * pnd, const uint8_t ui8Bits); -bool pn53x_wrap_frame (const byte_t * pbtTx, const size_t szTxBits, const byte_t * pbtTxPar, byte_t * pbtFrame, +bool pn53x_wrap_frame (const uint8_t * pbtTx, const size_t szTxBits, const uint8_t * pbtTxPar, uint8_t * pbtFrame, size_t * pszFrameBits); -bool pn53x_unwrap_frame (const byte_t * pbtFrame, const size_t szFrameBits, byte_t * pbtRx, size_t * pszRxBits, - byte_t * pbtRxPar); -bool pn53x_decode_target_data (const byte_t * pbtRawData, size_t szRawData, +bool pn53x_unwrap_frame (const uint8_t * pbtFrame, const size_t szFrameBits, uint8_t * pbtRx, size_t * pszRxBits, + uint8_t * pbtRxPar); +bool pn53x_decode_target_data (const uint8_t * pbtRawData, size_t szRawData, pn53x_type chip_type, nfc_modulationype nmt, nfc_target_info * pnti); bool pn53x_read_register (nfc_device * pnd, uint16_t ui16Reg, uint8_t * ui8Value); @@ -278,7 +278,7 @@ bool pn53x_idle (nfc_device * pnd); bool pn53x_initiator_init (nfc_device * pnd); bool pn53x_initiator_select_passive_target (nfc_device * pnd, const nfc_modulation nm, - const byte_t * pbtInitData, const size_t szInitData, + const uint8_t * pbtInitData, const size_t szInitData, nfc_target * pnt); bool pn53x_initiator_poll_target (nfc_device * pnd, const nfc_modulation * pnmModulations, const size_t szModulations, @@ -288,24 +288,24 @@ bool pn53x_initiator_select_dep_target (nfc_device * pnd, const nfc_dep_mode ndm, const nfc_baud_rate nbr, const nfc_dep_info * pndiInitiator, nfc_target * pnt); -bool pn53x_initiator_transceive_bits (nfc_device * pnd, const byte_t * pbtTx, const size_t szTxBits, - const byte_t * pbtTxPar, byte_t * pbtRx, size_t * pszRxBits, - byte_t * pbtRxPar); -bool pn53x_initiator_transceive_bytes (nfc_device * pnd, const byte_t * pbtTx, const size_t szTx, - byte_t * pbtRx, size_t * pszRx, struct timeval *timeout); -bool pn53x_initiator_transceive_bits_timed (nfc_device * pnd, const byte_t * pbtTx, const size_t szTxBits, - const byte_t * pbtTxPar, byte_t * pbtRx, size_t * pszRxBits, - byte_t * pbtRxPar, uint32_t * cycles); -bool pn53x_initiator_transceive_bytes_timed (nfc_device * pnd, const byte_t * pbtTx, const size_t szTx, - byte_t * pbtRx, size_t * pszRx, uint32_t * cycles); +bool pn53x_initiator_transceive_bits (nfc_device * pnd, const uint8_t * pbtTx, const size_t szTxBits, + const uint8_t * pbtTxPar, uint8_t * pbtRx, size_t * pszRxBits, + uint8_t * pbtRxPar); +bool pn53x_initiator_transceive_bytes (nfc_device * pnd, const uint8_t * pbtTx, const size_t szTx, + uint8_t * pbtRx, size_t * pszRx, struct timeval *timeout); +bool pn53x_initiator_transceive_bits_timed (nfc_device * pnd, const uint8_t * pbtTx, const size_t szTxBits, + const uint8_t * pbtTxPar, uint8_t * pbtRx, size_t * pszRxBits, + uint8_t * pbtRxPar, uint32_t * cycles); +bool pn53x_initiator_transceive_bytes_timed (nfc_device * pnd, const uint8_t * pbtTx, const size_t szTx, + uint8_t * pbtRx, size_t * pszRx, uint32_t * cycles); bool pn53x_initiator_deselect_target (nfc_device * pnd); // NFC device as Target functions -bool pn53x_target_init (nfc_device * pnd, nfc_target * pnt, byte_t * pbtRx, size_t * pszRx); -bool pn53x_target_receive_bits (nfc_device * pnd, byte_t * pbtRx, size_t * pszRxBits, byte_t * pbtRxPar); -bool pn53x_target_receive_bytes (nfc_device * pnd, byte_t * pbtRx, size_t * pszRx, struct timeval *timeout); -bool pn53x_target_send_bits (nfc_device * pnd, const byte_t * pbtTx, const size_t szTxBits, const byte_t * pbtTxPar); -bool pn53x_target_send_bytes (nfc_device * pnd, const byte_t * pbtTx, const size_t szTx, struct timeval *timeout); +bool pn53x_target_init (nfc_device * pnd, nfc_target * pnt, uint8_t * pbtRx, size_t * pszRx); +bool pn53x_target_receive_bits (nfc_device * pnd, uint8_t * pbtRx, size_t * pszRxBits, uint8_t * pbtRxPar); +bool pn53x_target_receive_bytes (nfc_device * pnd, uint8_t * pbtRx, size_t * pszRx, struct timeval *timeout); +bool pn53x_target_send_bits (nfc_device * pnd, const uint8_t * pbtTx, const size_t szTxBits, const uint8_t * pbtTxPar); +bool pn53x_target_send_bytes (nfc_device * pnd, const uint8_t * pbtTx, const size_t szTx, struct timeval *timeout); // Error handling functions const char *pn53x_strerror (const nfc_device * pnd); @@ -315,26 +315,26 @@ bool pn53x_SetParameters (nfc_device * pnd, const uint8_t ui8Value); bool pn53x_SAMConfiguration (nfc_device * pnd, const pn532_sam_mode mode, struct timeval *timeout); bool pn53x_PowerDown (nfc_device * pnd); bool pn53x_InListPassiveTarget (nfc_device * pnd, const pn53x_modulation pmInitModulation, - const byte_t szMaxTargets, const byte_t * pbtInitiatorData, - const size_t szInitiatorDataLen, byte_t * pbtTargetsData, size_t * pszTargetsData, + const uint8_t szMaxTargets, const uint8_t * pbtInitiatorData, + const size_t szInitiatorDataLen, uint8_t * pbtTargetsData, size_t * pszTargetsData, struct timeval *timeout); bool pn53x_InDeselect (nfc_device * pnd, const uint8_t ui8Target); bool pn53x_InRelease (nfc_device * pnd, const uint8_t ui8Target); bool pn53x_InAutoPoll (nfc_device * pnd, const pn53x_target_type * ppttTargetTypes, const size_t szTargetTypes, - const byte_t btPollNr, const byte_t btPeriod, nfc_target * pntTargets, + const uint8_t btPollNr, const uint8_t btPeriod, nfc_target * pntTargets, size_t * pszTargetFound); bool pn53x_InJumpForDEP (nfc_device * pnd, const nfc_dep_mode ndm, const nfc_baud_rate nbr, - const byte_t * pbtPassiveInitiatorData, - const byte_t * pbtNFCID3i, - const byte_t * pbtGB, const size_t szGB, + const uint8_t * pbtPassiveInitiatorData, + const uint8_t * pbtNFCID3i, + const uint8_t * pbtGB, const size_t szGB, nfc_target * pnt); bool pn53x_TgInitAsTarget (nfc_device * pnd, pn53x_target_mode ptm, - const byte_t * pbtMifareParams, - const byte_t * pbtTkt, size_t szTkt, - const byte_t * pbtFeliCaParams, - const byte_t * pbtNFCID3t, const byte_t * pbtGB, const size_t szGB, - byte_t * pbtRx, size_t * pszRx, byte_t * pbtModeByte); + const uint8_t * pbtMifareParams, + const uint8_t * pbtTkt, size_t szTkt, + const uint8_t * pbtFeliCaParams, + const uint8_t * pbtNFCID3t, const uint8_t * pbtGB, const size_t szGB, + uint8_t * pbtRx, size_t * pszRx, uint8_t * pbtModeByte); // RFConfiguration bool pn53x_RFConfiguration__RF_field (nfc_device * pnd, bool bEnable); @@ -343,9 +343,9 @@ bool pn53x_RFConfiguration__MaxRtyCOM (nfc_device * pnd, const uint8_t MaxRty bool pn53x_RFConfiguration__MaxRetries (nfc_device * pnd, const uint8_t MxRtyATR, const uint8_t MxRtyPSL, const uint8_t MxRtyPassiveActivation); // Misc -bool pn53x_check_ack_frame (nfc_device * pnd, const byte_t * pbtRxFrame, const size_t szRxFrameLen); -bool pn53x_check_error_frame (nfc_device * pnd, const byte_t * pbtRxFrame, const size_t szRxFrameLen); -bool pn53x_build_frame (byte_t * pbtFrame, size_t * pszFrame, const byte_t * pbtData, const size_t szData); +bool pn53x_check_ack_frame (nfc_device * pnd, const uint8_t * pbtRxFrame, const size_t szRxFrameLen); +bool pn53x_check_error_frame (nfc_device * pnd, const uint8_t * pbtRxFrame, const size_t szRxFrameLen); +bool pn53x_build_frame (uint8_t * pbtFrame, size_t * pszFrame, const uint8_t * pbtData, const size_t szData); void pn53x_data_new (nfc_device * pnd, const struct pn53x_io* io); void pn53x_data_free (nfc_device * pnd); diff --git a/libnfc/drivers/acr122.c b/libnfc/drivers/acr122.c index 3c3f349..15bc719 100644 --- a/libnfc/drivers/acr122.c +++ b/libnfc/drivers/acr122.c @@ -89,7 +89,7 @@ const char *supported_devices[] = { struct acr122_data { SCARDHANDLE hCard; SCARD_IO_REQUEST ioCard; - byte_t abtRx[ACR122_RESPONSE_LEN]; + uint8_t abtRx[ACR122_RESPONSE_LEN]; size_t szRx; }; @@ -310,7 +310,7 @@ acr122_disconnect (nfc_device * pnd) } bool -acr122_send (nfc_device * pnd, const byte_t * pbtData, const size_t szData, struct timeval *timeout) +acr122_send (nfc_device * pnd, const uint8_t * pbtData, const size_t szData, struct timeval *timeout) { // FIXME: timeout is not handled (void) timeout; @@ -323,7 +323,7 @@ acr122_send (nfc_device * pnd, const byte_t * pbtData, const size_t szData, stru // Prepare and transmit the send buffer const size_t szTxBuf = szData + 6; - byte_t abtTxBuf[ACR122_WRAP_LEN + ACR122_COMMAND_LEN] = { 0xFF, 0x00, 0x00, 0x00, szData + 1, 0xD4 }; + uint8_t abtTxBuf[ACR122_WRAP_LEN + ACR122_COMMAND_LEN] = { 0xFF, 0x00, 0x00, 0x00, szData + 1, 0xD4 }; memcpy (abtTxBuf + 6, pbtData, szData); LOG_HEX ("TX", abtTxBuf, szTxBuf); @@ -381,13 +381,13 @@ acr122_send (nfc_device * pnd, const byte_t * pbtData, const size_t szData, stru } int -acr122_receive (nfc_device * pnd, byte_t * pbtData, const size_t szData, struct timeval *timeout) +acr122_receive (nfc_device * pnd, uint8_t * pbtData, const size_t szData, struct timeval *timeout) { // FIXME: timeout is not handled (void) timeout; int len; - byte_t abtRxCmd[5] = { 0xFF, 0xC0, 0x00, 0x00 }; + uint8_t abtRxCmd[5] = { 0xFF, 0xC0, 0x00, 0x00 }; if (DRIVER_DATA (pnd)->ioCard.dwProtocol == SCARD_PROTOCOL_T0) { /* @@ -424,16 +424,16 @@ acr122_receive (nfc_device * pnd, byte_t * pbtData, const size_t szData, struct char * acr122_firmware (nfc_device *pnd) { - byte_t abtGetFw[5] = { 0xFF, 0x00, 0x48, 0x00, 0x00 }; + uint8_t abtGetFw[5] = { 0xFF, 0x00, 0x48, 0x00, 0x00 }; uint32_t uiResult; static char abtFw[11]; DWORD dwFwLen = sizeof (abtFw); memset (abtFw, 0x00, sizeof (abtFw)); if (DRIVER_DATA (pnd)->ioCard.dwProtocol == SCARD_PROTOCOL_UNDEFINED) { - uiResult = SCardControl (DRIVER_DATA (pnd)->hCard, IOCTL_CCID_ESCAPE_SCARD_CTL_CODE, abtGetFw, sizeof (abtGetFw), (byte_t *) abtFw, dwFwLen-1, &dwFwLen); + uiResult = SCardControl (DRIVER_DATA (pnd)->hCard, IOCTL_CCID_ESCAPE_SCARD_CTL_CODE, abtGetFw, sizeof (abtGetFw), (uint8_t *) abtFw, dwFwLen-1, &dwFwLen); } else { - uiResult = SCardTransmit (DRIVER_DATA (pnd)->hCard, &(DRIVER_DATA (pnd)->ioCard), abtGetFw, sizeof (abtGetFw), NULL, (byte_t *) abtFw, &dwFwLen); + uiResult = SCardTransmit (DRIVER_DATA (pnd)->hCard, &(DRIVER_DATA (pnd)->ioCard), abtGetFw, sizeof (abtGetFw), NULL, (uint8_t *) abtFw, &dwFwLen); } if (uiResult != SCARD_S_SUCCESS) { @@ -447,8 +447,8 @@ acr122_firmware (nfc_device *pnd) bool acr122_led_red (nfc_device *pnd, bool bOn) { - byte_t abtLed[9] = { 0xFF, 0x00, 0x40, 0x05, 0x04, 0x00, 0x00, 0x00, 0x00 }; - byte_t abtBuf[2]; + uint8_t abtLed[9] = { 0xFF, 0x00, 0x40, 0x05, 0x04, 0x00, 0x00, 0x00, 0x00 }; + uint8_t abtBuf[2]; DWORD dwBufLen = sizeof (abtBuf); (void) bOn; if (DRIVER_DATA (pnd)->ioCard.dwProtocol == SCARD_PROTOCOL_UNDEFINED) { diff --git a/libnfc/drivers/acr122.h b/libnfc/drivers/acr122.h index a139cb2..3f91047 100644 --- a/libnfc/drivers/acr122.h +++ b/libnfc/drivers/acr122.h @@ -30,8 +30,8 @@ bool acr122_probe (nfc_connstring connstrings[], size_t connstrings_len, size // Functions used by developer to handle connection to this device nfc_device *acr122_connect (const nfc_connstring connstring); -bool acr122_send (nfc_device * pnd, const byte_t * pbtData, const size_t szData, struct timeval *timeout); -int acr122_receive (nfc_device * pnd, byte_t * pbtData, const size_t szData, struct timeval *timeout); +bool acr122_send (nfc_device * pnd, const uint8_t * pbtData, const size_t szData, struct timeval *timeout); +int acr122_receive (nfc_device * pnd, uint8_t * pbtData, const size_t szData, struct timeval *timeout); void acr122_disconnect (nfc_device * pnd); extern const struct nfc_driver_t acr122_driver; diff --git a/libnfc/drivers/arygon.c b/libnfc/drivers/arygon.c index d39cd07..b90f124 100644 --- a/libnfc/drivers/arygon.c +++ b/libnfc/drivers/arygon.c @@ -84,9 +84,9 @@ struct arygon_data { #endif }; -static const byte_t arygon_error_none[] = "FF000000\x0d\x0a"; -static const byte_t arygon_error_incomplete_command[] = "FF0C0000\x0d\x0a"; -static const byte_t arygon_error_unknown_mode[] = "FF060000\x0d\x0a"; +static const uint8_t arygon_error_none[] = "FF000000\x0d\x0a"; +static const uint8_t arygon_error_incomplete_command[] = "FF0C0000\x0d\x0a"; +static const uint8_t arygon_error_unknown_mode[] = "FF060000\x0d\x0a"; bool arygon_reset_tama (nfc_device * pnd); void arygon_firmware (nfc_device * pnd, char * str); @@ -304,12 +304,12 @@ arygon_disconnect (nfc_device * pnd) #define ARYGON_TX_BUFFER_LEN (PN53x_NORMAL_FRAME__DATA_MAX_LEN + PN53x_NORMAL_FRAME__OVERHEAD + 1) #define ARYGON_RX_BUFFER_LEN (PN53x_EXTENDED_FRAME__DATA_MAX_LEN + PN53x_EXTENDED_FRAME__OVERHEAD) bool -arygon_tama_send (nfc_device * pnd, const byte_t * pbtData, const size_t szData, struct timeval *timeout) +arygon_tama_send (nfc_device * pnd, const uint8_t * pbtData, const size_t szData, struct timeval *timeout) { // Before sending anything, we need to discard from any junk bytes uart_flush_input (DRIVER_DATA(pnd)->port); - byte_t abtFrame[ARYGON_TX_BUFFER_LEN] = { DEV_ARYGON_PROTOCOL_TAMA, 0x00, 0x00, 0xff }; // Every packet must start with "0x32 0x00 0x00 0xff" + uint8_t abtFrame[ARYGON_TX_BUFFER_LEN] = { DEV_ARYGON_PROTOCOL_TAMA, 0x00, 0x00, 0xff }; // Every packet must start with "0x32 0x00 0x00 0xff" size_t szFrame = 0; if (szData > PN53x_NORMAL_FRAME__DATA_MAX_LEN) { @@ -331,7 +331,7 @@ arygon_tama_send (nfc_device * pnd, const byte_t * pbtData, const size_t szData, return false; } - byte_t abtRxBuf[6]; + uint8_t abtRxBuf[6]; res = uart_receive (DRIVER_DATA (pnd)->port, abtRxBuf, sizeof (abtRxBuf), 0, timeout); if (res != 0) { log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Unable to read ACK"); @@ -357,7 +357,7 @@ int arygon_abort (nfc_device *pnd) { // Send a valid TAMA packet to wakup the PN53x (we will not have an answer, according to Arygon manual) - byte_t dummy[] = { 0x32, 0x00, 0x00, 0xff, 0x09, 0xf7, 0xd4, 0x00, 0x00, 0x6c, 0x69, 0x62, 0x6e, 0x66, 0x63, 0xbe, 0x00 }; + uint8_t dummy[] = { 0x32, 0x00, 0x00, 0xff, 0x09, 0xf7, 0xd4, 0x00, 0x00, 0x6c, 0x69, 0x62, 0x6e, 0x66, 0x63, 0xbe, 0x00 }; uart_send (DRIVER_DATA (pnd)->port, dummy, sizeof (dummy), NULL); @@ -366,9 +366,9 @@ arygon_abort (nfc_device *pnd) } int -arygon_tama_receive (nfc_device * pnd, byte_t * pbtData, const size_t szDataLen, struct timeval *timeout) +arygon_tama_receive (nfc_device * pnd, uint8_t * pbtData, const size_t szDataLen, struct timeval *timeout) { - byte_t abtRxBuf[5]; + uint8_t abtRxBuf[5]; size_t len; void * abort_p = NULL; @@ -393,7 +393,7 @@ arygon_tama_receive (nfc_device * pnd, byte_t * pbtData, const size_t szDataLen, return -1; } - const byte_t pn53x_preamble[3] = { 0x00, 0x00, 0xff }; + const uint8_t pn53x_preamble[3] = { 0x00, 0x00, 0xff }; if (0 != (memcmp (abtRxBuf, pn53x_preamble, 3))) { log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Frame preamble+start code mismatch"); pnd->iLastError = ECOMIO; @@ -462,7 +462,7 @@ arygon_tama_receive (nfc_device * pnd, byte_t * pbtData, const size_t szDataLen, return -1; } - byte_t btDCS = (256 - 0xD5); + uint8_t btDCS = (256 - 0xD5); btDCS -= CHIP_DATA (pnd)->ui8LastCommand + 1; for (size_t szPos = 0; szPos < len; szPos++) { btDCS -= pbtData[szPos]; @@ -486,8 +486,8 @@ arygon_tama_receive (nfc_device * pnd, byte_t * pbtData, const size_t szDataLen, void arygon_firmware (nfc_device * pnd, char * str) { - const byte_t arygon_firmware_version_cmd[] = { DEV_ARYGON_PROTOCOL_ARYGON_ASCII, 'a', 'v' }; - byte_t abtRx[16]; + const uint8_t arygon_firmware_version_cmd[] = { DEV_ARYGON_PROTOCOL_ARYGON_ASCII, 'a', 'v' }; + uint8_t abtRx[16]; size_t szRx = sizeof(abtRx); @@ -503,7 +503,7 @@ arygon_firmware (nfc_device * pnd, char * str) } if ( 0 == memcmp (abtRx, arygon_error_none, 6)) { - byte_t * p = abtRx + 6; + uint8_t * p = abtRx + 6; unsigned int szData; sscanf ((const char*)p, "%02x%s", &szData, p); memcpy (str, p, szData); @@ -514,8 +514,8 @@ arygon_firmware (nfc_device * pnd, char * str) bool arygon_reset_tama (nfc_device * pnd) { - const byte_t arygon_reset_tama_cmd[] = { DEV_ARYGON_PROTOCOL_ARYGON_ASCII, 'a', 'r' }; - byte_t abtRx[10]; // Attempted response is 10 bytes long + const uint8_t arygon_reset_tama_cmd[] = { DEV_ARYGON_PROTOCOL_ARYGON_ASCII, 'a', 'r' }; + uint8_t abtRx[10]; // Attempted response is 10 bytes long size_t szRx = sizeof(abtRx); int res; diff --git a/libnfc/drivers/arygon.h b/libnfc/drivers/arygon.h index 482a97b..f5014b3 100644 --- a/libnfc/drivers/arygon.h +++ b/libnfc/drivers/arygon.h @@ -35,8 +35,8 @@ bool arygon_probe (nfc_connstring connstrings[], size_t connstrings_len, size nfc_device *arygon_connect (const nfc_connstring connstring); void arygon_disconnect (nfc_device * pnd); -bool arygon_tama_send (nfc_device * pnd, const byte_t * pbtData, const size_t szData, struct timeval *timeout); -int arygon_tama_receive (nfc_device * pnd, byte_t * pbtData, const size_t szDat, struct timeval *timeouta); +bool arygon_tama_send (nfc_device * pnd, const uint8_t * pbtData, const size_t szData, struct timeval *timeout); +int arygon_tama_receive (nfc_device * pnd, uint8_t * pbtData, const size_t szDat, struct timeval *timeouta); extern const struct nfc_driver_t arygon_driver; diff --git a/libnfc/drivers/pn532_uart.c b/libnfc/drivers/pn532_uart.c index 45ec262..47b7027 100644 --- a/libnfc/drivers/pn532_uart.c +++ b/libnfc/drivers/pn532_uart.c @@ -281,7 +281,7 @@ int pn532_uart_wakeup (nfc_device * pnd) { /* High Speed Unit (HSU) wake up consist to send 0x55 and wait a "long" delay for PN532 being wakeup. */ - const byte_t pn532_wakeup_preamble[] = { 0x55, 0x55, 0x00, 0x00, 0x00 }; + const uint8_t pn532_wakeup_preamble[] = { 0x55, 0x55, 0x00, 0x00, 0x00 }; int res = uart_send (DRIVER_DATA(pnd)->port, pn532_wakeup_preamble, sizeof (pn532_wakeup_preamble), NULL); CHIP_DATA(pnd)->power_mode = NORMAL; // PN532 should now be awake return res; @@ -289,7 +289,7 @@ pn532_uart_wakeup (nfc_device * pnd) #define PN532_BUFFER_LEN (PN53x_EXTENDED_FRAME__DATA_MAX_LEN + PN53x_EXTENDED_FRAME__OVERHEAD) bool -pn532_uart_send (nfc_device * pnd, const byte_t * pbtData, const size_t szData, struct timeval *timeout) +pn532_uart_send (nfc_device * pnd, const uint8_t * pbtData, const size_t szData, struct timeval *timeout) { // Before sending anything, we need to discard from any junk bytes uart_flush_input (DRIVER_DATA(pnd)->port); @@ -320,7 +320,7 @@ pn532_uart_send (nfc_device * pnd, const byte_t * pbtData, const size_t szData, break; }; - byte_t abtFrame[PN532_BUFFER_LEN] = { 0x00, 0x00, 0xff }; // Every packet must start with "00 00 ff" + uint8_t abtFrame[PN532_BUFFER_LEN] = { 0x00, 0x00, 0xff }; // Every packet must start with "00 00 ff" size_t szFrame = 0; if (!pn53x_build_frame (abtFrame, &szFrame, pbtData, szData)) { @@ -335,7 +335,7 @@ pn532_uart_send (nfc_device * pnd, const byte_t * pbtData, const size_t szData, return false; } - byte_t abtRxBuf[6]; + uint8_t abtRxBuf[6]; res = uart_receive (DRIVER_DATA(pnd)->port, abtRxBuf, 6, 0, timeout); if (res != 0) { log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Unable to read ACK"); @@ -352,9 +352,9 @@ pn532_uart_send (nfc_device * pnd, const byte_t * pbtData, const size_t szData, } int -pn532_uart_receive (nfc_device * pnd, byte_t * pbtData, const size_t szDataLen, struct timeval *timeout) +pn532_uart_receive (nfc_device * pnd, uint8_t * pbtData, const size_t szDataLen, struct timeval *timeout) { - byte_t abtRxBuf[5]; + uint8_t abtRxBuf[5]; size_t len; void * abort_p = NULL; @@ -376,7 +376,7 @@ pn532_uart_receive (nfc_device * pnd, byte_t * pbtData, const size_t szDataLen, return -1; } - const byte_t pn53x_preamble[3] = { 0x00, 0x00, 0xff }; + const uint8_t pn53x_preamble[3] = { 0x00, 0x00, 0xff }; if (0 != (memcmp (abtRxBuf, pn53x_preamble, 3))) { log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Frame preamble+start code mismatch"); pnd->iLastError = ECOMIO; @@ -454,7 +454,7 @@ pn532_uart_receive (nfc_device * pnd, byte_t * pbtData, const size_t szDataLen, return -1; } - byte_t btDCS = (256 - 0xD5); + uint8_t btDCS = (256 - 0xD5); btDCS -= CHIP_DATA (pnd)->ui8LastCommand + 1; for (size_t szPos = 0; szPos < len; szPos++) { btDCS -= pbtData[szPos]; diff --git a/libnfc/drivers/pn532_uart.h b/libnfc/drivers/pn532_uart.h index 20e0ca6..6f89541 100644 --- a/libnfc/drivers/pn532_uart.h +++ b/libnfc/drivers/pn532_uart.h @@ -33,8 +33,8 @@ bool pn532_uart_probe (nfc_connstring connstrings[], size_t connstrings_len, nfc_device *pn532_uart_connect (const nfc_connstring connstring); void pn532_uart_disconnect (nfc_device * pnd); -bool pn532_uart_send (nfc_device * pnd, const byte_t * pbtData, const size_t szData, struct timeval *timeout); -int pn532_uart_receive (nfc_device * pnd, byte_t * pbtData, const size_t szData, struct timeval *timeout); +bool pn532_uart_send (nfc_device * pnd, const uint8_t * pbtData, const size_t szData, struct timeval *timeout); +int pn532_uart_receive (nfc_device * pnd, uint8_t * pbtData, const size_t szData, struct timeval *timeout); extern const struct nfc_driver_t pn532_uart_driver; diff --git a/libnfc/drivers/pn53x_usb.c b/libnfc/drivers/pn53x_usb.c index 41e92ca..c76bee9 100644 --- a/libnfc/drivers/pn53x_usb.c +++ b/libnfc/drivers/pn53x_usb.c @@ -121,7 +121,7 @@ bool pn53x_usb_get_usb_device_name (struct usb_device *dev, usb_dev_handle *udev bool pn53x_usb_init (nfc_device *pnd); int -pn53x_usb_bulk_read (struct pn53x_usb_data *data, byte_t abtRx[], const size_t szRx, struct timeval *timeout) +pn53x_usb_bulk_read (struct pn53x_usb_data *data, uint8_t abtRx[], const size_t szRx, struct timeval *timeout) { int timeout_ms = USB_INFINITE_TIMEOUT; if (timeout) { @@ -143,7 +143,7 @@ pn53x_usb_bulk_read (struct pn53x_usb_data *data, byte_t abtRx[], const size_t s } int -pn53x_usb_bulk_write (struct pn53x_usb_data *data, byte_t abtTx[], const size_t szTx, struct timeval *timeout) +pn53x_usb_bulk_write (struct pn53x_usb_data *data, uint8_t abtTx[], const size_t szTx, struct timeval *timeout) { LOG_HEX ("TX", abtTx, szTx); int timeout_ms = USB_INFINITE_TIMEOUT; @@ -519,9 +519,9 @@ pn53x_usb_disconnect (nfc_device * pnd) #define PN53X_USB_BUFFER_LEN (PN53x_EXTENDED_FRAME__DATA_MAX_LEN + PN53x_EXTENDED_FRAME__OVERHEAD) bool -pn53x_usb_send (nfc_device * pnd, const byte_t * pbtData, const size_t szData, struct timeval *timeout) +pn53x_usb_send (nfc_device * pnd, const uint8_t * pbtData, const size_t szData, struct timeval *timeout) { - byte_t abtFrame[PN53X_USB_BUFFER_LEN] = { 0x00, 0x00, 0xff }; // Every packet must start with "00 00 ff" + uint8_t abtFrame[PN53X_USB_BUFFER_LEN] = { 0x00, 0x00, 0xff }; // Every packet must start with "00 00 ff" size_t szFrame = 0; pn53x_build_frame (abtFrame, &szFrame, pbtData, szData); @@ -533,7 +533,7 @@ pn53x_usb_send (nfc_device * pnd, const byte_t * pbtData, const size_t szData, s return false; } - byte_t abtRxBuf[PN53X_USB_BUFFER_LEN]; + uint8_t abtRxBuf[PN53X_USB_BUFFER_LEN]; res = pn53x_usb_bulk_read (DRIVER_DATA (pnd), abtRxBuf, sizeof (abtRxBuf), timeout); if (res < 0) { pnd->iLastError = ECOMIO; @@ -552,7 +552,7 @@ pn53x_usb_send (nfc_device * pnd, const byte_t * pbtData, const size_t szData, s // pn53x_usb_receive()) will be able to retreive the correct response // packet. // FIXME Sony reader is also affected by this bug but NACK is not supported - int res = pn53x_usb_bulk_write (DRIVER_DATA (pnd), (byte_t *)pn53x_nack_frame, sizeof(pn53x_nack_frame), timeout); + int res = pn53x_usb_bulk_write (DRIVER_DATA (pnd), (uint8_t *)pn53x_nack_frame, sizeof(pn53x_nack_frame), timeout); if (res < 0) { pnd->iLastError = ECOMIO; // try to interrupt current device state @@ -565,12 +565,12 @@ pn53x_usb_send (nfc_device * pnd, const byte_t * pbtData, const size_t szData, s } int -pn53x_usb_receive (nfc_device * pnd, byte_t * pbtData, const size_t szDataLen, struct timeval *timeout) +pn53x_usb_receive (nfc_device * pnd, uint8_t * pbtData, const size_t szDataLen, struct timeval *timeout) { size_t len; off_t offset = 0; - byte_t abtRxBuf[PN53X_USB_BUFFER_LEN]; + uint8_t abtRxBuf[PN53X_USB_BUFFER_LEN]; int res; /* @@ -627,7 +627,7 @@ read: return -1; } - const byte_t pn53x_preamble[3] = { 0x00, 0x00, 0xff }; + const uint8_t pn53x_preamble[3] = { 0x00, 0x00, 0xff }; if (0 != (memcmp (abtRxBuf, pn53x_preamble, 3))) { log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Frame preamble+start code mismatch"); pnd->iLastError = ECOMIO; @@ -691,7 +691,7 @@ read: memcpy (pbtData, abtRxBuf + offset, len); offset += len; - byte_t btDCS = (256 - 0xD5); + uint8_t btDCS = (256 - 0xD5); btDCS -= CHIP_DATA (pnd)->ui8LastCommand + 1; for (size_t szPos = 0; szPos < len; szPos++) { btDCS -= pbtData[szPos]; @@ -717,7 +717,7 @@ read: int pn53x_usb_ack (nfc_device * pnd) { - return pn53x_usb_bulk_write (DRIVER_DATA (pnd), (byte_t *) pn53x_ack_frame, sizeof (pn53x_ack_frame), NULL); + return pn53x_usb_bulk_write (DRIVER_DATA (pnd), (uint8_t *) pn53x_ack_frame, sizeof (pn53x_ack_frame), NULL); } bool @@ -725,13 +725,13 @@ pn53x_usb_init (nfc_device *pnd) { // Sometimes PN53x USB doesn't reply ACK one the first frame, so we need to send a dummy one... //pn53x_check_communication (pnd); // Sony RC-S360 doesn't support this command for now so let's use a get_firmware_version instead: - const byte_t abtCmd[] = { GetFirmwareVersion }; + const uint8_t abtCmd[] = { GetFirmwareVersion }; pn53x_transceive (pnd, abtCmd, sizeof (abtCmd), NULL, NULL, NULL); // ...and we don't care about error pnd->iLastError = 0; if (SONY_RCS360 == DRIVER_DATA (pnd)->model) { log_put (LOG_CATEGORY, NFC_PRIORITY_TRACE, "SONY RC-S360 initialization."); - const byte_t abtCmd2[] = { 0x18, 0x01 }; + const uint8_t abtCmd2[] = { 0x18, 0x01 }; pn53x_transceive (pnd, abtCmd2, sizeof (abtCmd2), NULL, NULL, NULL); pn53x_usb_ack (pnd); } diff --git a/libnfc/drivers/pn53x_usb.h b/libnfc/drivers/pn53x_usb.h index 9d8b637..83661b8 100644 --- a/libnfc/drivers/pn53x_usb.h +++ b/libnfc/drivers/pn53x_usb.h @@ -31,8 +31,8 @@ bool pn53x_usb_probe (nfc_connstring connstrings[], size_t connstrings_len, size_t * pszDeviceFound); nfc_device *pn53x_usb_connect (const nfc_connstring connstring); -bool pn53x_usb_send (nfc_device * pnd, const byte_t * pbtData, const size_t szData, struct timeval *timeout); -int pn53x_usb_receive (nfc_device * pnd, byte_t * pbtData, const size_t szData, struct timeval *timeout); +bool pn53x_usb_send (nfc_device * pnd, const uint8_t * pbtData, const size_t szData, struct timeval *timeout); +int pn53x_usb_receive (nfc_device * pnd, uint8_t * pbtData, const size_t szData, struct timeval *timeout); void pn53x_usb_disconnect (nfc_device * pnd); extern const struct nfc_driver_t pn53x_usb_driver; diff --git a/libnfc/iso14443-subr.c b/libnfc/iso14443-subr.c index 97afbd1..42e291e 100644 --- a/libnfc/iso14443-subr.c +++ b/libnfc/iso14443-subr.c @@ -32,30 +32,30 @@ #include void -iso14443a_crc (byte_t * pbtData, size_t szLen, byte_t * pbtCrc) +iso14443a_crc (uint8_t * pbtData, size_t szLen, uint8_t * pbtCrc) { - byte_t bt; + uint8_t bt; uint32_t wCrc = 0x6363; do { bt = *pbtData++; - bt = (bt ^ (byte_t) (wCrc & 0x00FF)); + bt = (bt ^ (uint8_t) (wCrc & 0x00FF)); bt = (bt ^ (bt << 4)); wCrc = (wCrc >> 8) ^ ((uint32_t) bt << 8) ^ ((uint32_t) bt << 3) ^ ((uint32_t) bt >> 4); } while (--szLen); - *pbtCrc++ = (byte_t) (wCrc & 0xFF); - *pbtCrc = (byte_t) ((wCrc >> 8) & 0xFF); + *pbtCrc++ = (uint8_t) (wCrc & 0xFF); + *pbtCrc = (uint8_t) ((wCrc >> 8) & 0xFF); } void -iso14443a_crc_append (byte_t * pbtData, size_t szLen) +iso14443a_crc_append (uint8_t * pbtData, size_t szLen) { iso14443a_crc (pbtData, szLen, pbtData + szLen); } -byte_t * -iso14443a_locate_historical_bytes (byte_t * pbtAts, size_t szAts, size_t * pszTk) +uint8_t * +iso14443a_locate_historical_bytes (uint8_t * pbtAts, size_t szAts, size_t * pszTk) { if (szAts) { size_t offset = 1; @@ -82,7 +82,7 @@ iso14443a_locate_historical_bytes (byte_t * pbtAts, size_t szAts, size_t * pszTk * @see ISO/IEC 14443-3 (6.4.4 UID contents and cascade levels) */ void -iso14443_cascade_uid (const byte_t abtUID[], const size_t szUID, byte_t * pbtCascadedUID, size_t * pszCascadedUID) +iso14443_cascade_uid (const uint8_t abtUID[], const size_t szUID, uint8_t * pbtCascadedUID, size_t * pszCascadedUID) { switch (szUID) { case 7: diff --git a/libnfc/mirror-subr.c b/libnfc/mirror-subr.c index 7b5953b..d85ef88 100644 --- a/libnfc/mirror-subr.c +++ b/libnfc/mirror-subr.c @@ -30,7 +30,7 @@ #include "mirror-subr.h" -static const byte_t ByteMirror[256] = { +static const uint8_t ByteMirror[256] = { 0x00, 0x80, 0x40, 0xc0, 0x20, 0xa0, 0x60, 0xe0, 0x10, 0x90, 0x50, 0xd0, 0x30, 0xb0, 0x70, 0xf0, 0x08, 0x88, 0x48, 0xc8, 0x28, 0xa8, 0x68, 0xe8, 0x18, 0x98, 0x58, 0xd8, 0x38, 0xb8, 0x78, 0xf8, 0x04, 0x84, 0x44, 0xc4, 0x24, 0xa4, 0x64, @@ -53,14 +53,14 @@ static const byte_t ByteMirror[256] = { 0xef, 0x1f, 0x9f, 0x5f, 0xdf, 0x3f, 0xbf, 0x7f, 0xff }; -byte_t -mirror (byte_t bt) +uint8_t +mirror (uint8_t bt) { return ByteMirror[bt]; } void -mirror_bytes (byte_t * pbts, size_t szLen) +mirror_bytes (uint8_t * pbts, size_t szLen) { size_t szByteNr; @@ -73,13 +73,13 @@ mirror_bytes (byte_t * pbts, size_t szLen) uint32_t mirror32 (uint32_t ui32Bits) { - mirror_bytes ((byte_t *) & ui32Bits, 4); + mirror_bytes ((uint8_t *) & ui32Bits, 4); return ui32Bits; } uint64_t mirror64 (uint64_t ui64Bits) { - mirror_bytes ((byte_t *) & ui64Bits, 8); + mirror_bytes ((uint8_t *) & ui64Bits, 8); return ui64Bits; } diff --git a/libnfc/mirror-subr.h b/libnfc/mirror-subr.h index c795f7e..0560053 100644 --- a/libnfc/mirror-subr.h +++ b/libnfc/mirror-subr.h @@ -29,9 +29,9 @@ # include -byte_t mirror (byte_t bt); +uint8_t mirror (uint8_t bt); uint32_t mirror32 (uint32_t ui32Bits); uint64_t mirror64 (uint64_t ui64Bits); -void mirror_byte_ts (byte_t * pbts, size_t szLen); +void mirror_uint8_ts (uint8_t * pbts, size_t szLen); #endif // _LIBNFC_MIRROR_SUBR_H_ diff --git a/libnfc/nfc-emulation.c b/libnfc/nfc-emulation.c index d9b622c..52e9cc3 100644 --- a/libnfc/nfc-emulation.c +++ b/libnfc/nfc-emulation.c @@ -30,9 +30,9 @@ int nfc_emulate_target (nfc_device* pnd, struct nfc_emulator *emulator) { - byte_t abtRx[ISO7816_SHORT_R_APDU_MAX_LEN]; + uint8_t abtRx[ISO7816_SHORT_R_APDU_MAX_LEN]; size_t szRx = sizeof(abtRx); - byte_t abtTx[ISO7816_SHORT_C_APDU_MAX_LEN]; + uint8_t abtTx[ISO7816_SHORT_C_APDU_MAX_LEN]; int res = 0; if (!nfc_target_init (pnd, emulator->target, abtRx, &szRx)) { diff --git a/libnfc/nfc-internal.c b/libnfc/nfc-internal.c index bd74b30..6f84d84 100644 --- a/libnfc/nfc-internal.c +++ b/libnfc/nfc-internal.c @@ -25,36 +25,36 @@ #include void -prepare_initiator_data (const nfc_modulation nm, byte_t **ppbtInitiatorData, size_t * pszInitiatorData) +prepare_initiator_data (const nfc_modulation nm, uint8_t **ppbtInitiatorData, size_t * pszInitiatorData) { switch (nm.nmt) { case NMT_ISO14443B: { // Application Family Identifier (AFI) must equals 0x00 in order to wakeup all ISO14443-B PICCs (see ISO/IEC 14443-3) - *ppbtInitiatorData = (byte_t *) "\x00"; + *ppbtInitiatorData = (uint8_t *) "\x00"; *pszInitiatorData = 1; } break; case NMT_ISO14443BI: { // APGEN - *ppbtInitiatorData = (byte_t *) "\x01\x0b\x3f\x80"; + *ppbtInitiatorData = (uint8_t *) "\x01\x0b\x3f\x80"; *pszInitiatorData = 4; } break; case NMT_ISO14443B2SR: { // Get_UID - *ppbtInitiatorData = (byte_t *) "\x0b"; + *ppbtInitiatorData = (uint8_t *) "\x0b"; *pszInitiatorData = 1; } break; case NMT_ISO14443B2CT: { // SELECT-ALL - *ppbtInitiatorData = (byte_t *) "\x9F\xFF\xFF"; + *ppbtInitiatorData = (uint8_t *) "\x9F\xFF\xFF"; *pszInitiatorData = 3; } break; case NMT_FELICA: { // polling payload must be present (see ISO/IEC 18092 11.2.2.5) - *ppbtInitiatorData = (byte_t *) "\x00\xff\xff\x01\x00"; + *ppbtInitiatorData = (uint8_t *) "\x00\xff\xff\x01\x00"; *pszInitiatorData = 5; } break; diff --git a/libnfc/nfc-internal.h b/libnfc/nfc-internal.h index 1617286..e3f16b0 100644 --- a/libnfc/nfc-internal.h +++ b/libnfc/nfc-internal.h @@ -132,20 +132,20 @@ struct nfc_driver_t { const char *(*strerror) (const nfc_device * pnd); bool (*initiator_init) (nfc_device * pnd); - bool (*initiator_select_passive_target) (nfc_device * pnd, const nfc_modulation nm, const byte_t * pbtInitData, const size_t szInitData, nfc_target * pnt); + bool (*initiator_select_passive_target) (nfc_device * pnd, const nfc_modulation nm, const uint8_t * pbtInitData, const size_t szInitData, nfc_target * pnt); bool (*initiator_poll_target) (nfc_device * pnd, const nfc_modulation * pnmModulations, const size_t szModulations, const uint8_t uiPollNr, const uint8_t btPeriod, nfc_target * pnt); bool (*initiator_select_dep_target) (nfc_device * pnd, const nfc_dep_mode ndm, const nfc_baud_rate nbr, const nfc_dep_info * pndiInitiator, nfc_target * pnt); bool (*initiator_deselect_target) (nfc_device * pnd); - bool (*initiator_transceive_bytes) (nfc_device * pnd, const byte_t * pbtTx, const size_t szTx, byte_t * pbtRx, size_t * pszRx, struct timeval *timeout); - bool (*initiator_transceive_bits) (nfc_device * pnd, const byte_t * pbtTx, const size_t szTxBits, const byte_t * pbtTxPar, byte_t * pbtRx, size_t * pszRxBits, byte_t * pbtRxPar); - bool (*initiator_transceive_bytes_timed) (nfc_device * pnd, const byte_t * pbtTx, const size_t szTx, byte_t * pbtRx, size_t * pszRx, uint32_t * cycles); - bool (*initiator_transceive_bits_timed) (nfc_device * pnd, const byte_t * pbtTx, const size_t szTxBits, const byte_t * pbtTxPar, byte_t * pbtRx, size_t * pszRxBits, byte_t * pbtRxPar, uint32_t * cycles); + bool (*initiator_transceive_bytes) (nfc_device * pnd, const uint8_t * pbtTx, const size_t szTx, uint8_t * pbtRx, size_t * pszRx, struct timeval *timeout); + bool (*initiator_transceive_bits) (nfc_device * pnd, const uint8_t * pbtTx, const size_t szTxBits, const uint8_t * pbtTxPar, uint8_t * pbtRx, size_t * pszRxBits, uint8_t * pbtRxPar); + bool (*initiator_transceive_bytes_timed) (nfc_device * pnd, const uint8_t * pbtTx, const size_t szTx, uint8_t * pbtRx, size_t * pszRx, uint32_t * cycles); + bool (*initiator_transceive_bits_timed) (nfc_device * pnd, const uint8_t * pbtTx, const size_t szTxBits, const uint8_t * pbtTxPar, uint8_t * pbtRx, size_t * pszRxBits, uint8_t * pbtRxPar, uint32_t * cycles); - bool (*target_init) (nfc_device * pnd, nfc_target * pnt, byte_t * pbtRx, size_t * pszRx); - bool (*target_send_bytes) (nfc_device * pnd, const byte_t * pbtTx, const size_t szTx, struct timeval *timeout); - bool (*target_receive_bytes) (nfc_device * pnd, byte_t * pbtRx, size_t * pszRx, struct timeval *timeout); - bool (*target_send_bits) (nfc_device * pnd, const byte_t * pbtTx, const size_t szTxBits, const byte_t * pbtTxPar); - bool (*target_receive_bits) (nfc_device * pnd, byte_t * pbtRx, size_t * pszRxBits, byte_t * pbtRxPar); + bool (*target_init) (nfc_device * pnd, nfc_target * pnt, uint8_t * pbtRx, size_t * pszRx); + bool (*target_send_bytes) (nfc_device * pnd, const uint8_t * pbtTx, const size_t szTx, struct timeval *timeout); + bool (*target_receive_bytes) (nfc_device * pnd, uint8_t * pbtRx, size_t * pszRx, struct timeval *timeout); + bool (*target_send_bits) (nfc_device * pnd, const uint8_t * pbtTx, const size_t szTxBits, const uint8_t * pbtTxPar); + bool (*target_receive_bits) (nfc_device * pnd, uint8_t * pbtRx, size_t * pszRxBits, uint8_t * pbtRxPar); bool (*configure) (nfc_device * pnd, const nfc_device_option ndo, const bool bEnable); @@ -156,8 +156,8 @@ struct nfc_driver_t { nfc_device *nfc_device_new (void); void nfc_device_free (nfc_device *nfc_device); -void iso14443_cascade_uid (const byte_t abtUID[], const size_t szUID, byte_t * pbtCascadedUID, size_t * pszCascadedUID); +void iso14443_cascade_uid (const uint8_t abtUID[], const size_t szUID, uint8_t * pbtCascadedUID, size_t * pszCascadedUID); -void prepare_initiator_data (const nfc_modulation nm, byte_t **ppbtInitiatorData, size_t * pszInitiatorData); +void prepare_initiator_data (const nfc_modulation nm, uint8_t **ppbtInitiatorData, size_t * pszInitiatorData); #endif // __NFC_INTERNAL_H__ diff --git a/libnfc/nfc.c b/libnfc/nfc.c index aeea3da..95b4db8 100644 --- a/libnfc/nfc.c +++ b/libnfc/nfc.c @@ -306,10 +306,10 @@ nfc_initiator_init (nfc_device * pnd) bool nfc_initiator_select_passive_target (nfc_device * pnd, const nfc_modulation nm, - const byte_t * pbtInitData, const size_t szInitData, + const uint8_t * pbtInitData, const size_t szInitData, nfc_target * pnt) { - byte_t abtInit[MAX(12, szInitData)]; + uint8_t abtInit[MAX(12, szInitData)]; size_t szInit; switch (nm.nmt) { @@ -350,7 +350,7 @@ nfc_initiator_list_passive_targets (nfc_device * pnd, { nfc_target nt; size_t szTargetFound = 0; - byte_t *pbtInitData = NULL; + uint8_t *pbtInitData = NULL; size_t szInitDataLen = 0; pnd->iLastError = 0; @@ -479,7 +479,7 @@ nfc_initiator_deselect_target (nfc_device * pnd) * @warning The configuration option \a NDO_HANDLE_PARITY must be set to \c true (the default value). */ bool -nfc_initiator_transceive_bytes (nfc_device * pnd, const byte_t * pbtTx, const size_t szTx, byte_t * pbtRx, +nfc_initiator_transceive_bytes (nfc_device * pnd, const uint8_t * pbtTx, const size_t szTx, uint8_t * pbtRx, size_t * pszRx, struct timeval *timeout) { HAL (initiator_transceive_bytes, pnd, pbtTx, szTx, pbtRx, pszRx, timeout) @@ -521,8 +521,8 @@ nfc_initiator_transceive_bytes (nfc_device * pnd, const byte_t * pbtTx, const si * CRC bytes. Using this feature you are able to simulate these frames. */ bool -nfc_initiator_transceive_bits (nfc_device * pnd, const byte_t * pbtTx, const size_t szTxBits, const byte_t * pbtTxPar, - byte_t * pbtRx, size_t * pszRxBits, byte_t * pbtRxPar) +nfc_initiator_transceive_bits (nfc_device * pnd, const uint8_t * pbtTx, const size_t szTxBits, const uint8_t * pbtTxPar, + uint8_t * pbtRx, size_t * pszRxBits, uint8_t * pbtRxPar) { HAL (initiator_transceive_bits, pnd, pbtTx, szTxBits, pbtTxPar, pbtRx, pszRxBits, pbtRxPar); } @@ -548,7 +548,7 @@ nfc_initiator_transceive_bits (nfc_device * pnd, const byte_t * pbtTx, const siz * @warning The configuration option \a NDO_HANDLE_PARITY must be set to \c true (the default value). */ bool -nfc_initiator_transceive_bytes_timed (nfc_device * pnd, const byte_t * pbtTx, const size_t szTx, byte_t * pbtRx, +nfc_initiator_transceive_bytes_timed (nfc_device * pnd, const uint8_t * pbtTx, const size_t szTx, uint8_t * pbtRx, size_t * pszRx, uint32_t * cycles) { HAL (initiator_transceive_bytes_timed, pnd, pbtTx, szTx, pbtRx, pszRx, cycles) @@ -576,8 +576,8 @@ nfc_initiator_transceive_bytes_timed (nfc_device * pnd, const byte_t * pbtTx, co * @warning The configuration option \a NDO_HANDLE_PARITY must be set to \c true (the default value). */ bool -nfc_initiator_transceive_bits_timed (nfc_device * pnd, const byte_t * pbtTx, const size_t szTxBits, const byte_t * pbtTxPar, - byte_t * pbtRx, size_t * pszRxBits, byte_t * pbtRxPar, uint32_t * cycles) +nfc_initiator_transceive_bits_timed (nfc_device * pnd, const uint8_t * pbtTx, const size_t szTxBits, const uint8_t * pbtTxPar, + uint8_t * pbtRx, size_t * pszRxBits, uint8_t * pbtRxPar, uint32_t * cycles) { HAL (initiator_transceive_bits_timed, pnd, pbtTx, szTxBits, pbtTxPar, pbtRx, pszRxBits, pbtRxPar, cycles); } @@ -613,7 +613,7 @@ nfc_initiator_transceive_bits_timed (nfc_device * pnd, const byte_t * pbtTx, con * receive functions can be used. */ bool -nfc_target_init (nfc_device * pnd, nfc_target * pnt, byte_t * pbtRx, size_t * pszRx) +nfc_target_init (nfc_device * pnd, nfc_target * pnt, uint8_t * pbtRx, size_t * pszRx) { // Disallow invalid frame if (!nfc_configure (pnd, NDO_ACCEPT_INVALID_FRAMES, false)) @@ -691,7 +691,7 @@ nfc_abort_command (nfc_device * pnd) * If timeout is a null pointer, the function blocks indefinitely (until an error is raised or function is completed). */ bool -nfc_target_send_bytes (nfc_device * pnd, const byte_t * pbtTx, const size_t szTx, struct timeval *timeout) +nfc_target_send_bytes (nfc_device * pnd, const uint8_t * pbtTx, const size_t szTx, struct timeval *timeout) { HAL (target_send_bytes, pnd, pbtTx, szTx, timeout); } @@ -711,7 +711,7 @@ nfc_target_send_bytes (nfc_device * pnd, const byte_t * pbtTx, const size_t szTx * If timeout is a null pointer, the function blocks indefinitely (until an error is raised or function is completed). */ bool -nfc_target_receive_bytes (nfc_device * pnd, byte_t * pbtRx, size_t * pszRx, struct timeval *timeout) +nfc_target_receive_bytes (nfc_device * pnd, uint8_t * pbtRx, size_t * pszRx, struct timeval *timeout) { HAL (target_receive_bytes, pnd, pbtRx, pszRx, timeout); } @@ -724,7 +724,7 @@ nfc_target_receive_bytes (nfc_device * pnd, byte_t * pbtRx, size_t * pszRx, stru * using the specified NFC device (configured as \e target). */ bool -nfc_target_send_bits (nfc_device * pnd, const byte_t * pbtTx, const size_t szTxBits, const byte_t * pbtTxPar) +nfc_target_send_bits (nfc_device * pnd, const uint8_t * pbtTx, const size_t szTxBits, const uint8_t * pbtTxPar) { HAL (target_send_bits, pnd, pbtTx, szTxBits, pbtTxPar); } @@ -741,7 +741,7 @@ nfc_target_send_bits (nfc_device * pnd, const byte_t * pbtTx, const size_t szTxB * frames. */ bool -nfc_target_receive_bits (nfc_device * pnd, byte_t * pbtRx, size_t * pszRxBits, byte_t * pbtRxPar) +nfc_target_receive_bits (nfc_device * pnd, uint8_t * pbtRx, size_t * pszRxBits, uint8_t * pbtRxPar) { HAL (target_receive_bits, pnd, pbtRx, pszRxBits, pbtRxPar); } diff --git a/test/test_dep.c b/test/test_dep.c index 0c84678..3c7312a 100644 --- a/test/test_dep.c +++ b/test/test_dep.c @@ -80,21 +80,21 @@ target_thread (void *arg) }, }; - byte_t abtRx[1024]; + uint8_t abtRx[1024]; size_t szRx = sizeof (abtRx); bool res = nfc_target_init (device, &nt, abtRx, &szRx); // cut_assert_true (res, cut_message ("Can't initialize NFC device as target")); - byte_t abtAtrRes[] = "\x11\xd4\x00\x01\xfe\x12\x34\x56\x78\x90\x12\x00\x00\x00\x00\x00\x00"; + uint8_t abtAtrRes[] = "\x11\xd4\x00\x01\xfe\x12\x34\x56\x78\x90\x12\x00\x00\x00\x00\x00\x00"; // cut_assert_equal_memory (abtAtrRes, sizeof (abtAtrRes) - 1, abtRx, szRx, cut_message ("Invalid received ATR_RES")); res = nfc_target_receive_bytes (device, abtRx, &szRx); // cut_assert_true (res, cut_message ("Can't receive bytes from initiator")); - byte_t abtAttRx[] = "Hello DEP target!"; + uint8_t abtAttRx[] = "Hello DEP target!"; // cut_assert_equal_memory (abtAttRx, sizeof (abtAttRx), abtRx, szRx, cut_message ("Invalid received data")); - byte_t abtTx[] = "Hello DEP initiator!"; + uint8_t abtTx[] = "Hello DEP initiator!"; res = nfc_target_send_bytes (device, abtTx, sizeof(abtTx)); // cut_assert_true (res, cut_message ("Can't send bytes to initiator")); #endif @@ -133,13 +133,13 @@ initiator_thread (void *arg) // cut_assert_equal_int (NDM_PASSIVE, nt.nti.ndi.ndm, cut_message ("Invalid target DEP mode")); // cut_assert_equal_memory ("\x12\x34\x56\x78", 4, nt.nti.ndi.abtGB, nt.nti.ndi.szGB, cut_message ("Invalid target general bytes")); - byte_t abtTx[] = "Hello DEP target!"; - byte_t abtRx[1024]; + uint8_t abtTx[] = "Hello DEP target!"; + uint8_t abtRx[1024]; size_t szRx = sizeof (abtRx); res = nfc_initiator_transceive_bytes (device, abtTx, sizeof (abtTx), abtRx, &szRx); // cut_assert_true (res, cut_message ("Can't transceive bytes to target")); - byte_t abtAttRx[] = "Hello DEP initiator!"; + uint8_t abtAttRx[] = "Hello DEP initiator!"; // cut_assert_equal_memory (abtAttRx, sizeof (abtAttRx), abtRx, szRx, cut_message ("Invalid received data")); res = nfc_initiator_deselect_target (device); diff --git a/utils/mifare.c b/utils/mifare.c index 32679c1..a074d3a 100644 --- a/utils/mifare.c +++ b/utils/mifare.c @@ -50,10 +50,10 @@ bool nfc_initiator_mifare_cmd (nfc_device * pnd, const mifare_cmd mc, const uint8_t ui8Block, mifare_param * pmp) { - byte_t abtRx[265]; + uint8_t abtRx[265]; size_t szRx = sizeof(abtRx); size_t szParamLen; - byte_t abtCmd[265]; + uint8_t abtCmd[265]; bool bEasyFraming; abtCmd[0] = mc; // The MIFARE Classic command @@ -92,7 +92,7 @@ nfc_initiator_mifare_cmd (nfc_device * pnd, const mifare_cmd mc, const uint8_t u // When available, copy the parameter bytes if (szParamLen) - memcpy (abtCmd + 2, (byte_t *) pmp, szParamLen); + memcpy (abtCmd + 2, (uint8_t *) pmp, szParamLen); bEasyFraming = pnd->bEasyFraming; if (!nfc_configure (pnd, NDO_EASY_FRAMING, true)) { diff --git a/utils/mifare.h b/utils/mifare.h index 2b44f9e..726d669 100644 --- a/utils/mifare.h +++ b/utils/mifare.h @@ -38,7 +38,7 @@ # include -// Compiler directive, set struct alignment to 1 byte_t for compatibility +// Compiler directive, set struct alignment to 1 uint8_t for compatibility # pragma pack(1) typedef enum { @@ -54,16 +54,16 @@ typedef enum { // MIFARE command params struct mifare_param_auth { - byte_t abtKey[6]; - byte_t abtUid[4]; + uint8_t abtKey[6]; + uint8_t abtUid[4]; }; struct mifare_param_data { - byte_t abtData[16]; + uint8_t abtData[16]; }; struct mifare_param_value { - byte_t abtValue[4]; + uint8_t abtValue[4]; }; typedef union { @@ -77,26 +77,26 @@ typedef union { bool nfc_initiator_mifare_cmd (nfc_device * pnd, const mifare_cmd mc, const uint8_t ui8Block, mifare_param * pmp); -// Compiler directive, set struct alignment to 1 byte_t for compatibility +// Compiler directive, set struct alignment to 1 uint8_t for compatibility # pragma pack(1) // MIFARE Classic typedef struct { - byte_t abtUID[4]; - byte_t btBCC; - byte_t btUnknown; - byte_t abtATQA[2]; - byte_t abtUnknown[8]; + uint8_t abtUID[4]; + uint8_t btBCC; + uint8_t btUnknown; + uint8_t abtATQA[2]; + uint8_t abtUnknown[8]; } mifare_classic_block_manufacturer; typedef struct { - byte_t abtData[16]; + uint8_t abtData[16]; } mifare_classic_block_data; typedef struct { - byte_t abtKeyA[6]; - byte_t abtAccessBits[4]; - byte_t abtKeyB[6]; + uint8_t abtKeyA[6]; + uint8_t abtAccessBits[4]; + uint8_t abtKeyB[6]; } mifare_classic_block_trailer; typedef union { @@ -111,17 +111,17 @@ typedef struct { // MIFARE Ultralight typedef struct { - byte_t sn0[3]; - byte_t btBCC0; - byte_t sn1[4]; - byte_t btBCC1; - byte_t internal; - byte_t lock[2]; - byte_t otp[4]; + uint8_t sn0[3]; + uint8_t btBCC0; + uint8_t sn1[4]; + uint8_t btBCC1; + uint8_t internal; + uint8_t lock[2]; + uint8_t otp[4]; } mifareul_block_manufacturer; typedef struct { - byte_t abtData[16]; + uint8_t abtData[16]; } mifareul_block_data; typedef union { diff --git a/utils/nfc-emulate-forum-tag4.c b/utils/nfc-emulate-forum-tag4.c index 51ea535..b2f4bd1 100644 --- a/utils/nfc-emulate-forum-tag4.c +++ b/utils/nfc-emulate-forum-tag4.c @@ -113,7 +113,7 @@ uint8_t nfcforum_capability_container[] = { #define ISO144434A_RATS 0xE0 int -nfcforum_tag4_io (struct nfc_emulator *emulator, const byte_t *data_in, const size_t data_in_len, byte_t *data_out, const size_t data_out_len) +nfcforum_tag4_io (struct nfc_emulator *emulator, const uint8_t *data_in, const size_t data_in_len, uint8_t *data_out, const size_t data_out_len) { int res = 0; diff --git a/utils/nfc-mfclassic.c b/utils/nfc-mfclassic.c index 440e6f2..a7a3907 100644 --- a/utils/nfc-mfclassic.c +++ b/utils/nfc-mfclassic.c @@ -60,7 +60,7 @@ static mifare_classic_tag mtDump; static bool bUseKeyA; static bool bUseKeyFile; static uint8_t uiBlocks; -static byte_t keys[] = { +static uint8_t keys[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xd3, 0xf7, 0xd3, 0xf7, 0xd3, 0xf7, 0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, @@ -81,18 +81,18 @@ static size_t num_keys = sizeof (keys) / 6; #define MAX_FRAME_LEN 264 -static byte_t abtRx[MAX_FRAME_LEN]; +static uint8_t abtRx[MAX_FRAME_LEN]; static size_t szRxBits; static size_t szRx = sizeof(abtRx); -byte_t abtHalt[4] = { 0x50, 0x00, 0x00, 0x00 }; +uint8_t abtHalt[4] = { 0x50, 0x00, 0x00, 0x00 }; // special unlock command -byte_t abtUnlock1[1] = { 0x40 }; -byte_t abtUnlock2[1] = { 0x43 }; +uint8_t abtUnlock1[1] = { 0x40 }; +uint8_t abtUnlock2[1] = { 0x43 }; static bool -transmit_bits (const byte_t * pbtTx, const size_t szTxBits) +transmit_bits (const uint8_t * pbtTx, const size_t szTxBits) { // Show transmitted command printf ("Sent bits: "); @@ -110,7 +110,7 @@ transmit_bits (const byte_t * pbtTx, const size_t szTxBits) static bool -transmit_bytes (const byte_t * pbtTx, const size_t szTx) +transmit_bytes (const uint8_t * pbtTx, const size_t szTx) { // Show transmitted command printf ("Sent bits: "); @@ -458,7 +458,7 @@ int main (int argc, const char *argv[]) { action_t atAction = ACTION_USAGE; - byte_t *pbtUID; + uint8_t *pbtUID; FILE *pfKeys = NULL; FILE *pfDump = NULL; int unlock= 0; @@ -571,7 +571,7 @@ main (int argc, const char *argv[]) pbtUID = nt.nti.nai.abtUid; if (bUseKeyFile) { - byte_t fileUid[4]; + uint8_t fileUid[4]; memcpy (fileUid, mtKeys.amb[0].mbm.abtUID, 4); // Compare if key dump UID is the same as the current tag UID, at least for the first 4 bytes if (memcmp (nt.nti.nai.abtUid, fileUid, 4) != 0) { diff --git a/utils/nfc-mfsetuid.c b/utils/nfc-mfsetuid.c index 79f398f..63dcd2c 100644 --- a/utils/nfc-mfsetuid.c +++ b/utils/nfc-mfsetuid.c @@ -56,14 +56,14 @@ #define MAX_FRAME_LEN 264 -static byte_t abtRx[MAX_FRAME_LEN]; +static uint8_t abtRx[MAX_FRAME_LEN]; static size_t szRxBits; static size_t szRx = sizeof(abtRx); -static byte_t abtRawUid[12]; -static byte_t abtAtqa[2]; -static byte_t abtSak; -static byte_t abtAts[MAX_FRAME_LEN]; -static byte_t szAts = 0; +static uint8_t abtRawUid[12]; +static uint8_t abtAtqa[2]; +static uint8_t abtSak; +static uint8_t abtAts[MAX_FRAME_LEN]; +static uint8_t szAts = 0; static size_t szCL = 1;//Always start with Cascade Level 1 (CL1) static nfc_device *pnd; @@ -71,24 +71,24 @@ bool quiet_output = false; bool iso_ats_supported = false; // ISO14443A Anti-Collision Commands -byte_t abtReqa[1] = { 0x26 }; -byte_t abtSelectAll[2] = { 0x93, 0x20 }; -byte_t abtSelectTag[9] = { 0x93, 0x70, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; -byte_t abtRats[4] = { 0xe0, 0x50, 0x00, 0x00 }; -byte_t abtHalt[4] = { 0x50, 0x00, 0x00, 0x00 }; +uint8_t abtReqa[1] = { 0x26 }; +uint8_t abtSelectAll[2] = { 0x93, 0x20 }; +uint8_t abtSelectTag[9] = { 0x93, 0x70, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; +uint8_t abtRats[4] = { 0xe0, 0x50, 0x00, 0x00 }; +uint8_t abtHalt[4] = { 0x50, 0x00, 0x00, 0x00 }; #define CASCADE_BIT 0x04 // special unlock command -byte_t abtUnlock1[1] = { 0x40 }; -byte_t abtUnlock2[1] = { 0x43 }; -byte_t abtWipe[1] = { 0x41 }; -byte_t abtWrite[4] = { 0xa0, 0x00, 0x5f, 0xb1 }; -byte_t abtData[18] = { 0x01, 0x23, 0x45, 0x67, 0x00, 0x08, 0x04, 0x00, 0x46, 0x59, 0x25, 0x58, 0x49, 0x10, 0x23, 0x02, 0x23, 0xeb }; -byte_t abtBlank[18] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x07, 0x80, 0x69, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x36, 0xCC }; +uint8_t abtUnlock1[1] = { 0x40 }; +uint8_t abtUnlock2[1] = { 0x43 }; +uint8_t abtWipe[1] = { 0x41 }; +uint8_t abtWrite[4] = { 0xa0, 0x00, 0x5f, 0xb1 }; +uint8_t abtData[18] = { 0x01, 0x23, 0x45, 0x67, 0x00, 0x08, 0x04, 0x00, 0x46, 0x59, 0x25, 0x58, 0x49, 0x10, 0x23, 0x02, 0x23, 0xeb }; +uint8_t abtBlank[18] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x07, 0x80, 0x69, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x36, 0xCC }; static bool -transmit_bits (const byte_t * pbtTx, const size_t szTxBits) +transmit_bits (const uint8_t * pbtTx, const size_t szTxBits) { // Show transmitted command if (!quiet_output) { @@ -110,7 +110,7 @@ transmit_bits (const byte_t * pbtTx, const size_t szTxBits) static bool -transmit_bytes (const byte_t * pbtTx, const size_t szTx) +transmit_bytes (const uint8_t * pbtTx, const size_t szTx) { // Show transmitted command if (!quiet_output) { diff --git a/utils/nfc-read-forum-tag3.c b/utils/nfc-read-forum-tag3.c index c634ea5..7d96966 100644 --- a/utils/nfc-read-forum-tag3.c +++ b/utils/nfc-read-forum-tag3.c @@ -77,7 +77,7 @@ void stop_select (int sig) } void -build_felica_frame(const nfc_felica_info nfi, const byte_t command, const byte_t* payload, const size_t payload_len, byte_t * frame, size_t * frame_len) +build_felica_frame(const nfc_felica_info nfi, const uint8_t command, const uint8_t* payload, const size_t payload_len, uint8_t * frame, size_t * frame_len) { frame[0] = 1 + 1 + 8 + payload_len; *frame_len = frame[0]; @@ -88,9 +88,9 @@ build_felica_frame(const nfc_felica_info nfi, const byte_t command, const byte_t #define CHECK 0x06 int -nfc_forum_tag_type3_check (nfc_device *pnd, const nfc_target nt, const uint16_t block, const uint8_t block_count, byte_t * data, size_t * data_len) +nfc_forum_tag_type3_check (nfc_device *pnd, const nfc_target nt, const uint16_t block, const uint8_t block_count, uint8_t * data, size_t * data_len) { - byte_t payload[1024] = { + uint8_t payload[1024] = { 1, // Services 0x0B, 0x00, // NFC Forum Tag Type 3's Service code block_count, @@ -109,11 +109,11 @@ nfc_forum_tag_type3_check (nfc_device *pnd, const nfc_target nt, const uint16_t } } - byte_t frame[1024]; + uint8_t frame[1024]; size_t frame_len = sizeof(frame); build_felica_frame (nt.nti.nfi, CHECK, payload, payload_len, frame, &frame_len); - byte_t res[1024]; + uint8_t res[1024]; size_t res_len; if (!nfc_initiator_transceive_bytes (pnd, frame, frame_len, res, &res_len, NULL)) { @@ -137,8 +137,8 @@ nfc_forum_tag_type3_check (nfc_device *pnd, const nfc_target nt, const uint16_t // NFCID2 does not match return -1; } - const byte_t status_flag1 = res[10]; - const byte_t status_flag2 = res[11]; + const uint8_t status_flag1 = res[10]; + const uint8_t status_flag2 = res[11]; if ((status_flag1) || (status_flag2)) { // Felica card's error fprintf (stderr, "Status bytes: %02x, %02x\n", status_flag1, status_flag2); @@ -218,7 +218,7 @@ main(int argc, char *argv[]) int error = EXIT_SUCCESS; // Polling payload (SENSF_REQ) must be present (see NFC Digital Protol) - const byte_t *pbtSensfReq = (byte_t*)"\x00\xff\xff\x01\x00"; + const uint8_t *pbtSensfReq = (uint8_t*)"\x00\xff\xff\x01\x00"; if (!nfc_initiator_select_passive_target(pnd, nm, pbtSensfReq, 5, &nt)) { nfc_perror (pnd, "nfc_initiator_select_passive_target"); error = EXIT_FAILURE; @@ -226,10 +226,10 @@ main(int argc, char *argv[]) } // Check if System Code equals 0x12fc - const byte_t abtNfcForumSysCode[] = { 0x12, 0xfc }; + const uint8_t abtNfcForumSysCode[] = { 0x12, 0xfc }; if (0 != memcmp (nt.nti.nfi.abtSysCode, abtNfcForumSysCode, 2)) { // Retry with special polling - const byte_t *pbtSensfReqNfcForum = (byte_t*)"\x00\x12\xfc\x01\x00"; + const uint8_t *pbtSensfReqNfcForum = (uint8_t*)"\x00\x12\xfc\x01\x00"; if (!nfc_initiator_select_passive_target(pnd, nm, pbtSensfReqNfcForum, 5, &nt)) { nfc_perror (pnd, "nfc_initiator_select_passive_target"); error = EXIT_FAILURE; @@ -251,7 +251,7 @@ main(int argc, char *argv[]) goto error; } - byte_t data[1024]; + uint8_t data[1024]; size_t data_len = sizeof(data); int len; diff --git a/utils/nfc-relay-picc.c b/utils/nfc-relay-picc.c index 82b26a0..4f0b9d9 100644 --- a/utils/nfc-relay-picc.c +++ b/utils/nfc-relay-picc.c @@ -57,9 +57,9 @@ #define MAX_FRAME_LEN 264 #define MAX_DEVICE_COUNT 2 -static byte_t abtCapdu[MAX_FRAME_LEN]; +static uint8_t abtCapdu[MAX_FRAME_LEN]; static size_t szCapduLen; -static byte_t abtRapdu[MAX_FRAME_LEN]; +static uint8_t abtRapdu[MAX_FRAME_LEN]; static size_t szRapduLen; static nfc_device *pndInitiator; static nfc_device *pndTarget; @@ -92,7 +92,7 @@ print_usage (char *argv[]) printf ("\t-n N\tAdds a waiting time of N seconds (integer) in the relay to mimic long distance.\n"); } -bool print_hex_fd4 (const byte_t * pbtData, const size_t szBytes, const char * pchPrefix) +bool print_hex_fd4 (const uint8_t * pbtData, const size_t szBytes, const char * pchPrefix) { size_t szPos; if (szBytes > MAX_FRAME_LEN) { @@ -114,7 +114,7 @@ bool print_hex_fd4 (const byte_t * pbtData, const size_t szBytes, const char * p return EXIT_SUCCESS; } -bool scan_hex_fd3 (byte_t *pbtData, size_t *pszBytes, const char * pchPrefix) +bool scan_hex_fd3 (uint8_t *pbtData, size_t *pszBytes, const char * pchPrefix) { size_t szPos; unsigned int uiBytes; @@ -326,11 +326,11 @@ main (int argc, char *argv[]) // PC/SC pseudo-ATR = 3B 80 80 01 01 if there is no historical bytes // Creates ATS and copy max 48 bytes of Tk: - byte_t * pbtTk; + uint8_t * pbtTk; size_t szTk; pbtTk = iso14443a_locate_historical_bytes (ntEmulatedTarget.nti.nai.abtAts, ntEmulatedTarget.nti.nai.szAtsLen, &szTk); szTk = (szTk > 48) ? 48 : szTk; - byte_t pbtTkt[48]; + uint8_t pbtTkt[48]; memcpy(pbtTkt, pbtTk, szTk); ntEmulatedTarget.nti.nai.abtAts[0] = 0x75; ntEmulatedTarget.nti.nai.abtAts[1] = 0x33; diff --git a/utils/nfc-utils.c b/utils/nfc-utils.c index 9b87844..51c4a1f 100644 --- a/utils/nfc-utils.c +++ b/utils/nfc-utils.c @@ -33,7 +33,7 @@ #include "nfc-utils.h" -static const byte_t OddParity[256] = { +static const uint8_t OddParity[256] = { 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0, @@ -52,14 +52,14 @@ static const byte_t OddParity[256] = { 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1 }; -byte_t -oddparity (const byte_t bt) +uint8_t +oddparity (const uint8_t bt) { return OddParity[bt]; } void -oddparity_bytes_ts (const byte_t * pbtData, const size_t szLen, byte_t * pbtPar) +oddparity_bytes_ts (const uint8_t * pbtData, const size_t szLen, uint8_t * pbtPar) { size_t szByteNr; // Calculate the parity bits for the command @@ -69,7 +69,7 @@ oddparity_bytes_ts (const byte_t * pbtData, const size_t szLen, byte_t * pbtPar) } void -print_hex (const byte_t * pbtData, const size_t szBytes) +print_hex (const uint8_t * pbtData, const size_t szBytes) { size_t szPos; @@ -80,7 +80,7 @@ print_hex (const byte_t * pbtData, const size_t szBytes) } void -print_hex_bits (const byte_t * pbtData, const size_t szBits) +print_hex_bits (const uint8_t * pbtData, const size_t szBits) { uint8_t uRemainder; size_t szPos; @@ -102,7 +102,7 @@ print_hex_bits (const byte_t * pbtData, const size_t szBits) } void -print_hex_par (const byte_t * pbtData, const size_t szBits, const byte_t * pbtDataPar) +print_hex_par (const uint8_t * pbtData, const size_t szBits, const uint8_t * pbtDataPar) { uint8_t uRemainder; size_t szPos; @@ -202,7 +202,7 @@ print_nfc_iso14443a_info (const nfc_iso14443a_info nai, bool verbose) size_t offset = 1; if (nai.abtAts[0] & 0x10) { // TA(1) present - byte_t TA = nai.abtAts[offset]; + uint8_t TA = nai.abtAts[offset]; offset++; printf ("* Bit Rate Capability:\n"); if (TA == 0) { @@ -234,7 +234,7 @@ print_nfc_iso14443a_info (const nfc_iso14443a_info nai, bool verbose) } } if (nai.abtAts[0] & 0x20) { // TB(1) present - byte_t TB= nai.abtAts[offset]; + uint8_t TB= nai.abtAts[offset]; offset++; printf ("* Frame Waiting Time: %.4g ms\n",256.0*16.0*(1<<((TB & 0xf0) >> 4))/13560.0); if ((TB & 0x0f) == 0) { @@ -244,7 +244,7 @@ print_nfc_iso14443a_info (const nfc_iso14443a_info nai, bool verbose) } } if (nai.abtAts[0] & 0x40) { // TC(1) present - byte_t TC = nai.abtAts[offset]; + uint8_t TC = nai.abtAts[offset]; offset++; if (TC & 0x1) { printf("* Node ADdress supported\n"); @@ -260,20 +260,20 @@ print_nfc_iso14443a_info (const nfc_iso14443a_info nai, bool verbose) if (nai.szAtsLen > offset) { printf ("* Historical bytes Tk: " ); print_hex (nai.abtAts + offset, (nai.szAtsLen - offset)); - byte_t CIB = nai.abtAts[offset]; + uint8_t CIB = nai.abtAts[offset]; offset++; if (CIB != 0x00 && CIB != 0x10 && (CIB & 0xf0) != 0x80) { printf(" * Proprietary format\n"); if (CIB == 0xc1) { printf(" * Tag byte: Mifare or virtual cards of various types\n"); - byte_t L = nai.abtAts[offset]; + uint8_t L = nai.abtAts[offset]; offset++; if (L != (nai.szAtsLen - offset)) { printf(" * Warning: Type Identification Coding length (%i)", L); printf(" not matching Tk length (%zi)\n", (nai.szAtsLen - offset)); } if ((nai.szAtsLen - offset - 2) > 0) { // Omit 2 CRC bytes - byte_t CTC = nai.abtAts[offset]; + uint8_t CTC = nai.abtAts[offset]; offset++; printf(" * Chip Type: "); switch (CTC & 0xf0) { @@ -316,7 +316,7 @@ print_nfc_iso14443a_info (const nfc_iso14443a_info nai, bool verbose) } } if ((nai.szAtsLen - offset) > 0) { // Omit 2 CRC bytes - byte_t CVC = nai.abtAts[offset]; + uint8_t CVC = nai.abtAts[offset]; offset++; printf(" * Chip Status: "); switch (CVC & 0xf0) { @@ -350,7 +350,7 @@ print_nfc_iso14443a_info (const nfc_iso14443a_info nai, bool verbose) } } if ((nai.szAtsLen - offset) > 0) { // Omit 2 CRC bytes - byte_t VCS = nai.abtAts[offset]; + uint8_t VCS = nai.abtAts[offset]; offset++; printf(" * Specifics (Virtual Card Selection):\n"); if ((VCS & 0x09) == 0x00) { diff --git a/utils/nfc-utils.h b/utils/nfc-utils.h index f0ae97f..86c5d08 100644 --- a/utils/nfc-utils.h +++ b/utils/nfc-utils.h @@ -79,12 +79,12 @@ # define ERR(...) warnx ("ERROR: " __VA_ARGS__ ) #endif -byte_t oddparity (const byte_t bt); -void oddparity_byte_ts (const byte_t * pbtData, const size_t szLen, byte_t * pbtPar); +uint8_t oddparity (const uint8_t bt); +void oddparity_uint8_ts (const uint8_t * pbtData, const size_t szLen, uint8_t * pbtPar); -void print_hex (const byte_t * pbtData, const size_t szLen); -void print_hex_bits (const byte_t * pbtData, const size_t szBits); -void print_hex_par (const byte_t * pbtData, const size_t szBits, const byte_t * pbtDataPar); +void print_hex (const uint8_t * pbtData, const size_t szLen); +void print_hex_bits (const uint8_t * pbtData, const size_t szBits); +void print_hex_par (const uint8_t * pbtData, const size_t szBits, const uint8_t * pbtDataPar); void print_nfc_iso14443a_info (const nfc_iso14443a_info nai, bool verbose); void print_nfc_iso14443b_info (const nfc_iso14443b_info nbi, bool verbose); From 9e67423cff9be4acff3f71f2add06bc1545ea4ea Mon Sep 17 00:00:00 2001 From: Audrey Diacre Date: Thu, 24 Nov 2011 11:12:01 +0000 Subject: [PATCH 006/113] Fix some warnings in test. --- test/test_access_storm.c | 2 +- test/test_dep.c | 4 ++-- test/test_register_access.c | 2 +- test/test_register_endianness.c | 2 +- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/test/test_access_storm.c b/test/test_access_storm.c index ead2190..0fdc253 100644 --- a/test/test_access_storm.c +++ b/test/test_access_storm.c @@ -32,7 +32,7 @@ test_access_storm (void) nfc_device *device; nfc_target ant[MAX_TARGET_COUNT]; - device = nfc_connect (&(connstrings[i])); + device = nfc_connect (connstrings[i]); cut_assert_not_null (device, cut_message ("nfc_connect")); res = nfc_initiator_init(device); diff --git a/test/test_dep.c b/test/test_dep.c index 3c7312a..a065ff6 100644 --- a/test/test_dep.c +++ b/test/test_dep.c @@ -33,8 +33,8 @@ cut_setup (void) cut_omit ("At least two NFC devices must be plugged-in to run this test"); } - devices[TARGET] = nfc_connect (&connstrings[TARGET]); - devices[INITIATOR] = nfc_connect (&connstrings[INITIATOR]); + devices[TARGET] = nfc_connect (connstrings[TARGET]); + devices[INITIATOR] = nfc_connect (connstrings[INITIATOR]); signal (SIGINT, abort_test_by_keypress); } diff --git a/test/test_register_access.c b/test/test_register_access.c index 9363f9f..8bcab33 100644 --- a/test/test_register_access.c +++ b/test/test_register_access.c @@ -19,7 +19,7 @@ test_register_endianness (void) nfc_device *device; - device = nfc_connect (&(connstrings[0])); + device = nfc_connect (connstrings[0]); cut_assert_not_null (device, cut_message ("nfc_connect")); uint8_t value; diff --git a/test/test_register_endianness.c b/test/test_register_endianness.c index 2f7e6e5..479837b 100644 --- a/test/test_register_endianness.c +++ b/test/test_register_endianness.c @@ -20,7 +20,7 @@ test_register_endianness (void) nfc_device *device; - device = nfc_connect (&(connstrings[0])); + device = nfc_connect (connstrings[0]); cut_assert_not_null (device, cut_message ("nfc_connect")); uint8_t value; From dad36039361018a542e7011d84a9d7b98119cffa Mon Sep 17 00:00:00 2001 From: Audrey Diacre Date: Thu, 24 Nov 2011 16:45:27 +0000 Subject: [PATCH 007/113] Remove whitespace after star symbol for pointers --- include/nfc/nfc.h | 54 +++++++++++++++++++++++------------------------ 1 file changed, 27 insertions(+), 27 deletions(-) diff --git a/include/nfc/nfc.h b/include/nfc/nfc.h index 3babb1b..55fe65b 100644 --- a/include/nfc/nfc.h +++ b/include/nfc/nfc.h @@ -65,43 +65,43 @@ extern "C" { /* NFC Device/Hardware manipulation */ NFC_EXPORT bool nfc_get_default_device (nfc_connstring *connstring); NFC_EXPORT nfc_device *nfc_connect (const nfc_connstring connstring); - NFC_EXPORT void nfc_disconnect (nfc_device * pnd); - NFC_EXPORT bool nfc_abort_command (nfc_device * pnd); - NFC_EXPORT void nfc_list_devices (nfc_connstring connstrings[], size_t connstrings_len, size_t * pszDeviceFound); - NFC_EXPORT bool nfc_configure (nfc_device * pnd, const nfc_device_option ndo, const bool bEnable); - NFC_EXPORT bool nfc_idle (nfc_device * pnd); + NFC_EXPORT void nfc_disconnect (nfc_device *pnd); + NFC_EXPORT bool nfc_abort_command (nfc_device *pnd); + NFC_EXPORT void nfc_list_devices (nfc_connstring connstrings[], size_t connstrings_len, size_t *pszDeviceFound); + NFC_EXPORT bool nfc_configure (nfc_device *pnd, const nfc_device_option ndo, const bool bEnable); + NFC_EXPORT bool nfc_idle (nfc_device *pnd); /* NFC initiator: act as "reader" */ - NFC_EXPORT bool nfc_initiator_init (nfc_device * pnd); - NFC_EXPORT bool nfc_initiator_select_passive_target (nfc_device * pnd, const nfc_modulation nm, const uint8_t * pbtInitData, const size_t szInitData, nfc_target * pnt); - NFC_EXPORT bool nfc_initiator_list_passive_targets (nfc_device * pnd, const nfc_modulation nm, nfc_target ant[], const size_t szTargets, size_t * pszTargetFound); - NFC_EXPORT bool nfc_initiator_poll_target (nfc_device * pnd, const nfc_modulation * pnmTargetTypes, const size_t szTargetTypes, const uint8_t uiPollNr, const uint8_t uiPeriod, nfc_target * pnt); - NFC_EXPORT bool nfc_initiator_select_dep_target (nfc_device * pnd, const nfc_dep_mode ndm, const nfc_baud_rate nbr, const nfc_dep_info * pndiInitiator, nfc_target * pnt); - NFC_EXPORT bool nfc_initiator_deselect_target (nfc_device * pnd); - NFC_EXPORT bool nfc_initiator_transceive_bytes (nfc_device * pnd, const uint8_t * pbtTx, const size_t szTx, uint8_t * pbtRx, size_t * pszRx, struct timeval *timeout); - NFC_EXPORT bool nfc_initiator_transceive_bits (nfc_device * pnd, const uint8_t * pbtTx, const size_t szTxBits, const uint8_t * pbtTxPar, uint8_t * pbtRx, size_t * pszRxBits, uint8_t * pbtRxPar); - NFC_EXPORT bool nfc_initiator_transceive_bytes_timed (nfc_device * pnd, const uint8_t * pbtTx, const size_t szTx, uint8_t * pbtRx, size_t * pszRx, uint32_t * cycles); - NFC_EXPORT bool nfc_initiator_transceive_bits_timed (nfc_device * pnd, const uint8_t * pbtTx, const size_t szTxBits, const uint8_t * pbtTxPar, uint8_t * pbtRx, size_t * pszRxBits, uint8_t * pbtRxPar, uint32_t * cycles); + NFC_EXPORT bool nfc_initiator_init (nfc_device *pnd); + NFC_EXPORT bool nfc_initiator_select_passive_target (nfc_device *pnd, const nfc_modulation nm, const uint8_t *pbtInitData, const size_t szInitData, nfc_target *pnt); + NFC_EXPORT bool nfc_initiator_list_passive_targets (nfc_device *pnd, const nfc_modulation nm, nfc_target ant[], const size_t szTargets, size_t *pszTargetFound); + NFC_EXPORT bool nfc_initiator_poll_target (nfc_device *pnd, const nfc_modulation *pnmTargetTypes, const size_t szTargetTypes, const uint8_t uiPollNr, const uint8_t uiPeriod, nfc_target *pnt); + NFC_EXPORT bool nfc_initiator_select_dep_target (nfc_device *pnd, const nfc_dep_mode ndm, const nfc_baud_rate nbr, const nfc_dep_info *pndiInitiator, nfc_target *pnt); + NFC_EXPORT bool nfc_initiator_deselect_target (nfc_device *pnd); + NFC_EXPORT bool nfc_initiator_transceive_bytes (nfc_device *pnd, const uint8_t *pbtTx, const size_t szTx, uint8_t *pbtRx, size_t *pszRx, struct timeval *timeout); + NFC_EXPORT bool nfc_initiator_transceive_bits (nfc_device *pnd, const uint8_t *pbtTx, const size_t szTxBits, const uint8_t *pbtTxPar, uint8_t *pbtRx, size_t *pszRxBits, uint8_t *pbtRxPar); + NFC_EXPORT bool nfc_initiator_transceive_bytes_timed (nfc_device *pnd, const uint8_t *pbtTx, const size_t szTx, uint8_t *pbtRx, size_t *pszRx, uint32_t *cycles); + NFC_EXPORT bool nfc_initiator_transceive_bits_timed (nfc_device *pnd, const uint8_t *pbtTx, const size_t szTxBits, const uint8_t *pbtTxPar, uint8_t *pbtRx, size_t *pszRxBits, uint8_t *pbtRxPar, uint32_t *cycles); /* NFC target: act as tag (i.e. MIFARE Classic) or NFC target device. */ - NFC_EXPORT bool nfc_target_init (nfc_device * pnd, nfc_target * pnt, uint8_t * pbtRx, size_t * pszRx); - NFC_EXPORT bool nfc_target_send_bytes (nfc_device * pnd, const uint8_t * pbtTx, const size_t szTx, struct timeval *timout); - NFC_EXPORT bool nfc_target_receive_bytes (nfc_device * pnd, uint8_t * pbtRx, size_t * pszRx, struct timeval *timout); - NFC_EXPORT bool nfc_target_send_bits (nfc_device * pnd, const uint8_t * pbtTx, const size_t szTxBits, const uint8_t * pbtTxPar); - NFC_EXPORT bool nfc_target_receive_bits (nfc_device * pnd, uint8_t * pbtRx, size_t * pszRxBits, uint8_t * pbtRxPar); + NFC_EXPORT bool nfc_target_init (nfc_device *pnd, nfc_target *pnt, uint8_t *pbtRx, size_t *pszRx); + NFC_EXPORT bool nfc_target_send_bytes (nfc_device *pnd, const uint8_t *pbtTx, const size_t szTx, struct timeval *timeout); + NFC_EXPORT bool nfc_target_receive_bytes (nfc_device *pnd, uint8_t *pbtRx, size_t *pszRx, struct timeval *timeout); + NFC_EXPORT bool nfc_target_send_bits (nfc_device *pnd, const uint8_t *pbtTx, const size_t szTxBits, const uint8_t *pbtTxPar); + NFC_EXPORT bool nfc_target_receive_bits (nfc_device *pnd, uint8_t *pbtRx, size_t *pszRxBits, uint8_t *pbtRxPar); /* Error reporting */ - NFC_EXPORT const char *nfc_strerror (const nfc_device * pnd); - NFC_EXPORT int nfc_strerror_r (const nfc_device * pnd, char *pcStrErrBuf, size_t szBufLen); - NFC_EXPORT void nfc_perror (const nfc_device * pnd, const char *pcString); + NFC_EXPORT const char *nfc_strerror (const nfc_device *pnd); + NFC_EXPORT int nfc_strerror_r (const nfc_device *pnd, char *pcStrErrBuf, size_t szBufLen); + NFC_EXPORT void nfc_perror (const nfc_device *pnd, const char *pcString); /* Special data accessors */ - NFC_EXPORT const char *nfc_device_name (nfc_device * pnd); + NFC_EXPORT const char *nfc_device_name (nfc_device *pnd); /* Misc. functions */ - NFC_EXPORT void iso14443a_crc (uint8_t * pbtData, size_t szLen, uint8_t * pbtCrc); - NFC_EXPORT void iso14443a_crc_append (uint8_t * pbtData, size_t szLen); - NFC_EXPORT uint8_t * iso14443a_locate_historical_bytes (uint8_t * pbtAts, size_t szAts, size_t * pszTk); + NFC_EXPORT void iso14443a_crc (uint8_t *pbtData, size_t szLen, uint8_t *pbtCrc); + NFC_EXPORT void iso14443a_crc_append (uint8_t *pbtData, size_t szLen); + NFC_EXPORT uint8_t *iso14443a_locate_historical_bytes (uint8_t *pbtAts, size_t szAts, size_t *pszTk); NFC_EXPORT const char *nfc_version (void); /* PN53x specific errors */ From 5c7454a2f790e4323570504cbf1161d056a46e0c Mon Sep 17 00:00:00 2001 From: Audrey Diacre Date: Fri, 25 Nov 2011 11:37:30 +0000 Subject: [PATCH 008/113] Timeout is now integer. --- examples/nfc-anticol.c | 2 +- examples/nfc-dep-initiator.c | 2 +- examples/nfc-dep-target.c | 4 +- examples/nfc-emulate-tag.c | 4 +- examples/pn53x-diagnose.c | 6 +-- examples/pn53x-sam.c | 4 +- examples/pn53x-tamashell.c | 2 +- include/nfc/nfc.h | 6 +-- libnfc/buses/uart.h | 4 +- libnfc/buses/uart_posix.c | 13 +++--- libnfc/buses/uart_win32.c | 10 ++--- libnfc/chips/pn53x.c | 87 +++++++++++++++++------------------- libnfc/chips/pn53x.h | 16 +++---- libnfc/drivers/acr122.c | 4 +- libnfc/drivers/acr122.h | 4 +- libnfc/drivers/arygon.c | 18 +++----- libnfc/drivers/arygon.h | 4 +- libnfc/drivers/pn532_uart.c | 13 +++--- libnfc/drivers/pn532_uart.h | 4 +- libnfc/drivers/pn53x_usb.c | 31 ++++++------- libnfc/drivers/pn53x_usb.h | 4 +- libnfc/nfc-emulation.c | 4 +- libnfc/nfc-internal.h | 6 +-- libnfc/nfc.c | 12 ++--- utils/mifare.c | 2 +- utils/nfc-mfclassic.c | 2 +- utils/nfc-mfsetuid.c | 2 +- utils/nfc-read-forum-tag3.c | 2 +- utils/nfc-relay-picc.c | 6 +-- 29 files changed, 133 insertions(+), 145 deletions(-) diff --git a/examples/nfc-anticol.c b/examples/nfc-anticol.c index f6b4ca4..7077949 100644 --- a/examples/nfc-anticol.c +++ b/examples/nfc-anticol.c @@ -105,7 +105,7 @@ transmit_bytes (const uint8_t * pbtTx, const size_t szTx) print_hex (pbtTx, szTx); } // Transmit the command bytes - if (!nfc_initiator_transceive_bytes (pnd, pbtTx, szTx, abtRx, &szRx, NULL)) + if (!nfc_initiator_transceive_bytes (pnd, pbtTx, szTx, abtRx, &szRx, 0)) return false; // Show received answer diff --git a/examples/nfc-dep-initiator.c b/examples/nfc-dep-initiator.c index d3ca65e..ae90495 100644 --- a/examples/nfc-dep-initiator.c +++ b/examples/nfc-dep-initiator.c @@ -93,7 +93,7 @@ main (int argc, const char *argv[]) print_nfc_target (nt, false); printf ("Sending: %s\n", abtTx); - if (!nfc_initiator_transceive_bytes (pnd, abtTx, sizeof(abtTx), abtRx, &szRx, NULL)) { + if (!nfc_initiator_transceive_bytes (pnd, abtTx, sizeof(abtTx), abtRx, &szRx, 0)) { nfc_perror(pnd, "nfc_initiator_transceive_bytes"); goto error; } diff --git a/examples/nfc-dep-target.c b/examples/nfc-dep-target.c index 2195ea8..b7b45a1 100644 --- a/examples/nfc-dep-target.c +++ b/examples/nfc-dep-target.c @@ -125,7 +125,7 @@ main (int argc, const char *argv[]) } printf("Initiator request received. Waiting for data...\n"); - if (!nfc_target_receive_bytes (pnd, abtRx, &szRx, NULL)) { + if (!nfc_target_receive_bytes (pnd, abtRx, &szRx, 0)) { nfc_perror(pnd, "nfc_target_receive_bytes"); goto error; } @@ -133,7 +133,7 @@ main (int argc, const char *argv[]) printf ("Received: %s\n", abtRx); printf ("Sending: %s\n", abtTx); - if (!nfc_target_send_bytes (pnd, abtTx, sizeof(abtTx), NULL)) { + if (!nfc_target_send_bytes (pnd, abtTx, sizeof(abtTx), 0)) { nfc_perror(pnd, "nfc_target_send_bytes"); goto error; } diff --git a/examples/nfc-emulate-tag.c b/examples/nfc-emulate-tag.c index e75b569..e07f645 100644 --- a/examples/nfc-emulate-tag.c +++ b/examples/nfc-emulate-tag.c @@ -148,7 +148,7 @@ nfc_target_emulate_tag(nfc_device* pnd, nfc_target * pnt) while ( loop ) { loop = target_io( pnt, abtRx, szRx, abtTx, &szTx ); if (szTx) { - if (!nfc_target_send_bytes(pnd, abtTx, szTx, NULL)) { + if (!nfc_target_send_bytes(pnd, abtTx, szTx, 0)) { nfc_perror (pnd, "nfc_target_send_bytes"); return false; } @@ -158,7 +158,7 @@ nfc_target_emulate_tag(nfc_device* pnd, nfc_target * pnt) nfc_configure (pnd, NDO_HANDLE_CRC, false); init_mfc_auth = false; } - if (!nfc_target_receive_bytes(pnd, abtRx, &szRx, NULL)) { + if (!nfc_target_receive_bytes(pnd, abtRx, &szRx, 0)) { nfc_perror (pnd, "nfc_target_receive_bytes"); return false; } diff --git a/examples/pn53x-diagnose.c b/examples/pn53x-diagnose.c index 5ff4bc1..979e4b9 100644 --- a/examples/pn53x-diagnose.c +++ b/examples/pn53x-diagnose.c @@ -88,7 +88,7 @@ main (int argc, const char *argv[]) printf ("NFC device [%s] connected.\n", pnd->acName); - result = pn53x_transceive (pnd, pncmd_diagnose_communication_line_test, sizeof (pncmd_diagnose_communication_line_test), abtRx, &szRx, NULL); + result = pn53x_transceive (pnd, pncmd_diagnose_communication_line_test, sizeof (pncmd_diagnose_communication_line_test), abtRx, &szRx, 0); if (result) { // Result of Diagnose ping for RC-S360 doesn't contain status byte so we've to handle both cases result = (memcmp (pncmd_diagnose_communication_line_test + 1, abtRx, sizeof (pncmd_diagnose_communication_line_test) - 1) == 0) || @@ -98,7 +98,7 @@ main (int argc, const char *argv[]) } printf (" Communication line test: %s\n", result ? "OK" : "Failed"); - result = pn53x_transceive (pnd, pncmd_diagnose_rom_test, sizeof (pncmd_diagnose_rom_test), abtRx, &szRx, NULL); + result = pn53x_transceive (pnd, pncmd_diagnose_rom_test, sizeof (pncmd_diagnose_rom_test), abtRx, &szRx, 0); if (result) { result = ((szRx == 1) && (abtRx[0] == 0x00)); } else { @@ -106,7 +106,7 @@ main (int argc, const char *argv[]) } printf (" ROM test: %s\n", result ? "OK" : "Failed"); - result = pn53x_transceive (pnd, pncmd_diagnose_ram_test, sizeof (pncmd_diagnose_ram_test), abtRx, &szRx, NULL); + result = pn53x_transceive (pnd, pncmd_diagnose_ram_test, sizeof (pncmd_diagnose_ram_test), abtRx, &szRx, 0); if (result) { result = ((szRx == 1) && (abtRx[0] == 0x00)); } else { diff --git a/examples/pn53x-sam.c b/examples/pn53x-sam.c index 11a4c29..7552325 100644 --- a/examples/pn53x-sam.c +++ b/examples/pn53x-sam.c @@ -110,7 +110,7 @@ main (int argc, const char *argv[]) // Connect with the SAM // FIXME: Its a private pn53x function - if (!pn53x_SAMConfiguration (pnd, mode, NULL)) { + if (!pn53x_SAMConfiguration (pnd, mode, 0)) { nfc_perror (pnd, "pn53x_SAMConfiguration"); exit (EXIT_FAILURE); } @@ -185,7 +185,7 @@ main (int argc, const char *argv[]) } // Disconnect from the SAM - pn53x_SAMConfiguration (pnd, PSM_NORMAL, NULL); + pn53x_SAMConfiguration (pnd, PSM_NORMAL, 0); // Disconnect from NFC device nfc_disconnect (pnd); diff --git a/examples/pn53x-tamashell.c b/examples/pn53x-tamashell.c index 3621d2c..b92bc6e 100644 --- a/examples/pn53x-tamashell.c +++ b/examples/pn53x-tamashell.c @@ -178,7 +178,7 @@ int main(int argc, const char* argv[]) print_hex((uint8_t*)abtTx,szTx); szRx = sizeof(abtRx); - if (!pn53x_transceive (pnd, abtTx, szTx, abtRx, &szRx, NULL)) { + if (!pn53x_transceive (pnd, abtTx, szTx, abtRx, &szRx, 0)) { free(cmd); nfc_perror (pnd, "Rx"); continue; diff --git a/include/nfc/nfc.h b/include/nfc/nfc.h index 55fe65b..b168d6a 100644 --- a/include/nfc/nfc.h +++ b/include/nfc/nfc.h @@ -78,15 +78,15 @@ extern "C" { NFC_EXPORT bool nfc_initiator_poll_target (nfc_device *pnd, const nfc_modulation *pnmTargetTypes, const size_t szTargetTypes, const uint8_t uiPollNr, const uint8_t uiPeriod, nfc_target *pnt); NFC_EXPORT bool nfc_initiator_select_dep_target (nfc_device *pnd, const nfc_dep_mode ndm, const nfc_baud_rate nbr, const nfc_dep_info *pndiInitiator, nfc_target *pnt); NFC_EXPORT bool nfc_initiator_deselect_target (nfc_device *pnd); - NFC_EXPORT bool nfc_initiator_transceive_bytes (nfc_device *pnd, const uint8_t *pbtTx, const size_t szTx, uint8_t *pbtRx, size_t *pszRx, struct timeval *timeout); + NFC_EXPORT bool nfc_initiator_transceive_bytes (nfc_device *pnd, const uint8_t *pbtTx, const size_t szTx, uint8_t *pbtRx, size_t *pszRx, int timeout); NFC_EXPORT bool nfc_initiator_transceive_bits (nfc_device *pnd, const uint8_t *pbtTx, const size_t szTxBits, const uint8_t *pbtTxPar, uint8_t *pbtRx, size_t *pszRxBits, uint8_t *pbtRxPar); NFC_EXPORT bool nfc_initiator_transceive_bytes_timed (nfc_device *pnd, const uint8_t *pbtTx, const size_t szTx, uint8_t *pbtRx, size_t *pszRx, uint32_t *cycles); NFC_EXPORT bool nfc_initiator_transceive_bits_timed (nfc_device *pnd, const uint8_t *pbtTx, const size_t szTxBits, const uint8_t *pbtTxPar, uint8_t *pbtRx, size_t *pszRxBits, uint8_t *pbtRxPar, uint32_t *cycles); /* NFC target: act as tag (i.e. MIFARE Classic) or NFC target device. */ NFC_EXPORT bool nfc_target_init (nfc_device *pnd, nfc_target *pnt, uint8_t *pbtRx, size_t *pszRx); - NFC_EXPORT bool nfc_target_send_bytes (nfc_device *pnd, const uint8_t *pbtTx, const size_t szTx, struct timeval *timeout); - NFC_EXPORT bool nfc_target_receive_bytes (nfc_device *pnd, uint8_t *pbtRx, size_t *pszRx, struct timeval *timeout); + NFC_EXPORT bool nfc_target_send_bytes (nfc_device *pnd, const uint8_t *pbtTx, const size_t szTx, int timeout); + NFC_EXPORT bool nfc_target_receive_bytes (nfc_device *pnd, uint8_t *pbtRx, size_t *pszRx, int timeout); NFC_EXPORT bool nfc_target_send_bits (nfc_device *pnd, const uint8_t *pbtTx, const size_t szTxBits, const uint8_t *pbtTxPar); NFC_EXPORT bool nfc_target_receive_bits (nfc_device *pnd, uint8_t *pbtRx, size_t *pszRxBits, uint8_t *pbtRxPar); diff --git a/libnfc/buses/uart.h b/libnfc/buses/uart.h index 24b4924..77b5658 100644 --- a/libnfc/buses/uart.h +++ b/libnfc/buses/uart.h @@ -49,8 +49,8 @@ void uart_flush_input (const serial_port sp); void uart_set_speed (serial_port sp, const uint32_t uiPortSpeed); uint32_t uart_get_speed (const serial_port sp); -int uart_receive (serial_port sp, uint8_t * pbtRx, const size_t szRx, void * abort_p, struct timeval *timeout); -int uart_send (serial_port sp, const uint8_t * pbtTx, const size_t szTx, struct timeval *timeout); +int uart_receive (serial_port sp, uint8_t * pbtRx, const size_t szRx, void * abort_p, int timeout); +int uart_send (serial_port sp, const uint8_t * pbtTx, const size_t szTx, int timeout); char **uart_list_ports (void); diff --git a/libnfc/buses/uart_posix.c b/libnfc/buses/uart_posix.c index e74b9b3..cd3d5e4 100644 --- a/libnfc/buses/uart_posix.c +++ b/libnfc/buses/uart_posix.c @@ -249,7 +249,7 @@ uart_close (const serial_port sp) * @return 0 on success, otherwise driver error code */ int -uart_receive (serial_port sp, uint8_t * pbtRx, const size_t szRx, void * abort_p, struct timeval *timeout) +uart_receive (serial_port sp, uint8_t * pbtRx, const size_t szRx, void * abort_p, int timeout) { int iAbortFd = abort_p ? *((int*)abort_p) : 0; int received_bytes_count = 0; @@ -272,12 +272,13 @@ select: * Make a copy so that it will be updated on these systems, */ struct timeval fixed_timeout; - if (timeout) { - fixed_timeout = *timeout; - timeout = &fixed_timeout; + if (timeout > 0) { + fixed_timeout.tv_sec = (timeout / 1000); + fixed_timeout.tv_usec = ((timeout % 1000) * 1000); + timeout = ((fixed_timeout.tv_sec * 1000) + (fixed_timeout.tv_usec / 1000)); } - res = select (MAX(UART_DATA(sp)->fd, iAbortFd) + 1, &rfds, NULL, NULL, timeout); + res = select (MAX(UART_DATA(sp)->fd, iAbortFd) + 1, &rfds, NULL, NULL, &fixed_timeout); if ((res < 0) && (EINTR == errno)) { // The system call was interupted by a signal and a signal handler was @@ -327,7 +328,7 @@ select: * @return 0 on success, otherwise a driver error is returned */ int -uart_send (serial_port sp, const uint8_t * pbtTx, const size_t szTx, struct timeval *timeout) +uart_send (serial_port sp, const uint8_t * pbtTx, const size_t szTx, int timeout) { (void) timeout; LOG_HEX ("TX", pbtTx, szTx); diff --git a/libnfc/buses/uart_win32.c b/libnfc/buses/uart_win32.c index 4a886bc..3e9c951 100644 --- a/libnfc/buses/uart_win32.c +++ b/libnfc/buses/uart_win32.c @@ -139,7 +139,7 @@ uart_get_speed (const serial_port sp) } int -uart_receive (serial_port sp, uint8_t * pbtRx, const size_t szRx, void * abort_p, struct timeval *timeout) +uart_receive (serial_port sp, uint8_t * pbtRx, const size_t szRx, void * abort_p, int timeout) { DWORD dwBytesToGet = (DWORD)szRx; DWORD dwBytesReceived = 0; @@ -147,7 +147,7 @@ uart_receive (serial_port sp, uint8_t * pbtRx, const size_t szRx, void * abort_p BOOL res; // XXX Put this part into uart_win32_timeouts () ? - DWORD timeout_ms = timeout ? ((timeout->tv_sec * 1000) + (timeout->tv_usec / 1000)) : 0; + DWORD timeout_ms = timeout; COMMTIMEOUTS timeouts; timeouts.ReadIntervalTimeout = 0; timeouts.ReadTotalTimeoutMultiplier = 0; @@ -194,16 +194,16 @@ uart_receive (serial_port sp, uint8_t * pbtRx, const size_t szRx, void * abort_p } int -uart_send (serial_port sp, const uint8_t * pbtTx, const size_t szTx, struct timeval *timeout) +uart_send (serial_port sp, const uint8_t * pbtTx, const size_t szTx, int timeout) { DWORD dwTxLen = 0; COMMTIMEOUTS timeouts; timeouts.ReadIntervalTimeout = 0; timeouts.ReadTotalTimeoutMultiplier = 0; - timeouts.ReadTotalTimeoutConstant = timeout ? ((timeout->tv_sec * 1000) + (timeout->tv_usec / 1000)) : 0; + timeouts.ReadTotalTimeoutConstant = timeout; timeouts.WriteTotalTimeoutMultiplier = 0; - timeouts.WriteTotalTimeoutConstant = timeout ? ((timeout->tv_sec * 1000) + (timeout->tv_usec / 1000)) : 0; + timeouts.WriteTotalTimeoutConstant = timeout; if (!SetCommTimeouts (((struct serial_port_windows *) sp)->hPort, &timeouts)) { log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "Unable to apply new timeout settings."); diff --git a/libnfc/chips/pn53x.c b/libnfc/chips/pn53x.c index 4e4b058..3b28fb9 100644 --- a/libnfc/chips/pn53x.c +++ b/libnfc/chips/pn53x.c @@ -100,7 +100,7 @@ pn53x_reset_settings(nfc_device * pnd) } bool -pn53x_transceive (nfc_device * pnd, const uint8_t * pbtTx, const size_t szTx, uint8_t * pbtRx, size_t *pszRx, struct timeval *timeout) +pn53x_transceive (nfc_device * pnd, const uint8_t * pbtTx, const size_t szTx, uint8_t * pbtRx, size_t *pszRx, int timeout) { if (CHIP_DATA (pnd)->wb_trigged) { if (!pn53x_writeback_register (pnd)) { @@ -109,8 +109,8 @@ pn53x_transceive (nfc_device * pnd, const uint8_t * pbtTx, const size_t szTx, ui } PNCMD_TRACE (pbtTx[0]); - if (timeout) - log_put (LOG_CATEGORY, NFC_PRIORITY_TRACE, "Timeout values: %li s, %li us", timeout->tv_sec, timeout->tv_usec); + if (timeout > 0) + log_put (LOG_CATEGORY, NFC_PRIORITY_TRACE, "Timeout values: %d", timeout); uint8_t abtRx[PN53x_EXTENDED_FRAME__DATA_MAX_LEN]; size_t szRx = sizeof(abtRx); @@ -469,7 +469,7 @@ pn53x_ReadRegister (nfc_device * pnd, uint16_t ui16RegisterAddress, uint8_t * ui size_t szRegValue = sizeof (abtRegValue); PNREG_TRACE (ui16RegisterAddress); - if (!pn53x_transceive (pnd, abtCmd, sizeof (abtCmd), abtRegValue, &szRegValue, NULL)) { + if (!pn53x_transceive (pnd, abtCmd, sizeof (abtCmd), abtRegValue, &szRegValue, 0)) { return false; } if (CHIP_DATA(pnd)->type == PN533) { @@ -491,7 +491,7 @@ pn53x_WriteRegister (nfc_device * pnd, const uint16_t ui16RegisterAddress, const { uint8_t abtCmd[] = { WriteRegister, ui16RegisterAddress >> 8, ui16RegisterAddress & 0xff, ui8Value }; PNREG_TRACE (ui16RegisterAddress); - return pn53x_transceive (pnd, abtCmd, sizeof (abtCmd), NULL, NULL, NULL); + return pn53x_transceive (pnd, abtCmd, sizeof (abtCmd), NULL, NULL, 0); } bool @@ -543,7 +543,7 @@ pn53x_writeback_register (nfc_device * pnd) uint8_t abtRes[PN53x_EXTENDED_FRAME__DATA_MAX_LEN]; size_t szRes = sizeof(abtRes); // It transceives the previously constructed ReadRegister command - if (!pn53x_transceive (pnd, abtReadRegisterCmd, BUFFER_SIZE (abtReadRegisterCmd), abtRes, &szRes, NULL)) { + if (!pn53x_transceive (pnd, abtReadRegisterCmd, BUFFER_SIZE (abtReadRegisterCmd), abtRes, &szRes, 0)) { return false; } size_t i = 0; @@ -581,7 +581,7 @@ pn53x_writeback_register (nfc_device * pnd) if (BUFFER_SIZE (abtWriteRegisterCmd) > 1) { // We need to write some registers - if (!pn53x_transceive (pnd, abtWriteRegisterCmd, BUFFER_SIZE (abtWriteRegisterCmd), NULL, NULL, NULL)) { + if (!pn53x_transceive (pnd, abtWriteRegisterCmd, BUFFER_SIZE (abtWriteRegisterCmd), NULL, NULL, 0)) { return false; } } @@ -594,7 +594,7 @@ pn53x_get_firmware_version (nfc_device * pnd, char abtFirmwareText[22]) const uint8_t abtCmd[] = { GetFirmwareVersion }; uint8_t abtFw[4]; size_t szFwLen = sizeof (abtFw); - if (!pn53x_transceive (pnd, abtCmd, sizeof (abtCmd), abtFw, &szFwLen, NULL)) { + if (!pn53x_transceive (pnd, abtCmd, sizeof (abtCmd), abtFw, &szFwLen, 0)) { return false; } // Determine which version of chip it is: PN531 will return only 2 bytes, while others return 4 bytes and have the first to tell the version IC @@ -832,11 +832,7 @@ pn53x_check_communication (nfc_device *pnd) uint8_t abtRx[sizeof(abtExpectedRx)]; size_t szRx = sizeof (abtRx); - struct timeval timeout; - timeout.tv_sec = 1; - timeout.tv_usec = 0; - - if (!pn53x_transceive (pnd, abtCmd, sizeof (abtCmd), abtRx, &szRx, &timeout)) + if (!pn53x_transceive (pnd, abtCmd, sizeof (abtCmd), abtRx, &szRx, 1000)) return false; return ((sizeof(abtExpectedRx) == szRx) && (0 == memcmp (abtRx, abtExpectedRx, sizeof(abtExpectedRx)))); @@ -860,7 +856,7 @@ pn53x_initiator_select_passive_target_ext (nfc_device * pnd, const nfc_modulation nm, const uint8_t * pbtInitData, const size_t szInitData, nfc_target * pnt, - struct timeval* timeout) + int timeout) { uint8_t abtTargetsData[PN53x_EXTENDED_FRAME__DATA_MAX_LEN]; size_t szTargetsData = sizeof(abtTargetsData); @@ -971,7 +967,7 @@ pn53x_initiator_select_passive_target (nfc_device * pnd, const uint8_t * pbtInitData, const size_t szInitData, nfc_target * pnt) { - return pn53x_initiator_select_passive_target_ext (pnd, nm, pbtInitData, szInitData, pnt, NULL); + return pn53x_initiator_select_passive_target_ext (pnd, nm, pbtInitData, szInitData, pnt, 0); } bool @@ -1024,11 +1020,8 @@ pn53x_initiator_poll_target (nfc_device * pnd, size_t szInitiatorData; prepare_initiator_data (pnmModulations[n], &pbtInitiatorData, &szInitiatorData); const int timeout_ms = uiPeriod * 150; - struct timeval timeout; - timeout.tv_sec = timeout_ms / 1000; - timeout.tv_usec = (timeout_ms - (timeout.tv_sec * 1000)) * 1000; - if (!pn53x_initiator_select_passive_target_ext (pnd, pnmModulations[n], pbtInitiatorData, szInitiatorData, pnt, &timeout)) { + if (!pn53x_initiator_select_passive_target_ext (pnd, pnmModulations[n], pbtInitiatorData, szInitiatorData, pnt, timeout_ms)) { if (pnd->iLastError != ECOMTIMEOUT) return false; } else { @@ -1105,7 +1098,7 @@ pn53x_initiator_transceive_bits (nfc_device * pnd, const uint8_t * pbtTx, const // We have to give the amount of bytes + (the command byte 0x42) uint8_t abtRx[PN53x_EXTENDED_FRAME__DATA_MAX_LEN]; size_t szRx = sizeof(abtRx); - if (!pn53x_transceive (pnd, abtCmd, szFrameBytes + 1, abtRx, &szRx, NULL)) + if (!pn53x_transceive (pnd, abtCmd, szFrameBytes + 1, abtRx, &szRx, 0)) return false; // Get the last bit-count that is stored in the received byte @@ -1135,7 +1128,7 @@ pn53x_initiator_transceive_bits (nfc_device * pnd, const uint8_t * pbtTx, const bool pn53x_initiator_transceive_bytes (nfc_device * pnd, const uint8_t * pbtTx, const size_t szTx, uint8_t * pbtRx, - size_t * pszRx, struct timeval *timeout) + size_t * pszRx, int timeout) { size_t szExtraTxLen; uint8_t abtCmd[PN53x_EXTENDED_FRAME__DATA_MAX_LEN]; @@ -1222,7 +1215,7 @@ uint32_t __pn53x_get_timer(nfc_device * pnd, const uint8_t last_cmd_byte) uint8_t abtRes[PN53x_EXTENDED_FRAME__DATA_MAX_LEN]; size_t szRes = sizeof(abtRes); // Let's send the previously constructed ReadRegister command - if (!pn53x_transceive (pnd, abtReadRegisterCmd, BUFFER_SIZE (abtReadRegisterCmd), abtRes, &szRes, NULL)) { + if (!pn53x_transceive (pnd, abtReadRegisterCmd, BUFFER_SIZE (abtReadRegisterCmd), abtRes, &szRes, 0)) { return false; } counter_hi = abtRes[off]; @@ -1312,7 +1305,7 @@ pn53x_initiator_transceive_bits_timed (nfc_device * pnd, const uint8_t * pbtTx, BUFFER_APPEND (abtWriteRegisterCmd, PN53X_REG_CIU_BitFraming & 0xff); BUFFER_APPEND (abtWriteRegisterCmd, SYMBOL_START_SEND | ((szTxBits % 8) & SYMBOL_TX_LAST_BITS)); // Let's send the previously constructed WriteRegister command - if (!pn53x_transceive (pnd, abtWriteRegisterCmd, BUFFER_SIZE (abtWriteRegisterCmd), NULL, NULL, NULL)) { + if (!pn53x_transceive (pnd, abtWriteRegisterCmd, BUFFER_SIZE (abtWriteRegisterCmd), NULL, NULL, 0)) { return false; } @@ -1344,7 +1337,7 @@ pn53x_initiator_transceive_bits_timed (nfc_device * pnd, const uint8_t * pbtTx, uint8_t abtRes[PN53x_EXTENDED_FRAME__DATA_MAX_LEN]; size_t szRes = sizeof(abtRes); // Let's send the previously constructed ReadRegister command - if (!pn53x_transceive (pnd, abtReadRegisterCmd, BUFFER_SIZE (abtReadRegisterCmd), abtRes, &szRes, NULL)) { + if (!pn53x_transceive (pnd, abtReadRegisterCmd, BUFFER_SIZE (abtReadRegisterCmd), abtRes, &szRes, 0)) { return false; } for (i = 0; i < sz; i++) { @@ -1407,7 +1400,7 @@ pn53x_initiator_transceive_bytes_timed (nfc_device * pnd, const uint8_t * pbtTx, BUFFER_APPEND (abtWriteRegisterCmd, PN53X_REG_CIU_BitFraming & 0xff); BUFFER_APPEND (abtWriteRegisterCmd, SYMBOL_START_SEND); // Let's send the previously constructed WriteRegister command - if (!pn53x_transceive (pnd, abtWriteRegisterCmd, BUFFER_SIZE (abtWriteRegisterCmd), NULL, NULL, NULL)) { + if (!pn53x_transceive (pnd, abtWriteRegisterCmd, BUFFER_SIZE (abtWriteRegisterCmd), NULL, NULL, 0)) { return false; } @@ -1439,7 +1432,7 @@ pn53x_initiator_transceive_bytes_timed (nfc_device * pnd, const uint8_t * pbtTx, uint8_t abtRes[PN53x_EXTENDED_FRAME__DATA_MAX_LEN]; size_t szRes = sizeof(abtRes); // Let's send the previously constructed ReadRegister command - if (!pn53x_transceive (pnd, abtReadRegisterCmd, BUFFER_SIZE (abtReadRegisterCmd), abtRes, &szRes, NULL)) { + if (!pn53x_transceive (pnd, abtReadRegisterCmd, BUFFER_SIZE (abtReadRegisterCmd), abtRes, &szRes, 0)) { return false; } for (i = 0; i < sz; i++) { @@ -1703,7 +1696,7 @@ pn53x_target_receive_bits (nfc_device * pnd, uint8_t * pbtRx, size_t * pszRxBits uint8_t abtRx[PN53x_EXTENDED_FRAME__DATA_MAX_LEN]; size_t szRx = sizeof (abtRx); // Try to gather a received frame from the reader - if (!pn53x_transceive (pnd, abtCmd, sizeof (abtCmd), abtRx, &szRx, NULL)) + if (!pn53x_transceive (pnd, abtCmd, sizeof (abtCmd), abtRx, &szRx, 0)) return false; // Get the last bit-count that is stored in the received byte @@ -1731,7 +1724,7 @@ pn53x_target_receive_bits (nfc_device * pnd, uint8_t * pbtRx, size_t * pszRxBits } bool -pn53x_target_receive_bytes (nfc_device * pnd, uint8_t * pbtRx, size_t * pszRx, struct timeval *timeout) +pn53x_target_receive_bytes (nfc_device * pnd, uint8_t * pbtRx, size_t * pszRx, int timeout) { uint8_t abtCmd[1]; @@ -1810,7 +1803,7 @@ pn53x_target_send_bits (nfc_device * pnd, const uint8_t * pbtTx, const size_t sz return false; // Try to send the bits to the reader - if (!pn53x_transceive (pnd, abtCmd, szFrameBytes + 1, NULL, NULL, NULL)) + if (!pn53x_transceive (pnd, abtCmd, szFrameBytes + 1, NULL, NULL, 0)) return false; // Everyting seems ok, return true @@ -1818,7 +1811,7 @@ pn53x_target_send_bits (nfc_device * pnd, const uint8_t * pbtTx, const size_t sz } bool -pn53x_target_send_bytes (nfc_device * pnd, const uint8_t * pbtTx, const size_t szTx, struct timeval *timeout) +pn53x_target_send_bytes (nfc_device * pnd, const uint8_t * pbtTx, const size_t szTx, int timeout) { uint8_t abtCmd[PN53x_EXTENDED_FRAME__DATA_MAX_LEN]; @@ -1936,7 +1929,7 @@ bool pn53x_RFConfiguration__RF_field (nfc_device * pnd, bool bEnable) { uint8_t abtCmd[] = { RFConfiguration, RFCI_FIELD, (bEnable) ? 0x01 : 0x00 }; - return pn53x_transceive (pnd, abtCmd, sizeof (abtCmd), NULL, NULL, NULL); + return pn53x_transceive (pnd, abtCmd, sizeof (abtCmd), NULL, NULL, 0); } bool @@ -1949,7 +1942,7 @@ pn53x_RFConfiguration__Various_timings (nfc_device * pnd, const uint8_t fATR_RES fATR_RES_Timeout, // ATR_RES timeout (default: 0x0B 102.4 ms) fRetryTimeout // TimeOut during non-DEP communications (default: 0x0A 51.2 ms) }; - return pn53x_transceive (pnd, abtCmd, sizeof (abtCmd), NULL, NULL, NULL); + return pn53x_transceive (pnd, abtCmd, sizeof (abtCmd), NULL, NULL, 0); } bool @@ -1960,7 +1953,7 @@ pn53x_RFConfiguration__MaxRtyCOM (nfc_device * pnd, const uint8_t MaxRtyCOM) RFCI_RETRY_DATA, MaxRtyCOM // MaxRtyCOM, default: 0x00 (no retry, only one try), inifite: 0xff }; - return pn53x_transceive (pnd, abtCmd, sizeof (abtCmd), NULL, NULL, NULL); + return pn53x_transceive (pnd, abtCmd, sizeof (abtCmd), NULL, NULL, 0); } bool @@ -1974,7 +1967,7 @@ pn53x_RFConfiguration__MaxRetries (nfc_device * pnd, const uint8_t MxRtyATR, con MxRtyPSL, // MxRtyPSL, default: 0x01 MxRtyPassiveActivation // MxRtyPassiveActivation, default: 0xff (0x00 leads to problems with PN531) }; - return pn53x_transceive (pnd, abtCmd, sizeof (abtCmd), NULL, NULL, NULL); + return pn53x_transceive (pnd, abtCmd, sizeof (abtCmd), NULL, NULL, 0); } bool @@ -1982,7 +1975,7 @@ pn53x_SetParameters (nfc_device * pnd, const uint8_t ui8Value) { uint8_t abtCmd[] = { SetParameters, ui8Value }; - if(!pn53x_transceive (pnd, abtCmd, sizeof (abtCmd), NULL, NULL, NULL)) { + if(!pn53x_transceive (pnd, abtCmd, sizeof (abtCmd), NULL, NULL, 0)) { return false; } // We save last parameters in register cache @@ -1991,7 +1984,7 @@ pn53x_SetParameters (nfc_device * pnd, const uint8_t ui8Value) } bool -pn53x_SAMConfiguration (nfc_device * pnd, const pn532_sam_mode ui8Mode, struct timeval *timeout) +pn53x_SAMConfiguration (nfc_device * pnd, const pn532_sam_mode ui8Mode, int timeout) { uint8_t abtCmd[] = { SAMConfiguration, ui8Mode, 0x00, 0x00 }; size_t szCmd = sizeof(abtCmd); @@ -2023,7 +2016,7 @@ bool pn53x_PowerDown (nfc_device * pnd) { uint8_t abtCmd[] = { PowerDown, 0xf0 }; - return (pn53x_transceive (pnd, abtCmd, sizeof (abtCmd), NULL, NULL, NULL)); + return (pn53x_transceive (pnd, abtCmd, sizeof (abtCmd), NULL, NULL, 0)); } /** @@ -2045,7 +2038,7 @@ pn53x_InListPassiveTarget (nfc_device * pnd, const pn53x_modulation pmInitModulation, const uint8_t szMaxTargets, const uint8_t * pbtInitiatorData, const size_t szInitiatorData, uint8_t * pbtTargetsData, size_t * pszTargetsData, - struct timeval* timeout) + int timeout) { uint8_t abtCmd[15] = { InListPassiveTarget }; @@ -2101,7 +2094,7 @@ pn53x_InDeselect (nfc_device * pnd, const uint8_t ui8Target) uint8_t abtStatus[PN53x_EXTENDED_FRAME__DATA_MAX_LEN]; size_t szStatus = sizeof(abtStatus); uint8_t abtCmdGetStatus[] = { GetGeneralStatus }; - if (!pn53x_transceive (pnd, abtCmdGetStatus, sizeof (abtCmdGetStatus), abtStatus, &szStatus, NULL)) { + if (!pn53x_transceive (pnd, abtCmdGetStatus, sizeof (abtCmdGetStatus), abtStatus, &szStatus, 0)) { return false; } if ((szStatus < 3) || (abtStatus[2] == 0)) { @@ -2109,10 +2102,10 @@ pn53x_InDeselect (nfc_device * pnd, const uint8_t ui8Target) } // No much choice what to deselect actually... uint8_t abtCmdRcs360[] = { InDeselect, 0x01, 0x01 }; - return (pn53x_transceive (pnd, abtCmdRcs360, sizeof (abtCmdRcs360), NULL, NULL, NULL)); + return (pn53x_transceive (pnd, abtCmdRcs360, sizeof (abtCmdRcs360), NULL, NULL, 0)); } uint8_t abtCmd[] = { InDeselect, ui8Target }; - return (pn53x_transceive (pnd, abtCmd, sizeof (abtCmd), NULL, NULL, NULL)); + return (pn53x_transceive (pnd, abtCmd, sizeof (abtCmd), NULL, NULL, 0)); } bool @@ -2123,7 +2116,7 @@ pn53x_InRelease (nfc_device * pnd, const uint8_t ui8Target) uint8_t abtStatus[PN53x_EXTENDED_FRAME__DATA_MAX_LEN]; size_t szStatus = sizeof(abtStatus); uint8_t abtCmdGetStatus[] = { GetGeneralStatus }; - if (!pn53x_transceive (pnd, abtCmdGetStatus, sizeof (abtCmdGetStatus), abtStatus, &szStatus, NULL)) { + if (!pn53x_transceive (pnd, abtCmdGetStatus, sizeof (abtCmdGetStatus), abtStatus, &szStatus, 0)) { return false; } if ((szStatus < 3) || (abtStatus[2] == 0)) { @@ -2131,10 +2124,10 @@ pn53x_InRelease (nfc_device * pnd, const uint8_t ui8Target) } // No much choice what to release actually... uint8_t abtCmdRcs360[] = { InRelease, 0x01, 0x01 }; - return (pn53x_transceive (pnd, abtCmdRcs360, sizeof (abtCmdRcs360), NULL, NULL, NULL)); + return (pn53x_transceive (pnd, abtCmdRcs360, sizeof (abtCmdRcs360), NULL, NULL, 0)); } uint8_t abtCmd[] = { InRelease, ui8Target }; - return (pn53x_transceive (pnd, abtCmd, sizeof (abtCmd), NULL, NULL, NULL)); + return (pn53x_transceive (pnd, abtCmd, sizeof (abtCmd), NULL, NULL, 0)); } bool @@ -2157,7 +2150,7 @@ pn53x_InAutoPoll (nfc_device * pnd, uint8_t abtRx[PN53x_EXTENDED_FRAME__DATA_MAX_LEN]; size_t szRx = sizeof(abtRx); - bool res = pn53x_transceive (pnd, abtCmd, szTxInAutoPoll, abtRx, &szRx, NULL); + bool res = pn53x_transceive (pnd, abtCmd, szTxInAutoPoll, abtRx, &szRx, 0); if (res == false) { return false; @@ -2266,7 +2259,7 @@ pn53x_InJumpForDEP (nfc_device * pnd, uint8_t abtRx[PN53x_EXTENDED_FRAME__DATA_MAX_LEN]; size_t szRx = sizeof (abtRx); // Try to find a target, call the transceive callback function of the current device - if (!pn53x_transceive (pnd, abtCmd, offset, abtRx, &szRx, NULL)) + if (!pn53x_transceive (pnd, abtCmd, offset, abtRx, &szRx, 0)) return false; // Make sure one target has been found, the PN53X returns 0x00 if none was available @@ -2347,7 +2340,7 @@ pn53x_TgInitAsTarget (nfc_device * pnd, pn53x_target_mode ptm, // Request the initialization as a target uint8_t abtRx[PN53x_EXTENDED_FRAME__DATA_MAX_LEN]; size_t szRx = sizeof (abtRx); - if (!pn53x_transceive (pnd, abtCmd, 36 + szOptionalBytes, abtRx, &szRx, NULL)) + if (!pn53x_transceive (pnd, abtCmd, 36 + szOptionalBytes, abtRx, &szRx, 0)) return false; // Note: the first byte is skip: diff --git a/libnfc/chips/pn53x.h b/libnfc/chips/pn53x.h index 6a1d229..5e57c3c 100644 --- a/libnfc/chips/pn53x.h +++ b/libnfc/chips/pn53x.h @@ -127,8 +127,8 @@ typedef enum { } pn53x_operating_mode; struct pn53x_io { - bool (*send)(nfc_device * pnd, const uint8_t * pbtData, const size_t szData, struct timeval *timeout); - int (*receive)(nfc_device * pnd, uint8_t * pbtData, const size_t szDataLen, struct timeval *timeout); + bool (*send)(nfc_device * pnd, const uint8_t * pbtData, const size_t szData, int timeout); + int (*receive)(nfc_device * pnd, uint8_t * pbtData, const size_t szDataLen, int timeout); }; /* defines */ @@ -256,7 +256,7 @@ extern const uint8_t pn53x_ack_frame[6]; extern const uint8_t pn53x_nack_frame[6]; bool pn53x_init(nfc_device * pnd); -bool pn53x_transceive (nfc_device * pnd, const uint8_t * pbtTx, const size_t szTx, uint8_t * pbtRx, size_t *pszRx, struct timeval *timeout); +bool pn53x_transceive (nfc_device * pnd, const uint8_t * pbtTx, const size_t szTx, uint8_t * pbtRx, size_t *pszRx, int timeout); bool pn53x_set_parameters (nfc_device * pnd, const uint8_t ui8Value, const bool bEnable); bool pn53x_set_tx_bits (nfc_device * pnd, const uint8_t ui8Bits); @@ -292,7 +292,7 @@ bool pn53x_initiator_transceive_bits (nfc_device * pnd, const uint8_t * pbtTx const uint8_t * pbtTxPar, uint8_t * pbtRx, size_t * pszRxBits, uint8_t * pbtRxPar); bool pn53x_initiator_transceive_bytes (nfc_device * pnd, const uint8_t * pbtTx, const size_t szTx, - uint8_t * pbtRx, size_t * pszRx, struct timeval *timeout); + uint8_t * pbtRx, size_t * pszRx, int timeout); bool pn53x_initiator_transceive_bits_timed (nfc_device * pnd, const uint8_t * pbtTx, const size_t szTxBits, const uint8_t * pbtTxPar, uint8_t * pbtRx, size_t * pszRxBits, uint8_t * pbtRxPar, uint32_t * cycles); @@ -303,21 +303,21 @@ bool pn53x_initiator_deselect_target (nfc_device * pnd); // NFC device as Target functions bool pn53x_target_init (nfc_device * pnd, nfc_target * pnt, uint8_t * pbtRx, size_t * pszRx); bool pn53x_target_receive_bits (nfc_device * pnd, uint8_t * pbtRx, size_t * pszRxBits, uint8_t * pbtRxPar); -bool pn53x_target_receive_bytes (nfc_device * pnd, uint8_t * pbtRx, size_t * pszRx, struct timeval *timeout); +bool pn53x_target_receive_bytes (nfc_device * pnd, uint8_t * pbtRx, size_t * pszRx, int timeout); bool pn53x_target_send_bits (nfc_device * pnd, const uint8_t * pbtTx, const size_t szTxBits, const uint8_t * pbtTxPar); -bool pn53x_target_send_bytes (nfc_device * pnd, const uint8_t * pbtTx, const size_t szTx, struct timeval *timeout); +bool pn53x_target_send_bytes (nfc_device * pnd, const uint8_t * pbtTx, const size_t szTx, int timeout); // Error handling functions const char *pn53x_strerror (const nfc_device * pnd); // C wrappers for PN53x commands bool pn53x_SetParameters (nfc_device * pnd, const uint8_t ui8Value); -bool pn53x_SAMConfiguration (nfc_device * pnd, const pn532_sam_mode mode, struct timeval *timeout); +bool pn53x_SAMConfiguration (nfc_device * pnd, const pn532_sam_mode mode, int timeout); bool pn53x_PowerDown (nfc_device * pnd); bool pn53x_InListPassiveTarget (nfc_device * pnd, const pn53x_modulation pmInitModulation, const uint8_t szMaxTargets, const uint8_t * pbtInitiatorData, const size_t szInitiatorDataLen, uint8_t * pbtTargetsData, size_t * pszTargetsData, - struct timeval *timeout); + int timeout); bool pn53x_InDeselect (nfc_device * pnd, const uint8_t ui8Target); bool pn53x_InRelease (nfc_device * pnd, const uint8_t ui8Target); bool pn53x_InAutoPoll (nfc_device * pnd, const pn53x_target_type * ppttTargetTypes, const size_t szTargetTypes, diff --git a/libnfc/drivers/acr122.c b/libnfc/drivers/acr122.c index 15bc719..23234fd 100644 --- a/libnfc/drivers/acr122.c +++ b/libnfc/drivers/acr122.c @@ -310,7 +310,7 @@ acr122_disconnect (nfc_device * pnd) } bool -acr122_send (nfc_device * pnd, const uint8_t * pbtData, const size_t szData, struct timeval *timeout) +acr122_send (nfc_device * pnd, const uint8_t * pbtData, const size_t szData, int timeout) { // FIXME: timeout is not handled (void) timeout; @@ -381,7 +381,7 @@ acr122_send (nfc_device * pnd, const uint8_t * pbtData, const size_t szData, str } int -acr122_receive (nfc_device * pnd, uint8_t * pbtData, const size_t szData, struct timeval *timeout) +acr122_receive (nfc_device * pnd, uint8_t * pbtData, const size_t szData, int timeout) { // FIXME: timeout is not handled (void) timeout; diff --git a/libnfc/drivers/acr122.h b/libnfc/drivers/acr122.h index 3f91047..00c46fc 100644 --- a/libnfc/drivers/acr122.h +++ b/libnfc/drivers/acr122.h @@ -30,8 +30,8 @@ bool acr122_probe (nfc_connstring connstrings[], size_t connstrings_len, size // Functions used by developer to handle connection to this device nfc_device *acr122_connect (const nfc_connstring connstring); -bool acr122_send (nfc_device * pnd, const uint8_t * pbtData, const size_t szData, struct timeval *timeout); -int acr122_receive (nfc_device * pnd, uint8_t * pbtData, const size_t szData, struct timeval *timeout); +bool acr122_send (nfc_device * pnd, const uint8_t * pbtData, const size_t szData, int timeout); +int acr122_receive (nfc_device * pnd, uint8_t * pbtData, const size_t szData, int timeout); void acr122_disconnect (nfc_device * pnd); extern const struct nfc_driver_t acr122_driver; diff --git a/libnfc/drivers/arygon.c b/libnfc/drivers/arygon.c index b90f124..f27a245 100644 --- a/libnfc/drivers/arygon.c +++ b/libnfc/drivers/arygon.c @@ -304,7 +304,7 @@ arygon_disconnect (nfc_device * pnd) #define ARYGON_TX_BUFFER_LEN (PN53x_NORMAL_FRAME__DATA_MAX_LEN + PN53x_NORMAL_FRAME__OVERHEAD + 1) #define ARYGON_RX_BUFFER_LEN (PN53x_EXTENDED_FRAME__DATA_MAX_LEN + PN53x_EXTENDED_FRAME__OVERHEAD) bool -arygon_tama_send (nfc_device * pnd, const uint8_t * pbtData, const size_t szData, struct timeval *timeout) +arygon_tama_send (nfc_device * pnd, const uint8_t * pbtData, const size_t szData, int timeout) { // Before sending anything, we need to discard from any junk bytes uart_flush_input (DRIVER_DATA(pnd)->port); @@ -359,14 +359,14 @@ arygon_abort (nfc_device *pnd) // Send a valid TAMA packet to wakup the PN53x (we will not have an answer, according to Arygon manual) uint8_t dummy[] = { 0x32, 0x00, 0x00, 0xff, 0x09, 0xf7, 0xd4, 0x00, 0x00, 0x6c, 0x69, 0x62, 0x6e, 0x66, 0x63, 0xbe, 0x00 }; - uart_send (DRIVER_DATA (pnd)->port, dummy, sizeof (dummy), NULL); + 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 : -1; } int -arygon_tama_receive (nfc_device * pnd, uint8_t * pbtData, const size_t szDataLen, struct timeval *timeout) +arygon_tama_receive (nfc_device * pnd, uint8_t * pbtData, const size_t szDataLen, int timeout) { uint8_t abtRxBuf[5]; size_t len; @@ -491,12 +491,12 @@ arygon_firmware (nfc_device * pnd, char * str) size_t szRx = sizeof(abtRx); - int res = uart_send (DRIVER_DATA (pnd)->port, arygon_firmware_version_cmd, sizeof (arygon_firmware_version_cmd), NULL); + int res = uart_send (DRIVER_DATA (pnd)->port, arygon_firmware_version_cmd, sizeof (arygon_firmware_version_cmd), 0); if (res != 0) { log_put (LOG_CATEGORY, NFC_PRIORITY_TRACE, "Unable to send ARYGON firmware command."); return; } - res = uart_receive (DRIVER_DATA (pnd)->port, abtRx, szRx, 0, NULL); + res = uart_receive (DRIVER_DATA (pnd)->port, abtRx, szRx, 0, 0); if (res != 0) { log_put (LOG_CATEGORY, NFC_PRIORITY_TRACE, "Unable to retrieve ARYGON firmware version."); return; @@ -519,15 +519,11 @@ arygon_reset_tama (nfc_device * pnd) size_t szRx = sizeof(abtRx); int res; - struct timeval tv; - tv.tv_sec = 1; - tv.tv_usec = 0; - - uart_send (DRIVER_DATA (pnd)->port, arygon_reset_tama_cmd, sizeof (arygon_reset_tama_cmd), &tv); + uart_send (DRIVER_DATA (pnd)->port, arygon_reset_tama_cmd, sizeof (arygon_reset_tama_cmd), 1000); // 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) - res = uart_receive (DRIVER_DATA (pnd)->port, abtRx, szRx, 0, &tv); + res = uart_receive (DRIVER_DATA (pnd)->port, abtRx, szRx, 0, 1000); if (res != 0) { log_put (LOG_CATEGORY, NFC_PRIORITY_TRACE, "No reply to 'reset TAMA' command."); return false; diff --git a/libnfc/drivers/arygon.h b/libnfc/drivers/arygon.h index f5014b3..a3d52f9 100644 --- a/libnfc/drivers/arygon.h +++ b/libnfc/drivers/arygon.h @@ -35,8 +35,8 @@ bool arygon_probe (nfc_connstring connstrings[], size_t connstrings_len, size nfc_device *arygon_connect (const nfc_connstring connstring); void arygon_disconnect (nfc_device * pnd); -bool arygon_tama_send (nfc_device * pnd, const uint8_t * pbtData, const size_t szData, struct timeval *timeout); -int arygon_tama_receive (nfc_device * pnd, uint8_t * pbtData, const size_t szDat, struct timeval *timeouta); +bool arygon_tama_send (nfc_device * pnd, const uint8_t * pbtData, const size_t szData, int timeout); +int arygon_tama_receive (nfc_device * pnd, uint8_t * pbtData, const size_t szDat, int timeouta); extern const struct nfc_driver_t arygon_driver; diff --git a/libnfc/drivers/pn532_uart.c b/libnfc/drivers/pn532_uart.c index 47b7027..e7759db 100644 --- a/libnfc/drivers/pn532_uart.c +++ b/libnfc/drivers/pn532_uart.c @@ -282,14 +282,14 @@ pn532_uart_wakeup (nfc_device * pnd) { /* High Speed Unit (HSU) wake up consist to send 0x55 and wait a "long" delay for PN532 being wakeup. */ const uint8_t pn532_wakeup_preamble[] = { 0x55, 0x55, 0x00, 0x00, 0x00 }; - int res = uart_send (DRIVER_DATA(pnd)->port, pn532_wakeup_preamble, sizeof (pn532_wakeup_preamble), NULL); + int res = uart_send (DRIVER_DATA(pnd)->port, pn532_wakeup_preamble, sizeof (pn532_wakeup_preamble), 0); CHIP_DATA(pnd)->power_mode = NORMAL; // PN532 should now be awake return res; } #define PN532_BUFFER_LEN (PN53x_EXTENDED_FRAME__DATA_MAX_LEN + PN53x_EXTENDED_FRAME__OVERHEAD) bool -pn532_uart_send (nfc_device * pnd, const uint8_t * pbtData, const size_t szData, struct timeval *timeout) +pn532_uart_send (nfc_device * pnd, const uint8_t * pbtData, const size_t szData, int timeout) { // Before sending anything, we need to discard from any junk bytes uart_flush_input (DRIVER_DATA(pnd)->port); @@ -301,10 +301,7 @@ pn532_uart_send (nfc_device * pnd, const uint8_t * pbtData, const size_t szData, 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 - struct timeval tv; - tv.tv_sec = 1; - tv.tv_usec = 0; - if (!pn53x_SAMConfiguration (pnd, 0x01, &tv)) { + if (!pn53x_SAMConfiguration (pnd, 0x01, 1000)) { return false; } } @@ -352,7 +349,7 @@ pn532_uart_send (nfc_device * pnd, const uint8_t * pbtData, const size_t szData, } int -pn532_uart_receive (nfc_device * pnd, uint8_t * pbtData, const size_t szDataLen, struct timeval *timeout) +pn532_uart_receive (nfc_device * pnd, uint8_t * pbtData, const size_t szDataLen, int timeout) { uint8_t abtRxBuf[5]; size_t len; @@ -483,7 +480,7 @@ pn532_uart_ack (nfc_device * pnd) return -1; } } - return (0 == uart_send (DRIVER_DATA(pnd)->port, pn53x_ack_frame, sizeof (pn53x_ack_frame), NULL)) ? 0 : -1; + return (0 == uart_send (DRIVER_DATA(pnd)->port, pn53x_ack_frame, sizeof (pn53x_ack_frame), 0)) ? 0 : -1; } bool diff --git a/libnfc/drivers/pn532_uart.h b/libnfc/drivers/pn532_uart.h index 6f89541..51874c0 100644 --- a/libnfc/drivers/pn532_uart.h +++ b/libnfc/drivers/pn532_uart.h @@ -33,8 +33,8 @@ bool pn532_uart_probe (nfc_connstring connstrings[], size_t connstrings_len, nfc_device *pn532_uart_connect (const nfc_connstring connstring); void pn532_uart_disconnect (nfc_device * pnd); -bool pn532_uart_send (nfc_device * pnd, const uint8_t * pbtData, const size_t szData, struct timeval *timeout); -int pn532_uart_receive (nfc_device * pnd, uint8_t * pbtData, const size_t szData, struct timeval *timeout); +bool pn532_uart_send (nfc_device * pnd, const uint8_t * pbtData, const size_t szData, int timeout); +int pn532_uart_receive (nfc_device * pnd, uint8_t * pbtData, const size_t szData, int timeout); extern const struct nfc_driver_t pn532_uart_driver; diff --git a/libnfc/drivers/pn53x_usb.c b/libnfc/drivers/pn53x_usb.c index c76bee9..5a9027d 100644 --- a/libnfc/drivers/pn53x_usb.c +++ b/libnfc/drivers/pn53x_usb.c @@ -121,11 +121,11 @@ bool pn53x_usb_get_usb_device_name (struct usb_device *dev, usb_dev_handle *udev bool pn53x_usb_init (nfc_device *pnd); int -pn53x_usb_bulk_read (struct pn53x_usb_data *data, uint8_t abtRx[], const size_t szRx, struct timeval *timeout) +pn53x_usb_bulk_read (struct pn53x_usb_data *data, uint8_t abtRx[], const size_t szRx, int timeout) { int timeout_ms = USB_INFINITE_TIMEOUT; - if (timeout) { - timeout_ms = timeout->tv_sec * 1000 + timeout->tv_usec / 1000; + if (timeout > 0) { + timeout_ms = timeout; if (timeout_ms == USB_INFINITE_TIMEOUT) { // timeout < 1 ms timeout_ms++; @@ -143,12 +143,12 @@ pn53x_usb_bulk_read (struct pn53x_usb_data *data, uint8_t abtRx[], const size_t } int -pn53x_usb_bulk_write (struct pn53x_usb_data *data, uint8_t abtTx[], const size_t szTx, struct timeval *timeout) +pn53x_usb_bulk_write (struct pn53x_usb_data *data, uint8_t abtTx[], const size_t szTx, int timeout) { LOG_HEX ("TX", abtTx, szTx); int timeout_ms = USB_INFINITE_TIMEOUT; - if (timeout) - timeout_ms = timeout->tv_sec * 1000 + timeout->tv_usec / 1000; + if (timeout > 0) + timeout_ms = timeout; int res = usb_bulk_write (data->pudh, data->uiEndPointOut, (char *) abtTx, szTx, timeout_ms); if (res > 0) { @@ -519,7 +519,7 @@ pn53x_usb_disconnect (nfc_device * pnd) #define PN53X_USB_BUFFER_LEN (PN53x_EXTENDED_FRAME__DATA_MAX_LEN + PN53x_EXTENDED_FRAME__OVERHEAD) bool -pn53x_usb_send (nfc_device * pnd, const uint8_t * pbtData, const size_t szData, struct timeval *timeout) +pn53x_usb_send (nfc_device * pnd, const uint8_t * pbtData, const size_t szData, int timeout) { uint8_t abtFrame[PN53X_USB_BUFFER_LEN] = { 0x00, 0x00, 0xff }; // Every packet must start with "00 00 ff" size_t szFrame = 0; @@ -565,7 +565,7 @@ pn53x_usb_send (nfc_device * pnd, const uint8_t * pbtData, const size_t szData, } int -pn53x_usb_receive (nfc_device * pnd, uint8_t * pbtData, const size_t szDataLen, struct timeval *timeout) +pn53x_usb_receive (nfc_device * pnd, uint8_t * pbtData, const size_t szDataLen, int timeout) { size_t len; off_t offset = 0; @@ -583,11 +583,12 @@ pn53x_usb_receive (nfc_device * pnd, uint8_t * pbtData, const size_t szDataLen, }; struct timeval remaining_time, usb_timeout; - if (timeout) { - remaining_time = *timeout; + if (timeout > 0) { + remaining_time.tv_sec = (timeout / 1000); + remaining_time.tv_usec = ((timeout % 1000) * 1000); } read: - if (timeout) { + if (timeout > 0) { // A user-provided timeout is set, we have to cut it in multiple chunk to be able to keep an nfc_abort_command() mecanism struct timeval tmp; if (1 == timeval_subtract (&tmp, &remaining_time, &fixed_timeout)) { @@ -607,7 +608,7 @@ read: pnd->iLastError = ECOMTIMEOUT; return -1; } - res = pn53x_usb_bulk_read (DRIVER_DATA (pnd), abtRxBuf, sizeof (abtRxBuf), &usb_timeout); + res = pn53x_usb_bulk_read (DRIVER_DATA (pnd), abtRxBuf, sizeof (abtRxBuf), ((usb_timeout.tv_sec * 1000) + (usb_timeout.tv_usec / 1000))); if (res == -USB_TIMEDOUT) { if (DRIVER_DATA (pnd)->abort_flag) { @@ -717,7 +718,7 @@ read: int pn53x_usb_ack (nfc_device * pnd) { - return pn53x_usb_bulk_write (DRIVER_DATA (pnd), (uint8_t *) pn53x_ack_frame, sizeof (pn53x_ack_frame), NULL); + return pn53x_usb_bulk_write (DRIVER_DATA (pnd), (uint8_t *) pn53x_ack_frame, sizeof (pn53x_ack_frame), 0); } bool @@ -726,13 +727,13 @@ pn53x_usb_init (nfc_device *pnd) // Sometimes PN53x USB doesn't reply ACK one the first frame, so we need to send a dummy one... //pn53x_check_communication (pnd); // Sony RC-S360 doesn't support this command for now so let's use a get_firmware_version instead: const uint8_t abtCmd[] = { GetFirmwareVersion }; - pn53x_transceive (pnd, abtCmd, sizeof (abtCmd), NULL, NULL, NULL); + pn53x_transceive (pnd, abtCmd, sizeof (abtCmd), NULL, NULL, 0); // ...and we don't care about error pnd->iLastError = 0; if (SONY_RCS360 == DRIVER_DATA (pnd)->model) { log_put (LOG_CATEGORY, NFC_PRIORITY_TRACE, "SONY RC-S360 initialization."); const uint8_t abtCmd2[] = { 0x18, 0x01 }; - pn53x_transceive (pnd, abtCmd2, sizeof (abtCmd2), NULL, NULL, NULL); + pn53x_transceive (pnd, abtCmd2, sizeof (abtCmd2), NULL, NULL, 0); pn53x_usb_ack (pnd); } diff --git a/libnfc/drivers/pn53x_usb.h b/libnfc/drivers/pn53x_usb.h index 83661b8..00f0d31 100644 --- a/libnfc/drivers/pn53x_usb.h +++ b/libnfc/drivers/pn53x_usb.h @@ -31,8 +31,8 @@ bool pn53x_usb_probe (nfc_connstring connstrings[], size_t connstrings_len, size_t * pszDeviceFound); nfc_device *pn53x_usb_connect (const nfc_connstring connstring); -bool pn53x_usb_send (nfc_device * pnd, const uint8_t * pbtData, const size_t szData, struct timeval *timeout); -int pn53x_usb_receive (nfc_device * pnd, uint8_t * pbtData, const size_t szData, struct timeval *timeout); +bool pn53x_usb_send (nfc_device * pnd, const uint8_t * pbtData, const size_t szData, int timeout); +int pn53x_usb_receive (nfc_device * pnd, uint8_t * pbtData, const size_t szData, int timeout); void pn53x_usb_disconnect (nfc_device * pnd); extern const struct nfc_driver_t pn53x_usb_driver; diff --git a/libnfc/nfc-emulation.c b/libnfc/nfc-emulation.c index 52e9cc3..c892837 100644 --- a/libnfc/nfc-emulation.c +++ b/libnfc/nfc-emulation.c @@ -42,12 +42,12 @@ nfc_emulate_target (nfc_device* pnd, struct nfc_emulator *emulator) while (res >= 0) { res = emulator->state_machine->io (emulator, abtRx, szRx, abtTx, sizeof (abtTx)); if (res > 0) { - if (!nfc_target_send_bytes(pnd, abtTx, res, NULL)) { + if (!nfc_target_send_bytes(pnd, abtTx, res, 0)) { return -1; } } if (res >= 0) { - if (!nfc_target_receive_bytes(pnd, abtRx, &szRx, NULL)) { + if (!nfc_target_receive_bytes(pnd, abtRx, &szRx, 0)) { return -1; } } diff --git a/libnfc/nfc-internal.h b/libnfc/nfc-internal.h index e3f16b0..04333e9 100644 --- a/libnfc/nfc-internal.h +++ b/libnfc/nfc-internal.h @@ -136,14 +136,14 @@ struct nfc_driver_t { bool (*initiator_poll_target) (nfc_device * pnd, const nfc_modulation * pnmModulations, const size_t szModulations, const uint8_t uiPollNr, const uint8_t btPeriod, nfc_target * pnt); bool (*initiator_select_dep_target) (nfc_device * pnd, const nfc_dep_mode ndm, const nfc_baud_rate nbr, const nfc_dep_info * pndiInitiator, nfc_target * pnt); bool (*initiator_deselect_target) (nfc_device * pnd); - bool (*initiator_transceive_bytes) (nfc_device * pnd, const uint8_t * pbtTx, const size_t szTx, uint8_t * pbtRx, size_t * pszRx, struct timeval *timeout); + bool (*initiator_transceive_bytes) (nfc_device * pnd, const uint8_t * pbtTx, const size_t szTx, uint8_t * pbtRx, size_t * pszRx, int timeout); bool (*initiator_transceive_bits) (nfc_device * pnd, const uint8_t * pbtTx, const size_t szTxBits, const uint8_t * pbtTxPar, uint8_t * pbtRx, size_t * pszRxBits, uint8_t * pbtRxPar); bool (*initiator_transceive_bytes_timed) (nfc_device * pnd, const uint8_t * pbtTx, const size_t szTx, uint8_t * pbtRx, size_t * pszRx, uint32_t * cycles); bool (*initiator_transceive_bits_timed) (nfc_device * pnd, const uint8_t * pbtTx, const size_t szTxBits, const uint8_t * pbtTxPar, uint8_t * pbtRx, size_t * pszRxBits, uint8_t * pbtRxPar, uint32_t * cycles); bool (*target_init) (nfc_device * pnd, nfc_target * pnt, uint8_t * pbtRx, size_t * pszRx); - bool (*target_send_bytes) (nfc_device * pnd, const uint8_t * pbtTx, const size_t szTx, struct timeval *timeout); - bool (*target_receive_bytes) (nfc_device * pnd, uint8_t * pbtRx, size_t * pszRx, struct timeval *timeout); + bool (*target_send_bytes) (nfc_device * pnd, const uint8_t * pbtTx, const size_t szTx, int timeout); + bool (*target_receive_bytes) (nfc_device * pnd, uint8_t * pbtRx, size_t * pszRx, int timeout); bool (*target_send_bits) (nfc_device * pnd, const uint8_t * pbtTx, const size_t szTxBits, const uint8_t * pbtTxPar); bool (*target_receive_bits) (nfc_device * pnd, uint8_t * pbtRx, size_t * pszRxBits, uint8_t * pbtRxPar); diff --git a/libnfc/nfc.c b/libnfc/nfc.c index 95b4db8..cb49bd0 100644 --- a/libnfc/nfc.c +++ b/libnfc/nfc.c @@ -461,7 +461,7 @@ nfc_initiator_deselect_target (nfc_device * pnd) * * @param pbtTx contains a byte array of the frame that needs to be transmitted. * @param szTx contains the length in bytes. - * @param timeout timeval struct pointer (NULL means infinite) + * @param timeout in milliseconds * * The NFC device (configured as initiator) will transmit the supplied bytes (\a pbtTx) to the target. * It waits for the response and stores the received bytes in the \a pbtRx byte array. @@ -480,7 +480,7 @@ nfc_initiator_deselect_target (nfc_device * pnd) */ bool nfc_initiator_transceive_bytes (nfc_device * pnd, const uint8_t * pbtTx, const size_t szTx, uint8_t * pbtRx, - size_t * pszRx, struct timeval *timeout) + size_t * pszRx, int timeout) { HAL (initiator_transceive_bytes, pnd, pbtTx, szTx, pbtRx, pszRx, timeout) } @@ -682,7 +682,7 @@ nfc_abort_command (nfc_device * pnd) * @param pnd \a nfc_device struct pointer that represent currently used device * @param pbtTx pointer to Tx buffer * @param szTx size of Tx buffer - * @param timeout timeval struct pointer (NULL means infinite) + * @param timeout in milliseconds * * This function make the NFC device (configured as \e target) send byte frames * (e.g. APDU responses) to the \e initiator. @@ -691,7 +691,7 @@ nfc_abort_command (nfc_device * pnd) * If timeout is a null pointer, the function blocks indefinitely (until an error is raised or function is completed). */ bool -nfc_target_send_bytes (nfc_device * pnd, const uint8_t * pbtTx, const size_t szTx, struct timeval *timeout) +nfc_target_send_bytes (nfc_device * pnd, const uint8_t * pbtTx, const size_t szTx, int timeout) { HAL (target_send_bytes, pnd, pbtTx, szTx, timeout); } @@ -703,7 +703,7 @@ nfc_target_send_bytes (nfc_device * pnd, const uint8_t * pbtTx, const size_t szT * @param pnd \a nfc_device struct pointer that represent currently used device * @param[out] pbtRx pointer to Rx buffer * @param[out] pszRx received byte count - * @param timeout timeval struct pointer (NULL means infinite) + * @param timeout in milliseconds * * This function retrieves bytes frames (e.g. ADPU) sent by the \e initiator to the NFC device (configured as \e target). * @@ -711,7 +711,7 @@ nfc_target_send_bytes (nfc_device * pnd, const uint8_t * pbtTx, const size_t szT * If timeout is a null pointer, the function blocks indefinitely (until an error is raised or function is completed). */ bool -nfc_target_receive_bytes (nfc_device * pnd, uint8_t * pbtRx, size_t * pszRx, struct timeval *timeout) +nfc_target_receive_bytes (nfc_device * pnd, uint8_t * pbtRx, size_t * pszRx, int timeout) { HAL (target_receive_bytes, pnd, pbtRx, pszRx, timeout); } diff --git a/utils/mifare.c b/utils/mifare.c index a074d3a..92ecfad 100644 --- a/utils/mifare.c +++ b/utils/mifare.c @@ -100,7 +100,7 @@ nfc_initiator_mifare_cmd (nfc_device * pnd, const mifare_cmd mc, const uint8_t u return false; } // Fire the mifare command - if (!nfc_initiator_transceive_bytes (pnd, abtCmd, 2 + szParamLen, abtRx, &szRx, NULL)) { + if (!nfc_initiator_transceive_bytes (pnd, abtCmd, 2 + szParamLen, abtRx, &szRx, 0)) { if (pnd->iLastError == EINVRXFRAM) { // "Invalid received frame" AKA EINVRXFRAM, usual means we are // authenticated on a sector but the requested MIFARE cmd (read, write) diff --git a/utils/nfc-mfclassic.c b/utils/nfc-mfclassic.c index a7a3907..9a7bb1b 100644 --- a/utils/nfc-mfclassic.c +++ b/utils/nfc-mfclassic.c @@ -116,7 +116,7 @@ transmit_bytes (const uint8_t * pbtTx, const size_t szTx) printf ("Sent bits: "); print_hex (pbtTx, szTx); // Transmit the command bytes - if (!nfc_initiator_transceive_bytes (pnd, pbtTx, szTx, abtRx, &szRx, NULL)) + if (!nfc_initiator_transceive_bytes (pnd, pbtTx, szTx, abtRx, &szRx, 0)) return false; // Show received answer diff --git a/utils/nfc-mfsetuid.c b/utils/nfc-mfsetuid.c index 63dcd2c..d610fcf 100644 --- a/utils/nfc-mfsetuid.c +++ b/utils/nfc-mfsetuid.c @@ -118,7 +118,7 @@ transmit_bytes (const uint8_t * pbtTx, const size_t szTx) print_hex (pbtTx, szTx); } // Transmit the command bytes - if (!nfc_initiator_transceive_bytes (pnd, pbtTx, szTx, abtRx, &szRx, NULL)) + if (!nfc_initiator_transceive_bytes (pnd, pbtTx, szTx, abtRx, &szRx, 0)) return false; // Show received answer diff --git a/utils/nfc-read-forum-tag3.c b/utils/nfc-read-forum-tag3.c index 7d96966..2ea3fc1 100644 --- a/utils/nfc-read-forum-tag3.c +++ b/utils/nfc-read-forum-tag3.c @@ -116,7 +116,7 @@ nfc_forum_tag_type3_check (nfc_device *pnd, const nfc_target nt, const uint16_t uint8_t res[1024]; size_t res_len; - if (!nfc_initiator_transceive_bytes (pnd, frame, frame_len, res, &res_len, NULL)) { + if (!nfc_initiator_transceive_bytes (pnd, frame, frame_len, res, &res_len, 0)) { return -1; } const size_t res_overhead = 1 + 1 + 8 + 2; // 1+1+8+2: LEN + CMD + NFCID2 + STATUS diff --git a/utils/nfc-relay-picc.c b/utils/nfc-relay-picc.c index 4f0b9d9..b8df5ec 100644 --- a/utils/nfc-relay-picc.c +++ b/utils/nfc-relay-picc.c @@ -370,7 +370,7 @@ main (int argc, char *argv[]) bool ret; if (!initiator_only_mode) { // Receive external reader command through target - if (!nfc_target_receive_bytes(pndTarget,abtCapdu,&szCapduLen, NULL)) { + if (!nfc_target_receive_bytes(pndTarget,abtCapdu,&szCapduLen, 0)) { nfc_perror (pndTarget, "nfc_target_receive_bytes"); if (!target_only_mode) { nfc_disconnect (pndInitiator); @@ -401,7 +401,7 @@ main (int argc, char *argv[]) if (!target_only_mode) { // Forward the frame to the original tag ret = nfc_initiator_transceive_bytes - (pndInitiator, abtCapdu, szCapduLen, abtRapdu, &szRapduLen, NULL); + (pndInitiator, abtCapdu, szCapduLen, abtRapdu, &szRapduLen, 0); } else { if (scan_hex_fd3(abtRapdu, &szRapduLen, "R-APDU") != EXIT_SUCCESS) { fprintf (stderr, "Error while scanning R-APDU from FD3\n"); @@ -425,7 +425,7 @@ main (int argc, char *argv[]) } if (!initiator_only_mode) { // Transmit the response bytes - if (!nfc_target_send_bytes(pndTarget, abtRapdu, szRapduLen, NULL)) { + if (!nfc_target_send_bytes(pndTarget, abtRapdu, szRapduLen, 0)) { nfc_perror (pndTarget, "nfc_target_send_bytes"); if (!target_only_mode) { nfc_disconnect (pndInitiator); From 90b5961b4093e06a90602d3c3e40277f39981808 Mon Sep 17 00:00:00 2001 From: Audrey Diacre Date: Fri, 25 Nov 2011 13:30:13 +0000 Subject: [PATCH 009/113] Rename fixed_timeout to timeout_tv and cleam useless allocation. --- libnfc/buses/uart_posix.c | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/libnfc/buses/uart_posix.c b/libnfc/buses/uart_posix.c index cd3d5e4..8017384 100644 --- a/libnfc/buses/uart_posix.c +++ b/libnfc/buses/uart_posix.c @@ -267,18 +267,13 @@ select: FD_SET (iAbortFd, &rfds); } - /* - * Some implementations (e.g. Linux) of select(2) will update *timeout. - * Make a copy so that it will be updated on these systems, - */ - struct timeval fixed_timeout; + struct timeval timeout_tv; if (timeout > 0) { - fixed_timeout.tv_sec = (timeout / 1000); - fixed_timeout.tv_usec = ((timeout % 1000) * 1000); - timeout = ((fixed_timeout.tv_sec * 1000) + (fixed_timeout.tv_usec / 1000)); + timeout_tv.tv_sec = (timeout / 1000); + timeout_tv.tv_usec = ((timeout % 1000) * 1000); } - res = select (MAX(UART_DATA(sp)->fd, iAbortFd) + 1, &rfds, NULL, NULL, &fixed_timeout); + res = select (MAX(UART_DATA(sp)->fd, iAbortFd) + 1, &rfds, NULL, NULL, &timeout_tv); if ((res < 0) && (EINTR == errno)) { // The system call was interupted by a signal and a signal handler was From 5a9a778879f083655a760ce10f944daa25b710d9 Mon Sep 17 00:00:00 2001 From: Audrey Diacre Date: Fri, 25 Nov 2011 13:32:29 +0000 Subject: [PATCH 010/113] Remove whitespace after star symbol for pointers --- libnfc/chips/pn53x.c | 162 +++++++++++++++++++++---------------------- libnfc/nfc.c | 70 +++++++++---------- 2 files changed, 116 insertions(+), 116 deletions(-) diff --git a/libnfc/chips/pn53x.c b/libnfc/chips/pn53x.c index 3b28fb9..95b337c 100644 --- a/libnfc/chips/pn53x.c +++ b/libnfc/chips/pn53x.c @@ -51,15 +51,15 @@ const uint8_t pn53x_nack_frame[] = { 0x00, 0x00, 0xff, 0xff, 0x00, 0x00 }; static const uint8_t pn53x_error_frame[] = { 0x00, 0x00, 0xff, 0x01, 0xff, 0x7f, 0x81, 0x00 }; /* prototypes */ -bool pn53x_reset_settings (nfc_device * pnd); -bool pn53x_writeback_register (nfc_device * pnd); +bool pn53x_reset_settings (nfc_device *pnd); +bool pn53x_writeback_register (nfc_device *pnd); nfc_modulation pn53x_ptt_to_nm (const pn53x_target_type ptt); pn53x_modulation pn53x_nm_to_pm (const nfc_modulation nm); pn53x_target_type pn53x_nm_to_ptt (const nfc_modulation nm); bool -pn53x_init(nfc_device * pnd) +pn53x_init(nfc_device *pnd) { // GetFirmwareVersion command is used to set PN53x chips type (PN531, PN532 or PN533) char abtFirmwareText[22]; @@ -89,7 +89,7 @@ pn53x_init(nfc_device * pnd) } bool -pn53x_reset_settings(nfc_device * pnd) +pn53x_reset_settings(nfc_device *pnd) { // Reset the ending transmission bits register, it is unknown what the last tranmission used there CHIP_DATA (pnd)->ui8TxBits = 0; @@ -100,7 +100,7 @@ pn53x_reset_settings(nfc_device * pnd) } bool -pn53x_transceive (nfc_device * pnd, const uint8_t * pbtTx, const size_t szTx, uint8_t * pbtRx, size_t *pszRx, int timeout) +pn53x_transceive (nfc_device *pnd, const uint8_t *pbtTx, const size_t szTx, uint8_t *pbtRx, size_t *pszRx, int timeout) { if (CHIP_DATA (pnd)->wb_trigged) { if (!pn53x_writeback_register (pnd)) { @@ -190,7 +190,7 @@ pn53x_transceive (nfc_device * pnd, const uint8_t * pbtTx, const size_t szTx, ui } bool -pn53x_set_parameters (nfc_device * pnd, const uint8_t ui8Parameter, const bool bEnable) +pn53x_set_parameters (nfc_device *pnd, const uint8_t ui8Parameter, const bool bEnable) { uint8_t ui8Value = (bEnable) ? (CHIP_DATA (pnd)->ui8Parameters | ui8Parameter) : (CHIP_DATA (pnd)->ui8Parameters & ~(ui8Parameter)); if (ui8Value != CHIP_DATA (pnd)->ui8Parameters) { @@ -200,7 +200,7 @@ pn53x_set_parameters (nfc_device * pnd, const uint8_t ui8Parameter, const bool b } bool -pn53x_set_tx_bits (nfc_device * pnd, const uint8_t ui8Bits) +pn53x_set_tx_bits (nfc_device *pnd, const uint8_t ui8Bits) { // Test if we need to update the transmission bits register setting if (CHIP_DATA (pnd)->ui8TxBits != ui8Bits) { @@ -215,8 +215,8 @@ pn53x_set_tx_bits (nfc_device * pnd, const uint8_t ui8Bits) } bool -pn53x_wrap_frame (const uint8_t * pbtTx, const size_t szTxBits, const uint8_t * pbtTxPar, - uint8_t * pbtFrame, size_t * pszFrameBits) +pn53x_wrap_frame (const uint8_t *pbtTx, const size_t szTxBits, const uint8_t *pbtTxPar, + uint8_t *pbtFrame, size_t *pszFrameBits) { uint8_t btFrame; uint8_t btData; @@ -271,8 +271,8 @@ pn53x_wrap_frame (const uint8_t * pbtTx, const size_t szTxBits, const uint8_t * } bool -pn53x_unwrap_frame (const uint8_t * pbtFrame, const size_t szFrameBits, uint8_t * pbtRx, size_t * pszRxBits, - uint8_t * pbtRxPar) +pn53x_unwrap_frame (const uint8_t *pbtFrame, const size_t szFrameBits, uint8_t *pbtRx, size_t *pszRxBits, + uint8_t *pbtRxPar) { uint8_t btFrame; uint8_t btData; @@ -318,8 +318,8 @@ pn53x_unwrap_frame (const uint8_t * pbtFrame, const size_t szFrameBits, uint8_t } bool -pn53x_decode_target_data (const uint8_t * pbtRawData, size_t szRawData, pn53x_type type, nfc_modulationype nmt, - nfc_target_info * pnti) +pn53x_decode_target_data (const uint8_t *pbtRawData, size_t szRawData, pn53x_type type, nfc_modulationype nmt, + nfc_target_info *pnti) { uint8_t szAttribRes; @@ -462,7 +462,7 @@ pn53x_decode_target_data (const uint8_t * pbtRawData, size_t szRawData, pn53x_ty } bool -pn53x_ReadRegister (nfc_device * pnd, uint16_t ui16RegisterAddress, uint8_t * ui8Value) +pn53x_ReadRegister (nfc_device *pnd, uint16_t ui16RegisterAddress, uint8_t *ui8Value) { uint8_t abtCmd[] = { ReadRegister, ui16RegisterAddress >> 8, ui16RegisterAddress & 0xff }; uint8_t abtRegValue[2]; @@ -481,13 +481,13 @@ pn53x_ReadRegister (nfc_device * pnd, uint16_t ui16RegisterAddress, uint8_t * ui return true; } -bool pn53x_read_register (nfc_device * pnd, uint16_t ui16RegisterAddress, uint8_t * ui8Value) +bool pn53x_read_register (nfc_device *pnd, uint16_t ui16RegisterAddress, uint8_t *ui8Value) { return pn53x_ReadRegister (pnd, ui16RegisterAddress, ui8Value); } bool -pn53x_WriteRegister (nfc_device * pnd, const uint16_t ui16RegisterAddress, const uint8_t ui8Value) +pn53x_WriteRegister (nfc_device *pnd, const uint16_t ui16RegisterAddress, const uint8_t ui8Value) { uint8_t abtCmd[] = { WriteRegister, ui16RegisterAddress >> 8, ui16RegisterAddress & 0xff, ui8Value }; PNREG_TRACE (ui16RegisterAddress); @@ -495,7 +495,7 @@ pn53x_WriteRegister (nfc_device * pnd, const uint16_t ui16RegisterAddress, const } bool -pn53x_write_register (nfc_device * pnd, const uint16_t ui16RegisterAddress, const uint8_t ui8SymbolMask, const uint8_t ui8Value) +pn53x_write_register (nfc_device *pnd, const uint16_t ui16RegisterAddress, const uint8_t ui8SymbolMask, const uint8_t ui8Value) { if ((ui16RegisterAddress < PN53X_CACHE_REGISTER_MIN_ADDRESS) || (ui16RegisterAddress > PN53X_CACHE_REGISTER_MAX_ADDRESS)) { // Direct write @@ -521,7 +521,7 @@ pn53x_write_register (nfc_device * pnd, const uint16_t ui16RegisterAddress, cons } bool -pn53x_writeback_register (nfc_device * pnd) +pn53x_writeback_register (nfc_device *pnd) { // TODO Check at each step (ReadRegister, WriteRegister) if we didn't exceed max supported frame length BUFFER_INIT (abtReadRegisterCmd, PN53x_EXTENDED_FRAME__DATA_MAX_LEN); @@ -589,7 +589,7 @@ pn53x_writeback_register (nfc_device * pnd) } bool -pn53x_get_firmware_version (nfc_device * pnd, char abtFirmwareText[22]) +pn53x_get_firmware_version (nfc_device *pnd, char abtFirmwareText[22]) { const uint8_t abtCmd[] = { GetFirmwareVersion }; uint8_t abtFw[4]; @@ -640,7 +640,7 @@ pn53x_get_firmware_version (nfc_device * pnd, char abtFirmwareText[22]) } bool -pn53x_configure (nfc_device * pnd, const nfc_device_option ndo, const bool bEnable) +pn53x_configure (nfc_device *pnd, const nfc_device_option ndo, const bool bEnable) { uint8_t btValue; switch (ndo) { @@ -839,7 +839,7 @@ pn53x_check_communication (nfc_device *pnd) } bool -pn53x_initiator_init (nfc_device * pnd) +pn53x_initiator_init (nfc_device *pnd) { pn53x_reset_settings(pnd); @@ -852,10 +852,10 @@ pn53x_initiator_init (nfc_device * pnd) } bool -pn53x_initiator_select_passive_target_ext (nfc_device * pnd, +pn53x_initiator_select_passive_target_ext (nfc_device *pnd, const nfc_modulation nm, - const uint8_t * pbtInitData, const size_t szInitData, - nfc_target * pnt, + const uint8_t *pbtInitData, const size_t szInitData, + nfc_target *pnt, int timeout) { uint8_t abtTargetsData[PN53x_EXTENDED_FRAME__DATA_MAX_LEN]; @@ -962,19 +962,19 @@ pn53x_initiator_select_passive_target_ext (nfc_device * pnd, } bool -pn53x_initiator_select_passive_target (nfc_device * pnd, +pn53x_initiator_select_passive_target (nfc_device *pnd, const nfc_modulation nm, - const uint8_t * pbtInitData, const size_t szInitData, - nfc_target * pnt) + const uint8_t *pbtInitData, const size_t szInitData, + nfc_target *pnt) { return pn53x_initiator_select_passive_target_ext (pnd, nm, pbtInitData, szInitData, pnt, 0); } bool -pn53x_initiator_poll_target (nfc_device * pnd, - const nfc_modulation * pnmModulations, const size_t szModulations, +pn53x_initiator_poll_target (nfc_device *pnd, + const nfc_modulation *pnmModulations, const size_t szModulations, const uint8_t uiPollNr, const uint8_t uiPeriod, - nfc_target * pnt) + nfc_target *pnt) { if (CHIP_DATA(pnd)->type == PN532) { size_t szTargetTypes = 0; @@ -1035,10 +1035,10 @@ pn53x_initiator_poll_target (nfc_device * pnd, } bool -pn53x_initiator_select_dep_target(nfc_device * pnd, +pn53x_initiator_select_dep_target(nfc_device *pnd, const nfc_dep_mode ndm, const nfc_baud_rate nbr, - const nfc_dep_info * pndiInitiator, - nfc_target * pnt) + const nfc_dep_info *pndiInitiator, + nfc_target *pnt) { const uint8_t abtPassiveInitiatorData[] = { 0x00, 0xff, 0xff, 0x00, 0x00 }; // Only for 212/424 kpbs: First 4 bytes shall be set like this according to NFCIP-1, last byte is TSN (Time Slot Number) const uint8_t * pbtPassiveInitiatorData = NULL; @@ -1063,8 +1063,8 @@ pn53x_initiator_select_dep_target(nfc_device * pnd, } bool -pn53x_initiator_transceive_bits (nfc_device * pnd, const uint8_t * pbtTx, const size_t szTxBits, - const uint8_t * pbtTxPar, uint8_t * pbtRx, size_t * pszRxBits, uint8_t * pbtRxPar) +pn53x_initiator_transceive_bits (nfc_device *pnd, const uint8_t *pbtTx, const size_t szTxBits, + const uint8_t *pbtTxPar, uint8_t *pbtRx, size_t *pszRxBits, uint8_t *pbtRxPar) { size_t szFrameBits = 0; size_t szFrameBytes = 0; @@ -1127,8 +1127,8 @@ pn53x_initiator_transceive_bits (nfc_device * pnd, const uint8_t * pbtTx, const } bool -pn53x_initiator_transceive_bytes (nfc_device * pnd, const uint8_t * pbtTx, const size_t szTx, uint8_t * pbtRx, - size_t * pszRx, int timeout) +pn53x_initiator_transceive_bytes (nfc_device *pnd, const uint8_t *pbtTx, const size_t szTx, uint8_t *pbtRx, + size_t *pszRx, int timeout) { size_t szExtraTxLen; uint8_t abtCmd[PN53x_EXTENDED_FRAME__DATA_MAX_LEN]; @@ -1173,7 +1173,7 @@ pn53x_initiator_transceive_bytes (nfc_device * pnd, const uint8_t * pbtTx, const return true; } -void __pn53x_init_timer(nfc_device * pnd, const uint32_t max_cycles) +void __pn53x_init_timer(nfc_device *pnd, const uint32_t max_cycles) { // The prescaler will dictate what will be the precision and // the largest delay to measure before saturation. Some examples: @@ -1194,7 +1194,7 @@ void __pn53x_init_timer(nfc_device * pnd, const uint32_t max_cycles) pn53x_write_register (pnd, PN53X_REG_CIU_TReloadVal_lo, 0xFF, reloadval & 0xFF); } -uint32_t __pn53x_get_timer(nfc_device * pnd, const uint8_t last_cmd_byte) +uint32_t __pn53x_get_timer(nfc_device *pnd, const uint8_t last_cmd_byte) { uint8_t parity; uint8_t counter_hi, counter_lo; @@ -1255,8 +1255,8 @@ uint32_t __pn53x_get_timer(nfc_device * pnd, const uint8_t last_cmd_byte) } bool -pn53x_initiator_transceive_bits_timed (nfc_device * pnd, const uint8_t * pbtTx, const size_t szTxBits, - const uint8_t * pbtTxPar, uint8_t * pbtRx, size_t * pszRxBits, uint8_t * pbtRxPar, uint32_t * cycles) +pn53x_initiator_transceive_bits_timed (nfc_device *pnd, const uint8_t *pbtTx, const size_t szTxBits, + const uint8_t *pbtTxPar, uint8_t *pbtRx, size_t *pszRxBits, uint8_t *pbtRxPar, uint32_t *cycles) { // TODO Do something with these bytes... (void) pbtTxPar; @@ -1357,8 +1357,8 @@ pn53x_initiator_transceive_bits_timed (nfc_device * pnd, const uint8_t * pbtTx, } bool -pn53x_initiator_transceive_bytes_timed (nfc_device * pnd, const uint8_t * pbtTx, const size_t szTx, uint8_t * pbtRx, - size_t * pszRx, uint32_t * cycles) +pn53x_initiator_transceive_bytes_timed (nfc_device *pnd, const uint8_t *pbtTx, const size_t szTx, uint8_t *pbtRx, + size_t *pszRx, uint32_t *cycles) { uint16_t i; uint8_t sz; @@ -1460,7 +1460,7 @@ pn53x_initiator_transceive_bytes_timed (nfc_device * pnd, const uint8_t * pbtTx, } bool -pn53x_initiator_deselect_target (nfc_device * pnd) +pn53x_initiator_deselect_target (nfc_device *pnd) { return (pn53x_InDeselect (pnd, 0)); // 0 mean deselect all selected targets } @@ -1468,7 +1468,7 @@ pn53x_initiator_deselect_target (nfc_device * pnd) #define SAK_ISO14443_4_COMPLIANT 0x20 #define SAK_ISO18092_COMPLIANT 0x40 bool -pn53x_target_init (nfc_device * pnd, nfc_target * pnt, uint8_t * pbtRx, size_t * pszRx) +pn53x_target_init (nfc_device *pnd, nfc_target *pnt, uint8_t *pbtRx, size_t *pszRx) { pn53x_reset_settings(pnd); @@ -1689,7 +1689,7 @@ pn53x_target_init (nfc_device * pnd, nfc_target * pnt, uint8_t * pbtRx, size_t * } bool -pn53x_target_receive_bits (nfc_device * pnd, uint8_t * pbtRx, size_t * pszRxBits, uint8_t * pbtRxPar) +pn53x_target_receive_bits (nfc_device *pnd, uint8_t *pbtRx, size_t *pszRxBits, uint8_t *pbtRxPar) { uint8_t abtCmd[] = { TgGetInitiatorCommand }; @@ -1724,7 +1724,7 @@ pn53x_target_receive_bits (nfc_device * pnd, uint8_t * pbtRx, size_t * pszRxBits } bool -pn53x_target_receive_bytes (nfc_device * pnd, uint8_t * pbtRx, size_t * pszRx, int timeout) +pn53x_target_receive_bytes (nfc_device *pnd, uint8_t *pbtRx, size_t *pszRx, int timeout) { uint8_t abtCmd[1]; @@ -1773,7 +1773,7 @@ pn53x_target_receive_bytes (nfc_device * pnd, uint8_t * pbtRx, size_t * pszRx, i } bool -pn53x_target_send_bits (nfc_device * pnd, const uint8_t * pbtTx, const size_t szTxBits, const uint8_t * pbtTxPar) +pn53x_target_send_bits (nfc_device *pnd, const uint8_t *pbtTx, const size_t szTxBits, const uint8_t *pbtTxPar) { size_t szFrameBits = 0; size_t szFrameBytes = 0; @@ -1811,7 +1811,7 @@ pn53x_target_send_bits (nfc_device * pnd, const uint8_t * pbtTx, const size_t sz } bool -pn53x_target_send_bytes (nfc_device * pnd, const uint8_t * pbtTx, const size_t szTx, int timeout) +pn53x_target_send_bytes (nfc_device *pnd, const uint8_t *pbtTx, const size_t szTx, int timeout) { uint8_t abtCmd[PN53x_EXTENDED_FRAME__DATA_MAX_LEN]; @@ -1910,7 +1910,7 @@ static struct sErrorMessage { }; const char * -pn53x_strerror (const nfc_device * pnd) +pn53x_strerror (const nfc_device *pnd) { const char *pcRes = "Unknown error"; size_t i; @@ -1926,14 +1926,14 @@ pn53x_strerror (const nfc_device * pnd) } bool -pn53x_RFConfiguration__RF_field (nfc_device * pnd, bool bEnable) +pn53x_RFConfiguration__RF_field (nfc_device *pnd, bool bEnable) { uint8_t abtCmd[] = { RFConfiguration, RFCI_FIELD, (bEnable) ? 0x01 : 0x00 }; return pn53x_transceive (pnd, abtCmd, sizeof (abtCmd), NULL, NULL, 0); } bool -pn53x_RFConfiguration__Various_timings (nfc_device * pnd, const uint8_t fATR_RES_Timeout, const uint8_t fRetryTimeout) +pn53x_RFConfiguration__Various_timings (nfc_device *pnd, const uint8_t fATR_RES_Timeout, const uint8_t fRetryTimeout) { uint8_t abtCmd[] = { RFConfiguration, @@ -1946,7 +1946,7 @@ pn53x_RFConfiguration__Various_timings (nfc_device * pnd, const uint8_t fATR_RES } bool -pn53x_RFConfiguration__MaxRtyCOM (nfc_device * pnd, const uint8_t MaxRtyCOM) +pn53x_RFConfiguration__MaxRtyCOM (nfc_device *pnd, const uint8_t MaxRtyCOM) { uint8_t abtCmd[] = { RFConfiguration, @@ -1957,7 +1957,7 @@ pn53x_RFConfiguration__MaxRtyCOM (nfc_device * pnd, const uint8_t MaxRtyCOM) } bool -pn53x_RFConfiguration__MaxRetries (nfc_device * pnd, const uint8_t MxRtyATR, const uint8_t MxRtyPSL, const uint8_t MxRtyPassiveActivation) +pn53x_RFConfiguration__MaxRetries (nfc_device *pnd, const uint8_t MxRtyATR, const uint8_t MxRtyPSL, const uint8_t MxRtyPassiveActivation) { // Retry format: 0x00 means only 1 try, 0xff means infinite uint8_t abtCmd[] = { @@ -1971,7 +1971,7 @@ pn53x_RFConfiguration__MaxRetries (nfc_device * pnd, const uint8_t MxRtyATR, con } bool -pn53x_SetParameters (nfc_device * pnd, const uint8_t ui8Value) +pn53x_SetParameters (nfc_device *pnd, const uint8_t ui8Value) { uint8_t abtCmd[] = { SetParameters, ui8Value }; @@ -1984,7 +1984,7 @@ pn53x_SetParameters (nfc_device * pnd, const uint8_t ui8Value) } bool -pn53x_SAMConfiguration (nfc_device * pnd, const pn532_sam_mode ui8Mode, int timeout) +pn53x_SAMConfiguration (nfc_device *pnd, const pn532_sam_mode ui8Mode, int timeout) { uint8_t abtCmd[] = { SAMConfiguration, ui8Mode, 0x00, 0x00 }; size_t szCmd = sizeof(abtCmd); @@ -2013,7 +2013,7 @@ pn53x_SAMConfiguration (nfc_device * pnd, const pn532_sam_mode ui8Mode, int time } bool -pn53x_PowerDown (nfc_device * pnd) +pn53x_PowerDown (nfc_device *pnd) { uint8_t abtCmd[] = { PowerDown, 0xf0 }; return (pn53x_transceive (pnd, abtCmd, sizeof (abtCmd), NULL, NULL, 0)); @@ -2034,10 +2034,10 @@ pn53x_PowerDown (nfc_device * pnd) * @note To decode theses TargetData[n], there is @fn pn53x_decode_target_data */ bool -pn53x_InListPassiveTarget (nfc_device * pnd, +pn53x_InListPassiveTarget (nfc_device *pnd, const pn53x_modulation pmInitModulation, const uint8_t szMaxTargets, - const uint8_t * pbtInitiatorData, const size_t szInitiatorData, - uint8_t * pbtTargetsData, size_t * pszTargetsData, + const uint8_t *pbtInitiatorData, const size_t szInitiatorData, + uint8_t *pbtTargetsData, size_t *pszTargetsData, int timeout) { uint8_t abtCmd[15] = { InListPassiveTarget }; @@ -2087,7 +2087,7 @@ pn53x_InListPassiveTarget (nfc_device * pnd, } bool -pn53x_InDeselect (nfc_device * pnd, const uint8_t ui8Target) +pn53x_InDeselect (nfc_device *pnd, const uint8_t ui8Target) { if (CHIP_DATA(pnd)->type == RCS360) { // We should do act here *only* if a target was previously selected @@ -2109,7 +2109,7 @@ pn53x_InDeselect (nfc_device * pnd, const uint8_t ui8Target) } bool -pn53x_InRelease (nfc_device * pnd, const uint8_t ui8Target) +pn53x_InRelease (nfc_device *pnd, const uint8_t ui8Target) { if (CHIP_DATA(pnd)->type == RCS360) { // We should do act here *only* if a target was previously selected @@ -2131,9 +2131,9 @@ pn53x_InRelease (nfc_device * pnd, const uint8_t ui8Target) } bool -pn53x_InAutoPoll (nfc_device * pnd, - const pn53x_target_type * ppttTargetTypes, const size_t szTargetTypes, - const uint8_t btPollNr, const uint8_t btPeriod, nfc_target * pntTargets, size_t * pszTargetFound) +pn53x_InAutoPoll (nfc_device *pnd, + const pn53x_target_type *ppttTargetTypes, const size_t szTargetTypes, + const uint8_t btPollNr, const uint8_t btPeriod, nfc_target * pntTargets, size_t *pszTargetFound) { if (CHIP_DATA(pnd)->type != PN532) { // This function is not supported by pn531 neither pn533 @@ -2193,13 +2193,13 @@ pn53x_InAutoPoll (nfc_device * pnd, * @param[out] pnt \a nfc_target which will be filled by this function */ bool -pn53x_InJumpForDEP (nfc_device * pnd, +pn53x_InJumpForDEP (nfc_device *pnd, const nfc_dep_mode ndm, const nfc_baud_rate nbr, - const uint8_t * pbtPassiveInitiatorData, - const uint8_t * pbtNFCID3i, - const uint8_t * pbtGBi, const size_t szGBi, - nfc_target * pnt) + const uint8_t *pbtPassiveInitiatorData, + const uint8_t *pbtNFCID3i, + const uint8_t *pbtGBi, const size_t szGBi, + nfc_target *pnt) { // Max frame size = 1 (Command) + 1 (ActPass) + 1 (Baud rate) + 1 (Next) + 5 (PassiveInitiatorData) + 10 (NFCID3) + 48 (General bytes) = 67 bytes uint8_t abtCmd[67] = { InJumpForDEP, (ndm == NDM_ACTIVE) ? 0x01 : 0x00 }; @@ -2287,12 +2287,12 @@ pn53x_InJumpForDEP (nfc_device * pnd, } bool -pn53x_TgInitAsTarget (nfc_device * pnd, pn53x_target_mode ptm, - const uint8_t * pbtMifareParams, - const uint8_t * pbtTkt, size_t szTkt, - const uint8_t * pbtFeliCaParams, - const uint8_t * pbtNFCID3t, const uint8_t * pbtGBt, const size_t szGBt, - uint8_t * pbtRx, size_t * pszRx, uint8_t * pbtModeByte) +pn53x_TgInitAsTarget (nfc_device *pnd, pn53x_target_mode ptm, + const uint8_t *pbtMifareParams, + const uint8_t *pbtTkt, size_t szTkt, + const uint8_t *pbtFeliCaParams, + const uint8_t *pbtNFCID3t, const uint8_t *pbtGBt, const size_t szGBt, + uint8_t *pbtRx, size_t * pszRx, uint8_t *pbtModeByte) { uint8_t abtCmd[39 + 47 + 48] = { TgInitAsTarget }; // Worst case: 39-byte base, 47 bytes max. for General Bytes, 48 bytes max. for Historical Bytes size_t szOptionalBytes = 0; @@ -2358,7 +2358,7 @@ pn53x_TgInitAsTarget (nfc_device * pnd, pn53x_target_mode ptm, } bool -pn53x_check_ack_frame (nfc_device * pnd, const uint8_t * pbtRxFrame, const size_t szRxFrameLen) +pn53x_check_ack_frame (nfc_device *pnd, const uint8_t *pbtRxFrame, const size_t szRxFrameLen) { if (szRxFrameLen >= sizeof (pn53x_ack_frame)) { if (0 == memcmp (pbtRxFrame, pn53x_ack_frame, sizeof (pn53x_ack_frame))) { @@ -2372,7 +2372,7 @@ pn53x_check_ack_frame (nfc_device * pnd, const uint8_t * pbtRxFrame, const size_ } bool -pn53x_check_error_frame (nfc_device * pnd, const uint8_t * pbtRxFrame, const size_t szRxFrameLen) +pn53x_check_error_frame (nfc_device *pnd, const uint8_t *pbtRxFrame, const size_t szRxFrameLen) { if (szRxFrameLen >= sizeof (pn53x_error_frame)) { if (0 == memcmp (pbtRxFrame, pn53x_error_frame, sizeof (pn53x_error_frame))) { @@ -2391,7 +2391,7 @@ pn53x_check_error_frame (nfc_device * pnd, const uint8_t * pbtRxFrame, const siz * @note The first byte of pbtData is the Command Code (CC) */ bool -pn53x_build_frame (uint8_t * pbtFrame, size_t * pszFrame, const uint8_t * pbtData, const size_t szData) +pn53x_build_frame (uint8_t *pbtFrame, size_t *pszFrame, const uint8_t *pbtData, const size_t szData) { if (szData <= PN53x_NORMAL_FRAME__DATA_MAX_LEN) { // LEN - Packet length = data length (len) + checksum (1) + end of stream marker (1) @@ -2607,7 +2607,7 @@ pn53x_nm_to_ptt(const nfc_modulation nm) } void -pn53x_data_new (nfc_device * pnd, const struct pn53x_io* io) +pn53x_data_new (nfc_device *pnd, const struct pn53x_io* io) { pnd->chip_data = malloc(sizeof(struct pn53x_data)); @@ -2633,7 +2633,7 @@ pn53x_data_new (nfc_device * pnd, const struct pn53x_io* io) } void -pn53x_data_free (nfc_device * pnd) +pn53x_data_free (nfc_device *pnd) { if (CHIP_DATA (pnd)->current_target) { free (CHIP_DATA (pnd)->current_target); diff --git a/libnfc/nfc.c b/libnfc/nfc.c index cb49bd0..8c4c627 100644 --- a/libnfc/nfc.c +++ b/libnfc/nfc.c @@ -78,7 +78,7 @@ const struct nfc_driver_t *nfc_drivers[] = { bool nfc_get_default_device (nfc_connstring *connstring) { - char * env_default_connstring = getenv ("LIBNFC_DEFAULT_DEVICE"); + char *env_default_connstring = getenv ("LIBNFC_DEFAULT_DEVICE"); if (NULL == env_default_connstring) { // LIBNFC_DEFAULT_DEVICE is not set, we fallback on probing for the first available device size_t szDeviceFound; @@ -163,7 +163,7 @@ nfc_connect (const nfc_connstring connstring) * Initiator's selected tag is disconnected and the device, including allocated \a nfc_device struct, is released. */ void -nfc_disconnect (nfc_device * pnd) +nfc_disconnect (nfc_device *pnd) { if (pnd) { // Go in idle mode @@ -182,7 +182,7 @@ nfc_disconnect (nfc_device * pnd) * @param[out] pszDeviceFound number of devices found. */ void -nfc_list_devices (nfc_connstring connstrings[] , size_t szDevices, size_t * pszDeviceFound) +nfc_list_devices (nfc_connstring connstrings[] , size_t szDevices, size_t *pszDeviceFound) { size_t szN; *pszDeviceFound = 0; @@ -216,7 +216,7 @@ nfc_list_devices (nfc_connstring connstrings[] , size_t szDevices, size_t * pszD * accept). */ bool -nfc_configure (nfc_device * pnd, const nfc_device_option ndo, const bool bEnable) +nfc_configure (nfc_device *pnd, const nfc_device_option ndo, const bool bEnable) { HAL (configure, pnd, ndo, bEnable); } @@ -242,7 +242,7 @@ nfc_configure (nfc_device * pnd, const nfc_device_option ndo, const bool bEnable * - RF field is shortly dropped (if it was enabled) then activated again */ bool -nfc_initiator_init (nfc_device * pnd) +nfc_initiator_init (nfc_device *pnd) { // Drop the field for a while if (!nfc_configure (pnd, NDO_ACTIVATE_FIELD, false)) @@ -304,10 +304,10 @@ nfc_initiator_init (nfc_device * pnd) * the initial modulation and speed (106, 212 or 424 kbps) should be supplied. */ bool -nfc_initiator_select_passive_target (nfc_device * pnd, +nfc_initiator_select_passive_target (nfc_device *pnd, const nfc_modulation nm, - const uint8_t * pbtInitData, const size_t szInitData, - nfc_target * pnt) + const uint8_t *pbtInitData, const size_t szInitData, + nfc_target *pnt) { uint8_t abtInit[MAX(12, szInitData)]; size_t szInit; @@ -344,9 +344,9 @@ nfc_initiator_select_passive_target (nfc_device * pnd, * should be supplied. */ bool -nfc_initiator_list_passive_targets (nfc_device * pnd, +nfc_initiator_list_passive_targets (nfc_device *pnd, const nfc_modulation nm, - nfc_target ant[], const size_t szTargets, size_t * pszTargetFound) + nfc_target ant[], const size_t szTargets, size_t *pszTargetFound) { nfc_target nt; size_t szTargetFound = 0; @@ -405,10 +405,10 @@ nfc_initiator_list_passive_targets (nfc_device * pnd, * @param[out] pnt pointer on \a nfc_target (over)writable struct */ bool -nfc_initiator_poll_target (nfc_device * pnd, - const nfc_modulation * pnmModulations, const size_t szModulations, +nfc_initiator_poll_target (nfc_device *pnd, + const nfc_modulation *pnmModulations, const size_t szModulations, const uint8_t uiPollNr, const uint8_t uiPeriod, - nfc_target * pnt) + nfc_target *pnt) { HAL (initiator_poll_target, pnd, pnmModulations, szModulations, uiPollNr, uiPeriod, pnt); } @@ -430,9 +430,9 @@ nfc_initiator_poll_target (nfc_device * pnd, * @note \a nfc_dep_info will be returned when the target was acquired successfully. */ bool -nfc_initiator_select_dep_target (nfc_device * pnd, +nfc_initiator_select_dep_target (nfc_device *pnd, const nfc_dep_mode ndm, const nfc_baud_rate nbr, - const nfc_dep_info * pndiInitiator, nfc_target * pnt) + const nfc_dep_info *pndiInitiator, nfc_target *pnt) { HAL (initiator_select_dep_target, pnd, ndm, nbr, pndiInitiator, pnt); } @@ -450,7 +450,7 @@ nfc_initiator_select_dep_target (nfc_device * pnd, * the next tag until the correct tag is found. */ bool -nfc_initiator_deselect_target (nfc_device * pnd) +nfc_initiator_deselect_target (nfc_device *pnd) { HAL (initiator_deselect_target, pnd); } @@ -479,8 +479,8 @@ nfc_initiator_deselect_target (nfc_device * pnd) * @warning The configuration option \a NDO_HANDLE_PARITY must be set to \c true (the default value). */ bool -nfc_initiator_transceive_bytes (nfc_device * pnd, const uint8_t * pbtTx, const size_t szTx, uint8_t * pbtRx, - size_t * pszRx, int timeout) +nfc_initiator_transceive_bytes (nfc_device *pnd, const uint8_t *pbtTx, const size_t szTx, uint8_t *pbtRx, + size_t *pszRx, int timeout) { HAL (initiator_transceive_bytes, pnd, pbtTx, szTx, pbtRx, pszRx, timeout) } @@ -521,8 +521,8 @@ nfc_initiator_transceive_bytes (nfc_device * pnd, const uint8_t * pbtTx, const s * CRC bytes. Using this feature you are able to simulate these frames. */ bool -nfc_initiator_transceive_bits (nfc_device * pnd, const uint8_t * pbtTx, const size_t szTxBits, const uint8_t * pbtTxPar, - uint8_t * pbtRx, size_t * pszRxBits, uint8_t * pbtRxPar) +nfc_initiator_transceive_bits (nfc_device *pnd, const uint8_t *pbtTx, const size_t szTxBits, const uint8_t *pbtTxPar, + uint8_t *pbtRx, size_t *pszRxBits, uint8_t *pbtRxPar) { HAL (initiator_transceive_bits, pnd, pbtTx, szTxBits, pbtTxPar, pbtRx, pszRxBits, pbtRxPar); } @@ -548,8 +548,8 @@ nfc_initiator_transceive_bits (nfc_device * pnd, const uint8_t * pbtTx, const si * @warning The configuration option \a NDO_HANDLE_PARITY must be set to \c true (the default value). */ bool -nfc_initiator_transceive_bytes_timed (nfc_device * pnd, const uint8_t * pbtTx, const size_t szTx, uint8_t * pbtRx, - size_t * pszRx, uint32_t * cycles) +nfc_initiator_transceive_bytes_timed (nfc_device *pnd, const uint8_t *pbtTx, const size_t szTx, uint8_t *pbtRx, + size_t *pszRx, uint32_t *cycles) { HAL (initiator_transceive_bytes_timed, pnd, pbtTx, szTx, pbtRx, pszRx, cycles) } @@ -576,8 +576,8 @@ nfc_initiator_transceive_bytes_timed (nfc_device * pnd, const uint8_t * pbtTx, c * @warning The configuration option \a NDO_HANDLE_PARITY must be set to \c true (the default value). */ bool -nfc_initiator_transceive_bits_timed (nfc_device * pnd, const uint8_t * pbtTx, const size_t szTxBits, const uint8_t * pbtTxPar, - uint8_t * pbtRx, size_t * pszRxBits, uint8_t * pbtRxPar, uint32_t * cycles) +nfc_initiator_transceive_bits_timed (nfc_device *pnd, const uint8_t *pbtTx, const size_t szTxBits, const uint8_t *pbtTxPar, + uint8_t *pbtRx, size_t *pszRxBits, uint8_t *pbtRxPar, uint32_t *cycles) { HAL (initiator_transceive_bits_timed, pnd, pbtTx, szTxBits, pbtTxPar, pbtRx, pszRxBits, pbtRxPar, cycles); } @@ -613,7 +613,7 @@ nfc_initiator_transceive_bits_timed (nfc_device * pnd, const uint8_t * pbtTx, co * receive functions can be used. */ bool -nfc_target_init (nfc_device * pnd, nfc_target * pnt, uint8_t * pbtRx, size_t * pszRx) +nfc_target_init (nfc_device *pnd, nfc_target *pnt, uint8_t *pbtRx, size_t * pszRx) { // Disallow invalid frame if (!nfc_configure (pnd, NDO_ACCEPT_INVALID_FRAMES, false)) @@ -653,7 +653,7 @@ nfc_target_init (nfc_device * pnd, nfc_target * pnt, uint8_t * pbtRx, size_t * p * In target mode, the emulation is stoped (no target available from external initiator) and the device is set to low power mode (if avaible). */ bool -nfc_idle (nfc_device * pnd) +nfc_idle (nfc_device *pnd) { HAL (idle, pnd); } @@ -670,7 +670,7 @@ nfc_idle (nfc_device * pnd) * @note The blocking function (ie. nfc_target_init()) will failed with DEABORT error. */ bool -nfc_abort_command (nfc_device * pnd) +nfc_abort_command (nfc_device *pnd) { HAL (abort_command, pnd); } @@ -691,7 +691,7 @@ nfc_abort_command (nfc_device * pnd) * If timeout is a null pointer, the function blocks indefinitely (until an error is raised or function is completed). */ bool -nfc_target_send_bytes (nfc_device * pnd, const uint8_t * pbtTx, const size_t szTx, int timeout) +nfc_target_send_bytes (nfc_device *pnd, const uint8_t *pbtTx, const size_t szTx, int timeout) { HAL (target_send_bytes, pnd, pbtTx, szTx, timeout); } @@ -711,7 +711,7 @@ nfc_target_send_bytes (nfc_device * pnd, const uint8_t * pbtTx, const size_t szT * If timeout is a null pointer, the function blocks indefinitely (until an error is raised or function is completed). */ bool -nfc_target_receive_bytes (nfc_device * pnd, uint8_t * pbtRx, size_t * pszRx, int timeout) +nfc_target_receive_bytes (nfc_device *pnd, uint8_t *pbtRx, size_t *pszRx, int timeout) { HAL (target_receive_bytes, pnd, pbtRx, pszRx, timeout); } @@ -724,7 +724,7 @@ nfc_target_receive_bytes (nfc_device * pnd, uint8_t * pbtRx, size_t * pszRx, int * using the specified NFC device (configured as \e target). */ bool -nfc_target_send_bits (nfc_device * pnd, const uint8_t * pbtTx, const size_t szTxBits, const uint8_t * pbtTxPar) +nfc_target_send_bits (nfc_device *pnd, const uint8_t *pbtTx, const size_t szTxBits, const uint8_t *pbtTxPar) { HAL (target_send_bits, pnd, pbtTx, szTxBits, pbtTxPar); } @@ -741,7 +741,7 @@ nfc_target_send_bits (nfc_device * pnd, const uint8_t * pbtTx, const size_t szTx * frames. */ bool -nfc_target_receive_bits (nfc_device * pnd, uint8_t * pbtRx, size_t * pszRxBits, uint8_t * pbtRxPar) +nfc_target_receive_bits (nfc_device *pnd, uint8_t *pbtRx, size_t *pszRxBits, uint8_t *pbtRxPar) { HAL (target_receive_bits, pnd, pbtRx, pszRxBits, pbtRxPar); } @@ -751,7 +751,7 @@ nfc_target_receive_bits (nfc_device * pnd, uint8_t * pbtRx, size_t * pszRxBits, * @return Returns a string */ const char * -nfc_strerror (const nfc_device * pnd) +nfc_strerror (const nfc_device *pnd) { return pnd->driver->strerror (pnd); } @@ -761,7 +761,7 @@ nfc_strerror (const nfc_device * pnd) * @return Returns 0 upon success */ int -nfc_strerror_r (const nfc_device * pnd, char *pcStrErrBuf, size_t szBufLen) +nfc_strerror_r (const nfc_device *pnd, char *pcStrErrBuf, size_t szBufLen) { return (snprintf (pcStrErrBuf, szBufLen, "%s", nfc_strerror (pnd)) < 0) ? -1 : 0; } @@ -770,7 +770,7 @@ nfc_strerror_r (const nfc_device * pnd, char *pcStrErrBuf, size_t szBufLen) * @brief Display the PCD error a-la perror */ void -nfc_perror (const nfc_device * pnd, const char *pcString) +nfc_perror (const nfc_device *pnd, const char *pcString) { fprintf (stderr, "%s: %s\n", pcString, nfc_strerror (pnd)); } @@ -782,7 +782,7 @@ nfc_perror (const nfc_device * pnd, const char *pcString) * @return Returns a string with the device name */ const char * -nfc_device_name (nfc_device * pnd) +nfc_device_name (nfc_device *pnd) { return pnd->acName; } From 52bc5853d85fd2c511b8de5ebb7c81a63ed9899d Mon Sep 17 00:00:00 2001 From: Audrey Diacre Date: Fri, 25 Nov 2011 15:21:10 +0000 Subject: [PATCH 011/113] Remove whitespace after star symbol for pointers --- examples/nfc-anticol.c | 4 +- examples/nfc-emulate-tag.c | 4 +- examples/pn53x-tamashell.c | 8 +- include/nfc/nfc-types.h | 4 +- libnfc/buses/uart.h | 4 +- libnfc/buses/uart_posix.c | 4 +- libnfc/buses/uart_win32.c | 2 +- libnfc/chips/pn53x-internal.h | 6 +- libnfc/chips/pn53x.c | 16 ++-- libnfc/chips/pn53x.h | 154 +++++++++++++++++----------------- libnfc/drivers/acr122.c | 8 +- libnfc/drivers/acr122.h | 8 +- libnfc/drivers/arygon.c | 24 +++--- libnfc/drivers/arygon.h | 8 +- libnfc/drivers/pn532_uart.c | 22 ++--- libnfc/drivers/pn532_uart.h | 8 +- libnfc/drivers/pn53x_usb.c | 16 ++-- libnfc/drivers/pn53x_usb.h | 8 +- libnfc/iso14443-subr.c | 8 +- libnfc/mirror-subr.c | 2 +- libnfc/mirror-subr.h | 2 +- libnfc/nfc-emulation.c | 2 +- libnfc/nfc-internal.h | 38 ++++----- utils/mifare.c | 2 +- utils/mifare.h | 2 +- utils/nfc-mfclassic.c | 6 +- utils/nfc-mfsetuid.c | 4 +- utils/nfc-read-forum-tag3.c | 4 +- utils/nfc-relay-picc.c | 4 +- utils/nfc-utils.c | 8 +- utils/nfc-utils.h | 8 +- 31 files changed, 199 insertions(+), 199 deletions(-) diff --git a/examples/nfc-anticol.c b/examples/nfc-anticol.c index 7077949..710dd6a 100644 --- a/examples/nfc-anticol.c +++ b/examples/nfc-anticol.c @@ -75,7 +75,7 @@ uint8_t abtHalt[4] = { 0x50, 0x00, 0x00, 0x00 }; #define CASCADE_BIT 0x04 static bool -transmit_bits (const uint8_t * pbtTx, const size_t szTxBits) +transmit_bits (const uint8_t *pbtTx, const size_t szTxBits) { // Show transmitted command if (!quiet_output) { @@ -97,7 +97,7 @@ transmit_bits (const uint8_t * pbtTx, const size_t szTxBits) static bool -transmit_bytes (const uint8_t * pbtTx, const size_t szTx) +transmit_bytes (const uint8_t *pbtTx, const size_t szTx) { // Show transmitted command if (!quiet_output) { diff --git a/examples/nfc-emulate-tag.c b/examples/nfc-emulate-tag.c index e07f645..62dcd32 100644 --- a/examples/nfc-emulate-tag.c +++ b/examples/nfc-emulate-tag.c @@ -71,7 +71,7 @@ intr_hdlr (void) } bool -target_io( nfc_target * pnt, const uint8_t * pbtInput, const size_t szInput, uint8_t * pbtOutput, size_t *pszOutput ) +target_io( nfc_target *pnt, const uint8_t *pbtInput, const size_t szInput, uint8_t *pbtOutput, size_t *pszOutput ) { bool loop = true; *pszOutput = 0; @@ -134,7 +134,7 @@ target_io( nfc_target * pnt, const uint8_t * pbtInput, const size_t szInput, uin } bool -nfc_target_emulate_tag(nfc_device* pnd, nfc_target * pnt) +nfc_target_emulate_tag(nfc_device *pnd, nfc_target *pnt) { size_t szTx; uint8_t abtTx[MAX_FRAME_LEN]; diff --git a/examples/pn53x-tamashell.c b/examples/pn53x-tamashell.c index b92bc6e..8fe5c61 100644 --- a/examples/pn53x-tamashell.c +++ b/examples/pn53x-tamashell.c @@ -71,7 +71,7 @@ int main(int argc, const char* argv[]) { - nfc_device* pnd; + nfc_device *pnd; uint8_t abtRx[MAX_FRAME_LEN]; uint8_t abtTx[MAX_FRAME_LEN]; size_t szRx = sizeof(abtRx); @@ -97,8 +97,8 @@ int main(int argc, const char* argv[]) printf ("Connected to NFC reader: %s\n", pnd->acName); nfc_initiator_init(pnd); - char * cmd; - char * prompt="> "; + char *cmd; + char *prompt = "> "; while(1) { int offset=0; #if defined(HAVE_READLINE) @@ -113,7 +113,7 @@ int main(int argc, const char* argv[]) } else { #endif //HAVE_READLINE size_t n = 255; - char * ret = NULL; + char *ret = NULL; cmd = malloc(n); printf("%s", prompt); fflush(0); diff --git a/include/nfc/nfc-types.h b/include/nfc/nfc-types.h index 3ada1b0..28f4a21 100644 --- a/include/nfc/nfc-types.h +++ b/include/nfc/nfc-types.h @@ -42,8 +42,8 @@ typedef struct { /** Driver's functions for handling device specific wrapping */ const struct nfc_driver_t *driver; - void* driver_data; - void* chip_data; + void *driver_data; + void *chip_data; /** Device name string, including device wrapper firmware */ char acName[DEVICE_NAME_LENGTH]; diff --git a/libnfc/buses/uart.h b/libnfc/buses/uart.h index 77b5658..728d998 100644 --- a/libnfc/buses/uart.h +++ b/libnfc/buses/uart.h @@ -49,8 +49,8 @@ void uart_flush_input (const serial_port sp); void uart_set_speed (serial_port sp, const uint32_t uiPortSpeed); uint32_t uart_get_speed (const serial_port sp); -int uart_receive (serial_port sp, uint8_t * pbtRx, const size_t szRx, void * abort_p, int timeout); -int uart_send (serial_port sp, const uint8_t * pbtTx, const size_t szTx, int timeout); +int uart_receive (serial_port sp, uint8_t *pbtRx, const size_t szRx, void *abort_p, int timeout); +int uart_send (serial_port sp, const uint8_t *pbtTx, const size_t szTx, int timeout); char **uart_list_ports (void); diff --git a/libnfc/buses/uart_posix.c b/libnfc/buses/uart_posix.c index 8017384..f427cbe 100644 --- a/libnfc/buses/uart_posix.c +++ b/libnfc/buses/uart_posix.c @@ -249,7 +249,7 @@ uart_close (const serial_port sp) * @return 0 on success, otherwise driver error code */ int -uart_receive (serial_port sp, uint8_t * pbtRx, const size_t szRx, void * abort_p, int timeout) +uart_receive (serial_port sp, uint8_t *pbtRx, const size_t szRx, void *abort_p, int timeout) { int iAbortFd = abort_p ? *((int*)abort_p) : 0; int received_bytes_count = 0; @@ -323,7 +323,7 @@ select: * @return 0 on success, otherwise a driver error is returned */ int -uart_send (serial_port sp, const uint8_t * pbtTx, const size_t szTx, int timeout) +uart_send (serial_port sp, const uint8_t *pbtTx, const size_t szTx, int timeout) { (void) timeout; LOG_HEX ("TX", pbtTx, szTx); diff --git a/libnfc/buses/uart_win32.c b/libnfc/buses/uart_win32.c index 3e9c951..8ae25ef 100644 --- a/libnfc/buses/uart_win32.c +++ b/libnfc/buses/uart_win32.c @@ -238,7 +238,7 @@ BOOL is_port_available(int nPort) char ** uart_list_ports (void) { - char ** availablePorts = malloc((1 + MAX_SERIAL_PORT_WIN) * sizeof(char*)); + char **availablePorts = malloc((1 + MAX_SERIAL_PORT_WIN) * sizeof(char*)); int curIndex = 0; int i; for (i = 1; i <= MAX_SERIAL_PORT_WIN; i++) { diff --git a/libnfc/chips/pn53x-internal.h b/libnfc/chips/pn53x-internal.h index b133d38..ceb8a80 100644 --- a/libnfc/chips/pn53x-internal.h +++ b/libnfc/chips/pn53x-internal.h @@ -115,7 +115,7 @@ typedef struct { uint8_t ui8Code; uint8_t ui8CompatFlags; #ifdef LOGGING - const char * abtCommandText; + const char *abtCommandText; #endif } pn53x_command; @@ -203,8 +203,8 @@ static const pn53x_command pn53x_commands[] = { #ifdef LOGGING typedef struct { uint16_t ui16Address; - const char * abtRegisterText; - const char * abtRegisterDescription; + const char *abtRegisterText; + const char *abtRegisterDescription; } pn53x_register; # define PNREG( X, Y ) { X , #X, Y } diff --git a/libnfc/chips/pn53x.c b/libnfc/chips/pn53x.c index 95b337c..46b873c 100644 --- a/libnfc/chips/pn53x.c +++ b/libnfc/chips/pn53x.c @@ -1447,7 +1447,7 @@ pn53x_initiator_transceive_bytes_timed (nfc_device *pnd, const uint8_t *pbtTx, c // Recv corrected timer value if (pnd->bCrc) { // We've to compute CRC ourselves to know last byte actually sent - uint8_t * pbtTxRaw; + uint8_t *pbtTxRaw; pbtTxRaw = (uint8_t *) malloc(szTx+2); memcpy (pbtTxRaw, pbtTx, szTx); iso14443a_crc_append (pbtTxRaw, szTx); @@ -1519,15 +1519,15 @@ pn53x_target_init (nfc_device *pnd, nfc_target *pnt, uint8_t *pbtRx, size_t *psz return false; uint8_t abtMifareParams[6]; - uint8_t * pbtMifareParams = NULL; - uint8_t * pbtTkt = NULL; + uint8_t *pbtMifareParams = NULL; + uint8_t *pbtTkt = NULL; size_t szTkt = 0; uint8_t abtFeliCaParams[18]; - uint8_t * pbtFeliCaParams = NULL; + uint8_t *pbtFeliCaParams = NULL; - const uint8_t * pbtNFCID3t = NULL; - const uint8_t * pbtGBt = NULL; + const uint8_t *pbtNFCID3t = NULL; + const uint8_t *pbtGBt = NULL; size_t szGBt = 0; switch(pnt->nm.nmt) { @@ -2292,7 +2292,7 @@ pn53x_TgInitAsTarget (nfc_device *pnd, pn53x_target_mode ptm, const uint8_t *pbtTkt, size_t szTkt, const uint8_t *pbtFeliCaParams, const uint8_t *pbtNFCID3t, const uint8_t *pbtGBt, const size_t szGBt, - uint8_t *pbtRx, size_t * pszRx, uint8_t *pbtModeByte) + uint8_t *pbtRx, size_t *pszRx, uint8_t *pbtModeByte) { uint8_t abtCmd[39 + 47 + 48] = { TgInitAsTarget }; // Worst case: 39-byte base, 47 bytes max. for General Bytes, 48 bytes max. for Historical Bytes size_t szOptionalBytes = 0; @@ -2607,7 +2607,7 @@ pn53x_nm_to_ptt(const nfc_modulation nm) } void -pn53x_data_new (nfc_device *pnd, const struct pn53x_io* io) +pn53x_data_new (nfc_device *pnd, const struct pn53x_io *io) { pnd->chip_data = malloc(sizeof(struct pn53x_data)); diff --git a/libnfc/chips/pn53x.h b/libnfc/chips/pn53x.h index 5e57c3c..431dd42 100644 --- a/libnfc/chips/pn53x.h +++ b/libnfc/chips/pn53x.h @@ -127,8 +127,8 @@ typedef enum { } pn53x_operating_mode; struct pn53x_io { - bool (*send)(nfc_device * pnd, const uint8_t * pbtData, const size_t szData, int timeout); - int (*receive)(nfc_device * pnd, uint8_t * pbtData, const size_t szDataLen, int timeout); + bool (*send)(nfc_device *pnd, const uint8_t *pbtData, const size_t szData, int timeout); + int (*receive)(nfc_device *pnd, uint8_t *pbtData, const size_t szDataLen, int timeout); }; /* defines */ @@ -143,9 +143,9 @@ struct pn53x_data { /** Current operating mode */ pn53x_operating_mode operating_mode; /** Current emulated target */ - nfc_target* current_target; + nfc_target *current_target; /** PN53x I/O functions stored in struct */ - const struct pn53x_io * io; + const struct pn53x_io *io; /** Register cache for REG_CIU_BIT_FRAMING, SYMBOL_TX_LAST_BITS: The last TX bits setting, we need to reset this if it does not apply anymore */ uint8_t ui8TxBits; /** Register cache for SetParameters function. */ @@ -255,99 +255,99 @@ typedef enum { extern const uint8_t pn53x_ack_frame[6]; extern const uint8_t pn53x_nack_frame[6]; -bool pn53x_init(nfc_device * pnd); -bool pn53x_transceive (nfc_device * pnd, const uint8_t * pbtTx, const size_t szTx, uint8_t * pbtRx, size_t *pszRx, int timeout); +bool pn53x_init(nfc_device *pnd); +bool pn53x_transceive (nfc_device *pnd, const uint8_t *pbtTx, const size_t szTx, uint8_t *pbtRx, size_t *pszRx, int timeout); -bool pn53x_set_parameters (nfc_device * pnd, const uint8_t ui8Value, const bool bEnable); -bool pn53x_set_tx_bits (nfc_device * pnd, const uint8_t ui8Bits); -bool pn53x_wrap_frame (const uint8_t * pbtTx, const size_t szTxBits, const uint8_t * pbtTxPar, uint8_t * pbtFrame, - size_t * pszFrameBits); -bool pn53x_unwrap_frame (const uint8_t * pbtFrame, const size_t szFrameBits, uint8_t * pbtRx, size_t * pszRxBits, - uint8_t * pbtRxPar); -bool pn53x_decode_target_data (const uint8_t * pbtRawData, size_t szRawData, +bool pn53x_set_parameters (nfc_device *pnd, const uint8_t ui8Value, const bool bEnable); +bool pn53x_set_tx_bits (nfc_device *pnd, const uint8_t ui8Bits); +bool pn53x_wrap_frame (const uint8_t *pbtTx, const size_t szTxBits, const uint8_t *pbtTxPar, uint8_t *pbtFrame, + size_t *pszFrameBits); +bool pn53x_unwrap_frame (const uint8_t *pbtFrame, const size_t szFrameBits, uint8_t *pbtRx, size_t *pszRxBits, + uint8_t *pbtRxPar); +bool pn53x_decode_target_data (const uint8_t *pbtRawData, size_t szRawData, pn53x_type chip_type, nfc_modulationype nmt, - nfc_target_info * pnti); -bool pn53x_read_register (nfc_device * pnd, uint16_t ui16Reg, uint8_t * ui8Value); -bool pn53x_write_register (nfc_device * pnd, uint16_t ui16Reg, uint8_t ui8SymbolMask, uint8_t ui8Value); -bool pn53x_get_firmware_version (nfc_device * pnd, char abtFirmwareText[22]); -bool pn53x_configure (nfc_device * pnd, const nfc_device_option ndo, const bool bEnable); + nfc_target_info *pnti); +bool pn53x_read_register (nfc_device *pnd, uint16_t ui16Reg, uint8_t *ui8Value); +bool pn53x_write_register (nfc_device *pnd, uint16_t ui16Reg, uint8_t ui8SymbolMask, uint8_t ui8Value); +bool pn53x_get_firmware_version (nfc_device *pnd, char abtFirmwareText[22]); +bool pn53x_configure (nfc_device *pnd, const nfc_device_option ndo, const bool bEnable); bool pn53x_check_communication (nfc_device *pnd); -bool pn53x_idle (nfc_device * pnd); +bool pn53x_idle (nfc_device *pnd); // NFC device as Initiator functions -bool pn53x_initiator_init (nfc_device * pnd); -bool pn53x_initiator_select_passive_target (nfc_device * pnd, +bool pn53x_initiator_init (nfc_device *pnd); +bool pn53x_initiator_select_passive_target (nfc_device *pnd, const nfc_modulation nm, - const uint8_t * pbtInitData, const size_t szInitData, - nfc_target * pnt); -bool pn53x_initiator_poll_target (nfc_device * pnd, - const nfc_modulation * pnmModulations, const size_t szModulations, + const uint8_t *pbtInitData, const size_t szInitData, + nfc_target *pnt); +bool pn53x_initiator_poll_target (nfc_device *pnd, + const nfc_modulation *pnmModulations, const size_t szModulations, const uint8_t uiPollNr, const uint8_t uiPeriod, - nfc_target * pnt); -bool pn53x_initiator_select_dep_target (nfc_device * pnd, + nfc_target *pnt); +bool pn53x_initiator_select_dep_target (nfc_device *pnd, const nfc_dep_mode ndm, const nfc_baud_rate nbr, - const nfc_dep_info * pndiInitiator, - nfc_target * pnt); -bool pn53x_initiator_transceive_bits (nfc_device * pnd, const uint8_t * pbtTx, const size_t szTxBits, - const uint8_t * pbtTxPar, uint8_t * pbtRx, size_t * pszRxBits, - uint8_t * pbtRxPar); -bool pn53x_initiator_transceive_bytes (nfc_device * pnd, const uint8_t * pbtTx, const size_t szTx, - uint8_t * pbtRx, size_t * pszRx, int timeout); -bool pn53x_initiator_transceive_bits_timed (nfc_device * pnd, const uint8_t * pbtTx, const size_t szTxBits, - const uint8_t * pbtTxPar, uint8_t * pbtRx, size_t * pszRxBits, - uint8_t * pbtRxPar, uint32_t * cycles); -bool pn53x_initiator_transceive_bytes_timed (nfc_device * pnd, const uint8_t * pbtTx, const size_t szTx, - uint8_t * pbtRx, size_t * pszRx, uint32_t * cycles); -bool pn53x_initiator_deselect_target (nfc_device * pnd); + const nfc_dep_info *pndiInitiator, + nfc_target *pnt); +bool pn53x_initiator_transceive_bits (nfc_device *pnd, const uint8_t *pbtTx, const size_t szTxBits, + const uint8_t *pbtTxPar, uint8_t *pbtRx, size_t *pszRxBits, + uint8_t *pbtRxPar); +bool pn53x_initiator_transceive_bytes (nfc_device *pnd, const uint8_t *pbtTx, const size_t szTx, + uint8_t *pbtRx, size_t *pszRx, int timeout); +bool pn53x_initiator_transceive_bits_timed (nfc_device *pnd, const uint8_t *pbtTx, const size_t szTxBits, + const uint8_t *pbtTxPar, uint8_t *pbtRx, size_t *pszRxBits, + uint8_t *pbtRxPar, uint32_t *cycles); +bool pn53x_initiator_transceive_bytes_timed (nfc_device *pnd, const uint8_t *pbtTx, const size_t szTx, + uint8_t *pbtRx, size_t *pszRx, uint32_t *cycles); +bool pn53x_initiator_deselect_target (nfc_device *pnd); // NFC device as Target functions -bool pn53x_target_init (nfc_device * pnd, nfc_target * pnt, uint8_t * pbtRx, size_t * pszRx); -bool pn53x_target_receive_bits (nfc_device * pnd, uint8_t * pbtRx, size_t * pszRxBits, uint8_t * pbtRxPar); -bool pn53x_target_receive_bytes (nfc_device * pnd, uint8_t * pbtRx, size_t * pszRx, int timeout); -bool pn53x_target_send_bits (nfc_device * pnd, const uint8_t * pbtTx, const size_t szTxBits, const uint8_t * pbtTxPar); -bool pn53x_target_send_bytes (nfc_device * pnd, const uint8_t * pbtTx, const size_t szTx, int timeout); +bool pn53x_target_init (nfc_device *pnd, nfc_target *pnt, uint8_t *pbtRx, size_t *pszRx); +bool pn53x_target_receive_bits (nfc_device *pnd, uint8_t *pbtRx, size_t *pszRxBits, uint8_t *pbtRxPar); +bool pn53x_target_receive_bytes (nfc_device *pnd, uint8_t *pbtRx, size_t *pszRx, int timeout); +bool pn53x_target_send_bits (nfc_device *pnd, const uint8_t *pbtTx, const size_t szTxBits, const uint8_t *pbtTxPar); +bool pn53x_target_send_bytes (nfc_device *pnd, const uint8_t *pbtTx, const size_t szTx, int timeout); // Error handling functions -const char *pn53x_strerror (const nfc_device * pnd); +const char *pn53x_strerror (const nfc_device *pnd); // C wrappers for PN53x commands -bool pn53x_SetParameters (nfc_device * pnd, const uint8_t ui8Value); -bool pn53x_SAMConfiguration (nfc_device * pnd, const pn532_sam_mode mode, int timeout); -bool pn53x_PowerDown (nfc_device * pnd); -bool pn53x_InListPassiveTarget (nfc_device * pnd, const pn53x_modulation pmInitModulation, - const uint8_t szMaxTargets, const uint8_t * pbtInitiatorData, - const size_t szInitiatorDataLen, uint8_t * pbtTargetsData, size_t * pszTargetsData, +bool pn53x_SetParameters (nfc_device *pnd, const uint8_t ui8Value); +bool pn53x_SAMConfiguration (nfc_device *pnd, const pn532_sam_mode mode, int timeout); +bool pn53x_PowerDown (nfc_device *pnd); +bool pn53x_InListPassiveTarget (nfc_device *pnd, const pn53x_modulation pmInitModulation, + const uint8_t szMaxTargets, const uint8_t *pbtInitiatorData, + const size_t szInitiatorDataLen, uint8_t *pbtTargetsData, size_t *pszTargetsData, int timeout); -bool pn53x_InDeselect (nfc_device * pnd, const uint8_t ui8Target); -bool pn53x_InRelease (nfc_device * pnd, const uint8_t ui8Target); -bool pn53x_InAutoPoll (nfc_device * pnd, const pn53x_target_type * ppttTargetTypes, const size_t szTargetTypes, - const uint8_t btPollNr, const uint8_t btPeriod, nfc_target * pntTargets, - size_t * pszTargetFound); -bool pn53x_InJumpForDEP (nfc_device * pnd, +bool pn53x_InDeselect (nfc_device *pnd, const uint8_t ui8Target); +bool pn53x_InRelease (nfc_device *pnd, const uint8_t ui8Target); +bool pn53x_InAutoPoll (nfc_device *pnd, const pn53x_target_type *ppttTargetTypes, const size_t szTargetTypes, + const uint8_t btPollNr, const uint8_t btPeriod, nfc_target *pntTargets, + size_t *pszTargetFound); +bool pn53x_InJumpForDEP (nfc_device *pnd, const nfc_dep_mode ndm, const nfc_baud_rate nbr, - const uint8_t * pbtPassiveInitiatorData, - const uint8_t * pbtNFCID3i, - const uint8_t * pbtGB, const size_t szGB, - nfc_target * pnt); -bool pn53x_TgInitAsTarget (nfc_device * pnd, pn53x_target_mode ptm, - const uint8_t * pbtMifareParams, - const uint8_t * pbtTkt, size_t szTkt, - const uint8_t * pbtFeliCaParams, - const uint8_t * pbtNFCID3t, const uint8_t * pbtGB, const size_t szGB, - uint8_t * pbtRx, size_t * pszRx, uint8_t * pbtModeByte); + const uint8_t *pbtPassiveInitiatorData, + const uint8_t *pbtNFCID3i, + const uint8_t *pbtGB, const size_t szGB, + nfc_target *pnt); +bool pn53x_TgInitAsTarget (nfc_device *pnd, pn53x_target_mode ptm, + const uint8_t *pbtMifareParams, + const uint8_t *pbtTkt, size_t szTkt, + const uint8_t *pbtFeliCaParams, + const uint8_t *pbtNFCID3t, const uint8_t *pbtGB, const size_t szGB, + uint8_t *pbtRx, size_t *pszRx, uint8_t *pbtModeByte); // RFConfiguration -bool pn53x_RFConfiguration__RF_field (nfc_device * pnd, bool bEnable); -bool pn53x_RFConfiguration__Various_timings (nfc_device * pnd, const uint8_t fATR_RES_Timeout, const uint8_t fRetryTimeout); -bool pn53x_RFConfiguration__MaxRtyCOM (nfc_device * pnd, const uint8_t MaxRtyCOM); -bool pn53x_RFConfiguration__MaxRetries (nfc_device * pnd, const uint8_t MxRtyATR, const uint8_t MxRtyPSL, const uint8_t MxRtyPassiveActivation); +bool pn53x_RFConfiguration__RF_field (nfc_device *pnd, bool bEnable); +bool pn53x_RFConfiguration__Various_timings (nfc_device *pnd, const uint8_t fATR_RES_Timeout, const uint8_t fRetryTimeout); +bool pn53x_RFConfiguration__MaxRtyCOM (nfc_device *pnd, const uint8_t MaxRtyCOM); +bool pn53x_RFConfiguration__MaxRetries (nfc_device *pnd, const uint8_t MxRtyATR, const uint8_t MxRtyPSL, const uint8_t MxRtyPassiveActivation); // Misc -bool pn53x_check_ack_frame (nfc_device * pnd, const uint8_t * pbtRxFrame, const size_t szRxFrameLen); -bool pn53x_check_error_frame (nfc_device * pnd, const uint8_t * pbtRxFrame, const size_t szRxFrameLen); -bool pn53x_build_frame (uint8_t * pbtFrame, size_t * pszFrame, const uint8_t * pbtData, const size_t szData); +bool pn53x_check_ack_frame (nfc_device *pnd, const uint8_t *pbtRxFrame, const size_t szRxFrameLen); +bool pn53x_check_error_frame (nfc_device *pnd, const uint8_t *pbtRxFrame, const size_t szRxFrameLen); +bool pn53x_build_frame (uint8_t *pbtFrame, size_t *pszFrame, const uint8_t *pbtData, const size_t szData); -void pn53x_data_new (nfc_device * pnd, const struct pn53x_io* io); -void pn53x_data_free (nfc_device * pnd); +void pn53x_data_new (nfc_device *pnd, const struct pn53x_io *io); +void pn53x_data_free (nfc_device *pnd); #endif // __NFC_CHIPS_PN53X_H__ diff --git a/libnfc/drivers/acr122.c b/libnfc/drivers/acr122.c index 23234fd..dd7e2a6 100644 --- a/libnfc/drivers/acr122.c +++ b/libnfc/drivers/acr122.c @@ -133,7 +133,7 @@ acr122_free_scardcontext (void) * @return true if succeeded, false otherwise. */ bool -acr122_probe (nfc_connstring connstrings[], size_t connstrings_len, size_t * pszDeviceFound) +acr122_probe (nfc_connstring connstrings[], size_t connstrings_len, size_t *pszDeviceFound) { size_t szPos = 0; char acDeviceNames[256 + 64 * PCSC_MAX_DEVICES]; @@ -300,7 +300,7 @@ error: } void -acr122_disconnect (nfc_device * pnd) +acr122_disconnect (nfc_device *pnd) { SCardDisconnect (DRIVER_DATA (pnd)->hCard, SCARD_LEAVE_CARD); acr122_free_scardcontext (); @@ -310,7 +310,7 @@ acr122_disconnect (nfc_device * pnd) } bool -acr122_send (nfc_device * pnd, const uint8_t * pbtData, const size_t szData, int timeout) +acr122_send (nfc_device *pnd, const uint8_t *pbtData, const size_t szData, int timeout) { // FIXME: timeout is not handled (void) timeout; @@ -381,7 +381,7 @@ acr122_send (nfc_device * pnd, const uint8_t * pbtData, const size_t szData, int } int -acr122_receive (nfc_device * pnd, uint8_t * pbtData, const size_t szData, int timeout) +acr122_receive (nfc_device *pnd, uint8_t *pbtData, const size_t szData, int timeout) { // FIXME: timeout is not handled (void) timeout; diff --git a/libnfc/drivers/acr122.h b/libnfc/drivers/acr122.h index 00c46fc..b364258 100644 --- a/libnfc/drivers/acr122.h +++ b/libnfc/drivers/acr122.h @@ -26,13 +26,13 @@ # include -bool acr122_probe (nfc_connstring connstrings[], size_t connstrings_len, size_t * pszDeviceFound); +bool acr122_probe (nfc_connstring connstrings[], size_t connstrings_len, size_t *pszDeviceFound); // Functions used by developer to handle connection to this device nfc_device *acr122_connect (const nfc_connstring connstring); -bool acr122_send (nfc_device * pnd, const uint8_t * pbtData, const size_t szData, int timeout); -int acr122_receive (nfc_device * pnd, uint8_t * pbtData, const size_t szData, int timeout); -void acr122_disconnect (nfc_device * pnd); +bool acr122_send (nfc_device *pnd, const uint8_t *pbtData, const size_t szData, int timeout); +int acr122_receive (nfc_device *pnd, uint8_t *pbtData, const size_t szData, int timeout); +void acr122_disconnect (nfc_device *pnd); extern const struct nfc_driver_t acr122_driver; diff --git a/libnfc/drivers/arygon.c b/libnfc/drivers/arygon.c index f27a245..ecd36b6 100644 --- a/libnfc/drivers/arygon.c +++ b/libnfc/drivers/arygon.c @@ -88,11 +88,11 @@ static const uint8_t arygon_error_none[] = "FF000000\x0d\x0a"; static const uint8_t arygon_error_incomplete_command[] = "FF0C0000\x0d\x0a"; static const uint8_t arygon_error_unknown_mode[] = "FF060000\x0d\x0a"; -bool arygon_reset_tama (nfc_device * pnd); -void arygon_firmware (nfc_device * pnd, char * str); +bool arygon_reset_tama (nfc_device *pnd); +void arygon_firmware (nfc_device *pnd, char *str); bool -arygon_probe (nfc_connstring connstrings[], size_t connstrings_len, size_t * pszDeviceFound) +arygon_probe (nfc_connstring connstrings[], size_t connstrings_len, size_t *pszDeviceFound) { /** @note: Due to UART bus we can't know if its really an ARYGON without * sending some commands. But using this way to probe devices, we can @@ -197,7 +197,7 @@ arygon_connstring_decode (const nfc_connstring connstring, struct arygon_descrip strncpy (desc->port, port, sizeof(desc->port)-1); desc->port[sizeof(desc->port)-1] = '\0'; - const char* speed_s = strtok (NULL, ":"); + const char *speed_s = strtok (NULL, ":"); if (!speed_s) { // speed not specified (or parsing error) free (cs); @@ -286,7 +286,7 @@ arygon_connect (const nfc_connstring connstring) } void -arygon_disconnect (nfc_device * pnd) +arygon_disconnect (nfc_device *pnd) { // Release UART port uart_close (DRIVER_DATA (pnd)->port); @@ -304,7 +304,7 @@ arygon_disconnect (nfc_device * pnd) #define ARYGON_TX_BUFFER_LEN (PN53x_NORMAL_FRAME__DATA_MAX_LEN + PN53x_NORMAL_FRAME__OVERHEAD + 1) #define ARYGON_RX_BUFFER_LEN (PN53x_EXTENDED_FRAME__DATA_MAX_LEN + PN53x_EXTENDED_FRAME__OVERHEAD) bool -arygon_tama_send (nfc_device * pnd, const uint8_t * pbtData, const size_t szData, int timeout) +arygon_tama_send (nfc_device *pnd, const uint8_t *pbtData, const size_t szData, int timeout) { // Before sending anything, we need to discard from any junk bytes uart_flush_input (DRIVER_DATA(pnd)->port); @@ -366,11 +366,11 @@ arygon_abort (nfc_device *pnd) } int -arygon_tama_receive (nfc_device * pnd, uint8_t * pbtData, const size_t szDataLen, int timeout) +arygon_tama_receive (nfc_device *pnd, uint8_t *pbtData, const size_t szDataLen, int timeout) { uint8_t abtRxBuf[5]; size_t len; - void * abort_p = NULL; + void *abort_p = NULL; #ifndef WIN32 abort_p = &(DRIVER_DATA (pnd)->iAbortFds[1]); @@ -484,7 +484,7 @@ arygon_tama_receive (nfc_device * pnd, uint8_t * pbtData, const size_t szDataLen } void -arygon_firmware (nfc_device * pnd, char * str) +arygon_firmware (nfc_device *pnd, char *str) { const uint8_t arygon_firmware_version_cmd[] = { DEV_ARYGON_PROTOCOL_ARYGON_ASCII, 'a', 'v' }; uint8_t abtRx[16]; @@ -503,7 +503,7 @@ arygon_firmware (nfc_device * pnd, char * str) } if ( 0 == memcmp (abtRx, arygon_error_none, 6)) { - uint8_t * p = abtRx + 6; + uint8_t *p = abtRx + 6; unsigned int szData; sscanf ((const char*)p, "%02x%s", &szData, p); memcpy (str, p, szData); @@ -512,7 +512,7 @@ arygon_firmware (nfc_device * pnd, char * str) } bool -arygon_reset_tama (nfc_device * pnd) +arygon_reset_tama (nfc_device *pnd) { const uint8_t arygon_reset_tama_cmd[] = { DEV_ARYGON_PROTOCOL_ARYGON_ASCII, 'a', 'r' }; uint8_t abtRx[10]; // Attempted response is 10 bytes long @@ -537,7 +537,7 @@ arygon_reset_tama (nfc_device * pnd) } bool -arygon_abort_command (nfc_device * pnd) +arygon_abort_command (nfc_device *pnd) { if (pnd) { #ifndef WIN32 diff --git a/libnfc/drivers/arygon.h b/libnfc/drivers/arygon.h index a3d52f9..b63fbc2 100644 --- a/libnfc/drivers/arygon.h +++ b/libnfc/drivers/arygon.h @@ -30,13 +30,13 @@ # include -bool arygon_probe (nfc_connstring connstrings[], size_t connstrings_len, size_t * pszDeviceFound); +bool arygon_probe (nfc_connstring connstrings[], size_t connstrings_len, size_t *pszDeviceFound); nfc_device *arygon_connect (const nfc_connstring connstring); -void arygon_disconnect (nfc_device * pnd); +void arygon_disconnect (nfc_device *pnd); -bool arygon_tama_send (nfc_device * pnd, const uint8_t * pbtData, const size_t szData, int timeout); -int arygon_tama_receive (nfc_device * pnd, uint8_t * pbtData, const size_t szDat, int timeouta); +bool arygon_tama_send (nfc_device *pnd, const uint8_t *pbtData, const size_t szData, int timeout); +int arygon_tama_receive (nfc_device *pnd, uint8_t *pbtData, const size_t szDat, int timeouta); extern const struct nfc_driver_t arygon_driver; diff --git a/libnfc/drivers/pn532_uart.c b/libnfc/drivers/pn532_uart.c index e7759db..7b61090 100644 --- a/libnfc/drivers/pn532_uart.c +++ b/libnfc/drivers/pn532_uart.c @@ -48,8 +48,8 @@ #define PN532_UART_DRIVER_NAME "pn532_uart" #define LOG_CATEGORY "libnfc.driver.pn532_uart" -int pn532_uart_ack (nfc_device * pnd); -int pn532_uart_wakeup (nfc_device * pnd); +int pn532_uart_ack (nfc_device *pnd); +int pn532_uart_wakeup (nfc_device *pnd); const struct pn53x_io pn532_uart_io; @@ -65,7 +65,7 @@ struct pn532_uart_data { #define DRIVER_DATA(pnd) ((struct pn532_uart_data*)(pnd->driver_data)) bool -pn532_uart_probe (nfc_connstring connstrings[], size_t connstrings_len, size_t * pszDeviceFound) +pn532_uart_probe (nfc_connstring connstrings[], size_t connstrings_len, size_t *pszDeviceFound) { /** @note: Due to UART bus we can't know if its really a pn532 without * sending some PN53x commands. But using this way to probe devices, we can @@ -178,7 +178,7 @@ pn532_connstring_decode (const nfc_connstring connstring, struct pn532_uart_desc strncpy (desc->port, port, sizeof(desc->port)-1); desc->port[sizeof(desc->port)-1] = '\0'; - const char* speed_s = strtok (NULL, ":"); + const char *speed_s = strtok (NULL, ":"); if (!speed_s) { // speed not specified (or parsing error) free (cs); @@ -262,7 +262,7 @@ pn532_uart_connect (const nfc_connstring connstring) } void -pn532_uart_disconnect (nfc_device * pnd) +pn532_uart_disconnect (nfc_device *pnd) { // Release UART port uart_close (DRIVER_DATA(pnd)->port); @@ -278,7 +278,7 @@ pn532_uart_disconnect (nfc_device * pnd) } int -pn532_uart_wakeup (nfc_device * pnd) +pn532_uart_wakeup (nfc_device *pnd) { /* High Speed Unit (HSU) wake up consist to send 0x55 and wait a "long" delay for PN532 being wakeup. */ const uint8_t pn532_wakeup_preamble[] = { 0x55, 0x55, 0x00, 0x00, 0x00 }; @@ -289,7 +289,7 @@ pn532_uart_wakeup (nfc_device * pnd) #define PN532_BUFFER_LEN (PN53x_EXTENDED_FRAME__DATA_MAX_LEN + PN53x_EXTENDED_FRAME__OVERHEAD) bool -pn532_uart_send (nfc_device * pnd, const uint8_t * pbtData, const size_t szData, int timeout) +pn532_uart_send (nfc_device *pnd, const uint8_t *pbtData, const size_t szData, int timeout) { // Before sending anything, we need to discard from any junk bytes uart_flush_input (DRIVER_DATA(pnd)->port); @@ -349,11 +349,11 @@ pn532_uart_send (nfc_device * pnd, const uint8_t * pbtData, const size_t szData, } int -pn532_uart_receive (nfc_device * pnd, uint8_t * pbtData, const size_t szDataLen, int timeout) +pn532_uart_receive (nfc_device *pnd, uint8_t *pbtData, const size_t szDataLen, int timeout) { uint8_t abtRxBuf[5]; size_t len; - void * abort_p = NULL; + void *abort_p = NULL; #ifndef WIN32 abort_p = &(DRIVER_DATA (pnd)->iAbortFds[1]); @@ -473,7 +473,7 @@ pn532_uart_receive (nfc_device * pnd, uint8_t * pbtData, const size_t szDataLen, } int -pn532_uart_ack (nfc_device * pnd) +pn532_uart_ack (nfc_device *pnd) { if (POWERDOWN == CHIP_DATA(pnd)->power_mode) { if (-1 == pn532_uart_wakeup(pnd)) { @@ -484,7 +484,7 @@ pn532_uart_ack (nfc_device * pnd) } bool -pn532_uart_abort_command (nfc_device * pnd) +pn532_uart_abort_command (nfc_device *pnd) { if (pnd) { #ifndef WIN32 diff --git a/libnfc/drivers/pn532_uart.h b/libnfc/drivers/pn532_uart.h index 51874c0..74194f1 100644 --- a/libnfc/drivers/pn532_uart.h +++ b/libnfc/drivers/pn532_uart.h @@ -29,12 +29,12 @@ # include -bool pn532_uart_probe (nfc_connstring connstrings[], size_t connstrings_len, size_t * pszDeviceFound); +bool pn532_uart_probe (nfc_connstring connstrings[], size_t connstrings_len, size_t *pszDeviceFound); nfc_device *pn532_uart_connect (const nfc_connstring connstring); -void pn532_uart_disconnect (nfc_device * pnd); -bool pn532_uart_send (nfc_device * pnd, const uint8_t * pbtData, const size_t szData, int timeout); -int pn532_uart_receive (nfc_device * pnd, uint8_t * pbtData, const size_t szData, int timeout); +void pn532_uart_disconnect (nfc_device *pnd); +bool pn532_uart_send (nfc_device *pnd, const uint8_t *pbtData, const size_t szData, int timeout); +int pn532_uart_receive (nfc_device *pnd, uint8_t *pbtData, const size_t szData, int timeout); extern const struct nfc_driver_t pn532_uart_driver; diff --git a/libnfc/drivers/pn53x_usb.c b/libnfc/drivers/pn53x_usb.c index 5a9027d..b8d2ed8 100644 --- a/libnfc/drivers/pn53x_usb.c +++ b/libnfc/drivers/pn53x_usb.c @@ -190,7 +190,7 @@ pn53x_usb_get_device_model (uint16_t vendor_id, uint16_t product_id) return UNKNOWN; } -int pn53x_usb_ack (nfc_device * pnd); +int pn53x_usb_ack (nfc_device *pnd); // Find transfer endpoints for bulk transfers void @@ -223,7 +223,7 @@ pn53x_usb_get_end_points (struct usb_device *dev, struct pn53x_usb_data *data) } bool -pn53x_usb_probe (nfc_connstring connstrings[], size_t connstrings_len, size_t * pszDeviceFound) +pn53x_usb_probe (nfc_connstring connstrings[], size_t connstrings_len, size_t *pszDeviceFound) { usb_init (); @@ -492,7 +492,7 @@ error: } void -pn53x_usb_disconnect (nfc_device * pnd) +pn53x_usb_disconnect (nfc_device *pnd) { pn53x_usb_ack (pnd); @@ -519,7 +519,7 @@ pn53x_usb_disconnect (nfc_device * pnd) #define PN53X_USB_BUFFER_LEN (PN53x_EXTENDED_FRAME__DATA_MAX_LEN + PN53x_EXTENDED_FRAME__OVERHEAD) bool -pn53x_usb_send (nfc_device * pnd, const uint8_t * pbtData, const size_t szData, int timeout) +pn53x_usb_send (nfc_device *pnd, const uint8_t *pbtData, const size_t szData, int timeout) { uint8_t abtFrame[PN53X_USB_BUFFER_LEN] = { 0x00, 0x00, 0xff }; // Every packet must start with "00 00 ff" size_t szFrame = 0; @@ -565,7 +565,7 @@ pn53x_usb_send (nfc_device * pnd, const uint8_t * pbtData, const size_t szData, } int -pn53x_usb_receive (nfc_device * pnd, uint8_t * pbtData, const size_t szDataLen, int timeout) +pn53x_usb_receive (nfc_device *pnd, uint8_t *pbtData, const size_t szDataLen, int timeout) { size_t len; off_t offset = 0; @@ -716,7 +716,7 @@ read: } int -pn53x_usb_ack (nfc_device * pnd) +pn53x_usb_ack (nfc_device *pnd) { return pn53x_usb_bulk_write (DRIVER_DATA (pnd), (uint8_t *) pn53x_ack_frame, sizeof (pn53x_ack_frame), 0); } @@ -779,7 +779,7 @@ On ASK LoGO hardware: } bool -pn53x_usb_configure (nfc_device * pnd, const nfc_device_option ndo, const bool bEnable) +pn53x_usb_configure (nfc_device *pnd, const nfc_device_option ndo, const bool bEnable) { if (!pn53x_configure (pnd, ndo, bEnable)) return false; @@ -807,7 +807,7 @@ pn53x_usb_configure (nfc_device * pnd, const nfc_device_option ndo, const bool b } bool -pn53x_usb_abort_command (nfc_device * pnd) +pn53x_usb_abort_command (nfc_device *pnd) { DRIVER_DATA (pnd)->abort_flag = true; return true; diff --git a/libnfc/drivers/pn53x_usb.h b/libnfc/drivers/pn53x_usb.h index 00f0d31..21dbfdb 100644 --- a/libnfc/drivers/pn53x_usb.h +++ b/libnfc/drivers/pn53x_usb.h @@ -29,11 +29,11 @@ # include -bool pn53x_usb_probe (nfc_connstring connstrings[], size_t connstrings_len, size_t * pszDeviceFound); +bool pn53x_usb_probe (nfc_connstring connstrings[], size_t connstrings_len, size_t *pszDeviceFound); nfc_device *pn53x_usb_connect (const nfc_connstring connstring); -bool pn53x_usb_send (nfc_device * pnd, const uint8_t * pbtData, const size_t szData, int timeout); -int pn53x_usb_receive (nfc_device * pnd, uint8_t * pbtData, const size_t szData, int timeout); -void pn53x_usb_disconnect (nfc_device * pnd); +bool pn53x_usb_send (nfc_device *pnd, const uint8_t *pbtData, const size_t szData, int timeout); +int pn53x_usb_receive (nfc_device *pnd, uint8_t *pbtData, const size_t szData, int timeout); +void pn53x_usb_disconnect (nfc_device *pnd); extern const struct nfc_driver_t pn53x_usb_driver; diff --git a/libnfc/iso14443-subr.c b/libnfc/iso14443-subr.c index 42e291e..ec0e002 100644 --- a/libnfc/iso14443-subr.c +++ b/libnfc/iso14443-subr.c @@ -32,7 +32,7 @@ #include void -iso14443a_crc (uint8_t * pbtData, size_t szLen, uint8_t * pbtCrc) +iso14443a_crc (uint8_t *pbtData, size_t szLen, uint8_t *pbtCrc) { uint8_t bt; uint32_t wCrc = 0x6363; @@ -49,13 +49,13 @@ iso14443a_crc (uint8_t * pbtData, size_t szLen, uint8_t * pbtCrc) } void -iso14443a_crc_append (uint8_t * pbtData, size_t szLen) +iso14443a_crc_append (uint8_t *pbtData, size_t szLen) { iso14443a_crc (pbtData, szLen, pbtData + szLen); } uint8_t * -iso14443a_locate_historical_bytes (uint8_t * pbtAts, size_t szAts, size_t * pszTk) +iso14443a_locate_historical_bytes (uint8_t *pbtAts, size_t szAts, size_t *pszTk) { if (szAts) { size_t offset = 1; @@ -82,7 +82,7 @@ iso14443a_locate_historical_bytes (uint8_t * pbtAts, size_t szAts, size_t * pszT * @see ISO/IEC 14443-3 (6.4.4 UID contents and cascade levels) */ void -iso14443_cascade_uid (const uint8_t abtUID[], const size_t szUID, uint8_t * pbtCascadedUID, size_t * pszCascadedUID) +iso14443_cascade_uid (const uint8_t abtUID[], const size_t szUID, uint8_t *pbtCascadedUID, size_t *pszCascadedUID) { switch (szUID) { case 7: diff --git a/libnfc/mirror-subr.c b/libnfc/mirror-subr.c index d85ef88..361038f 100644 --- a/libnfc/mirror-subr.c +++ b/libnfc/mirror-subr.c @@ -60,7 +60,7 @@ mirror (uint8_t bt) } void -mirror_bytes (uint8_t * pbts, size_t szLen) +mirror_bytes (uint8_t *pbts, size_t szLen) { size_t szByteNr; diff --git a/libnfc/mirror-subr.h b/libnfc/mirror-subr.h index 0560053..081bc88 100644 --- a/libnfc/mirror-subr.h +++ b/libnfc/mirror-subr.h @@ -32,6 +32,6 @@ uint8_t mirror (uint8_t bt); uint32_t mirror32 (uint32_t ui32Bits); uint64_t mirror64 (uint64_t ui64Bits); -void mirror_uint8_ts (uint8_t * pbts, size_t szLen); +void mirror_uint8_ts (uint8_t *pbts, size_t szLen); #endif // _LIBNFC_MIRROR_SUBR_H_ diff --git a/libnfc/nfc-emulation.c b/libnfc/nfc-emulation.c index c892837..c8a7af9 100644 --- a/libnfc/nfc-emulation.c +++ b/libnfc/nfc-emulation.c @@ -28,7 +28,7 @@ #include "iso7816.h" int -nfc_emulate_target (nfc_device* pnd, struct nfc_emulator *emulator) +nfc_emulate_target (nfc_device *pnd, struct nfc_emulator *emulator) { uint8_t abtRx[ISO7816_SHORT_R_APDU_MAX_LEN]; size_t szRx = sizeof(abtRx); diff --git a/libnfc/nfc-internal.h b/libnfc/nfc-internal.h index 04333e9..d34c706 100644 --- a/libnfc/nfc-internal.h +++ b/libnfc/nfc-internal.h @@ -128,29 +128,29 @@ struct nfc_driver_t { const char *name; bool (*probe)(nfc_connstring connstrings[], size_t connstrings_len, size_t * pszDeviceFound); nfc_device * (*connect) (const nfc_connstring connstring); - void (*disconnect) (nfc_device * pnd); - const char *(*strerror) (const nfc_device * pnd); + void (*disconnect) (nfc_device *pnd); + const char *(*strerror) (const nfc_device *pnd); - bool (*initiator_init) (nfc_device * pnd); - bool (*initiator_select_passive_target) (nfc_device * pnd, const nfc_modulation nm, const uint8_t * pbtInitData, const size_t szInitData, nfc_target * pnt); - bool (*initiator_poll_target) (nfc_device * pnd, const nfc_modulation * pnmModulations, const size_t szModulations, const uint8_t uiPollNr, const uint8_t btPeriod, nfc_target * pnt); - bool (*initiator_select_dep_target) (nfc_device * pnd, const nfc_dep_mode ndm, const nfc_baud_rate nbr, const nfc_dep_info * pndiInitiator, nfc_target * pnt); - bool (*initiator_deselect_target) (nfc_device * pnd); - bool (*initiator_transceive_bytes) (nfc_device * pnd, const uint8_t * pbtTx, const size_t szTx, uint8_t * pbtRx, size_t * pszRx, int timeout); - bool (*initiator_transceive_bits) (nfc_device * pnd, const uint8_t * pbtTx, const size_t szTxBits, const uint8_t * pbtTxPar, uint8_t * pbtRx, size_t * pszRxBits, uint8_t * pbtRxPar); - bool (*initiator_transceive_bytes_timed) (nfc_device * pnd, const uint8_t * pbtTx, const size_t szTx, uint8_t * pbtRx, size_t * pszRx, uint32_t * cycles); - bool (*initiator_transceive_bits_timed) (nfc_device * pnd, const uint8_t * pbtTx, const size_t szTxBits, const uint8_t * pbtTxPar, uint8_t * pbtRx, size_t * pszRxBits, uint8_t * pbtRxPar, uint32_t * cycles); + bool (*initiator_init) (nfc_device *pnd); + bool (*initiator_select_passive_target) (nfc_device *pnd, const nfc_modulation nm, const uint8_t * pbtInitData, const size_t szInitData, nfc_target * pnt); + bool (*initiator_poll_target) (nfc_device *pnd, const nfc_modulation * pnmModulations, const size_t szModulations, const uint8_t uiPollNr, const uint8_t btPeriod, nfc_target * pnt); + bool (*initiator_select_dep_target) (nfc_device *pnd, const nfc_dep_mode ndm, const nfc_baud_rate nbr, const nfc_dep_info * pndiInitiator, nfc_target * pnt); + bool (*initiator_deselect_target) (nfc_device *pnd); + bool (*initiator_transceive_bytes) (nfc_device *pnd, const uint8_t * pbtTx, const size_t szTx, uint8_t * pbtRx, size_t * pszRx, int timeout); + bool (*initiator_transceive_bits) (nfc_device *pnd, const uint8_t * pbtTx, const size_t szTxBits, const uint8_t * pbtTxPar, uint8_t * pbtRx, size_t * pszRxBits, uint8_t * pbtRxPar); + bool (*initiator_transceive_bytes_timed) (nfc_device *pnd, const uint8_t * pbtTx, const size_t szTx, uint8_t * pbtRx, size_t * pszRx, uint32_t * cycles); + bool (*initiator_transceive_bits_timed) (nfc_device *pnd, const uint8_t * pbtTx, const size_t szTxBits, const uint8_t * pbtTxPar, uint8_t * pbtRx, size_t * pszRxBits, uint8_t * pbtRxPar, uint32_t * cycles); - bool (*target_init) (nfc_device * pnd, nfc_target * pnt, uint8_t * pbtRx, size_t * pszRx); - bool (*target_send_bytes) (nfc_device * pnd, const uint8_t * pbtTx, const size_t szTx, int timeout); - bool (*target_receive_bytes) (nfc_device * pnd, uint8_t * pbtRx, size_t * pszRx, int timeout); - bool (*target_send_bits) (nfc_device * pnd, const uint8_t * pbtTx, const size_t szTxBits, const uint8_t * pbtTxPar); - bool (*target_receive_bits) (nfc_device * pnd, uint8_t * pbtRx, size_t * pszRxBits, uint8_t * pbtRxPar); + bool (*target_init) (nfc_device *pnd, nfc_target * pnt, uint8_t * pbtRx, size_t * pszRx); + bool (*target_send_bytes) (nfc_device *pnd, const uint8_t * pbtTx, const size_t szTx, int timeout); + bool (*target_receive_bytes) (nfc_device *pnd, uint8_t * pbtRx, size_t * pszRx, int timeout); + bool (*target_send_bits) (nfc_device *pnd, const uint8_t * pbtTx, const size_t szTxBits, const uint8_t * pbtTxPar); + bool (*target_receive_bits) (nfc_device *pnd, uint8_t * pbtRx, size_t * pszRxBits, uint8_t * pbtRxPar); - bool (*configure) (nfc_device * pnd, const nfc_device_option ndo, const bool bEnable); + bool (*configure) (nfc_device *pnd, const nfc_device_option ndo, const bool bEnable); - bool (*abort_command) (nfc_device * pnd); - bool (*idle) (nfc_device * pnd); + bool (*abort_command) (nfc_device *pnd); + bool (*idle) (nfc_device *pnd); }; nfc_device *nfc_device_new (void); diff --git a/utils/mifare.c b/utils/mifare.c index 92ecfad..91daf0d 100644 --- a/utils/mifare.c +++ b/utils/mifare.c @@ -48,7 +48,7 @@ * The MIFARE Classic Specification (http://www.nxp.com/acrobat/other/identification/M001053_MF1ICS50_rev5_3.pdf) explains more about this process. */ bool -nfc_initiator_mifare_cmd (nfc_device * pnd, const mifare_cmd mc, const uint8_t ui8Block, mifare_param * pmp) +nfc_initiator_mifare_cmd (nfc_device *pnd, const mifare_cmd mc, const uint8_t ui8Block, mifare_param *pmp) { uint8_t abtRx[265]; size_t szRx = sizeof(abtRx); diff --git a/utils/mifare.h b/utils/mifare.h index 726d669..5260308 100644 --- a/utils/mifare.h +++ b/utils/mifare.h @@ -75,7 +75,7 @@ typedef union { // Reset struct alignment to default # pragma pack() -bool nfc_initiator_mifare_cmd (nfc_device * pnd, const mifare_cmd mc, const uint8_t ui8Block, mifare_param * pmp); +bool nfc_initiator_mifare_cmd (nfc_device *pnd, const mifare_cmd mc, const uint8_t ui8Block, mifare_param *pmp); // Compiler directive, set struct alignment to 1 uint8_t for compatibility # pragma pack(1) diff --git a/utils/nfc-mfclassic.c b/utils/nfc-mfclassic.c index 9a7bb1b..35c3d33 100644 --- a/utils/nfc-mfclassic.c +++ b/utils/nfc-mfclassic.c @@ -92,7 +92,7 @@ uint8_t abtUnlock1[1] = { 0x40 }; uint8_t abtUnlock2[1] = { 0x43 }; static bool -transmit_bits (const uint8_t * pbtTx, const size_t szTxBits) +transmit_bits (const uint8_t *pbtTx, const size_t szTxBits) { // Show transmitted command printf ("Sent bits: "); @@ -110,7 +110,7 @@ transmit_bits (const uint8_t * pbtTx, const size_t szTxBits) static bool -transmit_bytes (const uint8_t * pbtTx, const size_t szTx) +transmit_bytes (const uint8_t *pbtTx, const size_t szTx) { // Show transmitted command printf ("Sent bits: "); @@ -127,7 +127,7 @@ transmit_bytes (const uint8_t * pbtTx, const size_t szTx) } static void -print_success_or_failure (bool bFailure, uint32_t * uiBlockCounter) +print_success_or_failure (bool bFailure, uint32_t *uiBlockCounter) { printf ("%c", (bFailure) ? 'x' : '.'); if (uiBlockCounter && !bFailure) diff --git a/utils/nfc-mfsetuid.c b/utils/nfc-mfsetuid.c index d610fcf..0c33a53 100644 --- a/utils/nfc-mfsetuid.c +++ b/utils/nfc-mfsetuid.c @@ -88,7 +88,7 @@ uint8_t abtBlank[18] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x07, 0x80, 0 static bool -transmit_bits (const uint8_t * pbtTx, const size_t szTxBits) +transmit_bits (const uint8_t *pbtTx, const size_t szTxBits) { // Show transmitted command if (!quiet_output) { @@ -110,7 +110,7 @@ transmit_bits (const uint8_t * pbtTx, const size_t szTxBits) static bool -transmit_bytes (const uint8_t * pbtTx, const size_t szTx) +transmit_bytes (const uint8_t *pbtTx, const size_t szTx) { // Show transmitted command if (!quiet_output) { diff --git a/utils/nfc-read-forum-tag3.c b/utils/nfc-read-forum-tag3.c index 2ea3fc1..ccba19d 100644 --- a/utils/nfc-read-forum-tag3.c +++ b/utils/nfc-read-forum-tag3.c @@ -77,7 +77,7 @@ void stop_select (int sig) } void -build_felica_frame(const nfc_felica_info nfi, const uint8_t command, const uint8_t* payload, const size_t payload_len, uint8_t * frame, size_t * frame_len) +build_felica_frame(const nfc_felica_info nfi, const uint8_t command, const uint8_t *payload, const size_t payload_len, uint8_t *frame, size_t *frame_len) { frame[0] = 1 + 1 + 8 + payload_len; *frame_len = frame[0]; @@ -88,7 +88,7 @@ build_felica_frame(const nfc_felica_info nfi, const uint8_t command, const uint8 #define CHECK 0x06 int -nfc_forum_tag_type3_check (nfc_device *pnd, const nfc_target nt, const uint16_t block, const uint8_t block_count, uint8_t * data, size_t * data_len) +nfc_forum_tag_type3_check (nfc_device *pnd, const nfc_target nt, const uint16_t block, const uint8_t block_count, uint8_t *data, size_t *data_len) { uint8_t payload[1024] = { 1, // Services diff --git a/utils/nfc-relay-picc.c b/utils/nfc-relay-picc.c index b8df5ec..1ac0391 100644 --- a/utils/nfc-relay-picc.c +++ b/utils/nfc-relay-picc.c @@ -92,7 +92,7 @@ print_usage (char *argv[]) printf ("\t-n N\tAdds a waiting time of N seconds (integer) in the relay to mimic long distance.\n"); } -bool print_hex_fd4 (const uint8_t * pbtData, const size_t szBytes, const char * pchPrefix) +bool print_hex_fd4 (const uint8_t *pbtData, const size_t szBytes, const char *pchPrefix) { size_t szPos; if (szBytes > MAX_FRAME_LEN) { @@ -114,7 +114,7 @@ bool print_hex_fd4 (const uint8_t * pbtData, const size_t szBytes, const char * return EXIT_SUCCESS; } -bool scan_hex_fd3 (uint8_t *pbtData, size_t *pszBytes, const char * pchPrefix) +bool scan_hex_fd3 (uint8_t *pbtData, size_t *pszBytes, const char *pchPrefix) { size_t szPos; unsigned int uiBytes; diff --git a/utils/nfc-utils.c b/utils/nfc-utils.c index 51c4a1f..b0e81d5 100644 --- a/utils/nfc-utils.c +++ b/utils/nfc-utils.c @@ -59,7 +59,7 @@ oddparity (const uint8_t bt) } void -oddparity_bytes_ts (const uint8_t * pbtData, const size_t szLen, uint8_t * pbtPar) +oddparity_bytes_ts (const uint8_t *pbtData, const size_t szLen, uint8_t *pbtPar) { size_t szByteNr; // Calculate the parity bits for the command @@ -69,7 +69,7 @@ oddparity_bytes_ts (const uint8_t * pbtData, const size_t szLen, uint8_t * pbtPa } void -print_hex (const uint8_t * pbtData, const size_t szBytes) +print_hex (const uint8_t *pbtData, const size_t szBytes) { size_t szPos; @@ -80,7 +80,7 @@ print_hex (const uint8_t * pbtData, const size_t szBytes) } void -print_hex_bits (const uint8_t * pbtData, const size_t szBits) +print_hex_bits (const uint8_t *pbtData, const size_t szBits) { uint8_t uRemainder; size_t szPos; @@ -102,7 +102,7 @@ print_hex_bits (const uint8_t * pbtData, const size_t szBits) } void -print_hex_par (const uint8_t * pbtData, const size_t szBits, const uint8_t * pbtDataPar) +print_hex_par (const uint8_t *pbtData, const size_t szBits, const uint8_t *pbtDataPar) { uint8_t uRemainder; size_t szPos; diff --git a/utils/nfc-utils.h b/utils/nfc-utils.h index 86c5d08..50abdf3 100644 --- a/utils/nfc-utils.h +++ b/utils/nfc-utils.h @@ -80,11 +80,11 @@ #endif uint8_t oddparity (const uint8_t bt); -void oddparity_uint8_ts (const uint8_t * pbtData, const size_t szLen, uint8_t * pbtPar); +void oddparity_uint8_ts (const uint8_t *pbtData, const size_t szLen, uint8_t *pbtPar); -void print_hex (const uint8_t * pbtData, const size_t szLen); -void print_hex_bits (const uint8_t * pbtData, const size_t szBits); -void print_hex_par (const uint8_t * pbtData, const size_t szBits, const uint8_t * pbtDataPar); +void print_hex (const uint8_t *pbtData, const size_t szLen); +void print_hex_bits (const uint8_t *pbtData, const size_t szBits); +void print_hex_par (const uint8_t *pbtData, const size_t szBits, const uint8_t *pbtDataPar); void print_nfc_iso14443a_info (const nfc_iso14443a_info nai, bool verbose); void print_nfc_iso14443b_info (const nfc_iso14443b_info nbi, bool verbose); From 297a8566a0755d7909b3ae9f212f21b4eb86b330 Mon Sep 17 00:00:00 2001 From: Audrey Diacre Date: Fri, 25 Nov 2011 15:47:24 +0000 Subject: [PATCH 012/113] Oups, too quick replacement commited in r1183 --- include/nfc/nfc-types.h | 6 +++--- libnfc/chips/pn53x.c | 2 +- libnfc/chips/pn53x.h | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/include/nfc/nfc-types.h b/include/nfc/nfc-types.h index 28f4a21..647ccce 100644 --- a/include/nfc/nfc-types.h +++ b/include/nfc/nfc-types.h @@ -282,7 +282,7 @@ typedef enum { } nfc_baud_rate; /** - * @enum nfc_modulationype + * @enum nfc_modulation_type * @brief NFC modulation type enumeration */ typedef enum { @@ -294,14 +294,14 @@ typedef enum { NMT_ISO14443B2CT, // ISO14443-2B ASK CTx NMT_FELICA, NMT_DEP, -} nfc_modulationype; +} nfc_modulation_type; /** * @struct nfc_modulation * @brief NFC modulation structure */ typedef struct { - nfc_modulationype nmt; + nfc_modulation_type nmt; nfc_baud_rate nbr; } nfc_modulation; diff --git a/libnfc/chips/pn53x.c b/libnfc/chips/pn53x.c index 46b873c..8627622 100644 --- a/libnfc/chips/pn53x.c +++ b/libnfc/chips/pn53x.c @@ -318,7 +318,7 @@ pn53x_unwrap_frame (const uint8_t *pbtFrame, const size_t szFrameBits, uint8_t * } bool -pn53x_decode_target_data (const uint8_t *pbtRawData, size_t szRawData, pn53x_type type, nfc_modulationype nmt, +pn53x_decode_target_data (const uint8_t *pbtRawData, size_t szRawData, pn53x_type type, nfc_modulation_type nmt, nfc_target_info *pnti) { uint8_t szAttribRes; diff --git a/libnfc/chips/pn53x.h b/libnfc/chips/pn53x.h index 431dd42..e892f0b 100644 --- a/libnfc/chips/pn53x.h +++ b/libnfc/chips/pn53x.h @@ -265,7 +265,7 @@ bool pn53x_wrap_frame (const uint8_t *pbtTx, const size_t szTxBits, const uin bool pn53x_unwrap_frame (const uint8_t *pbtFrame, const size_t szFrameBits, uint8_t *pbtRx, size_t *pszRxBits, uint8_t *pbtRxPar); bool pn53x_decode_target_data (const uint8_t *pbtRawData, size_t szRawData, - pn53x_type chip_type, nfc_modulationype nmt, + pn53x_type chip_type, nfc_modulation_type nmt, nfc_target_info *pnti); bool pn53x_read_register (nfc_device *pnd, uint16_t ui16Reg, uint8_t *ui8Value); bool pn53x_write_register (nfc_device *pnd, uint16_t ui16Reg, uint8_t ui8SymbolMask, uint8_t ui8Value); From e76e531d2daef1f60cdb8f7be3f9e7b079904129 Mon Sep 17 00:00:00 2001 From: Romuald Conty Date: Fri, 25 Nov 2011 16:13:40 +0000 Subject: [PATCH 013/113] pn53x_usb: clean timeout handling. --- libnfc/drivers/pn53x_usb.c | 72 ++++++++++++-------------------------- log4crc | 2 +- 2 files changed, 23 insertions(+), 51 deletions(-) diff --git a/libnfc/drivers/pn53x_usb.c b/libnfc/drivers/pn53x_usb.c index b8d2ed8..cb8ac34 100644 --- a/libnfc/drivers/pn53x_usb.c +++ b/libnfc/drivers/pn53x_usb.c @@ -121,18 +121,9 @@ bool pn53x_usb_get_usb_device_name (struct usb_device *dev, usb_dev_handle *udev bool pn53x_usb_init (nfc_device *pnd); int -pn53x_usb_bulk_read (struct pn53x_usb_data *data, uint8_t abtRx[], const size_t szRx, int timeout) +pn53x_usb_bulk_read (struct pn53x_usb_data *data, uint8_t abtRx[], const size_t szRx, const int timeout) { - int timeout_ms = USB_INFINITE_TIMEOUT; - if (timeout > 0) { - timeout_ms = timeout; - if (timeout_ms == USB_INFINITE_TIMEOUT) { - // timeout < 1 ms - timeout_ms++; - } - } - - int res = usb_bulk_read (data->pudh, data->uiEndPointIn, (char *) abtRx, szRx, timeout_ms); + int res = usb_bulk_read (data->pudh, data->uiEndPointIn, (char *) abtRx, szRx, timeout); if (res > 0) { LOG_HEX ("RX", abtRx, res); } else if (res < 0) { @@ -143,18 +134,14 @@ pn53x_usb_bulk_read (struct pn53x_usb_data *data, uint8_t abtRx[], const size_t } int -pn53x_usb_bulk_write (struct pn53x_usb_data *data, uint8_t abtTx[], const size_t szTx, int timeout) +pn53x_usb_bulk_write (struct pn53x_usb_data *data, uint8_t abtTx[], const size_t szTx, const int timeout) { LOG_HEX ("TX", abtTx, szTx); - int timeout_ms = USB_INFINITE_TIMEOUT; - if (timeout > 0) - timeout_ms = timeout; - - int res = usb_bulk_write (data->pudh, data->uiEndPointOut, (char *) abtTx, szTx, timeout_ms); + int res = usb_bulk_write (data->pudh, data->uiEndPointOut, (char *) abtTx, szTx, timeout); if (res > 0) { // HACK This little hack is a well know problem of USB, see http://www.libusb.org/ticket/6 for more details if ((res % data->uiMaxPacketSize) == 0) { - usb_bulk_write (data->pudh, data->uiEndPointOut, "\0", 0, timeout_ms); + usb_bulk_write (data->pudh, data->uiEndPointOut, "\0", 0, timeout); } } else { log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "Unable to write to USB (%s)", _usb_strerror (res)); @@ -519,7 +506,7 @@ pn53x_usb_disconnect (nfc_device *pnd) #define PN53X_USB_BUFFER_LEN (PN53x_EXTENDED_FRAME__DATA_MAX_LEN + PN53x_EXTENDED_FRAME__OVERHEAD) bool -pn53x_usb_send (nfc_device *pnd, const uint8_t *pbtData, const size_t szData, int timeout) +pn53x_usb_send (nfc_device *pnd, const uint8_t *pbtData, const size_t szData, const int timeout) { uint8_t abtFrame[PN53X_USB_BUFFER_LEN] = { 0x00, 0x00, 0xff }; // Every packet must start with "00 00 ff" size_t szFrame = 0; @@ -564,8 +551,9 @@ pn53x_usb_send (nfc_device *pnd, const uint8_t *pbtData, const size_t szData, in return true; } +#define USB_TIMEOUT_PER_PASS 200 int -pn53x_usb_receive (nfc_device *pnd, uint8_t *pbtData, const size_t szDataLen, int timeout) +pn53x_usb_receive (nfc_device *pnd, uint8_t *pbtData, const size_t szDataLen, const int timeout) { size_t len; off_t offset = 0; @@ -574,41 +562,25 @@ pn53x_usb_receive (nfc_device *pnd, uint8_t *pbtData, const size_t szDataLen, in int res; /* - * If no timeout is specified but the command is blocking, force a 250ms + * If no timeout is specified but the command is blocking, force a 200ms (USB_TIMEOUT_PER_PASS) * timeout to allow breaking the loop if the user wants to stop it. */ - const struct timeval fixed_timeout = { - .tv_sec = 0, - .tv_usec = 250000, - }; - - struct timeval remaining_time, usb_timeout; - if (timeout > 0) { - remaining_time.tv_sec = (timeout / 1000); - remaining_time.tv_usec = ((timeout % 1000) * 1000); - } + int usb_timeout; + int remaining_time = timeout; read: - if (timeout > 0) { - // A user-provided timeout is set, we have to cut it in multiple chunk to be able to keep an nfc_abort_command() mecanism - struct timeval tmp; - if (1 == timeval_subtract (&tmp, &remaining_time, &fixed_timeout)) { - // The subtraction result is negative - usb_timeout = remaining_time; - remaining_time.tv_sec = 0; - remaining_time.tv_usec = 0; - } else { - usb_timeout = fixed_timeout; - remaining_time = tmp; - } + if (timeout == USB_INFINITE_TIMEOUT) { + usb_timeout = USB_TIMEOUT_PER_PASS; } else { - // No user-provided timeout, we will wait infinitely but we need nfc_abort_command() mecanism. - usb_timeout = fixed_timeout; + remaining_time -= USB_TIMEOUT_PER_PASS; + if (remaining_time <= 0) { + pnd->iLastError = ECOMTIMEOUT; + return -1; + } else { + usb_timeout = MIN(remaining_time, USB_TIMEOUT_PER_PASS); + } } - if ((usb_timeout.tv_sec == 0) && (usb_timeout.tv_usec == 0)) { - pnd->iLastError = ECOMTIMEOUT; - return -1; - } - res = pn53x_usb_bulk_read (DRIVER_DATA (pnd), abtRxBuf, sizeof (abtRxBuf), ((usb_timeout.tv_sec * 1000) + (usb_timeout.tv_usec / 1000))); + + res = pn53x_usb_bulk_read (DRIVER_DATA (pnd), abtRxBuf, sizeof (abtRxBuf), usb_timeout); if (res == -USB_TIMEDOUT) { if (DRIVER_DATA (pnd)->abort_flag) { diff --git a/log4crc b/log4crc index 1b94d68..8dbf69d 100644 --- a/log4crc +++ b/log4crc @@ -21,5 +21,5 @@ - + From 3b657ee5475c25c6eb78503a561e8eb394132901 Mon Sep 17 00:00:00 2001 From: Audrey Diacre Date: Wed, 30 Nov 2011 11:21:01 +0000 Subject: [PATCH 014/113] quick_start_example1 is now compiled when running make check or distcheck (Fixes Issue 178) --- examples/Makefile.am | 8 ++++++++ examples/doc/quick_start_example1.c | 19 ++++++++++--------- 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/examples/Makefile.am b/examples/Makefile.am index db402e5..c0eda51 100644 --- a/examples/Makefile.am +++ b/examples/Makefile.am @@ -17,6 +17,9 @@ bin_PROGRAMS += \ pn53x-tamashell endif +check_PROGRAMS = \ + quick_start_example1 + # set the include path found by configure INCLUDES = $(all_includes) $(LIBNFC_CFLAGS) @@ -67,6 +70,11 @@ pn53x_tamashell_LDADD = $(top_builddir)/libnfc/libnfc.la \ $(top_builddir)/utils/libnfcutils.la pn53x_tamashell_LDFLAGS = @READLINE_LIBS@ +quick_start_example1_SOURCES = doc/quick_start_example1.c +quick_start_example1_LDADD = $(top_builddir)/libnfc/libnfc.la \ + $(top_builddir)/utils/libnfcutils.la + + dist_man_MANS = \ nfc-anticol.1 \ nfc-dep-initiator.1 \ diff --git a/examples/doc/quick_start_example1.c b/examples/doc/quick_start_example1.c index d828bd0..073fe02 100644 --- a/examples/doc/quick_start_example1.c +++ b/examples/doc/quick_start_example1.c @@ -5,13 +5,14 @@ #include #include -#include "nfc-utils.h" +#include "utils/nfc-utils.h" +#include "libnfc/chips/pn53x.h" int main (int argc, const char *argv[]) { nfc_device *pnd; - nfc_target_info nti; + nfc_target nt; // Display libnfc version const char *acLibnfcVersion = nfc_version (); @@ -34,17 +35,17 @@ main (int argc, const char *argv[]) .nmt = NMT_ISO14443A, .nbr = NBR_106, }; - if (nfc_initiator_select_passive_target (pnd, PM_ISO14443A_106, NULL, 0, &nti)) { + if (nfc_initiator_select_passive_target (pnd, nmMifare, NULL, 0, &nt)) { printf ("The following (NFC) ISO14443A tag was found:\n"); printf (" ATQA (SENS_RES): "); - print_hex (nti.nai.abtAtqa, 2); - printf (" UID (NFCID%c): ", (nti.nai.abtUid[0] == 0x08 ? '3' : '1')); - print_hex (nti.nai.abtUid, nti.nai.szUidLen); + print_hex (nt.nti.nai.abtAtqa, 2); + printf (" UID (NFCID%c): ", (nt.nti.nai.abtUid[0] == 0x08 ? '3' : '1')); + print_hex (nt.nti.nai.abtUid, nt.nti.nai.szUidLen); printf (" SAK (SEL_RES): "); - print_hex (&nti.nai.btSak, 1); - if (nti.nai.szAtsLen) { + print_hex (&nt.nti.nai.btSak, 1); + if (nt.nti.nai.szAtsLen) { printf (" ATS (ATR): "); - print_hex (nti.nai.abtAts, nti.nai.szAtsLen); + print_hex (nt.nti.nai.abtAts, nt.nti.nai.szAtsLen); } } // Disconnect from NFC device From c286eec920376d58be018f42fdedf4c802596143 Mon Sep 17 00:00:00 2001 From: Romuald Conty Date: Tue, 6 Dec 2011 10:05:35 +0000 Subject: [PATCH 015/113] DEP enhancements: - Add timeout on InJumpForDEP (pn53x) and initiator_select_dep (libnfc API) - test_dep now works again (except 424Kbps) - Fix ndi.ndm feeling when select a DEP target Important: test_dep does not work on PN53x_USB devices after running previous tests --- examples/nfc-dep-initiator.c | 2 +- include/nfc/nfc.h | 2 +- libnfc/chips/pn53x.c | 13 ++-- libnfc/chips/pn53x.h | 6 +- libnfc/drivers/pn53x_usb.c | 1 + libnfc/nfc-internal.h | 2 +- libnfc/nfc.c | 4 +- test/test_dep.c | 140 ++++++++++++++++++++++++++--------- 8 files changed, 123 insertions(+), 47 deletions(-) diff --git a/examples/nfc-dep-initiator.c b/examples/nfc-dep-initiator.c index ae90495..e7ad00e 100644 --- a/examples/nfc-dep-initiator.c +++ b/examples/nfc-dep-initiator.c @@ -86,7 +86,7 @@ main (int argc, const char *argv[]) return EXIT_FAILURE; } - if(!nfc_initiator_select_dep_target (pnd, NDM_PASSIVE, NBR_212, NULL, &nt)) { + if(!nfc_initiator_select_dep_target (pnd, NDM_PASSIVE, NBR_212, NULL, &nt, 1000)) { nfc_perror(pnd, "nfc_initiator_select_dep_target"); goto error; } diff --git a/include/nfc/nfc.h b/include/nfc/nfc.h index b168d6a..6d34b2f 100644 --- a/include/nfc/nfc.h +++ b/include/nfc/nfc.h @@ -76,7 +76,7 @@ extern "C" { NFC_EXPORT bool nfc_initiator_select_passive_target (nfc_device *pnd, const nfc_modulation nm, const uint8_t *pbtInitData, const size_t szInitData, nfc_target *pnt); NFC_EXPORT bool nfc_initiator_list_passive_targets (nfc_device *pnd, const nfc_modulation nm, nfc_target ant[], const size_t szTargets, size_t *pszTargetFound); NFC_EXPORT bool nfc_initiator_poll_target (nfc_device *pnd, const nfc_modulation *pnmTargetTypes, const size_t szTargetTypes, const uint8_t uiPollNr, const uint8_t uiPeriod, nfc_target *pnt); - NFC_EXPORT bool nfc_initiator_select_dep_target (nfc_device *pnd, const nfc_dep_mode ndm, const nfc_baud_rate nbr, const nfc_dep_info *pndiInitiator, nfc_target *pnt); + NFC_EXPORT bool nfc_initiator_select_dep_target (nfc_device *pnd, const nfc_dep_mode ndm, const nfc_baud_rate nbr, const nfc_dep_info *pndiInitiator, nfc_target *pnt, const int timeout); NFC_EXPORT bool nfc_initiator_deselect_target (nfc_device *pnd); NFC_EXPORT bool nfc_initiator_transceive_bytes (nfc_device *pnd, const uint8_t *pbtTx, const size_t szTx, uint8_t *pbtRx, size_t *pszRx, int timeout); NFC_EXPORT bool nfc_initiator_transceive_bits (nfc_device *pnd, const uint8_t *pbtTx, const size_t szTxBits, const uint8_t *pbtTxPar, uint8_t *pbtRx, size_t *pszRxBits, uint8_t *pbtRxPar); diff --git a/libnfc/chips/pn53x.c b/libnfc/chips/pn53x.c index 8627622..befa800 100644 --- a/libnfc/chips/pn53x.c +++ b/libnfc/chips/pn53x.c @@ -1038,7 +1038,8 @@ bool pn53x_initiator_select_dep_target(nfc_device *pnd, const nfc_dep_mode ndm, const nfc_baud_rate nbr, const nfc_dep_info *pndiInitiator, - nfc_target *pnt) + nfc_target *pnt, + const int timeout) { const uint8_t abtPassiveInitiatorData[] = { 0x00, 0xff, 0xff, 0x00, 0x00 }; // Only for 212/424 kpbs: First 4 bytes shall be set like this according to NFCIP-1, last byte is TSN (Time Slot Number) const uint8_t * pbtPassiveInitiatorData = NULL; @@ -1056,9 +1057,9 @@ pn53x_initiator_select_dep_target(nfc_device *pnd, } if (pndiInitiator) { - return pn53x_InJumpForDEP (pnd, ndm, nbr, pbtPassiveInitiatorData, pndiInitiator->abtNFCID3, pndiInitiator->abtGB, pndiInitiator->szGB, pnt); + return pn53x_InJumpForDEP (pnd, ndm, nbr, pbtPassiveInitiatorData, pndiInitiator->abtNFCID3, pndiInitiator->abtGB, pndiInitiator->szGB, pnt, timeout); } else { - return pn53x_InJumpForDEP (pnd, ndm, nbr, pbtPassiveInitiatorData, NULL, NULL, 0, pnt); + return pn53x_InJumpForDEP (pnd, ndm, nbr, pbtPassiveInitiatorData, NULL, NULL, 0, pnt, timeout); } } @@ -2199,7 +2200,8 @@ pn53x_InJumpForDEP (nfc_device *pnd, const uint8_t *pbtPassiveInitiatorData, const uint8_t *pbtNFCID3i, const uint8_t *pbtGBi, const size_t szGBi, - nfc_target *pnt) + nfc_target *pnt, + const int timeout) { // Max frame size = 1 (Command) + 1 (ActPass) + 1 (Baud rate) + 1 (Next) + 5 (PassiveInitiatorData) + 10 (NFCID3) + 48 (General bytes) = 67 bytes uint8_t abtCmd[67] = { InJumpForDEP, (ndm == NDM_ACTIVE) ? 0x01 : 0x00 }; @@ -2259,7 +2261,7 @@ pn53x_InJumpForDEP (nfc_device *pnd, uint8_t abtRx[PN53x_EXTENDED_FRAME__DATA_MAX_LEN]; size_t szRx = sizeof (abtRx); // Try to find a target, call the transceive callback function of the current device - if (!pn53x_transceive (pnd, abtCmd, offset, abtRx, &szRx, 0)) + if (!pn53x_transceive (pnd, abtCmd, offset, abtRx, &szRx, timeout)) return false; // Make sure one target has been found, the PN53X returns 0x00 if none was available @@ -2270,6 +2272,7 @@ pn53x_InJumpForDEP (nfc_device *pnd, if (pnt) { pnt->nm.nmt = NMT_DEP; pnt->nm.nbr = nbr; + pnt->nti.ndi.ndm = ndm; memcpy (pnt->nti.ndi.abtNFCID3, abtRx + 2, 10); pnt->nti.ndi.btDID = abtRx[12]; pnt->nti.ndi.btBS = abtRx[13]; diff --git a/libnfc/chips/pn53x.h b/libnfc/chips/pn53x.h index e892f0b..872e449 100644 --- a/libnfc/chips/pn53x.h +++ b/libnfc/chips/pn53x.h @@ -287,7 +287,8 @@ bool pn53x_initiator_poll_target (nfc_device *pnd, bool pn53x_initiator_select_dep_target (nfc_device *pnd, const nfc_dep_mode ndm, const nfc_baud_rate nbr, const nfc_dep_info *pndiInitiator, - nfc_target *pnt); + nfc_target *pnt, + const int timeout); bool pn53x_initiator_transceive_bits (nfc_device *pnd, const uint8_t *pbtTx, const size_t szTxBits, const uint8_t *pbtTxPar, uint8_t *pbtRx, size_t *pszRxBits, uint8_t *pbtRxPar); @@ -328,7 +329,8 @@ bool pn53x_InJumpForDEP (nfc_device *pnd, const uint8_t *pbtPassiveInitiatorData, const uint8_t *pbtNFCID3i, const uint8_t *pbtGB, const size_t szGB, - nfc_target *pnt); + nfc_target *pnt, + const int timeout); bool pn53x_TgInitAsTarget (nfc_device *pnd, pn53x_target_mode ptm, const uint8_t *pbtMifareParams, const uint8_t *pbtTkt, size_t szTkt, diff --git a/libnfc/drivers/pn53x_usb.c b/libnfc/drivers/pn53x_usb.c index cb8ac34..2f22b7a 100644 --- a/libnfc/drivers/pn53x_usb.c +++ b/libnfc/drivers/pn53x_usb.c @@ -571,6 +571,7 @@ read: if (timeout == USB_INFINITE_TIMEOUT) { usb_timeout = USB_TIMEOUT_PER_PASS; } else { + // A user-provided timeout is set, we have to cut it in multiple chunk to be able to keep an nfc_abort_command() mecanism remaining_time -= USB_TIMEOUT_PER_PASS; if (remaining_time <= 0) { pnd->iLastError = ECOMTIMEOUT; diff --git a/libnfc/nfc-internal.h b/libnfc/nfc-internal.h index d34c706..e59edc4 100644 --- a/libnfc/nfc-internal.h +++ b/libnfc/nfc-internal.h @@ -134,7 +134,7 @@ struct nfc_driver_t { bool (*initiator_init) (nfc_device *pnd); bool (*initiator_select_passive_target) (nfc_device *pnd, const nfc_modulation nm, const uint8_t * pbtInitData, const size_t szInitData, nfc_target * pnt); bool (*initiator_poll_target) (nfc_device *pnd, const nfc_modulation * pnmModulations, const size_t szModulations, const uint8_t uiPollNr, const uint8_t btPeriod, nfc_target * pnt); - bool (*initiator_select_dep_target) (nfc_device *pnd, const nfc_dep_mode ndm, const nfc_baud_rate nbr, const nfc_dep_info * pndiInitiator, nfc_target * pnt); + bool (*initiator_select_dep_target) (nfc_device *pnd, const nfc_dep_mode ndm, const nfc_baud_rate nbr, const nfc_dep_info * pndiInitiator, nfc_target * pnt, const int timeout); bool (*initiator_deselect_target) (nfc_device *pnd); bool (*initiator_transceive_bytes) (nfc_device *pnd, const uint8_t * pbtTx, const size_t szTx, uint8_t * pbtRx, size_t * pszRx, int timeout); bool (*initiator_transceive_bits) (nfc_device *pnd, const uint8_t * pbtTx, const size_t szTxBits, const uint8_t * pbtTxPar, uint8_t * pbtRx, size_t * pszRxBits, uint8_t * pbtRxPar); diff --git a/libnfc/nfc.c b/libnfc/nfc.c index 8c4c627..975a7b3 100644 --- a/libnfc/nfc.c +++ b/libnfc/nfc.c @@ -432,9 +432,9 @@ nfc_initiator_poll_target (nfc_device *pnd, bool nfc_initiator_select_dep_target (nfc_device *pnd, const nfc_dep_mode ndm, const nfc_baud_rate nbr, - const nfc_dep_info *pndiInitiator, nfc_target *pnt) + const nfc_dep_info *pndiInitiator, nfc_target *pnt, const int timeout) { - HAL (initiator_select_dep_target, pnd, ndm, nbr, pndiInitiator, pnt); + HAL (initiator_select_dep_target, pnd, ndm, nbr, pndiInitiator, pnt, timeout); } /** diff --git a/test/test_dep.c b/test/test_dep.c index a065ff6..bf81893 100644 --- a/test/test_dep.c +++ b/test/test_dep.c @@ -55,10 +55,10 @@ void * target_thread (void *arg) { intptr_t thread_res = 0; -// nfc_device *device = ((struct thread_data *) arg)->device; + nfc_device *device = ((struct thread_data *) arg)->device; cut_set_current_test_context (((struct thread_data *) arg)->cut_test_context); -#if 0 + printf ("=========== TARGET %s =========\n", nfc_device_name (device)); nfc_target nt = { .nm = { .nmt = NMT_DEP, @@ -69,7 +69,7 @@ target_thread (void *arg) .abtNFCID3 = { 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xAA }, .szGB = 4, .abtGB = { 0x12, 0x34, 0x56, 0x78 }, - .ndm = NDM_UNDEFINED, + .ndm = NDM_PASSIVE, /* These bytes are not used by nfc_target_init: the chip will provide them automatically to the initiator */ .btDID = 0x00, .btBS = 0x00, @@ -83,21 +83,46 @@ target_thread (void *arg) uint8_t abtRx[1024]; size_t szRx = sizeof (abtRx); bool res = nfc_target_init (device, &nt, abtRx, &szRx); - // cut_assert_true (res, cut_message ("Can't initialize NFC device as target")); + cut_assert_true (res, cut_message ("Can't initialize NFC device as target: %s", nfc_strerror (device))); - uint8_t abtAtrRes[] = "\x11\xd4\x00\x01\xfe\x12\x34\x56\x78\x90\x12\x00\x00\x00\x00\x00\x00"; - // cut_assert_equal_memory (abtAtrRes, sizeof (abtAtrRes) - 1, abtRx, szRx, cut_message ("Invalid received ATR_RES")); + const uint8_t abtAtrRes[] = "\x11\xd4\x00\x01\xfe\x12\x34\x56\x78\x90\x12\x00\x00\x00\x00\x00\x00"; + cut_assert_equal_memory (abtAtrRes, sizeof (abtAtrRes) - 1, abtRx, szRx, cut_message ("Invalid received ATR_RES")); + if (!res) { thread_res = -1; return (void*) thread_res; } - res = nfc_target_receive_bytes (device, abtRx, &szRx); - // cut_assert_true (res, cut_message ("Can't receive bytes from initiator")); + // First pass + res = nfc_target_receive_bytes (device, abtRx, &szRx, 500); + cut_assert_true (res, cut_message ("Can't receive bytes from initiator: %s", nfc_strerror (device))); - uint8_t abtAttRx[] = "Hello DEP target!"; - // cut_assert_equal_memory (abtAttRx, sizeof (abtAttRx), abtRx, szRx, cut_message ("Invalid received data")); + const uint8_t abtAttRx[] = "Hello DEP target!"; + cut_assert_equal_memory (abtAttRx, sizeof (abtAttRx), abtRx, szRx, cut_message ("Invalid received data")); + if (!res) { thread_res = -1; return (void*) thread_res; } - uint8_t abtTx[] = "Hello DEP initiator!"; - res = nfc_target_send_bytes (device, abtTx, sizeof(abtTx)); - // cut_assert_true (res, cut_message ("Can't send bytes to initiator")); -#endif + const uint8_t abtTx[] = "Hello DEP initiator!"; + res = nfc_target_send_bytes (device, abtTx, sizeof(abtTx), 500); + cut_assert_true (res, cut_message ("Can't send bytes to initiator: %s", nfc_strerror (device))); + if (!res) { thread_res = -1; return (void*) thread_res; } + + // Second pass + res = nfc_target_receive_bytes (device, abtRx, &szRx, 500); + cut_assert_true (res, cut_message ("Can't receive bytes from initiator: %s", nfc_strerror (device))); + + cut_assert_equal_memory (abtAttRx, sizeof (abtAttRx), abtRx, szRx, cut_message ("Invalid received data")); + if (!res) { thread_res = -1; return (void*) thread_res; } + + res = nfc_target_send_bytes (device, abtTx, sizeof(abtTx), 500); + cut_assert_true (res, cut_message ("Can't send bytes to initiator: %s", nfc_strerror (device))); + if (!res) { thread_res = -1; return (void*) thread_res; } + + // Third pass + res = nfc_target_receive_bytes (device, abtRx, &szRx, 500); + cut_assert_true (res, cut_message ("Can't receive bytes from initiator: %s", nfc_strerror (device))); + + cut_assert_equal_memory (abtAttRx, sizeof (abtAttRx), abtRx, szRx, cut_message ("Invalid received data")); + if (!res) { thread_res = -1; return (void*) thread_res; } + + res = nfc_target_send_bytes (device, abtTx, sizeof(abtTx), 500); + cut_assert_true (res, cut_message ("Can't send bytes to initiator: %s", nfc_strerror (device))); + if (!res) { thread_res = -1; return (void*) thread_res; } return (void *) thread_res; } @@ -106,45 +131,90 @@ void * initiator_thread (void *arg) { intptr_t thread_res = 0; -// nfc_device *device = ((struct thread_data *) arg)->device; + nfc_device *device = ((struct thread_data *) arg)->device; cut_set_current_test_context (((struct thread_data *) arg)->cut_test_context); - cut_fail("plop"); - -#if 0 /* * Wait some time for the other thread to initialise NFC device as target */ sleep (1); - printf ("====================================\n"); - printf ("Activating initiator...\n"); + printf ("=========== INITIATOR %s =========\n", nfc_device_name (device)); bool res = nfc_initiator_init (device); - // cut_assert_true (res, cut_message ("Can't initialize NFC device as initiator")); + cut_assert_true (res, cut_message ("Can't initialize NFC device as initiator: %s", nfc_strerror (device))); + if (!res) { thread_res = -1; return (void*) thread_res; } nfc_target nt; // Passive mode / 212Kbps - res = nfc_initiator_select_dep_target (device, NDM_PASSIVE, NBR_212, NULL, &nt); - // cut_assert_true (res, cut_message ("Can't select any DEP target")); - // cut_assert_equal_int (NMT_DEP, nt.nm.nmt, cut_message ("Invalid target modulation")); - // cut_assert_equal_int (NBR_212, nt.nm.nbr, cut_message ("Invalid target baud rate")); - // cut_assert_equal_memory ("\x11\x22\x33\x44\x55\x66\x77\x88\x99\xAA", 10, nt.nti.ndi.abtNFCID3, 10, cut_message ("Invalid target NFCID3")); - // cut_assert_equal_int (NDM_PASSIVE, nt.nti.ndi.ndm, cut_message ("Invalid target DEP mode")); - // cut_assert_equal_memory ("\x12\x34\x56\x78", 4, nt.nti.ndi.abtGB, nt.nti.ndi.szGB, cut_message ("Invalid target general bytes")); + printf ("=========== INITIATOR %s (Passive mode / 212Kbps) =========\n", nfc_device_name (device)); + res = nfc_initiator_select_dep_target (device, NDM_PASSIVE, NBR_212, NULL, &nt, 5000); + cut_assert_true (res, cut_message ("Can't select any DEP target: %s", nfc_strerror (device))); + cut_assert_equal_int (NMT_DEP, nt.nm.nmt, cut_message ("Invalid target modulation")); + cut_assert_equal_int (NBR_212, nt.nm.nbr, cut_message ("Invalid target baud rate")); + cut_assert_equal_memory ("\x11\x22\x33\x44\x55\x66\x77\x88\x99\xAA", 10, nt.nti.ndi.abtNFCID3, 10, cut_message ("Invalid target NFCID3")); + cut_assert_equal_int (NDM_PASSIVE, nt.nti.ndi.ndm, cut_message ("Invalid target DEP mode")); + cut_assert_equal_memory ("\x12\x34\x56\x78", 4, nt.nti.ndi.abtGB, nt.nti.ndi.szGB, cut_message ("Invalid target general bytes")); + if (!res) { thread_res = -1; return (void*) thread_res; } - uint8_t abtTx[] = "Hello DEP target!"; + const uint8_t abtTx[] = "Hello DEP target!"; uint8_t abtRx[1024]; size_t szRx = sizeof (abtRx); - res = nfc_initiator_transceive_bytes (device, abtTx, sizeof (abtTx), abtRx, &szRx); - // cut_assert_true (res, cut_message ("Can't transceive bytes to target")); + res = nfc_initiator_transceive_bytes (device, abtTx, sizeof (abtTx), abtRx, &szRx, 500); + cut_assert_true (res, cut_message ("Can't transceive bytes to target: %s", nfc_strerror (device))); - uint8_t abtAttRx[] = "Hello DEP initiator!"; - // cut_assert_equal_memory (abtAttRx, sizeof (abtAttRx), abtRx, szRx, cut_message ("Invalid received data")); + const uint8_t abtAttRx[] = "Hello DEP initiator!"; + cut_assert_equal_memory (abtAttRx, sizeof (abtAttRx), abtRx, szRx, cut_message ("Invalid received data")); + if (!res) { thread_res = -1; return (void*) thread_res; } res = nfc_initiator_deselect_target (device); - // cut_assert_true (res, cut_message ("Can't deselect target")); -#endif + cut_assert_true (res, cut_message ("Can't deselect target: %s", nfc_strerror (device))); + if (!res) { thread_res = -1; return (void*) thread_res; } + + // Passive mode / 212Kbps (second pass) + printf ("=========== INITIATOR %s (Passive mode / 212Kbps, second pass) =========\n", nfc_device_name (device)); + res = nfc_initiator_select_dep_target (device, NDM_PASSIVE, NBR_212, NULL, &nt, 1000); + cut_assert_true (res, cut_message ("Can't select any DEP target: %s", nfc_strerror (device))); + cut_assert_equal_int (NMT_DEP, nt.nm.nmt, cut_message ("Invalid target modulation")); + cut_assert_equal_int (NBR_212, nt.nm.nbr, cut_message ("Invalid target baud rate")); + cut_assert_equal_memory ("\x11\x22\x33\x44\x55\x66\x77\x88\x99\xAA", 10, nt.nti.ndi.abtNFCID3, 10, cut_message ("Invalid target NFCID3")); + cut_assert_equal_int (NDM_PASSIVE, nt.nti.ndi.ndm, cut_message ("Invalid target DEP mode")); + cut_assert_equal_memory ("\x12\x34\x56\x78", 4, nt.nti.ndi.abtGB, nt.nti.ndi.szGB, cut_message ("Invalid target general bytes")); + if (!res) { thread_res = -1; return (void*) thread_res; } + + szRx = sizeof (abtRx); + res = nfc_initiator_transceive_bytes (device, abtTx, sizeof (abtTx), abtRx, &szRx, 1000); + cut_assert_true (res, cut_message ("Can't transceive bytes to target: %s", nfc_strerror (device))); + + cut_assert_equal_memory (abtAttRx, sizeof (abtAttRx), abtRx, szRx, cut_message ("Invalid received data")); + if (!res) { thread_res = -1; return (void*) thread_res; } + + res = nfc_initiator_deselect_target (device); + cut_assert_true (res, cut_message ("Can't deselect target: %s", nfc_strerror (device))); + if (!res) { thread_res = -1; return (void*) thread_res; } + + // Passive mode / 424Kbps + printf ("=========== INITIATOR %s (Passive mode / 424Kbps) =========\n", nfc_device_name (device)); + res = nfc_initiator_select_dep_target (device, NDM_PASSIVE, NBR_424, NULL, &nt, 1000); + cut_assert_true (res, cut_message ("Can't select any DEP target: %s", nfc_strerror (device))); + cut_assert_equal_int (NMT_DEP, nt.nm.nmt, cut_message ("Invalid target modulation")); + cut_assert_equal_int (NBR_424, nt.nm.nbr, cut_message ("Invalid target baud rate")); + cut_assert_equal_memory ("\x11\x22\x33\x44\x55\x66\x77\x88\x99\xAA", 10, nt.nti.ndi.abtNFCID3, 10, cut_message ("Invalid target NFCID3")); + cut_assert_equal_int (NDM_PASSIVE, nt.nti.ndi.ndm, cut_message ("Invalid target DEP mode")); + cut_assert_equal_memory ("\x12\x34\x56\x78", 4, nt.nti.ndi.abtGB, nt.nti.ndi.szGB, cut_message ("Invalid target general bytes")); + if (!res) { thread_res = -1; return (void*) thread_res; } + + szRx = sizeof (abtRx); + res = nfc_initiator_transceive_bytes (device, abtTx, sizeof (abtTx), abtRx, &szRx, 5000); + cut_assert_true (res, cut_message ("Can't transceive bytes to target: %s", nfc_strerror (device))); + + cut_assert_equal_memory (abtAttRx, sizeof (abtAttRx), abtRx, szRx, cut_message ("Invalid received data")); + if (!res) { thread_res = -1; return (void*) thread_res; } + + res = nfc_initiator_deselect_target (device); + cut_assert_true (res, cut_message ("Can't deselect target: %s", nfc_strerror (device))); + if (!res) { thread_res = -1; return (void*) thread_res; } + return (void *) thread_res; } From 90c05c7d1361ef319331d8ca7fc5683a0a8d1a22 Mon Sep 17 00:00:00 2001 From: Romuald Conty Date: Wed, 7 Dec 2011 14:59:40 +0000 Subject: [PATCH 016/113] Add a printf-based logging when log4c is not available (I experiemented some bugs using log4c) --- examples/nfc-dep-target.c | 2 +- include/nfc/nfc-types.h | 3 ++ libnfc/Makefile.am | 15 +++++- libnfc/{log.c => log-log4c.c} | 1 + libnfc/log-printf.c | 66 +++++++++++++++++++++++++ libnfc/log.h | 93 +++++++++++++++++++++-------------- libnfc/nfc-internal.h | 2 +- 7 files changed, 140 insertions(+), 42 deletions(-) rename libnfc/{log.c => log-log4c.c} (98%) create mode 100644 libnfc/log-printf.c diff --git a/examples/nfc-dep-target.c b/examples/nfc-dep-target.c index b7b45a1..7ed991e 100644 --- a/examples/nfc-dep-target.c +++ b/examples/nfc-dep-target.c @@ -77,7 +77,7 @@ main (int argc, const char *argv[]) } else if (szDeviceFound > 1) { pnd = nfc_connect (connstrings[1]); } else { - printf("No device found."); + printf("No device found.\n"); return EXIT_FAILURE; } diff --git a/include/nfc/nfc-types.h b/include/nfc/nfc-types.h index 647ccce..bac47f5 100644 --- a/include/nfc/nfc-types.h +++ b/include/nfc/nfc-types.h @@ -69,6 +69,9 @@ typedef struct { int iLastError; } nfc_device; +/** + * Connection string + */ typedef char nfc_connstring[1024]; // Compiler directive, set struct alignment to 1 uint8_t for compatibility diff --git a/libnfc/Makefile.am b/libnfc/Makefile.am index f596549..a48d818 100644 --- a/libnfc/Makefile.am +++ b/libnfc/Makefile.am @@ -39,7 +39,18 @@ if HAS_LOG4C libnfc_la_CFLAGS += @log4c_CFLAGS@ libnfc_la_LIBADD += @log4c_LIBS@ - libnfc_la_SOURCES += log.c + libnfc_la_SOURCES += log-log4c.c +else +if WITH_DEBUG + libnfc_la_CFLAGS += @log4c_CFLAGS@ + libnfc_la_LIBADD += @log4c_LIBS@ + + libnfc_la_SOURCES += log-printf.c +endif endif -EXTRA_DIST = CMakeLists.txt +EXTRA_DIST = \ + CMakeLists.txt \ + log-log4c.c \ + log-printf.c + diff --git a/libnfc/log.c b/libnfc/log-log4c.c similarity index 98% rename from libnfc/log.c rename to libnfc/log-log4c.c index 5d12136..ef0cf2e 100644 --- a/libnfc/log.c +++ b/libnfc/log-log4c.c @@ -61,5 +61,6 @@ log_put (char *category, int priority, char *format, ...) va_list va; va_start (va, format); log4c_category_vlog (cat, priority, format, va); +// va_end (va); } } diff --git a/libnfc/log-printf.c b/libnfc/log-printf.c new file mode 100644 index 0000000..1ef4203 --- /dev/null +++ b/libnfc/log-printf.c @@ -0,0 +1,66 @@ +/*- + * Copyright (C) 2011, Romain Tartière, Romuald Conty + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as published by the + * Free Software Foundation, either version 3 of the License, or (at your + * option) any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program. If not, see + */ + +#include "config.h" + +#include +#include +#include + +#include "log.h" + +static uint8_t __log_init_counter = 0; + +int +log_init (void) +{ + int res = 0; + + if (__log_init_counter == 0) { + res = 0; + } + if (!res) { + __log_init_counter++; + } + return res; +} + +int +log_fini (void) +{ + int res = 0; + if (__log_init_counter >= 1) { + if (__log_init_counter == 1) { + res = 0; + } + __log_init_counter--; + } else { + res = -1; + } + return res; +} + +void +log_put (char *category, char *priority, char *format, ...) +{ + va_list va; + va_start (va, format); + printf ("%s\t%s\t", priority, category); + vprintf (format, va); + printf ("\n"); + va_end (va); +} diff --git a/libnfc/log.h b/libnfc/log.h index 90cb747..9d8b543 100644 --- a/libnfc/log.h +++ b/libnfc/log.h @@ -18,45 +18,62 @@ #ifndef __LOG_H__ #define __LOG_H__ +#ifdef HAVE_CONFIG_H +# include "config.h" +#endif // HAVE_CONFIG_H + #if defined(HAS_LOG4C) && HAS_LOG4C - -#define LOGGING 1 - -#include - -int log_init (void); -int log_fini (void); -void log_put (char *category, int priority, char *format, ...); - -#define NFC_PRIORITY_FATAL LOG4C_PRIORITY_FATAL -#define NFC_PRIORITY_ALERT LOG4C_PRIORITY_ALERT -#define NFC_PRIORITY_CRIT LOG4C_PRIORITY_CRIT -#define NFC_PRIORITY_ERROR LOG4C_PRIORITY_ERROR -#define NFC_PRIORITY_WARN LOG4C_PRIORITY_WARN -#define NFC_PRIORITY_NOTICE LOG4C_PRIORITY_NOTICE -#define NFC_PRIORITY_INFO LOG4C_PRIORITY_INFO -#define NFC_PRIORITY_DEBUG LOG4C_PRIORITY_DEBUG -#define NFC_PRIORITY_TRACE LOG4C_PRIORITY_TRACE - -#else /* HAS_LOG4C */ - -#define log_init() (0) -#define log_fini() (0) -#define log_msg(category, priority, message) do {} while (0) -#define log_set_appender(category, appender) do {} while (0) -#define log_put(category, priority, format, ...) do {} while (0) - -#define NFC_PRIORITY_FATAL 8 -#define NFC_PRIORITY_ALERT 7 -#define NFC_PRIORITY_CRIT 6 -#define NFC_PRIORITY_ERROR 5 -#define NFC_PRIORITY_WARN 4 -#define NFC_PRIORITY_NOTICE 3 -#define NFC_PRIORITY_INFO 2 -#define NFC_PRIORITY_DEBUG 1 -#define NFC_PRIORITY_TRACE 0 - -#endif /* HAS_LOG4C */ + // log4c have been detected so we use it.. + #include + #define LOGGING 1 + + int log_init (void); + int log_fini (void); + void log_put (char *category, int priority, char *format, ...); + + #define NFC_PRIORITY_FATAL LOG4C_PRIORITY_FATAL + #define NFC_PRIORITY_ALERT LOG4C_PRIORITY_ALERT + #define NFC_PRIORITY_CRIT LOG4C_PRIORITY_CRIT + #define NFC_PRIORITY_ERROR LOG4C_PRIORITY_ERROR + #define NFC_PRIORITY_WARN LOG4C_PRIORITY_WARN + #define NFC_PRIORITY_NOTICE LOG4C_PRIORITY_NOTICE + #define NFC_PRIORITY_INFO LOG4C_PRIORITY_INFO + #define NFC_PRIORITY_DEBUG LOG4C_PRIORITY_DEBUG + #define NFC_PRIORITY_TRACE LOG4C_PRIORITY_TRACE +#elif defined DEBUG + // log4c is not detected but user want debug features + #define LOGGING 1 + int log_init (void); + int log_fini (void); + void log_put (char *category, char *priority, char *format, ...); + + #define NFC_PRIORITY_FATAL "fatal" + #define NFC_PRIORITY_ALERT "alert" + #define NFC_PRIORITY_CRIT "critical" + #define NFC_PRIORITY_ERROR "error" + #define NFC_PRIORITY_WARN "warning" + #define NFC_PRIORITY_NOTICE "notice" + #define NFC_PRIORITY_INFO "info" + #define NFC_PRIORITY_DEBUG "debug" + #define NFC_PRIORITY_TRACE "trace" +#else + // No logging + #define log_init() (0) + #define log_fini() (0) + #define log_msg(category, priority, message) do {} while (0) + #define log_set_appender(category, appender) do {} while (0) + #define log_put(category, priority, format, ...) do {} while (0) + + #define NFC_PRIORITY_FATAL 8 + #define NFC_PRIORITY_ALERT 7 + #define NFC_PRIORITY_CRIT 6 + #define NFC_PRIORITY_ERROR 5 + #define NFC_PRIORITY_WARN 4 + #define NFC_PRIORITY_NOTICE 3 + #define NFC_PRIORITY_INFO 2 + #define NFC_PRIORITY_DEBUG 1 + #define NFC_PRIORITY_TRACE 0 +#endif /* HAS_LOG4C, DEBUG */ /** * @macro LOG_HEX diff --git a/libnfc/nfc-internal.h b/libnfc/nfc-internal.h index e59edc4..b52566a 100644 --- a/libnfc/nfc-internal.h +++ b/libnfc/nfc-internal.h @@ -127,7 +127,7 @@ struct nfc_driver_t { const char *name; bool (*probe)(nfc_connstring connstrings[], size_t connstrings_len, size_t * pszDeviceFound); - nfc_device * (*connect) (const nfc_connstring connstring); + nfc_device *(*connect) (const nfc_connstring connstring); void (*disconnect) (nfc_device *pnd); const char *(*strerror) (const nfc_device *pnd); From cc2622c49ff6a751fd1d4a199cf7f8de7ddaad73 Mon Sep 17 00:00:00 2001 From: Audrey Diacre Date: Fri, 9 Dec 2011 13:41:51 +0000 Subject: [PATCH 017/113] test/test_dep*: rename test_dep to test_dep_passive and add new 106kbps pass. --- test/Makefile.am | 6 ++-- test/{test_dep.c => test_dep_passive.c} | 47 ++++++++++++++++++++----- 2 files changed, 42 insertions(+), 11 deletions(-) rename test/{test_dep.c => test_dep_passive.c} (82%) diff --git a/test/Makefile.am b/test/Makefile.am index bc28640..9174bfa 100644 --- a/test/Makefile.am +++ b/test/Makefile.am @@ -9,7 +9,7 @@ TESTS_ENVIRONMENT = NO_MAKE=yes CUTTER="$(CUTTER)" cutter_unit_test_libs = \ test_access_storm.la \ - test_dep.la \ + test_dep_passive.la \ test_register_access.la \ test_register_endianness.la @@ -24,8 +24,8 @@ AM_LDFLAGS = -module -rpath $(libdir) -avoid-version -no-undefined test_access_storm_la_SOURCES = test_access_storm.c test_access_storm_la_LIBADD = $(top_builddir)/libnfc/libnfc.la -test_dep_la_SOURCES = test_dep.c -test_dep_la_LIBADD = $(top_builddir)/libnfc/libnfc.la +test_dep_passive_la_SOURCES = test_dep_passive.c +test_dep_passive_la_LIBADD = $(top_builddir)/libnfc/libnfc.la test_register_access_la_SOURCES = test_register_access.c test_register_access_la_LIBADD = $(top_builddir)/libnfc/libnfc.la diff --git a/test/test_dep.c b/test/test_dep_passive.c similarity index 82% rename from test/test_dep.c rename to test/test_dep_passive.c index bf81893..71ce58f 100644 --- a/test/test_dep.c +++ b/test/test_dep_passive.c @@ -85,8 +85,6 @@ target_thread (void *arg) bool res = nfc_target_init (device, &nt, abtRx, &szRx); cut_assert_true (res, cut_message ("Can't initialize NFC device as target: %s", nfc_strerror (device))); - const uint8_t abtAtrRes[] = "\x11\xd4\x00\x01\xfe\x12\x34\x56\x78\x90\x12\x00\x00\x00\x00\x00\x00"; - cut_assert_equal_memory (abtAtrRes, sizeof (abtAtrRes) - 1, abtRx, szRx, cut_message ("Invalid received ATR_RES")); if (!res) { thread_res = -1; return (void*) thread_res; } // First pass @@ -120,6 +118,17 @@ target_thread (void *arg) cut_assert_equal_memory (abtAttRx, sizeof (abtAttRx), abtRx, szRx, cut_message ("Invalid received data")); if (!res) { thread_res = -1; return (void*) thread_res; } + res = nfc_target_send_bytes (device, abtTx, sizeof(abtTx), 500); + cut_assert_true (res, cut_message ("Can't send bytes to initiator: %s", nfc_strerror (device))); + if (!res) { thread_res = -1; return (void*) thread_res; } + + // Fourth pass + res = nfc_target_receive_bytes (device, abtRx, &szRx, 500); + cut_assert_true (res, cut_message ("Can't receive bytes from initiator: %s", nfc_strerror (device))); + + cut_assert_equal_memory (abtAttRx, sizeof (abtAttRx), abtRx, szRx, cut_message ("Invalid received data")); + if (!res) { thread_res = -1; return (void*) thread_res; } + res = nfc_target_send_bytes (device, abtTx, sizeof(abtTx), 500); cut_assert_true (res, cut_message ("Can't send bytes to initiator: %s", nfc_strerror (device))); if (!res) { thread_res = -1; return (void*) thread_res; } @@ -146,12 +155,12 @@ initiator_thread (void *arg) nfc_target nt; - // Passive mode / 212Kbps - printf ("=========== INITIATOR %s (Passive mode / 212Kbps) =========\n", nfc_device_name (device)); - res = nfc_initiator_select_dep_target (device, NDM_PASSIVE, NBR_212, NULL, &nt, 5000); + // Passive mode / 106Kbps + printf ("=========== INITIATOR %s (Passive mode / 106Kbps) =========\n", nfc_device_name (device)); + res = nfc_initiator_select_dep_target (device, NDM_PASSIVE, NBR_106, NULL, &nt, 5000); cut_assert_true (res, cut_message ("Can't select any DEP target: %s", nfc_strerror (device))); cut_assert_equal_int (NMT_DEP, nt.nm.nmt, cut_message ("Invalid target modulation")); - cut_assert_equal_int (NBR_212, nt.nm.nbr, cut_message ("Invalid target baud rate")); + cut_assert_equal_int (NBR_106, nt.nm.nbr, cut_message ("Invalid target baud rate")); cut_assert_equal_memory ("\x11\x22\x33\x44\x55\x66\x77\x88\x99\xAA", 10, nt.nti.ndi.abtNFCID3, 10, cut_message ("Invalid target NFCID3")); cut_assert_equal_int (NDM_PASSIVE, nt.nti.ndi.ndm, cut_message ("Invalid target DEP mode")); cut_assert_equal_memory ("\x12\x34\x56\x78", 4, nt.nti.ndi.abtGB, nt.nti.ndi.szGB, cut_message ("Invalid target general bytes")); @@ -172,7 +181,7 @@ initiator_thread (void *arg) if (!res) { thread_res = -1; return (void*) thread_res; } // Passive mode / 212Kbps (second pass) - printf ("=========== INITIATOR %s (Passive mode / 212Kbps, second pass) =========\n", nfc_device_name (device)); + printf ("=========== INITIATOR %s (Passive mode / 212Kbps) =========\n", nfc_device_name (device)); res = nfc_initiator_select_dep_target (device, NDM_PASSIVE, NBR_212, NULL, &nt, 1000); cut_assert_true (res, cut_message ("Can't select any DEP target: %s", nfc_strerror (device))); cut_assert_equal_int (NMT_DEP, nt.nm.nmt, cut_message ("Invalid target modulation")); @@ -193,7 +202,29 @@ initiator_thread (void *arg) cut_assert_true (res, cut_message ("Can't deselect target: %s", nfc_strerror (device))); if (!res) { thread_res = -1; return (void*) thread_res; } - // Passive mode / 424Kbps + // Passive mode / 212Kbps + printf ("=========== INITIATOR %s (Passive mode / 212Kbps, second pass) =========\n", nfc_device_name (device)); + res = nfc_initiator_select_dep_target (device, NDM_PASSIVE, NBR_212, NULL, &nt, 1000); + cut_assert_true (res, cut_message ("Can't select any DEP target: %s", nfc_strerror (device))); + cut_assert_equal_int (NMT_DEP, nt.nm.nmt, cut_message ("Invalid target modulation")); + cut_assert_equal_int (NBR_212, nt.nm.nbr, cut_message ("Invalid target baud rate")); + cut_assert_equal_memory ("\x11\x22\x33\x44\x55\x66\x77\x88\x99\xAA", 10, nt.nti.ndi.abtNFCID3, 10, cut_message ("Invalid target NFCID3")); + cut_assert_equal_int (NDM_PASSIVE, nt.nti.ndi.ndm, cut_message ("Invalid target DEP mode")); + cut_assert_equal_memory ("\x12\x34\x56\x78", 4, nt.nti.ndi.abtGB, nt.nti.ndi.szGB, cut_message ("Invalid target general bytes")); + if (!res) { thread_res = -1; return (void*) thread_res; } + + szRx = sizeof (abtRx); + res = nfc_initiator_transceive_bytes (device, abtTx, sizeof (abtTx), abtRx, &szRx, 5000); + cut_assert_true (res, cut_message ("Can't transceive bytes to target: %s", nfc_strerror (device))); + + cut_assert_equal_memory (abtAttRx, sizeof (abtAttRx), abtRx, szRx, cut_message ("Invalid received data")); + if (!res) { thread_res = -1; return (void*) thread_res; } + + res = nfc_initiator_deselect_target (device); + cut_assert_true (res, cut_message ("Can't deselect target: %s", nfc_strerror (device))); + if (!res) { thread_res = -1; return (void*) thread_res; } + + // Passive mode / 424Kbps printf ("=========== INITIATOR %s (Passive mode / 424Kbps) =========\n", nfc_device_name (device)); res = nfc_initiator_select_dep_target (device, NDM_PASSIVE, NBR_424, NULL, &nt, 1000); cut_assert_true (res, cut_message ("Can't select any DEP target: %s", nfc_strerror (device))); From 1e66caecd69800a2566c0234ce08fe22b66447f8 Mon Sep 17 00:00:00 2001 From: Audrey Diacre Date: Fri, 9 Dec 2011 13:55:35 +0000 Subject: [PATCH 018/113] test/test_dep_active: add new DEP active test --- test/Makefile.am | 4 + test/test_dep_active.c | 279 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 283 insertions(+) create mode 100644 test/test_dep_active.c diff --git a/test/Makefile.am b/test/Makefile.am index 9174bfa..74bbb85 100644 --- a/test/Makefile.am +++ b/test/Makefile.am @@ -9,6 +9,7 @@ TESTS_ENVIRONMENT = NO_MAKE=yes CUTTER="$(CUTTER)" cutter_unit_test_libs = \ test_access_storm.la \ + test_dep_active.la \ test_dep_passive.la \ test_register_access.la \ test_register_endianness.la @@ -24,6 +25,9 @@ AM_LDFLAGS = -module -rpath $(libdir) -avoid-version -no-undefined test_access_storm_la_SOURCES = test_access_storm.c test_access_storm_la_LIBADD = $(top_builddir)/libnfc/libnfc.la +test_dep_active_la_SOURCES = test_dep_active.c +test_dep_active_la_LIBADD = $(top_builddir)/libnfc/libnfc.la + test_dep_passive_la_SOURCES = test_dep_passive.c test_dep_passive_la_LIBADD = $(top_builddir)/libnfc/libnfc.la diff --git a/test/test_dep_active.c b/test/test_dep_active.c new file mode 100644 index 0000000..b3174c4 --- /dev/null +++ b/test/test_dep_active.c @@ -0,0 +1,279 @@ +#include +#include +#include +#include + +#include "nfc/nfc.h" + +#define INITIATOR 0 +#define TARGET 1 + +pthread_t threads[2]; +nfc_connstring connstrings[2]; +nfc_device *devices[2]; +intptr_t result[2]; + +void +abort_test_by_keypress (int sig) +{ + (void) sig; + printf ("\033[0;1;31mSIGINT\033[0m"); + + nfc_abort_command (devices[INITIATOR]); + nfc_abort_command (devices[TARGET]); +} + +void +cut_setup (void) +{ + size_t n; + + nfc_list_devices (connstrings, 2, &n); + if (n < 2) { + cut_omit ("At least two NFC devices must be plugged-in to run this test"); + } + + devices[TARGET] = nfc_connect (connstrings[TARGET]); + devices[INITIATOR] = nfc_connect (connstrings[INITIATOR]); + + signal (SIGINT, abort_test_by_keypress); +} + +void +cut_teardown (void) +{ + nfc_disconnect (devices[TARGET]); + nfc_disconnect (devices[INITIATOR]); +} + +struct thread_data { + nfc_device *device; + void *cut_test_context; +}; + +void * +target_thread (void *arg) +{ + intptr_t thread_res = 0; + nfc_device *device = ((struct thread_data *) arg)->device; + cut_set_current_test_context (((struct thread_data *) arg)->cut_test_context); + + printf ("=========== TARGET %s =========\n", nfc_device_name (device)); + nfc_target nt = { + .nm = { + .nmt = NMT_DEP, + .nbr = NBR_UNDEFINED + }, + .nti = { + .ndi = { + .abtNFCID3 = { 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xAA }, + .szGB = 4, + .abtGB = { 0x12, 0x34, 0x56, 0x78 }, + .ndm = NDM_ACTIVE, + /* These bytes are not used by nfc_target_init: the chip will provide them automatically to the initiator */ + .btDID = 0x00, + .btBS = 0x00, + .btBR = 0x00, + .btTO = 0x00, + .btPP = 0x01, + }, + }, + }; + + uint8_t abtRx[1024]; + size_t szRx = sizeof (abtRx); + bool res = nfc_target_init (device, &nt, abtRx, &szRx); + cut_assert_true (res, cut_message ("Can't initialize NFC device as target: %s", nfc_strerror (device))); + + if (!res) { thread_res = -1; return (void*) thread_res; } + + // First pass + res = nfc_target_receive_bytes (device, abtRx, &szRx, 500); + cut_assert_true (res, cut_message ("Can't receive bytes from initiator: %s", nfc_strerror (device))); + + const uint8_t abtAttRx[] = "Hello DEP target!"; + cut_assert_equal_memory (abtAttRx, sizeof (abtAttRx), abtRx, szRx, cut_message ("Invalid received data")); + if (!res) { thread_res = -1; return (void*) thread_res; } + + const uint8_t abtTx[] = "Hello DEP initiator!"; + res = nfc_target_send_bytes (device, abtTx, sizeof(abtTx), 500); + cut_assert_true (res, cut_message ("Can't send bytes to initiator: %s", nfc_strerror (device))); + if (!res) { thread_res = -1; return (void*) thread_res; } + + // Second pass + res = nfc_target_receive_bytes (device, abtRx, &szRx, 500); + cut_assert_true (res, cut_message ("Can't receive bytes from initiator: %s", nfc_strerror (device))); + + cut_assert_equal_memory (abtAttRx, sizeof (abtAttRx), abtRx, szRx, cut_message ("Invalid received data")); + if (!res) { thread_res = -1; return (void*) thread_res; } + + res = nfc_target_send_bytes (device, abtTx, sizeof(abtTx), 500); + cut_assert_true (res, cut_message ("Can't send bytes to initiator: %s", nfc_strerror (device))); + if (!res) { thread_res = -1; return (void*) thread_res; } + + // Third pass + res = nfc_target_receive_bytes (device, abtRx, &szRx, 500); + cut_assert_true (res, cut_message ("Can't receive bytes from initiator: %s", nfc_strerror (device))); + + cut_assert_equal_memory (abtAttRx, sizeof (abtAttRx), abtRx, szRx, cut_message ("Invalid received data")); + if (!res) { thread_res = -1; return (void*) thread_res; } + + res = nfc_target_send_bytes (device, abtTx, sizeof(abtTx), 500); + cut_assert_true (res, cut_message ("Can't send bytes to initiator: %s", nfc_strerror (device))); + if (!res) { thread_res = -1; return (void*) thread_res; } + + // Fourth pass + res = nfc_target_receive_bytes (device, abtRx, &szRx, 500); + cut_assert_true (res, cut_message ("Can't receive bytes from initiator: %s", nfc_strerror (device))); + + cut_assert_equal_memory (abtAttRx, sizeof (abtAttRx), abtRx, szRx, cut_message ("Invalid received data")); + if (!res) { thread_res = -1; return (void*) thread_res; } + + res = nfc_target_send_bytes (device, abtTx, sizeof(abtTx), 500); + cut_assert_true (res, cut_message ("Can't send bytes to initiator: %s", nfc_strerror (device))); + if (!res) { thread_res = -1; return (void*) thread_res; } + + return (void *) thread_res; +} + +void * +initiator_thread (void *arg) +{ + intptr_t thread_res = 0; + nfc_device *device = ((struct thread_data *) arg)->device; + cut_set_current_test_context (((struct thread_data *) arg)->cut_test_context); + + /* + * Wait some time for the other thread to initialise NFC device as target + */ + sleep (1); + printf ("=========== INITIATOR %s =========\n", nfc_device_name (device)); + + bool res = nfc_initiator_init (device); + cut_assert_true (res, cut_message ("Can't initialize NFC device as initiator: %s", nfc_strerror (device))); + if (!res) { thread_res = -1; return (void*) thread_res; } + + nfc_target nt; + + // Active mode / 106Kbps + printf ("=========== INITIATOR %s (Active mode / 106Kbps) =========\n", nfc_device_name (device)); + res = nfc_initiator_select_dep_target (device, NDM_ACTIVE, NBR_106, NULL, &nt, 5000); + cut_assert_true (res, cut_message ("Can't select any DEP target: %s", nfc_strerror (device))); + cut_assert_equal_int (NMT_DEP, nt.nm.nmt, cut_message ("Invalid target modulation")); + cut_assert_equal_int (NBR_106, nt.nm.nbr, cut_message ("Invalid target baud rate")); + cut_assert_equal_memory ("\x11\x22\x33\x44\x55\x66\x77\x88\x99\xAA", 10, nt.nti.ndi.abtNFCID3, 10, cut_message ("Invalid target NFCID3")); + cut_assert_equal_int (NDM_ACTIVE, nt.nti.ndi.ndm, cut_message ("Invalid target DEP mode")); + cut_assert_equal_memory ("\x12\x34\x56\x78", 4, nt.nti.ndi.abtGB, nt.nti.ndi.szGB, cut_message ("Invalid target general bytes")); + if (!res) { thread_res = -1; return (void*) thread_res; } + + const uint8_t abtTx[] = "Hello DEP target!"; + uint8_t abtRx[1024]; + size_t szRx = sizeof (abtRx); + res = nfc_initiator_transceive_bytes (device, abtTx, sizeof (abtTx), abtRx, &szRx, 500); + cut_assert_true (res, cut_message ("Can't transceive bytes to target: %s", nfc_strerror (device))); + + const uint8_t abtAttRx[] = "Hello DEP initiator!"; + cut_assert_equal_memory (abtAttRx, sizeof (abtAttRx), abtRx, szRx, cut_message ("Invalid received data")); + if (!res) { thread_res = -1; return (void*) thread_res; } + + res = nfc_initiator_deselect_target (device); + cut_assert_true (res, cut_message ("Can't deselect target: %s", nfc_strerror (device))); + if (!res) { thread_res = -1; return (void*) thread_res; } + + // Active mode / 212Kbps (second pass) + printf ("=========== INITIATOR %s (Active mode / 212Kbps) =========\n", nfc_device_name (device)); + res = nfc_initiator_select_dep_target (device, NDM_ACTIVE, NBR_212, NULL, &nt, 1000); + cut_assert_true (res, cut_message ("Can't select any DEP target: %s", nfc_strerror (device))); + cut_assert_equal_int (NMT_DEP, nt.nm.nmt, cut_message ("Invalid target modulation")); + cut_assert_equal_int (NBR_212, nt.nm.nbr, cut_message ("Invalid target baud rate")); + cut_assert_equal_memory ("\x11\x22\x33\x44\x55\x66\x77\x88\x99\xAA", 10, nt.nti.ndi.abtNFCID3, 10, cut_message ("Invalid target NFCID3")); + cut_assert_equal_int (NDM_ACTIVE, nt.nti.ndi.ndm, cut_message ("Invalid target DEP mode")); + cut_assert_equal_memory ("\x12\x34\x56\x78", 4, nt.nti.ndi.abtGB, nt.nti.ndi.szGB, cut_message ("Invalid target general bytes")); + if (!res) { thread_res = -1; return (void*) thread_res; } + + // szRx = sizeof (abtRx); + res = nfc_initiator_transceive_bytes (device, abtTx, sizeof (abtTx), abtRx, &szRx, 1000); + cut_assert_true (res, cut_message ("Can't transceive bytes to target: %s", nfc_strerror (device))); + + cut_assert_equal_memory (abtAttRx, sizeof (abtAttRx), abtRx, szRx, cut_message ("Invalid received data")); + if (!res) { thread_res = -1; return (void*) thread_res; } + + res = nfc_initiator_deselect_target (device); + cut_assert_true (res, cut_message ("Can't deselect target: %s", nfc_strerror (device))); + if (!res) { thread_res = -1; return (void*) thread_res; } + + // Active mode / 212Kbps + printf ("=========== INITIATOR %s (Active mode / 212Kbps, second pass) =========\n", nfc_device_name (device)); + res = nfc_initiator_select_dep_target (device, NDM_ACTIVE, NBR_212, NULL, &nt, 1000); + cut_assert_true (res, cut_message ("Can't select any DEP target: %s", nfc_strerror (device))); + cut_assert_equal_int (NMT_DEP, nt.nm.nmt, cut_message ("Invalid target modulation")); + cut_assert_equal_int (NBR_212, nt.nm.nbr, cut_message ("Invalid target baud rate")); + cut_assert_equal_memory ("\x11\x22\x33\x44\x55\x66\x77\x88\x99\xAA", 10, nt.nti.ndi.abtNFCID3, 10, cut_message ("Invalid target NFCID3")); + cut_assert_equal_int (NDM_ACTIVE, nt.nti.ndi.ndm, cut_message ("Invalid target DEP mode")); + cut_assert_equal_memory ("\x12\x34\x56\x78", 4, nt.nti.ndi.abtGB, nt.nti.ndi.szGB, cut_message ("Invalid target general bytes")); + if (!res) { thread_res = -1; return (void*) thread_res; } + + szRx = sizeof (abtRx); + res = nfc_initiator_transceive_bytes (device, abtTx, sizeof (abtTx), abtRx, &szRx, 5000); + cut_assert_true (res, cut_message ("Can't transceive bytes to target: %s", nfc_strerror (device))); + + cut_assert_equal_memory (abtAttRx, sizeof (abtAttRx), abtRx, szRx, cut_message ("Invalid received data")); + if (!res) { thread_res = -1; return (void*) thread_res; } + + res = nfc_initiator_deselect_target (device); + cut_assert_true (res, cut_message ("Can't deselect target: %s", nfc_strerror (device))); + if (!res) { thread_res = -1; return (void*) thread_res; } + + // Active mode / 424Kbps + printf ("=========== INITIATOR %s (Active mode / 424Kbps) =========\n", nfc_device_name (device)); + res = nfc_initiator_select_dep_target (device, NDM_ACTIVE, NBR_424, NULL, &nt, 1000); + cut_assert_true (res, cut_message ("Can't select any DEP target: %s", nfc_strerror (device))); + cut_assert_equal_int (NMT_DEP, nt.nm.nmt, cut_message ("Invalid target modulation")); + cut_assert_equal_int (NBR_424, nt.nm.nbr, cut_message ("Invalid target baud rate")); + cut_assert_equal_memory ("\x11\x22\x33\x44\x55\x66\x77\x88\x99\xAA", 10, nt.nti.ndi.abtNFCID3, 10, cut_message ("Invalid target NFCID3")); + cut_assert_equal_int (NDM_ACTIVE, nt.nti.ndi.ndm, cut_message ("Invalid target DEP mode")); + cut_assert_equal_memory ("\x12\x34\x56\x78", 4, nt.nti.ndi.abtGB, nt.nti.ndi.szGB, cut_message ("Invalid target general bytes")); + if (!res) { thread_res = -1; return (void*) thread_res; } + + szRx = sizeof (abtRx); + res = nfc_initiator_transceive_bytes (device, abtTx, sizeof (abtTx), abtRx, &szRx, 5000); + cut_assert_true (res, cut_message ("Can't transceive bytes to target: %s", nfc_strerror (device))); + + cut_assert_equal_memory (abtAttRx, sizeof (abtAttRx), abtRx, szRx, cut_message ("Invalid received data")); + if (!res) { thread_res = -1; return (void*) thread_res; } + + res = nfc_initiator_deselect_target (device); + cut_assert_true (res, cut_message ("Can't deselect target: %s", nfc_strerror (device))); + if (!res) { thread_res = -1; return (void*) thread_res; } + + return (void *) thread_res; +} + +void +test_dep (void) +{ + int res; + + CutTestContext *test_context = cut_get_current_test_context (); + struct thread_data target_data = { + .device = devices[TARGET], + .cut_test_context = test_context, + }; + if ((res = pthread_create (&(threads[TARGET]), NULL, target_thread, &target_data))) + cut_fail ("pthread_create() returned %d", res); + + struct thread_data initiator_data = { + .device = devices[INITIATOR], + .cut_test_context = test_context, + }; + if ((res = pthread_create (&(threads[INITIATOR]), NULL, initiator_thread, &initiator_data))) + cut_fail ("pthread_create() returned %d", res); + + if ((res = pthread_join (threads[INITIATOR], (void *) &result[INITIATOR]))) + cut_fail ("pthread_join() returned %d", res); + if ((res = pthread_join (threads[TARGET], (void *) &result[TARGET]))) + cut_fail ("pthread_join() returned %d", res); + + cut_assert_equal_int (0, result[INITIATOR], cut_message ("Unexpected initiator return code")); + cut_assert_equal_int (0, result[TARGET], cut_message ("Unexpected target return code")); +} From 2ac8df2b289b6b849b7dab47fc89aa6a260a59a7 Mon Sep 17 00:00:00 2001 From: Romuald Conty Date: Fri, 9 Dec 2011 16:13:32 +0000 Subject: [PATCH 019/113] contrib: move udev and devd files into contrib/ --- Makefile.am | 4 +--- configure.ac | 2 ++ contrib/Makefile.am | 5 ++++- contrib/devd/Makefile.am | 2 ++ pn53x.conf => contrib/devd/pn53x.conf | 0 pn53x.rules => contrib/udev/42-pn53x.rules | 0 contrib/udev/Makefile.am | 2 ++ 7 files changed, 11 insertions(+), 4 deletions(-) create mode 100644 contrib/devd/Makefile.am rename pn53x.conf => contrib/devd/pn53x.conf (100%) rename pn53x.rules => contrib/udev/42-pn53x.rules (100%) create mode 100644 contrib/udev/Makefile.am diff --git a/Makefile.am b/Makefile.am index 0d595db..273cc52 100644 --- a/Makefile.am +++ b/Makefile.am @@ -10,9 +10,7 @@ pkgconfig_DATA = libnfc.pc EXTRA_DIST = \ CMakeLists.txt \ Doxyfile \ - README-Windows.txt \ - pn53x.conf \ - pn53x.rules + README-Windows.txt CLEANFILES = Doxygen.log coverage.info libnfc.pc diff --git a/configure.ac b/configure.ac index 18ed07c..d2c5a10 100644 --- a/configure.ac +++ b/configure.ac @@ -157,6 +157,8 @@ AC_CONFIG_FILES([ cmake/Makefile cmake/modules/Makefile contrib/Makefile + contrib/devd/Makefile + contrib/udev/Makefile contrib/win32/Makefile contrib/win32/sys/Makefile examples/Makefile diff --git a/contrib/Makefile.am b/contrib/Makefile.am index ed130fa..92701a4 100644 --- a/contrib/Makefile.am +++ b/contrib/Makefile.am @@ -1,4 +1,7 @@ -SUBDIRS = win32 +SUBDIRS = \ + devd \ + udev \ + win32 EXTRA_DIST = \ windows.h diff --git a/contrib/devd/Makefile.am b/contrib/devd/Makefile.am new file mode 100644 index 0000000..c846575 --- /dev/null +++ b/contrib/devd/Makefile.am @@ -0,0 +1,2 @@ +EXTRA_DIST = \ + pn53x.conf diff --git a/pn53x.conf b/contrib/devd/pn53x.conf similarity index 100% rename from pn53x.conf rename to contrib/devd/pn53x.conf diff --git a/pn53x.rules b/contrib/udev/42-pn53x.rules similarity index 100% rename from pn53x.rules rename to contrib/udev/42-pn53x.rules diff --git a/contrib/udev/Makefile.am b/contrib/udev/Makefile.am new file mode 100644 index 0000000..86655ae --- /dev/null +++ b/contrib/udev/Makefile.am @@ -0,0 +1,2 @@ +EXTRA_DIST = \ + 42-pn53x.rules From b3b14a3fb86b58f7bcfd6fe6437a451d6c28f091 Mon Sep 17 00:00:00 2001 From: Romuald Conty Date: Fri, 9 Dec 2011 16:14:50 +0000 Subject: [PATCH 020/113] debian: udev rules file renamed --- debian/changelog | 6 ++++++ debian/libnfc2.install | 2 +- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/debian/changelog b/debian/changelog index b94b163..91b4f01 100644 --- a/debian/changelog +++ b/debian/changelog @@ -1,3 +1,9 @@ +libnfc (1.5.1pre2.1-0) unstable; urgency=low + + * Udev rules file renamed accordingly to udev's README file + + -- Romuald Conty Fri, 9 Dec 2011 15:42:42 +0100 + libnfc (1.5.1-0) unstable; urgency=low * Move nfc-emulate-nfcforum-tag2, nfc-emulate-nfcforum-tag4 and diff --git a/debian/libnfc2.install b/debian/libnfc2.install index c944438..08cb976 100644 --- a/debian/libnfc2.install +++ b/debian/libnfc2.install @@ -1,2 +1,2 @@ debian/tmp/usr/lib/libnfc.so.* -pn53x.rules lib/udev/rules.d +contrib/udev/42-pn53x.rules lib/udev/rules.d/ From 9906e11572959d7b65ad40e0eeab1a370daf205f Mon Sep 17 00:00:00 2001 From: Audrey Diacre Date: Fri, 9 Dec 2011 16:36:29 +0000 Subject: [PATCH 021/113] oups: forgot to uncomment a line. --- test/test_dep_active.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/test_dep_active.c b/test/test_dep_active.c index b3174c4..65255c5 100644 --- a/test/test_dep_active.c +++ b/test/test_dep_active.c @@ -191,7 +191,7 @@ initiator_thread (void *arg) cut_assert_equal_memory ("\x12\x34\x56\x78", 4, nt.nti.ndi.abtGB, nt.nti.ndi.szGB, cut_message ("Invalid target general bytes")); if (!res) { thread_res = -1; return (void*) thread_res; } - // szRx = sizeof (abtRx); + szRx = sizeof (abtRx); res = nfc_initiator_transceive_bytes (device, abtTx, sizeof (abtTx), abtRx, &szRx, 1000); cut_assert_true (res, cut_message ("Can't transceive bytes to target: %s", nfc_strerror (device))); From 8b76d55fdc5b0c99695fff18ac14c7bfe96e51a4 Mon Sep 17 00:00:00 2001 From: Audrey Diacre Date: Wed, 14 Dec 2011 10:31:08 +0000 Subject: [PATCH 022/113] test/test_dep_active: fix multiple active DEP baudrates. --- test/Makefile.am | 3 +- test/test_dep_active.c | 145 ++++++++--------------------------------- utils/nfc-utils.h | 1 + 3 files changed, 29 insertions(+), 120 deletions(-) diff --git a/test/Makefile.am b/test/Makefile.am index 74bbb85..5075c36 100644 --- a/test/Makefile.am +++ b/test/Makefile.am @@ -26,7 +26,8 @@ test_access_storm_la_SOURCES = test_access_storm.c test_access_storm_la_LIBADD = $(top_builddir)/libnfc/libnfc.la test_dep_active_la_SOURCES = test_dep_active.c -test_dep_active_la_LIBADD = $(top_builddir)/libnfc/libnfc.la +test_dep_active_la_LIBADD = $(top_builddir)/libnfc/libnfc.la \ + $(top_builddir)/utils/libnfcutils.la test_dep_passive_la_SOURCES = test_dep_passive.c test_dep_passive_la_LIBADD = $(top_builddir)/libnfc/libnfc.la diff --git a/test/test_dep_active.c b/test/test_dep_active.c index 65255c5..9528de3 100644 --- a/test/test_dep_active.c +++ b/test/test_dep_active.c @@ -4,6 +4,7 @@ #include #include "nfc/nfc.h" +#include "utils/nfc-utils.h" #define INITIATOR 0 #define TARGET 1 @@ -49,6 +50,7 @@ cut_teardown (void) struct thread_data { nfc_device *device; void *cut_test_context; + nfc_baud_rate nbr; }; void * @@ -84,10 +86,8 @@ target_thread (void *arg) size_t szRx = sizeof (abtRx); bool res = nfc_target_init (device, &nt, abtRx, &szRx); cut_assert_true (res, cut_message ("Can't initialize NFC device as target: %s", nfc_strerror (device))); - if (!res) { thread_res = -1; return (void*) thread_res; } - // First pass res = nfc_target_receive_bytes (device, abtRx, &szRx, 500); cut_assert_true (res, cut_message ("Can't receive bytes from initiator: %s", nfc_strerror (device))); @@ -100,39 +100,6 @@ target_thread (void *arg) cut_assert_true (res, cut_message ("Can't send bytes to initiator: %s", nfc_strerror (device))); if (!res) { thread_res = -1; return (void*) thread_res; } - // Second pass - res = nfc_target_receive_bytes (device, abtRx, &szRx, 500); - cut_assert_true (res, cut_message ("Can't receive bytes from initiator: %s", nfc_strerror (device))); - - cut_assert_equal_memory (abtAttRx, sizeof (abtAttRx), abtRx, szRx, cut_message ("Invalid received data")); - if (!res) { thread_res = -1; return (void*) thread_res; } - - res = nfc_target_send_bytes (device, abtTx, sizeof(abtTx), 500); - cut_assert_true (res, cut_message ("Can't send bytes to initiator: %s", nfc_strerror (device))); - if (!res) { thread_res = -1; return (void*) thread_res; } - - // Third pass - res = nfc_target_receive_bytes (device, abtRx, &szRx, 500); - cut_assert_true (res, cut_message ("Can't receive bytes from initiator: %s", nfc_strerror (device))); - - cut_assert_equal_memory (abtAttRx, sizeof (abtAttRx), abtRx, szRx, cut_message ("Invalid received data")); - if (!res) { thread_res = -1; return (void*) thread_res; } - - res = nfc_target_send_bytes (device, abtTx, sizeof(abtTx), 500); - cut_assert_true (res, cut_message ("Can't send bytes to initiator: %s", nfc_strerror (device))); - if (!res) { thread_res = -1; return (void*) thread_res; } - - // Fourth pass - res = nfc_target_receive_bytes (device, abtRx, &szRx, 500); - cut_assert_true (res, cut_message ("Can't receive bytes from initiator: %s", nfc_strerror (device))); - - cut_assert_equal_memory (abtAttRx, sizeof (abtAttRx), abtRx, szRx, cut_message ("Invalid received data")); - if (!res) { thread_res = -1; return (void*) thread_res; } - - res = nfc_target_send_bytes (device, abtTx, sizeof(abtTx), 500); - cut_assert_true (res, cut_message ("Can't send bytes to initiator: %s", nfc_strerror (device))); - if (!res) { thread_res = -1; return (void*) thread_res; } - return (void *) thread_res; } @@ -142,25 +109,25 @@ initiator_thread (void *arg) intptr_t thread_res = 0; nfc_device *device = ((struct thread_data *) arg)->device; cut_set_current_test_context (((struct thread_data *) arg)->cut_test_context); + nfc_baud_rate nbr = (((struct thread_data *) arg)->nbr); /* * Wait some time for the other thread to initialise NFC device as target */ sleep (1); printf ("=========== INITIATOR %s =========\n", nfc_device_name (device)); - bool res = nfc_initiator_init (device); cut_assert_true (res, cut_message ("Can't initialize NFC device as initiator: %s", nfc_strerror (device))); if (!res) { thread_res = -1; return (void*) thread_res; } nfc_target nt; - // Active mode / 106Kbps - printf ("=========== INITIATOR %s (Active mode / 106Kbps) =========\n", nfc_device_name (device)); - res = nfc_initiator_select_dep_target (device, NDM_ACTIVE, NBR_106, NULL, &nt, 5000); + // Active mode + printf ("=========== INITIATOR %s (Active mode / %s Kbps) =========\n", nfc_device_name (device), str_nfc_baud_rate(nbr)); + res = nfc_initiator_select_dep_target (device, NDM_ACTIVE, nbr, NULL, &nt, 1000); cut_assert_true (res, cut_message ("Can't select any DEP target: %s", nfc_strerror (device))); cut_assert_equal_int (NMT_DEP, nt.nm.nmt, cut_message ("Invalid target modulation")); - cut_assert_equal_int (NBR_106, nt.nm.nbr, cut_message ("Invalid target baud rate")); + cut_assert_equal_int (nbr, nt.nm.nbr, cut_message ("Invalid target baud rate")); cut_assert_equal_memory ("\x11\x22\x33\x44\x55\x66\x77\x88\x99\xAA", 10, nt.nti.ndi.abtNFCID3, 10, cut_message ("Invalid target NFCID3")); cut_assert_equal_int (NDM_ACTIVE, nt.nti.ndi.ndm, cut_message ("Invalid target DEP mode")); cut_assert_equal_memory ("\x12\x34\x56\x78", 4, nt.nti.ndi.abtGB, nt.nti.ndi.szGB, cut_message ("Invalid target general bytes")); @@ -169,79 +136,12 @@ initiator_thread (void *arg) const uint8_t abtTx[] = "Hello DEP target!"; uint8_t abtRx[1024]; size_t szRx = sizeof (abtRx); - res = nfc_initiator_transceive_bytes (device, abtTx, sizeof (abtTx), abtRx, &szRx, 500); + res = nfc_initiator_transceive_bytes (device, abtTx, sizeof (abtTx), abtRx, &szRx, 5000); cut_assert_true (res, cut_message ("Can't transceive bytes to target: %s", nfc_strerror (device))); const uint8_t abtAttRx[] = "Hello DEP initiator!"; cut_assert_equal_memory (abtAttRx, sizeof (abtAttRx), abtRx, szRx, cut_message ("Invalid received data")); if (!res) { thread_res = -1; return (void*) thread_res; } - - res = nfc_initiator_deselect_target (device); - cut_assert_true (res, cut_message ("Can't deselect target: %s", nfc_strerror (device))); - if (!res) { thread_res = -1; return (void*) thread_res; } - - // Active mode / 212Kbps (second pass) - printf ("=========== INITIATOR %s (Active mode / 212Kbps) =========\n", nfc_device_name (device)); - res = nfc_initiator_select_dep_target (device, NDM_ACTIVE, NBR_212, NULL, &nt, 1000); - cut_assert_true (res, cut_message ("Can't select any DEP target: %s", nfc_strerror (device))); - cut_assert_equal_int (NMT_DEP, nt.nm.nmt, cut_message ("Invalid target modulation")); - cut_assert_equal_int (NBR_212, nt.nm.nbr, cut_message ("Invalid target baud rate")); - cut_assert_equal_memory ("\x11\x22\x33\x44\x55\x66\x77\x88\x99\xAA", 10, nt.nti.ndi.abtNFCID3, 10, cut_message ("Invalid target NFCID3")); - cut_assert_equal_int (NDM_ACTIVE, nt.nti.ndi.ndm, cut_message ("Invalid target DEP mode")); - cut_assert_equal_memory ("\x12\x34\x56\x78", 4, nt.nti.ndi.abtGB, nt.nti.ndi.szGB, cut_message ("Invalid target general bytes")); - if (!res) { thread_res = -1; return (void*) thread_res; } - - szRx = sizeof (abtRx); - res = nfc_initiator_transceive_bytes (device, abtTx, sizeof (abtTx), abtRx, &szRx, 1000); - cut_assert_true (res, cut_message ("Can't transceive bytes to target: %s", nfc_strerror (device))); - - cut_assert_equal_memory (abtAttRx, sizeof (abtAttRx), abtRx, szRx, cut_message ("Invalid received data")); - if (!res) { thread_res = -1; return (void*) thread_res; } - - res = nfc_initiator_deselect_target (device); - cut_assert_true (res, cut_message ("Can't deselect target: %s", nfc_strerror (device))); - if (!res) { thread_res = -1; return (void*) thread_res; } - - // Active mode / 212Kbps - printf ("=========== INITIATOR %s (Active mode / 212Kbps, second pass) =========\n", nfc_device_name (device)); - res = nfc_initiator_select_dep_target (device, NDM_ACTIVE, NBR_212, NULL, &nt, 1000); - cut_assert_true (res, cut_message ("Can't select any DEP target: %s", nfc_strerror (device))); - cut_assert_equal_int (NMT_DEP, nt.nm.nmt, cut_message ("Invalid target modulation")); - cut_assert_equal_int (NBR_212, nt.nm.nbr, cut_message ("Invalid target baud rate")); - cut_assert_equal_memory ("\x11\x22\x33\x44\x55\x66\x77\x88\x99\xAA", 10, nt.nti.ndi.abtNFCID3, 10, cut_message ("Invalid target NFCID3")); - cut_assert_equal_int (NDM_ACTIVE, nt.nti.ndi.ndm, cut_message ("Invalid target DEP mode")); - cut_assert_equal_memory ("\x12\x34\x56\x78", 4, nt.nti.ndi.abtGB, nt.nti.ndi.szGB, cut_message ("Invalid target general bytes")); - if (!res) { thread_res = -1; return (void*) thread_res; } - - szRx = sizeof (abtRx); - res = nfc_initiator_transceive_bytes (device, abtTx, sizeof (abtTx), abtRx, &szRx, 5000); - cut_assert_true (res, cut_message ("Can't transceive bytes to target: %s", nfc_strerror (device))); - - cut_assert_equal_memory (abtAttRx, sizeof (abtAttRx), abtRx, szRx, cut_message ("Invalid received data")); - if (!res) { thread_res = -1; return (void*) thread_res; } - - res = nfc_initiator_deselect_target (device); - cut_assert_true (res, cut_message ("Can't deselect target: %s", nfc_strerror (device))); - if (!res) { thread_res = -1; return (void*) thread_res; } - - // Active mode / 424Kbps - printf ("=========== INITIATOR %s (Active mode / 424Kbps) =========\n", nfc_device_name (device)); - res = nfc_initiator_select_dep_target (device, NDM_ACTIVE, NBR_424, NULL, &nt, 1000); - cut_assert_true (res, cut_message ("Can't select any DEP target: %s", nfc_strerror (device))); - cut_assert_equal_int (NMT_DEP, nt.nm.nmt, cut_message ("Invalid target modulation")); - cut_assert_equal_int (NBR_424, nt.nm.nbr, cut_message ("Invalid target baud rate")); - cut_assert_equal_memory ("\x11\x22\x33\x44\x55\x66\x77\x88\x99\xAA", 10, nt.nti.ndi.abtNFCID3, 10, cut_message ("Invalid target NFCID3")); - cut_assert_equal_int (NDM_ACTIVE, nt.nti.ndi.ndm, cut_message ("Invalid target DEP mode")); - cut_assert_equal_memory ("\x12\x34\x56\x78", 4, nt.nti.ndi.abtGB, nt.nti.ndi.szGB, cut_message ("Invalid target general bytes")); - if (!res) { thread_res = -1; return (void*) thread_res; } - - szRx = sizeof (abtRx); - res = nfc_initiator_transceive_bytes (device, abtTx, sizeof (abtTx), abtRx, &szRx, 5000); - cut_assert_true (res, cut_message ("Can't transceive bytes to target: %s", nfc_strerror (device))); - - cut_assert_equal_memory (abtAttRx, sizeof (abtAttRx), abtRx, szRx, cut_message ("Invalid received data")); - if (!res) { thread_res = -1; return (void*) thread_res; } - res = nfc_initiator_deselect_target (device); cut_assert_true (res, cut_message ("Can't deselect target: %s", nfc_strerror (device))); if (!res) { thread_res = -1; return (void*) thread_res; } @@ -253,27 +153,34 @@ void test_dep (void) { int res; + nfc_baud_rate nbrs[3] = { NBR_106, NBR_212, NBR_424}; CutTestContext *test_context = cut_get_current_test_context (); struct thread_data target_data = { .device = devices[TARGET], .cut_test_context = test_context, }; - if ((res = pthread_create (&(threads[TARGET]), NULL, target_thread, &target_data))) - cut_fail ("pthread_create() returned %d", res); - + struct thread_data initiator_data = { .device = devices[INITIATOR], .cut_test_context = test_context, }; - if ((res = pthread_create (&(threads[INITIATOR]), NULL, initiator_thread, &initiator_data))) - cut_fail ("pthread_create() returned %d", res); + + for (int i=0; i<3; i++) { + initiator_data.nbr = nbrs[i]; + + if ((res = pthread_create (&(threads[TARGET]), NULL, target_thread, &target_data))) + cut_fail ("pthread_create() returned %d", res); + if ((res = pthread_create (&(threads[INITIATOR]), NULL, initiator_thread, &initiator_data))) + cut_fail ("pthread_create() returned %d", res); - if ((res = pthread_join (threads[INITIATOR], (void *) &result[INITIATOR]))) - cut_fail ("pthread_join() returned %d", res); - if ((res = pthread_join (threads[TARGET], (void *) &result[TARGET]))) - cut_fail ("pthread_join() returned %d", res); + if ((res = pthread_join (threads[INITIATOR], (void *) &result[INITIATOR]))) + cut_fail ("pthread_join() returned %d", res); + if ((res = pthread_join (threads[TARGET], (void *) &result[TARGET]))) + cut_fail ("pthread_join() returned %d", res); - cut_assert_equal_int (0, result[INITIATOR], cut_message ("Unexpected initiator return code")); - cut_assert_equal_int (0, result[TARGET], cut_message ("Unexpected target return code")); + cut_assert_equal_int (0, result[INITIATOR], cut_message ("Unexpected initiator return code")); + cut_assert_equal_int (0, result[TARGET], cut_message ("Unexpected target return code")); + } + } diff --git a/utils/nfc-utils.h b/utils/nfc-utils.h index 50abdf3..4243ffc 100644 --- a/utils/nfc-utils.h +++ b/utils/nfc-utils.h @@ -94,6 +94,7 @@ void print_nfc_iso14443b2ct_info (const nfc_iso14443b2ct_info nci, bool verbo void print_nfc_felica_info (const nfc_felica_info nfi, bool verbose); void print_nfc_jewel_info (const nfc_jewel_info nji, bool verbose); void print_nfc_dep_info (const nfc_dep_info ndi, bool verbose); +const char * str_nfc_baud_rate (const nfc_baud_rate nbr); void print_nfc_target (const nfc_target nt, bool verbose); From 26b9c28f48b156a2056e81944ec8b4e5773d9d3d Mon Sep 17 00:00:00 2001 From: Romuald Conty Date: Wed, 14 Dec 2011 13:27:11 +0000 Subject: [PATCH 023/113] new properties to tune timeouts: - add nfc_properties (will replace nfc_options) - introduce some error codes - rework functions to use the new timeout_command value --- include/nfc/nfc-types.h | 24 ++++++++ include/nfc/nfc.h | 10 ++++ libnfc/chips/pn53x.c | 108 ++++++++++++++++++++++++++---------- libnfc/chips/pn53x.h | 11 +++- libnfc/drivers/acr122.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 | 17 ++++++ 10 files changed, 145 insertions(+), 30 deletions(-) diff --git a/include/nfc/nfc-types.h b/include/nfc/nfc-types.h index bac47f5..a43d7bc 100644 --- a/include/nfc/nfc-types.h +++ b/include/nfc/nfc-types.h @@ -74,6 +74,30 @@ typedef struct { */ typedef char nfc_connstring[1024]; +/** + * Properties + */ +typedef enum { +/** + * Default command processing timeout + * Property value's (duration) unit is ms and 0 means no timeout (infinite). + * Default value is set by driver layer + */ + NP_TIMEOUT_COMMAND, +/** + * Timeout between ATR_REQ and ATR_RES + * When the device is in initiator mode, a target is considered as mute if no + * valid ATR_RES is received within this timeout value. + * Default value for this property is 103 ms on PN53x based devices. + */ + NP_TIMEOUT_ATR, +/** + * Timeout value to give up reception from the target in case of no answer. + * Default value for this property is 52 ms). + */ + NP_TIMEOUT_COM, +} nfc_property; + // Compiler directive, set struct alignment to 1 uint8_t for compatibility # pragma pack(1) diff --git a/include/nfc/nfc.h b/include/nfc/nfc.h index 6d34b2f..62ca72a 100644 --- a/include/nfc/nfc.h +++ b/include/nfc/nfc.h @@ -98,12 +98,22 @@ extern "C" { /* Special data accessors */ NFC_EXPORT const char *nfc_device_name (nfc_device *pnd); +/* Properties accessors */ + NFC_EXPORT int nfc_device_set_property_int (nfc_device *pnd, const nfc_property property, const int value); +// NFC_EXPORT int nfc_device_set_property_bool (nfc_device *pnd, const nfc_property property, const int value); + /* Misc. functions */ NFC_EXPORT void iso14443a_crc (uint8_t *pbtData, size_t szLen, uint8_t *pbtCrc); NFC_EXPORT void iso14443a_crc_append (uint8_t *pbtData, size_t szLen); NFC_EXPORT uint8_t *iso14443a_locate_historical_bytes (uint8_t *pbtAts, size_t szAts, size_t *pszTk); NFC_EXPORT const char *nfc_version (void); +/* Error codes */ +#define NFC_SUCCESS 0 // No error +#define NFC_EIO -1 // Input / output error, device will not be usable anymore +#define NFC_ENOTSUP -2 // Operation not supported +#define NFC_EINVARG -3 // Invalid argument(s) + /* PN53x specific errors */ // TODO: Be not PN53x-specific here #define ETIMEOUT 0x01 diff --git a/libnfc/chips/pn53x.c b/libnfc/chips/pn53x.c index befa800..cf29ac3 100644 --- a/libnfc/chips/pn53x.c +++ b/libnfc/chips/pn53x.c @@ -111,6 +111,8 @@ pn53x_transceive (nfc_device *pnd, const uint8_t *pbtTx, const size_t szTx, uint PNCMD_TRACE (pbtTx[0]); if (timeout > 0) log_put (LOG_CATEGORY, NFC_PRIORITY_TRACE, "Timeout values: %d", timeout); + if (timeout == -1) + timeout = CHIP_DATA (pnd)->timeout_command; uint8_t abtRx[PN53x_EXTENDED_FRAME__DATA_MAX_LEN]; size_t szRx = sizeof(abtRx); @@ -161,6 +163,8 @@ pn53x_transceive (nfc_device *pnd, const uint8_t *pbtTx, const size_t szTx, uint case TgResponseToInitiator: case TgSetGeneralBytes: case TgSetMetaData: + if (pbtRx[0] & 0x80) { abort(); } // NAD detected + if (pbtRx[0] & 0x40) { abort(); } // MI detected pnd->iLastError = pbtRx[0] & 0x3f; break; case InDeselect: @@ -469,7 +473,7 @@ pn53x_ReadRegister (nfc_device *pnd, uint16_t ui16RegisterAddress, uint8_t *ui8V size_t szRegValue = sizeof (abtRegValue); PNREG_TRACE (ui16RegisterAddress); - if (!pn53x_transceive (pnd, abtCmd, sizeof (abtCmd), abtRegValue, &szRegValue, 0)) { + if (!pn53x_transceive (pnd, abtCmd, sizeof (abtCmd), abtRegValue, &szRegValue, -1)) { return false; } if (CHIP_DATA(pnd)->type == PN533) { @@ -491,7 +495,7 @@ pn53x_WriteRegister (nfc_device *pnd, const uint16_t ui16RegisterAddress, const { uint8_t abtCmd[] = { WriteRegister, ui16RegisterAddress >> 8, ui16RegisterAddress & 0xff, ui8Value }; PNREG_TRACE (ui16RegisterAddress); - return pn53x_transceive (pnd, abtCmd, sizeof (abtCmd), NULL, NULL, 0); + return pn53x_transceive (pnd, abtCmd, sizeof (abtCmd), NULL, NULL, -1); } bool @@ -543,7 +547,7 @@ pn53x_writeback_register (nfc_device *pnd) uint8_t abtRes[PN53x_EXTENDED_FRAME__DATA_MAX_LEN]; size_t szRes = sizeof(abtRes); // It transceives the previously constructed ReadRegister command - if (!pn53x_transceive (pnd, abtReadRegisterCmd, BUFFER_SIZE (abtReadRegisterCmd), abtRes, &szRes, 0)) { + if (!pn53x_transceive (pnd, abtReadRegisterCmd, BUFFER_SIZE (abtReadRegisterCmd), abtRes, &szRes, -1)) { return false; } size_t i = 0; @@ -581,7 +585,7 @@ pn53x_writeback_register (nfc_device *pnd) if (BUFFER_SIZE (abtWriteRegisterCmd) > 1) { // We need to write some registers - if (!pn53x_transceive (pnd, abtWriteRegisterCmd, BUFFER_SIZE (abtWriteRegisterCmd), NULL, NULL, 0)) { + if (!pn53x_transceive (pnd, abtWriteRegisterCmd, BUFFER_SIZE (abtWriteRegisterCmd), NULL, NULL, -1)) { return false; } } @@ -594,7 +598,7 @@ pn53x_get_firmware_version (nfc_device *pnd, char abtFirmwareText[22]) const uint8_t abtCmd[] = { GetFirmwareVersion }; uint8_t abtFw[4]; size_t szFwLen = sizeof (abtFw); - if (!pn53x_transceive (pnd, abtCmd, sizeof (abtCmd), abtFw, &szFwLen, 0)) { + if (!pn53x_transceive (pnd, abtCmd, sizeof (abtCmd), abtFw, &szFwLen, -1)) { return false; } // Determine which version of chip it is: PN531 will return only 2 bytes, while others return 4 bytes and have the first to tell the version IC @@ -776,6 +780,42 @@ pn53x_configure (nfc_device *pnd, const nfc_device_option ndo, const bool bEnabl return true; } +uint8_t +pn53x_int_to_timeout (const int ms) +{ + uint8_t res = 0; + if (ms) { + res = 0x10; + for (int i=3280; i>1; i/=2) { + if (ms > i) + break; + res--; + } + } + return res; +} + +int +pn53x_set_property_int (nfc_device *pnd, const nfc_property property, const int value) +{ + switch (property) { + case NP_TIMEOUT_COMMAND: + CHIP_DATA (pnd)->timeout_command = value; + break; + case NP_TIMEOUT_ATR: + CHIP_DATA (pnd)->timeout_atr = value; + return (pn53x_RFConfiguration__Various_timings (pnd, pn53x_int_to_timeout(CHIP_DATA (pnd)->timeout_atr), pn53x_int_to_timeout(CHIP_DATA (pnd)->timeout_communication))) ? NFC_SUCCESS : NFC_EIO; + break; + case NP_TIMEOUT_COM: + CHIP_DATA (pnd)->timeout_communication = value; + return (pn53x_RFConfiguration__Various_timings (pnd, pn53x_int_to_timeout(CHIP_DATA (pnd)->timeout_atr), pn53x_int_to_timeout(CHIP_DATA (pnd)->timeout_communication))) ? NFC_SUCCESS : NFC_EIO; + break; + default: + return NFC_ENOTSUP; + } + return NFC_SUCCESS; +} + bool pn53x_idle (nfc_device *pnd) { @@ -995,7 +1035,7 @@ pn53x_initiator_poll_target (nfc_device *pnd, } size_t szTargetFound = 0; nfc_target ntTargets[2]; - if (!pn53x_InAutoPoll (pnd, apttTargetTypes, szTargetTypes, uiPollNr, uiPeriod, ntTargets, &szTargetFound)) + if (!pn53x_InAutoPoll (pnd, apttTargetTypes, szTargetTypes, uiPollNr, uiPeriod, ntTargets, &szTargetFound, 0)) return false; switch (szTargetFound) { case 1: @@ -1099,7 +1139,7 @@ pn53x_initiator_transceive_bits (nfc_device *pnd, const uint8_t *pbtTx, const si // We have to give the amount of bytes + (the command byte 0x42) uint8_t abtRx[PN53x_EXTENDED_FRAME__DATA_MAX_LEN]; size_t szRx = sizeof(abtRx); - if (!pn53x_transceive (pnd, abtCmd, szFrameBytes + 1, abtRx, &szRx, 0)) + if (!pn53x_transceive (pnd, abtCmd, szFrameBytes + 1, abtRx, &szRx, -1)) return false; // Get the last bit-count that is stored in the received byte @@ -1216,7 +1256,7 @@ uint32_t __pn53x_get_timer(nfc_device *pnd, const uint8_t last_cmd_byte) uint8_t abtRes[PN53x_EXTENDED_FRAME__DATA_MAX_LEN]; size_t szRes = sizeof(abtRes); // Let's send the previously constructed ReadRegister command - if (!pn53x_transceive (pnd, abtReadRegisterCmd, BUFFER_SIZE (abtReadRegisterCmd), abtRes, &szRes, 0)) { + if (!pn53x_transceive (pnd, abtReadRegisterCmd, BUFFER_SIZE (abtReadRegisterCmd), abtRes, &szRes, -1)) { return false; } counter_hi = abtRes[off]; @@ -1306,7 +1346,7 @@ pn53x_initiator_transceive_bits_timed (nfc_device *pnd, const uint8_t *pbtTx, co BUFFER_APPEND (abtWriteRegisterCmd, PN53X_REG_CIU_BitFraming & 0xff); BUFFER_APPEND (abtWriteRegisterCmd, SYMBOL_START_SEND | ((szTxBits % 8) & SYMBOL_TX_LAST_BITS)); // Let's send the previously constructed WriteRegister command - if (!pn53x_transceive (pnd, abtWriteRegisterCmd, BUFFER_SIZE (abtWriteRegisterCmd), NULL, NULL, 0)) { + if (!pn53x_transceive (pnd, abtWriteRegisterCmd, BUFFER_SIZE (abtWriteRegisterCmd), NULL, NULL, -1)) { return false; } @@ -1338,7 +1378,7 @@ pn53x_initiator_transceive_bits_timed (nfc_device *pnd, const uint8_t *pbtTx, co uint8_t abtRes[PN53x_EXTENDED_FRAME__DATA_MAX_LEN]; size_t szRes = sizeof(abtRes); // Let's send the previously constructed ReadRegister command - if (!pn53x_transceive (pnd, abtReadRegisterCmd, BUFFER_SIZE (abtReadRegisterCmd), abtRes, &szRes, 0)) { + if (!pn53x_transceive (pnd, abtReadRegisterCmd, BUFFER_SIZE (abtReadRegisterCmd), abtRes, &szRes, -1)) { return false; } for (i = 0; i < sz; i++) { @@ -1401,7 +1441,7 @@ pn53x_initiator_transceive_bytes_timed (nfc_device *pnd, const uint8_t *pbtTx, c BUFFER_APPEND (abtWriteRegisterCmd, PN53X_REG_CIU_BitFraming & 0xff); BUFFER_APPEND (abtWriteRegisterCmd, SYMBOL_START_SEND); // Let's send the previously constructed WriteRegister command - if (!pn53x_transceive (pnd, abtWriteRegisterCmd, BUFFER_SIZE (abtWriteRegisterCmd), NULL, NULL, 0)) { + if (!pn53x_transceive (pnd, abtWriteRegisterCmd, BUFFER_SIZE (abtWriteRegisterCmd), NULL, NULL, -1)) { return false; } @@ -1433,7 +1473,7 @@ pn53x_initiator_transceive_bytes_timed (nfc_device *pnd, const uint8_t *pbtTx, c uint8_t abtRes[PN53x_EXTENDED_FRAME__DATA_MAX_LEN]; size_t szRes = sizeof(abtRes); // Let's send the previously constructed ReadRegister command - if (!pn53x_transceive (pnd, abtReadRegisterCmd, BUFFER_SIZE (abtReadRegisterCmd), abtRes, &szRes, 0)) { + if (!pn53x_transceive (pnd, abtReadRegisterCmd, BUFFER_SIZE (abtReadRegisterCmd), abtRes, &szRes, -1)) { return false; } for (i = 0; i < sz; i++) { @@ -1697,7 +1737,7 @@ pn53x_target_receive_bits (nfc_device *pnd, uint8_t *pbtRx, size_t *pszRxBits, u uint8_t abtRx[PN53x_EXTENDED_FRAME__DATA_MAX_LEN]; size_t szRx = sizeof (abtRx); // Try to gather a received frame from the reader - if (!pn53x_transceive (pnd, abtCmd, sizeof (abtCmd), abtRx, &szRx, 0)) + if (!pn53x_transceive (pnd, abtCmd, sizeof (abtCmd), abtRx, &szRx, -1)) return false; // Get the last bit-count that is stored in the received byte @@ -1804,7 +1844,7 @@ pn53x_target_send_bits (nfc_device *pnd, const uint8_t *pbtTx, const size_t szTx return false; // Try to send the bits to the reader - if (!pn53x_transceive (pnd, abtCmd, szFrameBytes + 1, NULL, NULL, 0)) + if (!pn53x_transceive (pnd, abtCmd, szFrameBytes + 1, NULL, NULL, -1)) return false; // Everyting seems ok, return true @@ -1930,7 +1970,7 @@ bool pn53x_RFConfiguration__RF_field (nfc_device *pnd, bool bEnable) { uint8_t abtCmd[] = { RFConfiguration, RFCI_FIELD, (bEnable) ? 0x01 : 0x00 }; - return pn53x_transceive (pnd, abtCmd, sizeof (abtCmd), NULL, NULL, 0); + return pn53x_transceive (pnd, abtCmd, sizeof (abtCmd), NULL, NULL, -1); } bool @@ -1943,7 +1983,7 @@ pn53x_RFConfiguration__Various_timings (nfc_device *pnd, const uint8_t fATR_RES_ fATR_RES_Timeout, // ATR_RES timeout (default: 0x0B 102.4 ms) fRetryTimeout // TimeOut during non-DEP communications (default: 0x0A 51.2 ms) }; - return pn53x_transceive (pnd, abtCmd, sizeof (abtCmd), NULL, NULL, 0); + return pn53x_transceive (pnd, abtCmd, sizeof (abtCmd), NULL, NULL, -1); } bool @@ -1954,7 +1994,7 @@ pn53x_RFConfiguration__MaxRtyCOM (nfc_device *pnd, const uint8_t MaxRtyCOM) RFCI_RETRY_DATA, MaxRtyCOM // MaxRtyCOM, default: 0x00 (no retry, only one try), inifite: 0xff }; - return pn53x_transceive (pnd, abtCmd, sizeof (abtCmd), NULL, NULL, 0); + return pn53x_transceive (pnd, abtCmd, sizeof (abtCmd), NULL, NULL, -1); } bool @@ -1968,7 +2008,7 @@ pn53x_RFConfiguration__MaxRetries (nfc_device *pnd, const uint8_t MxRtyATR, cons MxRtyPSL, // MxRtyPSL, default: 0x01 MxRtyPassiveActivation // MxRtyPassiveActivation, default: 0xff (0x00 leads to problems with PN531) }; - return pn53x_transceive (pnd, abtCmd, sizeof (abtCmd), NULL, NULL, 0); + return pn53x_transceive (pnd, abtCmd, sizeof (abtCmd), NULL, NULL, -1); } bool @@ -1976,7 +2016,7 @@ pn53x_SetParameters (nfc_device *pnd, const uint8_t ui8Value) { uint8_t abtCmd[] = { SetParameters, ui8Value }; - if(!pn53x_transceive (pnd, abtCmd, sizeof (abtCmd), NULL, NULL, 0)) { + if(!pn53x_transceive (pnd, abtCmd, sizeof (abtCmd), NULL, NULL, -1)) { return false; } // We save last parameters in register cache @@ -2017,7 +2057,7 @@ bool pn53x_PowerDown (nfc_device *pnd) { uint8_t abtCmd[] = { PowerDown, 0xf0 }; - return (pn53x_transceive (pnd, abtCmd, sizeof (abtCmd), NULL, NULL, 0)); + return (pn53x_transceive (pnd, abtCmd, sizeof (abtCmd), NULL, NULL, -1)); } /** @@ -2095,7 +2135,7 @@ pn53x_InDeselect (nfc_device *pnd, const uint8_t ui8Target) uint8_t abtStatus[PN53x_EXTENDED_FRAME__DATA_MAX_LEN]; size_t szStatus = sizeof(abtStatus); uint8_t abtCmdGetStatus[] = { GetGeneralStatus }; - if (!pn53x_transceive (pnd, abtCmdGetStatus, sizeof (abtCmdGetStatus), abtStatus, &szStatus, 0)) { + if (!pn53x_transceive (pnd, abtCmdGetStatus, sizeof (abtCmdGetStatus), abtStatus, &szStatus, -1)) { return false; } if ((szStatus < 3) || (abtStatus[2] == 0)) { @@ -2103,10 +2143,10 @@ pn53x_InDeselect (nfc_device *pnd, const uint8_t ui8Target) } // No much choice what to deselect actually... uint8_t abtCmdRcs360[] = { InDeselect, 0x01, 0x01 }; - return (pn53x_transceive (pnd, abtCmdRcs360, sizeof (abtCmdRcs360), NULL, NULL, 0)); + return (pn53x_transceive (pnd, abtCmdRcs360, sizeof (abtCmdRcs360), NULL, NULL, -1)); } uint8_t abtCmd[] = { InDeselect, ui8Target }; - return (pn53x_transceive (pnd, abtCmd, sizeof (abtCmd), NULL, NULL, 0)); + return (pn53x_transceive (pnd, abtCmd, sizeof (abtCmd), NULL, NULL, -1)); } bool @@ -2117,7 +2157,7 @@ pn53x_InRelease (nfc_device *pnd, const uint8_t ui8Target) uint8_t abtStatus[PN53x_EXTENDED_FRAME__DATA_MAX_LEN]; size_t szStatus = sizeof(abtStatus); uint8_t abtCmdGetStatus[] = { GetGeneralStatus }; - if (!pn53x_transceive (pnd, abtCmdGetStatus, sizeof (abtCmdGetStatus), abtStatus, &szStatus, 0)) { + if (!pn53x_transceive (pnd, abtCmdGetStatus, sizeof (abtCmdGetStatus), abtStatus, &szStatus, -1)) { return false; } if ((szStatus < 3) || (abtStatus[2] == 0)) { @@ -2125,16 +2165,17 @@ pn53x_InRelease (nfc_device *pnd, const uint8_t ui8Target) } // No much choice what to release actually... uint8_t abtCmdRcs360[] = { InRelease, 0x01, 0x01 }; - return (pn53x_transceive (pnd, abtCmdRcs360, sizeof (abtCmdRcs360), NULL, NULL, 0)); + return (pn53x_transceive (pnd, abtCmdRcs360, sizeof (abtCmdRcs360), NULL, NULL, -1)); } uint8_t abtCmd[] = { InRelease, ui8Target }; - return (pn53x_transceive (pnd, abtCmd, sizeof (abtCmd), NULL, NULL, 0)); + return (pn53x_transceive (pnd, abtCmd, sizeof (abtCmd), NULL, NULL, -1)); } bool pn53x_InAutoPoll (nfc_device *pnd, const pn53x_target_type *ppttTargetTypes, const size_t szTargetTypes, - const uint8_t btPollNr, const uint8_t btPeriod, nfc_target * pntTargets, size_t *pszTargetFound) + const uint8_t btPollNr, const uint8_t btPeriod, nfc_target * pntTargets, size_t *pszTargetFound, + const int timeout) { if (CHIP_DATA(pnd)->type != PN532) { // This function is not supported by pn531 neither pn533 @@ -2151,7 +2192,7 @@ pn53x_InAutoPoll (nfc_device *pnd, uint8_t abtRx[PN53x_EXTENDED_FRAME__DATA_MAX_LEN]; size_t szRx = sizeof(abtRx); - bool res = pn53x_transceive (pnd, abtCmd, szTxInAutoPoll, abtRx, &szRx, 0); + bool res = pn53x_transceive (pnd, abtCmd, szTxInAutoPoll, abtRx, &szRx, timeout); if (res == false) { return false; @@ -2343,7 +2384,7 @@ pn53x_TgInitAsTarget (nfc_device *pnd, pn53x_target_mode ptm, // Request the initialization as a target uint8_t abtRx[PN53x_EXTENDED_FRAME__DATA_MAX_LEN]; size_t szRx = sizeof (abtRx); - if (!pn53x_transceive (pnd, abtCmd, 36 + szOptionalBytes, abtRx, &szRx, 0)) + if (!pn53x_transceive (pnd, abtCmd, 36 + szOptionalBytes, abtRx, &szRx, -1)) return false; // Note: the first byte is skip: @@ -2633,6 +2674,15 @@ pn53x_data_new (nfc_device *pnd, const struct pn53x_io *io) // WriteBack cache is clean 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 ATR timeout (103 ms) + CHIP_DATA (pnd)->timeout_command = 103; + + // Set default communication timeout (52 ms) + CHIP_DATA (pnd)->timeout_command = 52; } void diff --git a/libnfc/chips/pn53x.h b/libnfc/chips/pn53x.h index 872e449..c1f96cc 100644 --- a/libnfc/chips/pn53x.h +++ b/libnfc/chips/pn53x.h @@ -160,6 +160,12 @@ struct pn53x_data { uint8_t wb_data[PN53X_CACHE_REGISTER_SIZE]; uint8_t wb_mask[PN53X_CACHE_REGISTER_SIZE]; bool wb_trigged; +/** Command timeout */ + int timeout_command; +/** ATR timeout */ + int timeout_atr; +/** Communication timeout */ + int timeout_communication; }; #define CHIP_DATA(pnd) ((struct pn53x_data*)(pnd->chip_data)) @@ -271,6 +277,8 @@ bool pn53x_read_register (nfc_device *pnd, uint16_t ui16Reg, uint8_t *ui8Valu bool pn53x_write_register (nfc_device *pnd, uint16_t ui16Reg, uint8_t ui8SymbolMask, uint8_t ui8Value); bool pn53x_get_firmware_version (nfc_device *pnd, char abtFirmwareText[22]); bool pn53x_configure (nfc_device *pnd, const nfc_device_option ndo, const bool bEnable); +int pn53x_set_property_int (nfc_device *pnd, const nfc_property property, const int value); + bool pn53x_check_communication (nfc_device *pnd); bool pn53x_idle (nfc_device *pnd); @@ -323,7 +331,8 @@ bool pn53x_InDeselect (nfc_device *pnd, const uint8_t ui8Target); bool pn53x_InRelease (nfc_device *pnd, const uint8_t ui8Target); bool pn53x_InAutoPoll (nfc_device *pnd, const pn53x_target_type *ppttTargetTypes, const size_t szTargetTypes, const uint8_t btPollNr, const uint8_t btPeriod, nfc_target *pntTargets, - size_t *pszTargetFound); + size_t *pszTargetFound, + const int timeout); bool pn53x_InJumpForDEP (nfc_device *pnd, const nfc_dep_mode ndm, const nfc_baud_rate nbr, const uint8_t *pbtPassiveInitiatorData, diff --git a/libnfc/drivers/acr122.c b/libnfc/drivers/acr122.c index dd7e2a6..91d85c2 100644 --- a/libnfc/drivers/acr122.c +++ b/libnfc/drivers/acr122.c @@ -488,6 +488,7 @@ const struct nfc_driver_t acr122_driver = { .target_receive_bits = pn53x_target_receive_bits, .configure = pn53x_configure, + .device_set_property_int = pn53x_set_property_int, .abort_command = NULL, // FIXME: abort is not supported in this driver .idle = NULL, // FIXME: idle is not supported in this driver diff --git a/libnfc/drivers/arygon.c b/libnfc/drivers/arygon.c index ecd36b6..89b53f2 100644 --- a/libnfc/drivers/arygon.c +++ b/libnfc/drivers/arygon.c @@ -580,6 +580,7 @@ const struct nfc_driver_t arygon_driver = { .target_receive_bits = pn53x_target_receive_bits, .configure = pn53x_configure, + .device_set_property_int = pn53x_set_property_int, .abort_command = arygon_abort_command, .idle = NULL, // FIXME arygon driver does not support idle() diff --git a/libnfc/drivers/pn532_uart.c b/libnfc/drivers/pn532_uart.c index 7b61090..33b1873 100644 --- a/libnfc/drivers/pn532_uart.c +++ b/libnfc/drivers/pn532_uart.c @@ -526,6 +526,7 @@ const struct nfc_driver_t pn532_uart_driver = { .target_receive_bits = pn53x_target_receive_bits, .configure = pn53x_configure, + .device_set_property_int = pn53x_set_property_int, .abort_command = pn532_uart_abort_command, .idle = pn53x_idle, diff --git a/libnfc/drivers/pn53x_usb.c b/libnfc/drivers/pn53x_usb.c index 2f22b7a..bcc01ed 100644 --- a/libnfc/drivers/pn53x_usb.c +++ b/libnfc/drivers/pn53x_usb.c @@ -815,6 +815,7 @@ const struct nfc_driver_t pn53x_usb_driver = { .target_receive_bits = pn53x_target_receive_bits, .configure = pn53x_usb_configure, + .device_set_property_int = pn53x_set_property_int, .abort_command = pn53x_usb_abort_command, .idle = pn53x_idle, diff --git a/libnfc/nfc-internal.h b/libnfc/nfc-internal.h index b52566a..89dccb1 100644 --- a/libnfc/nfc-internal.h +++ b/libnfc/nfc-internal.h @@ -148,6 +148,7 @@ struct nfc_driver_t { bool (*target_receive_bits) (nfc_device *pnd, uint8_t * pbtRx, size_t * pszRxBits, uint8_t * pbtRxPar); bool (*configure) (nfc_device *pnd, const nfc_device_option ndo, const bool bEnable); + int (*device_set_property_int) (nfc_device *pnd, const nfc_property property, const int value); bool (*abort_command) (nfc_device *pnd); bool (*idle) (nfc_device *pnd); diff --git a/libnfc/nfc.c b/libnfc/nfc.c index 975a7b3..8f12ce4 100644 --- a/libnfc/nfc.c +++ b/libnfc/nfc.c @@ -221,6 +221,23 @@ nfc_configure (nfc_device *pnd, const nfc_device_option ndo, const bool bEnable) HAL (configure, pnd, ndo, bEnable); } +/** + * @brief Set a device's integer-property value + * @return Returns 0 on success, otherwise returns libnfc's error code (negative value) + * @param pnd \a nfc_device struct pointer that represent currently used device + * @param property \a nfc_property which will be set + * @param value integer value + * + * Sets integer property. + * + * @see nfc_property enum values + */ +int +nfc_device_set_property_int (nfc_device *pnd, const nfc_property property, const int value) +{ + HAL (device_set_property_int, pnd, property, value); +} + /** * @brief Initialize NFC device as initiator (reader) * @return Returns \c true if action was successfully performed; otherwise returns \c false. From c181cb35ecfc36cd9127a57bece9ae118904933d Mon Sep 17 00:00:00 2001 From: Romuald Conty Date: Wed, 14 Dec 2011 15:03:29 +0000 Subject: [PATCH 024/113] set correctly default timeout values --- libnfc/chips/pn53x.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/libnfc/chips/pn53x.c b/libnfc/chips/pn53x.c index cf29ac3..e62f1d2 100644 --- a/libnfc/chips/pn53x.c +++ b/libnfc/chips/pn53x.c @@ -2679,10 +2679,10 @@ pn53x_data_new (nfc_device *pnd, const struct pn53x_io *io) CHIP_DATA (pnd)->timeout_command = 500; // Set default ATR timeout (103 ms) - CHIP_DATA (pnd)->timeout_command = 103; + CHIP_DATA (pnd)->timeout_atr = 103; // Set default communication timeout (52 ms) - CHIP_DATA (pnd)->timeout_command = 52; + CHIP_DATA (pnd)->timeout_communication = 52; } void From a615d969fdb0d07fb941df93fef1e6306abc87aa Mon Sep 17 00:00:00 2001 From: Audrey Diacre Date: Wed, 14 Dec 2011 16:01:00 +0000 Subject: [PATCH 025/113] nfc_properties replace now nfc_options and nfc_configure is replaced by nfc_device_set_property_bool which returns error code. --- examples/nfc-anticol.c | 12 +- examples/nfc-emulate-tag.c | 6 +- examples/nfc-emulate-uid.c | 4 +- examples/nfc-relay.c | 22 +-- examples/pn53x-sam.c | 4 +- include/nfc/nfc-types.h | 41 +++--- include/nfc/nfc.h | 4 +- libnfc/chips/pn53x.c | 286 ++++++++++++++++++------------------ libnfc/chips/pn53x.h | 2 +- libnfc/drivers/acr122.c | 2 +- libnfc/drivers/arygon.c | 2 +- libnfc/drivers/pn532_uart.c | 2 +- libnfc/drivers/pn53x_usb.c | 20 +-- libnfc/nfc-internal.h | 2 +- libnfc/nfc.c | 133 ++++++++--------- utils/mifare.c | 10 +- utils/nfc-mfclassic.c | 18 +-- utils/nfc-mfsetuid.c | 12 +- utils/nfc-mfultralight.c | 4 +- utils/nfc-read-forum-tag3.c | 4 +- 20 files changed, 292 insertions(+), 298 deletions(-) diff --git a/examples/nfc-anticol.c b/examples/nfc-anticol.c index 710dd6a..d6afe0d 100644 --- a/examples/nfc-anticol.c +++ b/examples/nfc-anticol.c @@ -160,18 +160,18 @@ main (int argc, char *argv[]) nfc_initiator_init (pnd); // Configure the CRC - if (!nfc_configure (pnd, NDO_HANDLE_CRC, false)) { - nfc_perror (pnd, "nfc_configure"); + if (nfc_device_set_property_bool (pnd, NP_HANDLE_CRC, false) < 0) { + nfc_perror (pnd, "nfc_device_set_property_bool"); exit (EXIT_FAILURE); } // Use raw send/receive methods - if (!nfc_configure (pnd, NDO_EASY_FRAMING, false)) { - nfc_perror (pnd, "nfc_configure"); + if (nfc_device_set_property_bool (pnd, NP_EASY_FRAMING, false) < 0) { + nfc_perror (pnd, "nfc_device_set_property_bool"); exit (EXIT_FAILURE); } // Disable 14443-4 autoswitching - if (!nfc_configure (pnd, NDO_AUTO_ISO14443_4, false)) { - nfc_perror (pnd, "nfc_configure"); + if (nfc_device_set_property_bool (pnd, NP_AUTO_ISO14443_4, false) < 0) { + nfc_perror (pnd, "nfc_device_set_property_bool"); exit (EXIT_FAILURE); } diff --git a/examples/nfc-emulate-tag.c b/examples/nfc-emulate-tag.c index 62dcd32..814d0b5 100644 --- a/examples/nfc-emulate-tag.c +++ b/examples/nfc-emulate-tag.c @@ -155,7 +155,7 @@ nfc_target_emulate_tag(nfc_device *pnd, nfc_target *pnt) } if ( loop ) { if ( init_mfc_auth ) { - nfc_configure (pnd, NDO_HANDLE_CRC, false); + nfc_device_set_property_bool (pnd, NP_HANDLE_CRC, false); init_mfc_auth = false; } if (!nfc_target_receive_bytes(pnd, abtRx, &szRx, 0)) { @@ -257,8 +257,8 @@ main (int argc, char *argv[]) printf ("%s will emulate this ISO14443-A tag:\n", argv[0]); print_nfc_iso14443a_info (nt.nti.nai, true); - // Switch off NDO_EASY_FRAMING if target is not ISO14443-4 - nfc_configure (pnd, NDO_EASY_FRAMING, (nt.nti.nai.btSak & SAK_ISO14443_4_COMPLIANT)); + // Switch off NP_EASY_FRAMING if target is not ISO14443-4 + nfc_device_set_property_bool (pnd, NP_EASY_FRAMING, (nt.nti.nai.btSak & SAK_ISO14443_4_COMPLIANT)); printf ("NFC device (configured as target) is now emulating the tag, please touch it with a second NFC device (initiator)\n"); if (!nfc_target_emulate_tag (pnd, &nt)) { nfc_perror (pnd, "nfc_target_emulate_tag"); diff --git a/examples/nfc-emulate-uid.c b/examples/nfc-emulate-uid.c index 4100ee8..5e762f1 100644 --- a/examples/nfc-emulate-uid.c +++ b/examples/nfc-emulate-uid.c @@ -163,8 +163,8 @@ main (int argc, char *argv[]) printf ("[+] Received initiator command: "); print_hex_bits (abtRecv, szRecvBits); printf ("[+] Configuring communication\n"); - if (!nfc_configure (pnd, NDO_HANDLE_CRC, false) || !nfc_configure (pnd, NDO_HANDLE_PARITY, true)) { - nfc_perror (pnd, "nfc_configure"); + if ((nfc_device_set_property_bool (pnd, NP_HANDLE_CRC, false) < 0) || (nfc_device_set_property_bool (pnd, NP_HANDLE_PARITY, true) < 0)) { + nfc_perror (pnd, "nfc_device_set_property_bool"); exit (EXIT_FAILURE); } printf ("[+] Done, the emulated tag is initialized with UID: %02X%02X%02X%02X\n\n", abtUidBcc[0], abtUidBcc[1], diff --git a/examples/nfc-relay.c b/examples/nfc-relay.c index d94da4b..aea0a56 100644 --- a/examples/nfc-relay.c +++ b/examples/nfc-relay.c @@ -152,9 +152,9 @@ main (int argc, char *argv[]) return EXIT_FAILURE; } printf ("%s", "Configuring emulator settings..."); - if (!nfc_configure (pndTag, NDO_HANDLE_CRC, false) || - !nfc_configure (pndTag, NDO_HANDLE_PARITY, false) || !nfc_configure (pndTag, NDO_ACCEPT_INVALID_FRAMES, true)) { - nfc_perror (pndTag, "nfc_configure"); + if ((nfc_device_set_property_bool (pndTag, NP_HANDLE_CRC, false) < 0) || + (nfc_device_set_property_bool (pndTag, NP_HANDLE_PARITY, false) < 0) || (nfc_device_set_property_bool (pndTag, NP_ACCEPT_INVALID_FRAMES, true)) < 0) { + nfc_perror (pndTag, "nfc_device_set_property_bool"); exit (EXIT_FAILURE); } printf ("%s", "Done, emulated tag is initialized"); @@ -165,10 +165,10 @@ main (int argc, char *argv[]) printf ("Connected to the NFC reader device: %s", pndReader->acName); printf ("%s", "Configuring NFC reader settings..."); nfc_initiator_init (pndReader); - if (!nfc_configure (pndReader, NDO_HANDLE_CRC, false) || - !nfc_configure (pndReader, NDO_HANDLE_PARITY, false) || - !nfc_configure (pndReader, NDO_ACCEPT_INVALID_FRAMES, true)) { - nfc_perror (pndReader, "nfc_configure"); + if ((nfc_device_set_property_bool (pndReader, NP_HANDLE_CRC, false) < 0) || + (nfc_device_set_property_bool (pndReader, NP_HANDLE_PARITY, false) < 0) || + (nfc_device_set_property_bool (pndReader, NP_ACCEPT_INVALID_FRAMES, true)) < 0) { + nfc_perror (pndReader, "nfc_device_set_property_bool"); exit (EXIT_FAILURE); } printf ("%s", "Done, relaying frames now!"); @@ -179,14 +179,14 @@ main (int argc, char *argv[]) // Drop down the field before sending a REQA command and start a new session if (szReaderRxBits == 7 && abtReaderRx[0] == 0x26) { // Drop down field for a very short time (original tag will reboot) - if (!nfc_configure (pndReader, NDO_ACTIVATE_FIELD, false)) { - nfc_perror (pndReader, "nfc_configure"); + if (nfc_device_set_property_bool (pndReader, NP_ACTIVATE_FIELD, false) < 0) { + nfc_perror (pndReader, "nfc_device_set_property_bool"); exit (EXIT_FAILURE); } if (!quiet_output) printf ("\n"); - if (!nfc_configure (pndReader, NDO_ACTIVATE_FIELD, true)) { - nfc_perror (pndReader, "nfc_configure"); + if (nfc_device_set_property_bool (pndReader, NP_ACTIVATE_FIELD, true) < 0) { + nfc_perror (pndReader, "nfc_device_set_property_bool"); exit (EXIT_FAILURE); } } diff --git a/examples/pn53x-sam.c b/examples/pn53x-sam.c index 7552325..ec5e4db 100644 --- a/examples/pn53x-sam.c +++ b/examples/pn53x-sam.c @@ -131,8 +131,8 @@ main (int argc, const char *argv[]) nfc_initiator_init (pnd); // Let the reader only try once to find a tag - if (!nfc_configure (pnd, NDO_INFINITE_SELECT, false)) { - nfc_perror (pnd, "nfc_configure"); + if (nfc_device_set_property_bool (pnd, NP_INFINITE_SELECT, false) < 0) { + nfc_perror (pnd, "nfc_device_set_property_bool"); exit (EXIT_FAILURE); } // Read the SAM's info diff --git a/include/nfc/nfc-types.h b/include/nfc/nfc-types.h index a43d7bc..5975d5b 100644 --- a/include/nfc/nfc-types.h +++ b/include/nfc/nfc-types.h @@ -96,24 +96,14 @@ typedef enum { * Default value for this property is 52 ms). */ NP_TIMEOUT_COM, -} nfc_property; - -// Compiler directive, set struct alignment to 1 uint8_t for compatibility -# pragma pack(1) - -/** - * @enum nfc_device_option - * @brief NFC device option - */ -typedef enum { -/** Let the PN53X chip handle the CRC bytes. This means that the chip appends + /** Let the PN53X chip handle the CRC bytes. This means that the chip appends * the CRC bytes to the frames that are transmitted. It will parse the last * bytes from received frames as incoming CRC bytes. They will be verified * against the used modulation and protocol. If an frame is expected with * incorrect CRC bytes this option should be disabled. Example frames where * this is useful are the ATQA and UID+BCC that are transmitted without CRC * bytes during the anti-collision phase of the ISO14443-A protocol. */ - NDO_HANDLE_CRC = 0x00, + NP_HANDLE_CRC = 0x00, /** Parity bits in the network layer of ISO14443-A are by default generated and * validated in the PN53X chip. This is a very convenient feature. On certain * times though it is useful to get full control of the transmitted data. The @@ -121,31 +111,31 @@ typedef enum { * parity bits. For interoperability it is required to be completely * compatible, including the arbitrary parity bits. When this option is * disabled, the functions to communicating bits should be used. */ - NDO_HANDLE_PARITY = 0x01, + NP_HANDLE_PARITY = 0x01, /** This option can be used to enable or disable the electronic field of the * NFC device. */ - NDO_ACTIVATE_FIELD = 0x10, + NP_ACTIVATE_FIELD = 0x10, /** The internal CRYPTO1 co-processor can be used to transmit messages * encrypted. This option is automatically activated after a successful MIFARE * Classic authentication. */ - NDO_ACTIVATE_CRYPTO1 = 0x11, + NP_ACTIVATE_CRYPTO1 = 0x11, /** The default configuration defines that the PN53X chip will try indefinitely * to invite a tag in the field to respond. This could be desired when it is * certain a tag will enter the field. On the other hand, when this is * uncertain, it will block the application. This option could best be compared * to the (NON)BLOCKING option used by (socket)network programming. */ - NDO_INFINITE_SELECT = 0x20, + NP_INFINITE_SELECT = 0x20, /** If this option is enabled, frames that carry less than 4 bits are allowed. * According to the standards these frames should normally be handles as * invalid frames. */ - NDO_ACCEPT_INVALID_FRAMES = 0x30, + NP_ACCEPT_INVALID_FRAMES = 0x30, /** If the NFC device should only listen to frames, it could be useful to let * it gather multiple frames in a sequence. They will be stored in the internal * FIFO of the PN53X chip. This could be retrieved by using the receive data * functions. Note that if the chip runs out of bytes (FIFO = 64 bytes long), * it will overwrite the first received frames, so quick retrieving of the * received data is desirable. */ - NDO_ACCEPT_MULTIPLE_FRAMES = 0x31, + NP_ACCEPT_MULTIPLE_FRAMES = 0x31, /** This option can be used to enable or disable the auto-switching mode to * ISO14443-4 is device is compliant. * In initiator mode, it means that NFC chip will send RATS automatically when @@ -153,16 +143,19 @@ typedef enum { * requested. * In target mode, with a NFC chip compiliant (ie. PN532), the chip will * emulate a 14443-4 PICC using hardware capability */ - NDO_AUTO_ISO14443_4 = 0x40, + NP_AUTO_ISO14443_4 = 0x40, /** Use automatic frames encapsulation and chaining. */ - NDO_EASY_FRAMING = 0x41, + NP_EASY_FRAMING = 0x41, /** Force the chip to switch in ISO14443-A */ - NDO_FORCE_ISO14443_A = 0x42, + NP_FORCE_ISO14443_A = 0x42, /** Force the chip to switch in ISO14443-B */ - NDO_FORCE_ISO14443_B = 0x43, + NP_FORCE_ISO14443_B = 0x43, /** Force the chip to run at 106 kbps */ - NDO_FORCE_SPEED_106 = 0x50, -} nfc_device_option; + NP_FORCE_SPEED_106 = 0x50, +} nfc_property; + +// Compiler directive, set struct alignment to 1 uint8_t for compatibility +# pragma pack(1) /** * @enum nfc_dep_mode diff --git a/include/nfc/nfc.h b/include/nfc/nfc.h index 62ca72a..825da4c 100644 --- a/include/nfc/nfc.h +++ b/include/nfc/nfc.h @@ -68,7 +68,6 @@ extern "C" { NFC_EXPORT void nfc_disconnect (nfc_device *pnd); NFC_EXPORT bool nfc_abort_command (nfc_device *pnd); NFC_EXPORT void nfc_list_devices (nfc_connstring connstrings[], size_t connstrings_len, size_t *pszDeviceFound); - NFC_EXPORT bool nfc_configure (nfc_device *pnd, const nfc_device_option ndo, const bool bEnable); NFC_EXPORT bool nfc_idle (nfc_device *pnd); /* NFC initiator: act as "reader" */ @@ -100,7 +99,7 @@ extern "C" { /* Properties accessors */ NFC_EXPORT int nfc_device_set_property_int (nfc_device *pnd, const nfc_property property, const int value); -// NFC_EXPORT int nfc_device_set_property_bool (nfc_device *pnd, const nfc_property property, const int value); + NFC_EXPORT int nfc_device_set_property_bool (nfc_device *pnd, const nfc_property property, const bool bEnable); /* Misc. functions */ NFC_EXPORT void iso14443a_crc (uint8_t *pbtData, size_t szLen, uint8_t *pbtCrc); @@ -113,6 +112,7 @@ extern "C" { #define NFC_EIO -1 // Input / output error, device will not be usable anymore #define NFC_ENOTSUP -2 // Operation not supported #define NFC_EINVARG -3 // Invalid argument(s) +#define NFC_DEVICE_ERROR -4 //Device error /* PN53x specific errors */ // TODO: Be not PN53x-specific here diff --git a/libnfc/chips/pn53x.c b/libnfc/chips/pn53x.c index e62f1d2..4498695 100644 --- a/libnfc/chips/pn53x.c +++ b/libnfc/chips/pn53x.c @@ -643,143 +643,6 @@ pn53x_get_firmware_version (nfc_device *pnd, char abtFirmwareText[22]) return true; } -bool -pn53x_configure (nfc_device *pnd, const nfc_device_option ndo, const bool bEnable) -{ - uint8_t btValue; - switch (ndo) { - case NDO_HANDLE_CRC: - // Enable or disable automatic receiving/sending of CRC bytes - if (bEnable == pnd->bCrc) { - // Nothing to do - return true; - } - // TX and RX are both represented by the symbol 0x80 - btValue = (bEnable) ? 0x80 : 0x00; - if (!pn53x_write_register (pnd, PN53X_REG_CIU_TxMode, SYMBOL_TX_CRC_ENABLE, btValue)) - return false; - if (!pn53x_write_register (pnd, PN53X_REG_CIU_RxMode, SYMBOL_RX_CRC_ENABLE, btValue)) - return false; - pnd->bCrc = bEnable; - break; - - case NDO_HANDLE_PARITY: - // Handle parity bit by PN53X chip or parse it as data bit - if (bEnable == pnd->bPar) - // Nothing to do - return true; - btValue = (bEnable) ? 0x00 : SYMBOL_PARITY_DISABLE; - if (!pn53x_write_register (pnd, PN53X_REG_CIU_ManualRCV, SYMBOL_PARITY_DISABLE, btValue)) - return false; - pnd->bPar = bEnable; - break; - - case NDO_EASY_FRAMING: - pnd->bEasyFraming = bEnable; - break; - - case NDO_ACTIVATE_FIELD: - { - return pn53x_RFConfiguration__RF_field (pnd, bEnable); - } - break; - - case NDO_ACTIVATE_CRYPTO1: - btValue = (bEnable) ? SYMBOL_MF_CRYPTO1_ON : 0x00; - if (!pn53x_write_register (pnd, PN53X_REG_CIU_Status2, SYMBOL_MF_CRYPTO1_ON, btValue)) - return false; - break; - - case NDO_INFINITE_SELECT: - { - // TODO Made some research around this point: - // timings could be tweak better than this, and maybe we can tweak timings - // to "gain" a sort-of hardware polling (ie. like PN532 does) - return pn53x_RFConfiguration__MaxRetries (pnd, - (bEnable) ? 0xff : 0x00, // MxRtyATR, default: active = 0xff, passive = 0x02 - (bEnable) ? 0xff : 0x00, // MxRtyPSL, default: 0x01 - (bEnable) ? 0xff : 0x02 // MxRtyPassiveActivation, default: 0xff (0x00 leads to problems with PN531) - ); - } - break; - - case NDO_ACCEPT_INVALID_FRAMES: - btValue = (bEnable) ? SYMBOL_RX_NO_ERROR : 0x00; - if (!pn53x_write_register (pnd, PN53X_REG_CIU_RxMode, SYMBOL_RX_NO_ERROR, btValue)) - return false; - break; - - case NDO_ACCEPT_MULTIPLE_FRAMES: - btValue = (bEnable) ? SYMBOL_RX_MULTIPLE : 0x00; - if (!pn53x_write_register (pnd, PN53X_REG_CIU_RxMode, SYMBOL_RX_MULTIPLE, btValue)) - return false; - return true; - break; - - case NDO_AUTO_ISO14443_4: - if (bEnable == pnd->bAutoIso14443_4) - // Nothing to do - return true; - pnd->bAutoIso14443_4 = bEnable; - return pn53x_set_parameters (pnd, PARAM_AUTO_RATS, bEnable); - break; - - case NDO_FORCE_ISO14443_A: - if(!bEnable) { - // Nothing to do - return true; - } - // Force pn53x to be in ISO14443-A mode - if (!pn53x_write_register (pnd, PN53X_REG_CIU_TxMode, SYMBOL_TX_FRAMING, 0x00)) { - return false; - } - if (!pn53x_write_register (pnd, PN53X_REG_CIU_RxMode, SYMBOL_RX_FRAMING, 0x00)) { - return false; - } - // Set the PN53X to force 100% ASK Modified miller decoding (default for 14443A cards) - if (!pn53x_write_register (pnd, PN53X_REG_CIU_TxAuto, SYMBOL_FORCE_100_ASK, 0x40)) - return false; - - return true; - break; - - case NDO_FORCE_ISO14443_B: - if(!bEnable) { - // Nothing to do - return true; - } - // Force pn53x to be in ISO14443-B mode - if (!pn53x_write_register (pnd, PN53X_REG_CIU_TxMode, SYMBOL_TX_FRAMING, 0x03)) { - return false; - } - if (!pn53x_write_register (pnd, PN53X_REG_CIU_RxMode, SYMBOL_RX_FRAMING, 0x03)) { - return false; - } - - return true; - break; - - case NDO_FORCE_SPEED_106: - if(!bEnable) { - // Nothing to do - return true; - } - // Force pn53x to be at 106 kbps - if (!pn53x_write_register (pnd, PN53X_REG_CIU_TxMode, SYMBOL_TX_SPEED, 0x00)) { - return false; - } - if (!pn53x_write_register (pnd, PN53X_REG_CIU_RxMode, SYMBOL_RX_SPEED, 0x00)) { - return false; - } - - return true; - break; - } - - // When we reach this, the configuration is completed and successful - return true; -} - uint8_t pn53x_int_to_timeout (const int ms) { @@ -816,6 +679,143 @@ pn53x_set_property_int (nfc_device *pnd, const nfc_property property, const int return NFC_SUCCESS; } +int +pn53x_set_property_bool (nfc_device *pnd, const nfc_property property, const bool bEnable) +{ + uint8_t btValue; + switch (property) { + case NP_HANDLE_CRC: + // Enable or disable automatic receiving/sending of CRC bytes + if (bEnable == pnd->bCrc) { + // Nothing to do + return NFC_SUCCESS; + } + // TX and RX are both represented by the symbol 0x80 + btValue = (bEnable) ? 0x80 : 0x00; + if (!pn53x_write_register (pnd, PN53X_REG_CIU_TxMode, SYMBOL_TX_CRC_ENABLE, btValue)) + return NFC_DEVICE_ERROR; + if (!pn53x_write_register (pnd, PN53X_REG_CIU_RxMode, SYMBOL_RX_CRC_ENABLE, btValue)) + return NFC_DEVICE_ERROR; + pnd->bCrc = bEnable; + break; + + case NP_HANDLE_PARITY: + // Handle parity bit by PN53X chip or parse it as data bit + if (bEnable == pnd->bPar) + // Nothing to do + return NFC_SUCCESS; + btValue = (bEnable) ? 0x00 : SYMBOL_PARITY_DISABLE; + if (!pn53x_write_register (pnd, PN53X_REG_CIU_ManualRCV, SYMBOL_PARITY_DISABLE, btValue)) + return NFC_DEVICE_ERROR; + pnd->bPar = bEnable; + break; + + case NP_EASY_FRAMING: + pnd->bEasyFraming = bEnable; + break; + + case NP_ACTIVATE_FIELD: + { + return pn53x_RFConfiguration__RF_field (pnd, bEnable); + } + break; + + case NP_ACTIVATE_CRYPTO1: + btValue = (bEnable) ? SYMBOL_MF_CRYPTO1_ON : 0x00; + if (!pn53x_write_register (pnd, PN53X_REG_CIU_Status2, SYMBOL_MF_CRYPTO1_ON, btValue)) + return NFC_DEVICE_ERROR; + break; + + case NP_INFINITE_SELECT: + { + // TODO Made some research around this point: + // timings could be tweak better than this, and maybe we can tweak timings + // to "gain" a sort-of hardware polling (ie. like PN532 does) + return pn53x_RFConfiguration__MaxRetries (pnd, + (bEnable) ? 0xff : 0x00, // MxRtyATR, default: active = 0xff, passive = 0x02 + (bEnable) ? 0xff : 0x00, // MxRtyPSL, default: 0x01 + (bEnable) ? 0xff : 0x02 // MxRtyPassiveActivation, default: 0xff (0x00 leads to problems with PN531) + ); + } + break; + + case NP_ACCEPT_INVALID_FRAMES: + btValue = (bEnable) ? SYMBOL_RX_NO_ERROR : 0x00; + if (!pn53x_write_register (pnd, PN53X_REG_CIU_RxMode, SYMBOL_RX_NO_ERROR, btValue)) + return NFC_DEVICE_ERROR; + break; + + case NP_ACCEPT_MULTIPLE_FRAMES: + btValue = (bEnable) ? SYMBOL_RX_MULTIPLE : 0x00; + if (!pn53x_write_register (pnd, PN53X_REG_CIU_RxMode, SYMBOL_RX_MULTIPLE, btValue)) + return NFC_DEVICE_ERROR; + return NFC_SUCCESS; + break; + + case NP_AUTO_ISO14443_4: + if (bEnable == pnd->bAutoIso14443_4) + // Nothing to do + return NFC_SUCCESS; + pnd->bAutoIso14443_4 = bEnable; + return pn53x_set_parameters (pnd, PARAM_AUTO_RATS, bEnable); + break; + + case NP_FORCE_ISO14443_A: + if(!bEnable) { + // Nothing to do + return NFC_SUCCESS; + } + // Force pn53x to be in ISO14443-A mode + if (!pn53x_write_register (pnd, PN53X_REG_CIU_TxMode, SYMBOL_TX_FRAMING, 0x00)) { + return NFC_DEVICE_ERROR; + } + if (!pn53x_write_register (pnd, PN53X_REG_CIU_RxMode, SYMBOL_RX_FRAMING, 0x00)) { + return NFC_DEVICE_ERROR; + } + // Set the PN53X to force 100% ASK Modified miller decoding (default for 14443A cards) + if (!pn53x_write_register (pnd, PN53X_REG_CIU_TxAuto, SYMBOL_FORCE_100_ASK, 0x40)) + return NFC_DEVICE_ERROR; + + return NFC_SUCCESS; + break; + + case NP_FORCE_ISO14443_B: + if(!bEnable) { + // Nothing to do + return NFC_SUCCESS; + } + // Force pn53x to be in ISO14443-B mode + if (!pn53x_write_register (pnd, PN53X_REG_CIU_TxMode, SYMBOL_TX_FRAMING, 0x03)) { + return NFC_DEVICE_ERROR; + } + if (!pn53x_write_register (pnd, PN53X_REG_CIU_RxMode, SYMBOL_RX_FRAMING, 0x03)) { + return NFC_DEVICE_ERROR; + } + + return NFC_SUCCESS; + break; + + case NP_FORCE_SPEED_106: + if(!bEnable) { + // Nothing to do + return NFC_SUCCESS; + } + // Force pn53x to be at 106 kbps + if (!pn53x_write_register (pnd, PN53X_REG_CIU_TxMode, SYMBOL_TX_SPEED, 0x00)) { + return NFC_DEVICE_ERROR; + } + if (!pn53x_write_register (pnd, PN53X_REG_CIU_RxMode, SYMBOL_RX_SPEED, 0x00)) { + return NFC_DEVICE_ERROR; + } + + return NFC_SUCCESS; + break; + } + + // When we reach this, the configuration is completed and successful + return NFC_SUCCESS; +} + bool pn53x_idle (nfc_device *pnd) { @@ -840,7 +840,7 @@ pn53x_idle (nfc_device *pnd) return false; } // Disable RF field to avoid heating - if (!nfc_configure (pnd, NDO_ACTIVATE_FIELD, false)) { + if (nfc_device_set_property_bool (pnd, NP_ACTIVATE_FIELD, false) < 0) { return false; } if (CHIP_DATA (pnd)->type == PN532) { @@ -908,13 +908,13 @@ pn53x_initiator_select_passive_target_ext (nfc_device *pnd, return false; } // No native support in InListPassiveTarget so we do discovery by hand - if (!nfc_configure (pnd, NDO_FORCE_ISO14443_B, true)) { + if (nfc_device_set_property_bool (pnd, NP_FORCE_ISO14443_B, true) < 0) { return false; } - if (!nfc_configure (pnd, NDO_FORCE_SPEED_106, true)) { + if (nfc_device_set_property_bool (pnd, NP_FORCE_SPEED_106, true) < 0) { return false; } - if (!nfc_configure (pnd, NDO_HANDLE_CRC, true)) { + if (nfc_device_set_property_bool (pnd, NP_HANDLE_CRC, true) < 0) { return false; } pnd->bEasyFraming = false; @@ -1051,7 +1051,7 @@ pn53x_initiator_poll_target (nfc_device *pnd, break; } } else { - pn53x_configure (pnd, NDO_INFINITE_SELECT, true); + pn53x_set_property_bool (pnd, NP_INFINITE_SELECT, true); // FIXME It does not support DEP targets do { for (size_t p=0; ptype == PN532) { // We have a PN532 if ((pnt->nti.nai.btSak & SAK_ISO14443_4_COMPLIANT) && (pnd->bAutoIso14443_4)) { - // We have a ISO14443-4 tag to emulate and NDO_AUTO_14443_4A option is enabled + // We have a ISO14443-4 tag to emulate and NP_AUTO_14443_4A option is enabled ptm |= PTM_ISO14443_4_PICC_ONLY; // We add ISO14443-4 restriction pn53x_set_parameters (pnd, PARAM_14443_4_PICC, true); } else { diff --git a/libnfc/chips/pn53x.h b/libnfc/chips/pn53x.h index c1f96cc..e727b8f 100644 --- a/libnfc/chips/pn53x.h +++ b/libnfc/chips/pn53x.h @@ -276,8 +276,8 @@ bool pn53x_decode_target_data (const uint8_t *pbtRawData, size_t szRawData, bool pn53x_read_register (nfc_device *pnd, uint16_t ui16Reg, uint8_t *ui8Value); bool pn53x_write_register (nfc_device *pnd, uint16_t ui16Reg, uint8_t ui8SymbolMask, uint8_t ui8Value); bool pn53x_get_firmware_version (nfc_device *pnd, char abtFirmwareText[22]); -bool pn53x_configure (nfc_device *pnd, const nfc_device_option ndo, const bool bEnable); int pn53x_set_property_int (nfc_device *pnd, const nfc_property property, const int value); +int pn53x_set_property_bool (nfc_device *pnd, const nfc_property property, const bool bEnable); bool pn53x_check_communication (nfc_device *pnd); bool pn53x_idle (nfc_device *pnd); diff --git a/libnfc/drivers/acr122.c b/libnfc/drivers/acr122.c index 91d85c2..48d400b 100644 --- a/libnfc/drivers/acr122.c +++ b/libnfc/drivers/acr122.c @@ -487,7 +487,7 @@ const struct nfc_driver_t acr122_driver = { .target_send_bits = pn53x_target_send_bits, .target_receive_bits = pn53x_target_receive_bits, - .configure = pn53x_configure, + .device_set_property_bool = pn53x_set_property_bool, .device_set_property_int = pn53x_set_property_int, .abort_command = NULL, // FIXME: abort is not supported in this driver diff --git a/libnfc/drivers/arygon.c b/libnfc/drivers/arygon.c index 89b53f2..11b4c5d 100644 --- a/libnfc/drivers/arygon.c +++ b/libnfc/drivers/arygon.c @@ -579,7 +579,7 @@ const struct nfc_driver_t arygon_driver = { .target_send_bits = pn53x_target_send_bits, .target_receive_bits = pn53x_target_receive_bits, - .configure = pn53x_configure, + .device_set_property_bool = pn53x_set_property_bool, .device_set_property_int = pn53x_set_property_int, .abort_command = arygon_abort_command, diff --git a/libnfc/drivers/pn532_uart.c b/libnfc/drivers/pn532_uart.c index 33b1873..f582d8b 100644 --- a/libnfc/drivers/pn532_uart.c +++ b/libnfc/drivers/pn532_uart.c @@ -525,7 +525,7 @@ const struct nfc_driver_t pn532_uart_driver = { .target_send_bits = pn53x_target_send_bits, .target_receive_bits = pn53x_target_receive_bits, - .configure = pn53x_configure, + .device_set_property_bool = pn53x_set_property_bool, .device_set_property_int = pn53x_set_property_int, .abort_command = pn532_uart_abort_command, diff --git a/libnfc/drivers/pn53x_usb.c b/libnfc/drivers/pn53x_usb.c index bcc01ed..d660e4f 100644 --- a/libnfc/drivers/pn53x_usb.c +++ b/libnfc/drivers/pn53x_usb.c @@ -751,32 +751,32 @@ On ASK LoGO hardware: return true; } -bool -pn53x_usb_configure (nfc_device *pnd, const nfc_device_option ndo, const bool bEnable) +int +pn53x_usb_set_property_bool (nfc_device *pnd, const nfc_property property, const bool bEnable) { - if (!pn53x_configure (pnd, ndo, bEnable)) - return false; + if (!pn53x_set_property_bool (pnd, property, bEnable)) + return NFC_DEVICE_ERROR; switch (DRIVER_DATA (pnd)->model) { case ASK_LOGO: - if (NDO_ACTIVATE_FIELD == ndo) { + if (NP_ACTIVATE_FIELD == property) { /* Switch on/off LED2 and Progressive Field GPIO according to ACTIVATE_FIELD option */ log_put (LOG_CATEGORY, NFC_PRIORITY_TRACE, "Switch progressive field %s", bEnable ? "On" : "Off"); if (!pn53x_write_register (pnd, PN53X_SFR_P3, _BV(P31) | _BV(P34), bEnable ? _BV (P34) : _BV (P31))) - return false; + return NFC_DEVICE_ERROR; } break; case SCM_SCL3711: - if (NDO_ACTIVATE_FIELD == ndo) { + if (NP_ACTIVATE_FIELD == property) { // Switch on/off LED according to ACTIVATE_FIELD option if (!pn53x_write_register (pnd, PN53X_SFR_P3, _BV (P32), bEnable ? 0 : _BV (P32))) - return false; + return NFC_DEVICE_ERROR; } break; default: break; } - return true; + return NFC_SUCCESS; } bool @@ -814,7 +814,7 @@ const struct nfc_driver_t pn53x_usb_driver = { .target_send_bits = pn53x_target_send_bits, .target_receive_bits = pn53x_target_receive_bits, - .configure = pn53x_usb_configure, + .device_set_property_bool = pn53x_usb_set_property_bool, .device_set_property_int = pn53x_set_property_int, .abort_command = pn53x_usb_abort_command, diff --git a/libnfc/nfc-internal.h b/libnfc/nfc-internal.h index 89dccb1..6cdad51 100644 --- a/libnfc/nfc-internal.h +++ b/libnfc/nfc-internal.h @@ -147,7 +147,7 @@ struct nfc_driver_t { bool (*target_send_bits) (nfc_device *pnd, const uint8_t * pbtTx, const size_t szTxBits, const uint8_t * pbtTxPar); bool (*target_receive_bits) (nfc_device *pnd, uint8_t * pbtRx, size_t * pszRxBits, uint8_t * pbtRxPar); - bool (*configure) (nfc_device *pnd, const nfc_device_option ndo, const bool bEnable); + int (*device_set_property_bool) (nfc_device *pnd, const nfc_property property, const bool bEnable); int (*device_set_property_int) (nfc_device *pnd, const nfc_property property, const int value); bool (*abort_command) (nfc_device *pnd); diff --git a/libnfc/nfc.c b/libnfc/nfc.c index 8f12ce4..4761886 100644 --- a/libnfc/nfc.c +++ b/libnfc/nfc.c @@ -203,24 +203,6 @@ nfc_list_devices (nfc_connstring connstrings[] , size_t szDevices, size_t *pszDe log_fini (); } -/** - * @brief Configure advanced NFC device settings - * @return Returns \c true if action was successfully performed; otherwise returns \c false. - * @param pnd \a nfc_device struct pointer that represent currently used device - * @param ndo \a nfc_device_option struct that contains option to set to device - * @param bEnable boolean to activate/disactivate the option - * - * Configures parameters and registers that control for example timing, - * modulation, frame and error handling. There are different categories for - * configuring the \e PN53X chip features (handle, activate, infinite and - * accept). - */ -bool -nfc_configure (nfc_device *pnd, const nfc_device_option ndo, const bool bEnable) -{ - HAL (configure, pnd, ndo, bEnable); -} - /** * @brief Set a device's integer-property value * @return Returns 0 on success, otherwise returns libnfc's error code (negative value) @@ -238,6 +220,25 @@ nfc_device_set_property_int (nfc_device *pnd, const nfc_property property, const HAL (device_set_property_int, pnd, property, value); } + +/** + * @brief Set a device's boolean-property value + * @return Returns 0 on success, otherwise returns libnfc's error code (negative value) + * @param pnd \a nfc_device struct pointer that represent currently used device + * @param property \a nfc_property which will be set + * @param bEnable boolean to activate/disactivate the property + * + * Configures parameters and registers that control for example timing, + * modulation, frame and error handling. There are different categories for + * configuring the \e PN53X chip features (handle, activate, infinite and + * accept). + */ +int +nfc_device_set_property_bool (nfc_device *pnd, const nfc_property property, const bool bEnable) +{ + HAL (device_set_property_bool, pnd, property, bEnable); +} + /** * @brief Initialize NFC device as initiator (reader) * @return Returns \c true if action was successfully performed; otherwise returns \c false. @@ -246,55 +247,55 @@ nfc_device_set_property_int (nfc_device *pnd, const nfc_property property, const * The NFC device is configured to function as RFID reader. * After initialization it can be used to communicate to passive RFID tags and active NFC devices. * The reader will act as initiator to communicate peer 2 peer (NFCIP) to other active NFC devices. - * - Crc is handled by the device (NDO_HANDLE_CRC = true) - * - Parity is handled the device (NDO_HANDLE_PARITY = true) - * - Cryto1 cipher is disabled (NDO_ACTIVATE_CRYPTO1 = false) - * - Easy framing is enabled (NDO_EASY_FRAMING = true) - * - Auto-switching in ISO14443-4 mode is enabled (NDO_AUTO_ISO14443_4 = true) - * - Invalid frames are not accepted (NDO_ACCEPT_INVALID_FRAMES = false) - * - Multiple frames are not accepted (NDO_ACCEPT_MULTIPLE_FRAMES = false) - * - 14443-A mode is activated (NDO_FORCE_ISO14443_A = true) - * - speed is set to 106 kbps (NDO_FORCE_SPEED_106 = true) - * - Let the device try forever to find a target (NDO_INFINITE_SELECT = true) + * - Crc is handled by the device (NP_HANDLE_CRC = true) + * - Parity is handled the device (NP_HANDLE_PARITY = true) + * - Cryto1 cipher is disabled (NP_ACTIVATE_CRYPTO1 = false) + * - Easy framing is enabled (NP_EASY_FRAMING = true) + * - Auto-switching in ISO14443-4 mode is enabled (NP_AUTO_ISO14443_4 = true) + * - Invalid frames are not accepted (NP_ACCEPT_INVALID_FRAMES = false) + * - Multiple frames are not accepted (NP_ACCEPT_MULTIPLE_FRAMES = false) + * - 14443-A mode is activated (NP_FORCE_ISO14443_A = true) + * - speed is set to 106 kbps (NP_FORCE_SPEED_106 = true) + * - Let the device try forever to find a target (NP_INFINITE_SELECT = true) * - RF field is shortly dropped (if it was enabled) then activated again */ bool nfc_initiator_init (nfc_device *pnd) { // Drop the field for a while - if (!nfc_configure (pnd, NDO_ACTIVATE_FIELD, false)) + if (nfc_device_set_property_bool (pnd, NP_ACTIVATE_FIELD, false) < 0) return false; // Enable field so more power consuming cards can power themselves up - if (!nfc_configure (pnd, NDO_ACTIVATE_FIELD, true)) + if (nfc_device_set_property_bool (pnd, NP_ACTIVATE_FIELD, true) < 0) return false; // Let the device try forever to find a target/tag - if (!nfc_configure (pnd, NDO_INFINITE_SELECT, true)) + if (nfc_device_set_property_bool (pnd, NP_INFINITE_SELECT, true) < 0) return false; // Activate auto ISO14443-4 switching by default - if (!nfc_configure (pnd, NDO_AUTO_ISO14443_4, true)) + if (nfc_device_set_property_bool (pnd, NP_AUTO_ISO14443_4, true) < 0) return false; // Force 14443-A mode - if (!nfc_configure (pnd, NDO_FORCE_ISO14443_A, true)) + if (nfc_device_set_property_bool (pnd, NP_FORCE_ISO14443_A, true) < 0) return false; // Force speed at 106kbps - if (!nfc_configure (pnd, NDO_FORCE_SPEED_106, true)) + if (nfc_device_set_property_bool (pnd, NP_FORCE_SPEED_106, true) < 0) return false; // Disallow invalid frame - if (!nfc_configure (pnd, NDO_ACCEPT_INVALID_FRAMES, false)) + if (nfc_device_set_property_bool (pnd, NP_ACCEPT_INVALID_FRAMES, false) < 0) return false; // Disallow multiple frames - if (!nfc_configure (pnd, NDO_ACCEPT_MULTIPLE_FRAMES, false)) + if (nfc_device_set_property_bool (pnd, NP_ACCEPT_MULTIPLE_FRAMES, false) < 0) return false; // Make sure we reset the CRC and parity to chip handling. - if (!nfc_configure (pnd, NDO_HANDLE_CRC, true)) + if (nfc_device_set_property_bool (pnd, NP_HANDLE_CRC, true) < 0) return false; - if (!nfc_configure (pnd, NDO_HANDLE_PARITY, true)) + if (nfc_device_set_property_bool (pnd, NP_HANDLE_PARITY, true) < 0) return false; // Activate "easy framing" feature by default - if (!nfc_configure (pnd, NDO_EASY_FRAMING, true)) + if (nfc_device_set_property_bool (pnd, NP_EASY_FRAMING, true) < 0) return false; // Deactivate the CRYPTO1 cipher, it may could cause problems when still active - if (!nfc_configure (pnd, NDO_ACTIVATE_CRYPTO1, false)) + if (nfc_device_set_property_bool (pnd, NP_ACTIVATE_CRYPTO1, false) < 0) return false; HAL (initiator_init, pnd); @@ -373,7 +374,7 @@ nfc_initiator_list_passive_targets (nfc_device *pnd, pnd->iLastError = 0; // Let the reader only try once to find a tag - if (!nfc_configure (pnd, NDO_INFINITE_SELECT, false)) { + if (nfc_device_set_property_bool (pnd, NP_INFINITE_SELECT, false) < 0) { return false; } @@ -486,14 +487,14 @@ nfc_initiator_deselect_target (nfc_device *pnd) * If timeout is not a null pointer, it specifies the maximum interval to wait for the function to be executed. * If timeout is a null pointer, the function blocks indefinitely (until an error is raised or function is completed). * - * If \a NDO_EASY_FRAMING option is disabled the frames will sent and received in raw mode: \e PN53x will not handle input neither output data. + * If \a NP_EASY_FRAMING option is disabled the frames will sent and received in raw mode: \e PN53x will not handle input neither output data. * * The parity bits are handled by the \e PN53x chip. The CRC can be generated automatically or handled manually. * Using this function, frames can be communicated very fast via the NFC initiator to the tag. * * Tests show that on average this way of communicating is much faster than using the regular driver/middle-ware (often supplied by manufacturers). * - * @warning The configuration option \a NDO_HANDLE_PARITY must be set to \c true (the default value). + * @warning The configuration option \a NP_HANDLE_PARITY must be set to \c true (the default value). */ bool nfc_initiator_transceive_bytes (nfc_device *pnd, const uint8_t *pbtTx, const size_t szTx, uint8_t *pbtRx, @@ -550,7 +551,7 @@ nfc_initiator_transceive_bits (nfc_device *pnd, const uint8_t *pbtTx, const size * * This function is similar to nfc_initiator_transceive_bytes() with the following differences: * - A precise cycles counter will indicate the number of cycles between emission & reception of frames. - * - It only supports mode with \a NDO_EASY_FRAMING option disabled. + * - It only supports mode with \a NP_EASY_FRAMING option disabled. * - Overall communication with the host is heavier and slower. * * Timer control: @@ -561,8 +562,8 @@ nfc_initiator_transceive_bits (nfc_device *pnd, const uint8_t *pbtTx, const size * - If you need to count more cycles, set *cycles to the maximum you expect but don't forget * you'll loose in precision and it'll take more time before timeout, so don't abuse! * - * @warning The configuration option \a NDO_EASY_FRAMING must be set to \c false. - * @warning The configuration option \a NDO_HANDLE_PARITY must be set to \c true (the default value). + * @warning The configuration option \a NP_EASY_FRAMING must be set to \c false. + * @warning The configuration option \a NP_HANDLE_PARITY must be set to \c true (the default value). */ bool nfc_initiator_transceive_bytes_timed (nfc_device *pnd, const uint8_t *pbtTx, const size_t szTx, uint8_t *pbtRx, @@ -577,7 +578,7 @@ nfc_initiator_transceive_bytes_timed (nfc_device *pnd, const uint8_t *pbtTx, con * * This function is similar to nfc_initiator_transceive_bits() with the following differences: * - A precise cycles counter will indicate the number of cycles between emission & reception of frames. - * - It only supports mode with \a NDO_EASY_FRAMING option disabled and CRC must be handled manually. + * - It only supports mode with \a NP_EASY_FRAMING option disabled and CRC must be handled manually. * - Overall communication with the host is heavier and slower. * * Timer control: @@ -588,9 +589,9 @@ nfc_initiator_transceive_bytes_timed (nfc_device *pnd, const uint8_t *pbtTx, con * - If you need to count more cycles, set *cycles to the maximum you expect but don't forget * you'll loose in precision and it'll take more time before timeout, so don't abuse! * - * @warning The configuration option \a NDO_EASY_FRAMING must be set to \c false. - * @warning The configuration option \a NDO_HANDLE_CRC must be set to \c false. - * @warning The configuration option \a NDO_HANDLE_PARITY must be set to \c true (the default value). + * @warning The configuration option \a NP_EASY_FRAMING must be set to \c false. + * @warning The configuration option \a NP_HANDLE_CRC must be set to \c false. + * @warning The configuration option \a NP_HANDLE_PARITY must be set to \c true (the default value). */ bool nfc_initiator_transceive_bits_timed (nfc_device *pnd, const uint8_t *pbtTx, const size_t szTxBits, const uint8_t *pbtTxPar, @@ -615,13 +616,13 @@ nfc_initiator_transceive_bits_timed (nfc_device *pnd, const uint8_t *pbtTx, cons * * This function initializes NFC device in \e target mode in order to emulate a * tag using the specified \a nfc_target_mode_t. - * - Crc is handled by the device (NDO_HANDLE_CRC = true) - * - Parity is handled the device (NDO_HANDLE_PARITY = true) - * - Cryto1 cipher is disabled (NDO_ACTIVATE_CRYPTO1 = false) - * - Auto-switching in ISO14443-4 mode is enabled (NDO_AUTO_ISO14443_4 = true) - * - Easy framing is disabled (NDO_EASY_FRAMING = false) - * - Invalid frames are not accepted (NDO_ACCEPT_INVALID_FRAMES = false) - * - Multiple frames are not accepted (NDO_ACCEPT_MULTIPLE_FRAMES = false) + * - Crc is handled by the device (NP_HANDLE_CRC = true) + * - Parity is handled the device (NP_HANDLE_PARITY = true) + * - Cryto1 cipher is disabled (NP_ACTIVATE_CRYPTO1 = false) + * - Auto-switching in ISO14443-4 mode is enabled (NP_AUTO_ISO14443_4 = true) + * - Easy framing is disabled (NP_EASY_FRAMING = false) + * - Invalid frames are not accepted (NP_ACCEPT_INVALID_FRAMES = false) + * - Multiple frames are not accepted (NP_ACCEPT_MULTIPLE_FRAMES = false) * - RF field is dropped * * @warning Be aware that this function will wait (hang) until a command is @@ -633,27 +634,27 @@ bool nfc_target_init (nfc_device *pnd, nfc_target *pnt, uint8_t *pbtRx, size_t * pszRx) { // Disallow invalid frame - if (!nfc_configure (pnd, NDO_ACCEPT_INVALID_FRAMES, false)) + if (nfc_device_set_property_bool (pnd, NP_ACCEPT_INVALID_FRAMES, false) < 0) return false; // Disallow multiple frames - if (!nfc_configure (pnd, NDO_ACCEPT_MULTIPLE_FRAMES, false)) + if (nfc_device_set_property_bool (pnd, NP_ACCEPT_MULTIPLE_FRAMES, false) < 0) return false; // Make sure we reset the CRC and parity to chip handling. - if (!nfc_configure (pnd, NDO_HANDLE_CRC, true)) + if (nfc_device_set_property_bool (pnd, NP_HANDLE_CRC, true) < 0) return false; - if (!nfc_configure (pnd, NDO_HANDLE_PARITY, true)) + if (nfc_device_set_property_bool (pnd, NP_HANDLE_PARITY, true) < 0) return false; // Activate auto ISO14443-4 switching by default - if (!nfc_configure (pnd, NDO_AUTO_ISO14443_4, true)) + if (nfc_device_set_property_bool (pnd, NP_AUTO_ISO14443_4, true) < 0) return false; // Activate "easy framing" feature by default - if (!nfc_configure (pnd, NDO_EASY_FRAMING, true)) + if (nfc_device_set_property_bool (pnd, NP_EASY_FRAMING, true) < 0) return false; // Deactivate the CRYPTO1 cipher, it may could cause problems when still active - if (!nfc_configure (pnd, NDO_ACTIVATE_CRYPTO1, false)) + if (nfc_device_set_property_bool (pnd, NP_ACTIVATE_CRYPTO1, false) < 0) return false; // Drop explicitely the field - if (!nfc_configure (pnd, NDO_ACTIVATE_FIELD, false)) + if (nfc_device_set_property_bool (pnd, NP_ACTIVATE_FIELD, false) < 0) return false; HAL (target_init, pnd, pnt, pbtRx, pszRx); @@ -754,7 +755,7 @@ nfc_target_send_bits (nfc_device *pnd, const uint8_t *pbtTx, const size_t szTxBi * the messages that are stored in the FIFO buffer of the \e PN53x chip. It * does not require to send any frame and thereby could be used to snoop frames * that are transmitted by a nearby \e initiator. @note Check out the - * NDO_ACCEPT_MULTIPLE_FRAMES configuration option to avoid losing transmitted + * NP_ACCEPT_MULTIPLE_FRAMES configuration option to avoid losing transmitted * frames. */ bool diff --git a/utils/mifare.c b/utils/mifare.c index 91daf0d..973fff9 100644 --- a/utils/mifare.c +++ b/utils/mifare.c @@ -95,8 +95,8 @@ nfc_initiator_mifare_cmd (nfc_device *pnd, const mifare_cmd mc, const uint8_t ui memcpy (abtCmd + 2, (uint8_t *) pmp, szParamLen); bEasyFraming = pnd->bEasyFraming; - if (!nfc_configure (pnd, NDO_EASY_FRAMING, true)) { - nfc_perror (pnd, "nfc_configure"); + if (nfc_device_set_property_bool (pnd, NP_EASY_FRAMING, true) < 0) { + nfc_perror (pnd, "nfc_device_set_property_bool"); return false; } // Fire the mifare command @@ -109,11 +109,11 @@ nfc_initiator_mifare_cmd (nfc_device *pnd, const mifare_cmd mc, const uint8_t ui } else { nfc_perror (pnd, "nfc_initiator_transceive_bytes"); } - nfc_configure (pnd, NDO_EASY_FRAMING, bEasyFraming); + nfc_device_set_property_bool (pnd, NP_EASY_FRAMING, bEasyFraming); return false; } - if (!nfc_configure (pnd, NDO_EASY_FRAMING, bEasyFraming)) { - nfc_perror (pnd, "nfc_configure"); + if (nfc_device_set_property_bool (pnd, NP_EASY_FRAMING, bEasyFraming) < 0) { + nfc_perror (pnd, "nfc_device_set_property_bool"); return false; } diff --git a/utils/nfc-mfclassic.c b/utils/nfc-mfclassic.c index 35c3d33..27a9aff 100644 --- a/utils/nfc-mfclassic.c +++ b/utils/nfc-mfclassic.c @@ -219,12 +219,12 @@ unlock_card() printf ("Unlocking card\n"); // Configure the CRC - if (!nfc_configure (pnd, NDO_HANDLE_CRC, false)) { + if (nfc_device_set_property_bool (pnd, NP_HANDLE_CRC, false) < 0) { nfc_perror (pnd, "nfc_configure"); exit (EXIT_FAILURE); } // Use raw send/receive methods - if (!nfc_configure (pnd, NDO_EASY_FRAMING, false)) { + if (nfc_device_set_property_bool (pnd, NP_EASY_FRAMING, false) < 0) { nfc_perror (pnd, "nfc_configure"); exit (EXIT_FAILURE); } @@ -243,13 +243,13 @@ unlock_card() // reset reader // Configure the CRC - if (!nfc_configure (pnd, NDO_HANDLE_CRC, true)) { - nfc_perror (pnd, "nfc_configure"); + if (nfc_device_set_property_bool (pnd, NP_HANDLE_CRC, true) < 0) { + nfc_perror (pnd, "nfc_device_set_property_bool"); exit (EXIT_FAILURE); } // Switch off raw send/receive methods - if (!nfc_configure (pnd, NDO_EASY_FRAMING, true)) { - nfc_perror (pnd, "nfc_configure"); + if (nfc_device_set_property_bool (pnd, NP_EASY_FRAMING, true) < 0) { + nfc_perror (pnd, "nfc_device_set_property_bool"); exit (EXIT_FAILURE); } return true; @@ -547,12 +547,12 @@ main (int argc, const char *argv[]) nfc_initiator_init (pnd); // Let the reader only try once to find a tag - if (!nfc_configure (pnd, NDO_INFINITE_SELECT, false)) { - nfc_perror (pnd, "nfc_configure"); + if (nfc_device_set_property_bool (pnd, NP_INFINITE_SELECT, false) < 0) { + nfc_perror (pnd, "nfc_device_set_property_bool"); exit (EXIT_FAILURE); } // Disable ISO14443-4 switching in order to read devices that emulate Mifare Classic with ISO14443-4 compliance. - nfc_configure (pnd, NDO_AUTO_ISO14443_4, false); + nfc_device_set_property_bool (pnd, NP_AUTO_ISO14443_4, false); printf ("Connected to NFC reader: %s\n", pnd->acName); diff --git a/utils/nfc-mfsetuid.c b/utils/nfc-mfsetuid.c index 0c33a53..a64f497 100644 --- a/utils/nfc-mfsetuid.c +++ b/utils/nfc-mfsetuid.c @@ -189,18 +189,18 @@ main (int argc, char *argv[]) nfc_initiator_init (pnd); // Configure the CRC - if (!nfc_configure (pnd, NDO_HANDLE_CRC, false)) { - nfc_perror (pnd, "nfc_configure"); + if (nfc_device_set_property_bool (pnd, NP_HANDLE_CRC, false) < 0) { + nfc_perror (pnd, "nfc_device_set_property_bool"); exit (EXIT_FAILURE); } // Use raw send/receive methods - if (!nfc_configure (pnd, NDO_EASY_FRAMING, false)) { - nfc_perror (pnd, "nfc_configure"); + if (nfc_device_set_property_bool (pnd, NP_EASY_FRAMING, false) < 0) { + nfc_perror (pnd, "nfc_device_set_property_bool"); exit (EXIT_FAILURE); } // Disable 14443-4 autoswitching - if (!nfc_configure (pnd, NDO_AUTO_ISO14443_4, false)) { - nfc_perror (pnd, "nfc_configure"); + if (nfc_device_set_property_bool (pnd, NP_AUTO_ISO14443_4, false) < 0) { + nfc_perror (pnd, "nfc_device_set_property_bool"); exit (EXIT_FAILURE); } diff --git a/utils/nfc-mfultralight.c b/utils/nfc-mfultralight.c index a9396c7..bfad59c 100644 --- a/utils/nfc-mfultralight.c +++ b/utils/nfc-mfultralight.c @@ -214,8 +214,8 @@ main (int argc, const char *argv[]) nfc_initiator_init (pnd); // Let the device only try once to find a tag - if (!nfc_configure (pnd, NDO_INFINITE_SELECT, false)) { - nfc_perror (pnd, "nfc_configure"); + if (nfc_device_set_property_bool (pnd, NP_INFINITE_SELECT, false) < 0) { + nfc_perror (pnd, "nfc_device_set_property_bool"); exit (EXIT_FAILURE); } diff --git a/utils/nfc-read-forum-tag3.c b/utils/nfc-read-forum-tag3.c index ccba19d..d8f8c73 100644 --- a/utils/nfc-read-forum-tag3.c +++ b/utils/nfc-read-forum-tag3.c @@ -245,8 +245,8 @@ main(int argc, char *argv[]) //print_nfc_felica_info(nt.nti.nfi, true); - if (!nfc_configure (pnd, NDO_EASY_FRAMING, false) || !nfc_configure (pnd, NDO_INFINITE_SELECT, false)) { - nfc_perror (pnd, "nfc_configure"); + if ((nfc_device_set_property_bool (pnd, NP_EASY_FRAMING, false) < 0) || (nfc_device_set_property_bool (pnd, NP_INFINITE_SELECT, false) < 0)) { + nfc_perror (pnd, "nfc_device_set_property_bool"); error = EXIT_FAILURE; goto error; } From 98355d36a7f3eb3bafcb7ec7891f2e20e7cd3472 Mon Sep 17 00:00:00 2001 From: Audrey Diacre Date: Thu, 15 Dec 2011 11:46:14 +0000 Subject: [PATCH 026/113] nfc_initiator_init returns now error code and nfc_initiator_list_passive_targets returns now the number of targets found or error code. --- examples/nfc-dep-initiator.c | 2 +- include/nfc/nfc.h | 4 +- libnfc/chips/pn53x.c | 6 +-- libnfc/chips/pn53x.h | 2 +- libnfc/nfc-internal.h | 2 +- libnfc/nfc.c | 69 +++++++++++++++-------------- test/test_access_storm.c | 10 ++--- test/test_dep_active.c | 2 +- test/test_dep_passive.c | 2 +- utils/nfc-list.c | 84 ++++++++++++++++++------------------ utils/nfc-relay-picc.c | 2 +- 11 files changed, 91 insertions(+), 94 deletions(-) diff --git a/examples/nfc-dep-initiator.c b/examples/nfc-dep-initiator.c index e7ad00e..4de2da5 100644 --- a/examples/nfc-dep-initiator.c +++ b/examples/nfc-dep-initiator.c @@ -81,7 +81,7 @@ main (int argc, const char *argv[]) signal (SIGINT, stop_dep_communication); - if (!nfc_initiator_init (pnd)) { + if (nfc_initiator_init (pnd) < 0) { nfc_perror(pnd, "nfc_initiator_init"); return EXIT_FAILURE; } diff --git a/include/nfc/nfc.h b/include/nfc/nfc.h index 825da4c..b55c1b8 100644 --- a/include/nfc/nfc.h +++ b/include/nfc/nfc.h @@ -71,9 +71,9 @@ extern "C" { NFC_EXPORT bool nfc_idle (nfc_device *pnd); /* NFC initiator: act as "reader" */ - NFC_EXPORT bool nfc_initiator_init (nfc_device *pnd); + NFC_EXPORT int nfc_initiator_init (nfc_device *pnd); NFC_EXPORT bool nfc_initiator_select_passive_target (nfc_device *pnd, const nfc_modulation nm, const uint8_t *pbtInitData, const size_t szInitData, nfc_target *pnt); - NFC_EXPORT bool nfc_initiator_list_passive_targets (nfc_device *pnd, const nfc_modulation nm, nfc_target ant[], const size_t szTargets, size_t *pszTargetFound); + NFC_EXPORT int nfc_initiator_list_passive_targets (nfc_device *pnd, const nfc_modulation nm, nfc_target ant[], const size_t szTargets); NFC_EXPORT bool nfc_initiator_poll_target (nfc_device *pnd, const nfc_modulation *pnmTargetTypes, const size_t szTargetTypes, const uint8_t uiPollNr, const uint8_t uiPeriod, nfc_target *pnt); NFC_EXPORT bool nfc_initiator_select_dep_target (nfc_device *pnd, const nfc_dep_mode ndm, const nfc_baud_rate nbr, const nfc_dep_info *pndiInitiator, nfc_target *pnt, const int timeout); NFC_EXPORT bool nfc_initiator_deselect_target (nfc_device *pnd); diff --git a/libnfc/chips/pn53x.c b/libnfc/chips/pn53x.c index 4498695..0a9c2ca 100644 --- a/libnfc/chips/pn53x.c +++ b/libnfc/chips/pn53x.c @@ -878,17 +878,17 @@ pn53x_check_communication (nfc_device *pnd) return ((sizeof(abtExpectedRx) == szRx) && (0 == memcmp (abtRx, abtExpectedRx, sizeof(abtExpectedRx)))); } -bool +int pn53x_initiator_init (nfc_device *pnd) { pn53x_reset_settings(pnd); // Configure the PN53X to be an Initiator or Reader/Writer if (!pn53x_write_register (pnd, PN53X_REG_CIU_Control, SYMBOL_INITIATOR, 0x10)) - return false; + return NFC_DEVICE_ERROR; CHIP_DATA (pnd)->operating_mode = INITIATOR; - return true; + return NFC_SUCCESS; } bool diff --git a/libnfc/chips/pn53x.h b/libnfc/chips/pn53x.h index e727b8f..d5f8ce4 100644 --- a/libnfc/chips/pn53x.h +++ b/libnfc/chips/pn53x.h @@ -283,7 +283,7 @@ bool pn53x_check_communication (nfc_device *pnd); bool pn53x_idle (nfc_device *pnd); // NFC device as Initiator functions -bool pn53x_initiator_init (nfc_device *pnd); +int pn53x_initiator_init (nfc_device *pnd); bool pn53x_initiator_select_passive_target (nfc_device *pnd, const nfc_modulation nm, const uint8_t *pbtInitData, const size_t szInitData, diff --git a/libnfc/nfc-internal.h b/libnfc/nfc-internal.h index 6cdad51..bfd703e 100644 --- a/libnfc/nfc-internal.h +++ b/libnfc/nfc-internal.h @@ -131,7 +131,7 @@ struct nfc_driver_t { void (*disconnect) (nfc_device *pnd); const char *(*strerror) (const nfc_device *pnd); - bool (*initiator_init) (nfc_device *pnd); + int (*initiator_init) (nfc_device *pnd); bool (*initiator_select_passive_target) (nfc_device *pnd, const nfc_modulation nm, const uint8_t * pbtInitData, const size_t szInitData, nfc_target * pnt); bool (*initiator_poll_target) (nfc_device *pnd, const nfc_modulation * pnmModulations, const size_t szModulations, const uint8_t uiPollNr, const uint8_t btPeriod, nfc_target * pnt); bool (*initiator_select_dep_target) (nfc_device *pnd, const nfc_dep_mode ndm, const nfc_baud_rate nbr, const nfc_dep_info * pndiInitiator, nfc_target * pnt, const int timeout); diff --git a/libnfc/nfc.c b/libnfc/nfc.c index 4761886..48641f3 100644 --- a/libnfc/nfc.c +++ b/libnfc/nfc.c @@ -241,7 +241,7 @@ nfc_device_set_property_bool (nfc_device *pnd, const nfc_property property, cons /** * @brief Initialize NFC device as initiator (reader) - * @return Returns \c true if action was successfully performed; otherwise returns \c false. + * @return Returns 0 on success, otherwise returns libnfc's error code (negative value) * @param pnd \a nfc_device struct pointer that represent currently used device * * The NFC device is configured to function as RFID reader. @@ -259,44 +259,45 @@ nfc_device_set_property_bool (nfc_device *pnd, const nfc_property property, cons * - Let the device try forever to find a target (NP_INFINITE_SELECT = true) * - RF field is shortly dropped (if it was enabled) then activated again */ -bool +int nfc_initiator_init (nfc_device *pnd) { + int res = 0; // Drop the field for a while - if (nfc_device_set_property_bool (pnd, NP_ACTIVATE_FIELD, false) < 0) - return false; + if ((res = nfc_device_set_property_bool (pnd, NP_ACTIVATE_FIELD, false)) < 0) + return res; // Enable field so more power consuming cards can power themselves up - if (nfc_device_set_property_bool (pnd, NP_ACTIVATE_FIELD, true) < 0) - return false; + if ((res = nfc_device_set_property_bool (pnd, NP_ACTIVATE_FIELD, true)) < 0) + return res; // Let the device try forever to find a target/tag - if (nfc_device_set_property_bool (pnd, NP_INFINITE_SELECT, true) < 0) - return false; + if ((res = nfc_device_set_property_bool (pnd, NP_INFINITE_SELECT, true)) < 0) + return res; // Activate auto ISO14443-4 switching by default - if (nfc_device_set_property_bool (pnd, NP_AUTO_ISO14443_4, true) < 0) - return false; + if ((res = nfc_device_set_property_bool (pnd, NP_AUTO_ISO14443_4, true)) < 0) + return res; // Force 14443-A mode - if (nfc_device_set_property_bool (pnd, NP_FORCE_ISO14443_A, true) < 0) - return false; + if ((res = nfc_device_set_property_bool (pnd, NP_FORCE_ISO14443_A, true)) < 0) + return res; // Force speed at 106kbps - if (nfc_device_set_property_bool (pnd, NP_FORCE_SPEED_106, true) < 0) - return false; + if ((res = nfc_device_set_property_bool (pnd, NP_FORCE_SPEED_106, true)) < 0) + return res; // Disallow invalid frame - if (nfc_device_set_property_bool (pnd, NP_ACCEPT_INVALID_FRAMES, false) < 0) - return false; + if ((res = nfc_device_set_property_bool (pnd, NP_ACCEPT_INVALID_FRAMES, false)) < 0) + return res; // Disallow multiple frames - if (nfc_device_set_property_bool (pnd, NP_ACCEPT_MULTIPLE_FRAMES, false) < 0) - return false; + if ((res = nfc_device_set_property_bool (pnd, NP_ACCEPT_MULTIPLE_FRAMES, false)) < 0) + return res; // Make sure we reset the CRC and parity to chip handling. - if (nfc_device_set_property_bool (pnd, NP_HANDLE_CRC, true) < 0) - return false; - if (nfc_device_set_property_bool (pnd, NP_HANDLE_PARITY, true) < 0) - return false; + if ((res = nfc_device_set_property_bool (pnd, NP_HANDLE_CRC, true)) < 0) + return res; + if ((res = nfc_device_set_property_bool (pnd, NP_HANDLE_PARITY, true)) < 0) + return res; // Activate "easy framing" feature by default - if (nfc_device_set_property_bool (pnd, NP_EASY_FRAMING, true) < 0) - return false; + if ((res = nfc_device_set_property_bool (pnd, NP_EASY_FRAMING, true)) < 0) + return res; // Deactivate the CRYPTO1 cipher, it may could cause problems when still active - if (nfc_device_set_property_bool (pnd, NP_ACTIVATE_CRYPTO1, false) < 0) - return false; + if ((res = nfc_device_set_property_bool (pnd, NP_ACTIVATE_CRYPTO1, false)) < 0) + return res; HAL (initiator_init, pnd); } @@ -346,13 +347,12 @@ nfc_initiator_select_passive_target (nfc_device *pnd, /** * @brief List passive or emulated tags - * @return Returns \c true if action was successfully performed; otherwise returns \c false. + * @return Returns the number of targets found on success, otherwise returns libnfc's error code (negative value) * * @param pnd \a nfc_device struct pointer that represent currently used device * @param nm desired modulation * @param[out] ant array of \a nfc_target that will be filled with targets info * @param szTargets size of \a ant (will be the max targets listed) - * @param[out] pszTargetFound pointer where target found counter will be stored * * The NFC device will try to find the available passive tags. Some NFC devices * are capable to emulate passive tags. The standards (ISO18092 and ECMA-340) @@ -361,21 +361,22 @@ nfc_initiator_select_passive_target (nfc_device *pnd, * with, therefore the initial modulation and speed (106, 212 or 424 kbps) * should be supplied. */ -bool +int nfc_initiator_list_passive_targets (nfc_device *pnd, const nfc_modulation nm, - nfc_target ant[], const size_t szTargets, size_t *pszTargetFound) + nfc_target ant[], const size_t szTargets) { nfc_target nt; size_t szTargetFound = 0; uint8_t *pbtInitData = NULL; size_t szInitDataLen = 0; + int res = 0; pnd->iLastError = 0; // Let the reader only try once to find a tag - if (nfc_device_set_property_bool (pnd, NP_INFINITE_SELECT, false) < 0) { - return false; + if ((res = nfc_device_set_property_bool (pnd, NP_INFINITE_SELECT, false)) < 0) { + return res; } prepare_initiator_data (nm, &pbtInitData, &szInitDataLen); @@ -404,9 +405,7 @@ nfc_initiator_list_passive_targets (nfc_device *pnd, break; } } - *pszTargetFound = szTargetFound; - - return true; + return szTargetFound; } /** diff --git a/test/test_access_storm.c b/test/test_access_storm.c index 0fdc253..2646e21 100644 --- a/test/test_access_storm.c +++ b/test/test_access_storm.c @@ -15,8 +15,8 @@ test_access_storm (void) { int n = NTESTS; nfc_connstring connstrings[MAX_DEVICE_COUNT]; - size_t device_count, ref_device_count, target_count; - bool res; + size_t device_count, ref_device_count; + int res = 0; nfc_list_devices (connstrings, MAX_DEVICE_COUNT, &ref_device_count); if (!ref_device_count) @@ -36,14 +36,14 @@ test_access_storm (void) cut_assert_not_null (device, cut_message ("nfc_connect")); res = nfc_initiator_init(device); - cut_assert_true (res, cut_message ("nfc_initiator_init")); + cut_assert_equal_int (0, res, cut_message ("nfc_initiator_init")); const nfc_modulation nm = { .nmt = NMT_ISO14443A, .nbr = NBR_106, }; - res = nfc_initiator_list_passive_targets(device, nm, ant, MAX_TARGET_COUNT, &target_count); - cut_assert_true (res, cut_message ("nfc_initiator_list_passive_targets")); + res = nfc_initiator_list_passive_targets(device, nm, ant, MAX_TARGET_COUNT); + cut_assert_operator_int (res, >=, 0, cut_message ("nfc_initiator_list_passive_targets")); nfc_disconnect (device); } diff --git a/test/test_dep_active.c b/test/test_dep_active.c index 9528de3..0b1cc20 100644 --- a/test/test_dep_active.c +++ b/test/test_dep_active.c @@ -117,7 +117,7 @@ initiator_thread (void *arg) sleep (1); printf ("=========== INITIATOR %s =========\n", nfc_device_name (device)); bool res = nfc_initiator_init (device); - cut_assert_true (res, cut_message ("Can't initialize NFC device as initiator: %s", nfc_strerror (device))); + cut_assert_equal_int (0, res, cut_message ("Can't initialize NFC device as initiator: %s", nfc_strerror (device))); if (!res) { thread_res = -1; return (void*) thread_res; } nfc_target nt; diff --git a/test/test_dep_passive.c b/test/test_dep_passive.c index 71ce58f..e17c1f6 100644 --- a/test/test_dep_passive.c +++ b/test/test_dep_passive.c @@ -150,7 +150,7 @@ initiator_thread (void *arg) printf ("=========== INITIATOR %s =========\n", nfc_device_name (device)); bool res = nfc_initiator_init (device); - cut_assert_true (res, cut_message ("Can't initialize NFC device as initiator: %s", nfc_strerror (device))); + cut_assert_equal_int (0, res, cut_message ("Can't initialize NFC device as initiator: %s", nfc_strerror (device))); if (!res) { thread_res = -1; return (void*) thread_res; } nfc_target nt; diff --git a/utils/nfc-list.c b/utils/nfc-list.c index 9c7f62e..8172a4e 100644 --- a/utils/nfc-list.c +++ b/utils/nfc-list.c @@ -64,9 +64,9 @@ main (int argc, const char *argv[]) { (void) argc; const char *acLibnfcVersion; - size_t szTargetFound; size_t i; bool verbose = false; + int res = 0; // Display libnfc version acLibnfcVersion = nfc_version (); @@ -124,12 +124,12 @@ main (int argc, const char *argv[]) nm.nmt = NMT_ISO14443A; nm.nbr = NBR_106; // List ISO14443A targets - if (nfc_initiator_list_passive_targets (pnd, nm, ant, MAX_TARGET_COUNT, &szTargetFound)) { - size_t n; - if (verbose || (szTargetFound > 0)) { - printf ("%d ISO14443A passive target(s) found%s\n", (int) szTargetFound, (szTargetFound == 0) ? ".\n" : ":"); + if ((res = nfc_initiator_list_passive_targets (pnd, nm, ant, MAX_TARGET_COUNT)) >= 0) { + int n; + if (verbose) { + printf ("%d ISO14443A passive target(s) found%s\n", res, (res == 0) ? ".\n" : ":"); } - for (n = 0; n < szTargetFound; n++) { + for (n = 0; n < res; n++) { print_nfc_iso14443a_info (ant[n].nti.nai, verbose); printf ("\n"); } @@ -138,26 +138,24 @@ main (int argc, const char *argv[]) nm.nmt = NMT_FELICA; nm.nbr = NBR_212; // List Felica tags - if (nfc_initiator_list_passive_targets (pnd, nm, ant, MAX_TARGET_COUNT, &szTargetFound)) { - size_t n; - if (verbose || (szTargetFound > 0)) { - printf ("%d Felica (212 kbps) passive target(s) found%s\n", (int) szTargetFound, - (szTargetFound == 0) ? ".\n" : ":"); + if (nfc_initiator_list_passive_targets (pnd, nm, ant, MAX_TARGET_COUNT) >= 0) { + int n; + if (verbose) { + printf ("%d Felica (212 kbps) passive target(s) found%s\n", res, (res == 0) ? ".\n" : ":"); } - for (n = 0; n < szTargetFound; n++) { + for (n = 0; n < res; n++) { print_nfc_felica_info (ant[n].nti.nfi, verbose); printf ("\n"); } } nm.nbr = NBR_424; - if (nfc_initiator_list_passive_targets (pnd, nm, ant, MAX_TARGET_COUNT, &szTargetFound)) { - size_t n; - if (verbose || (szTargetFound > 0)) { - printf ("%d Felica (424 kbps) passive target(s) found%s\n", (int) szTargetFound, - (szTargetFound == 0) ? ".\n" : ":"); + if (nfc_initiator_list_passive_targets (pnd, nm, ant, MAX_TARGET_COUNT) >= 0) { + int n; + if (verbose) { + printf ("%d Felica (424 kbps) passive target(s) found%s\n", res, (res == 0) ? ".\n" : ":"); } - for (n = 0; n < szTargetFound; n++) { + for (n = 0; n < res; n++) { print_nfc_felica_info (ant[n].nti.nfi, verbose); printf ("\n"); } @@ -166,12 +164,12 @@ main (int argc, const char *argv[]) nm.nmt = NMT_ISO14443B; nm.nbr = NBR_106; // List ISO14443B targets - if (nfc_initiator_list_passive_targets (pnd, nm, ant, MAX_TARGET_COUNT, &szTargetFound)) { - size_t n; - if (verbose || (szTargetFound > 0)) { - printf ("%d ISO14443B passive target(s) found%s\n", (int) szTargetFound, (szTargetFound == 0) ? ".\n" : ":"); + if (nfc_initiator_list_passive_targets (pnd, nm, ant, MAX_TARGET_COUNT) >= 0) { + int n; + if (verbose) { + printf ("%d ISO14443B passive target(s) found%s\n", res, (res == 0) ? ".\n" : ":"); } - for (n = 0; n < szTargetFound; n++) { + for (n = 0; n < res; n++) { print_nfc_iso14443b_info (ant[n].nti.nbi, verbose); printf ("\n"); } @@ -180,12 +178,12 @@ main (int argc, const char *argv[]) nm.nmt = NMT_ISO14443BI; nm.nbr = NBR_106; // List ISO14443B' targets - if (nfc_initiator_list_passive_targets (pnd, nm, ant, MAX_TARGET_COUNT, &szTargetFound)) { - size_t n; - if (verbose || (szTargetFound > 0)) { - printf ("%d ISO14443B' passive target(s) found%s\n", (int) szTargetFound, (szTargetFound == 0) ? ".\n" : ":"); + if (nfc_initiator_list_passive_targets (pnd, nm, ant, MAX_TARGET_COUNT) >= 0) { + int n; + if (verbose) { + printf ("%d ISO14443B' passive target(s) found%s\n", res, (res == 0) ? ".\n" : ":"); } - for (n = 0; n < szTargetFound; n++) { + for (n = 0; n < res; n++) { print_nfc_iso14443bi_info (ant[n].nti.nii, verbose); printf ("\n"); } @@ -194,12 +192,12 @@ main (int argc, const char *argv[]) nm.nmt = NMT_ISO14443B2SR; nm.nbr = NBR_106; // List ISO14443B-2 ST SRx family targets - if (nfc_initiator_list_passive_targets (pnd, nm, ant, MAX_TARGET_COUNT, &szTargetFound)) { - size_t n; - if (verbose || (szTargetFound > 0)) { - printf ("%d ISO14443B-2 ST SRx passive target(s) found%s\n", (int) szTargetFound, (szTargetFound == 0) ? ".\n" : ":"); + if (nfc_initiator_list_passive_targets (pnd, nm, ant, MAX_TARGET_COUNT) >= 0) { + int n; + if (verbose) { + printf ("%d ISO14443B-2 ST SRx passive target(s) found%s\n", res, (res == 0) ? ".\n" : ":"); } - for (n = 0; n < szTargetFound; n++) { + for (n = 0; n < res; n++) { print_nfc_iso14443b2sr_info (ant[n].nti.nsi, verbose); printf ("\n"); } @@ -208,12 +206,12 @@ main (int argc, const char *argv[]) nm.nmt = NMT_ISO14443B2CT; nm.nbr = NBR_106; // List ISO14443B-2 ASK CTx family targets - if (nfc_initiator_list_passive_targets (pnd, nm, ant, MAX_TARGET_COUNT, &szTargetFound)) { - size_t n; - if (verbose || (szTargetFound > 0)) { - printf ("%d ISO14443B-2 ASK CTx passive target(s) found%s\n", (int) szTargetFound, (szTargetFound == 0) ? ".\n" : ":"); + if (nfc_initiator_list_passive_targets (pnd, nm, ant, MAX_TARGET_COUNT) >= 0) { + int n; + if (verbose) { + printf ("%d ISO14443B-2 ASK CTx passive target(s) found%s\n", res, (res == 0) ? ".\n" : ":"); } - for (n = 0; n < szTargetFound; n++) { + for (n = 0; n < res; n++) { print_nfc_iso14443b2ct_info (ant[n].nti.nci, verbose); printf ("\n"); } @@ -222,12 +220,12 @@ main (int argc, const char *argv[]) nm.nmt = NMT_JEWEL; nm.nbr = NBR_106; // List Jewel targets - if (nfc_initiator_list_passive_targets(pnd, nm, ant, MAX_TARGET_COUNT, &szTargetFound )) { - size_t n; - if (verbose || (szTargetFound > 0)) { - printf("%d Jewel passive target(s) found%s\n", (int)szTargetFound, (szTargetFound==0)?".\n":":"); + if (nfc_initiator_list_passive_targets(pnd, nm, ant, MAX_TARGET_COUNT) >= 0) { + int n; + if (verbose) { + printf("%d Jewel passive target(s) found%s\n", res, (res == 0)?".\n":":"); } - for(n=0; nacName); - if (!nfc_initiator_init (pndInitiator)) { + if (nfc_initiator_init (pndInitiator) < 0) { printf ("Error: fail initializing initiator\n"); nfc_disconnect (pndInitiator); exit (EXIT_FAILURE); From 9bdc20353c8202f45455dc89819bd4ffb3bfe399 Mon Sep 17 00:00:00 2001 From: Romuald Conty Date: Thu, 15 Dec 2011 16:02:38 +0000 Subject: [PATCH 027/113] nfc_device struct is not exposed as public API anymore (partial commit) - split libnfc's errors and chip's errors - fix nfc_property enum --- include/nfc/nfc-types.h | 60 +++------- include/nfc/nfc.h | 22 ++-- libnfc/chips/pn53x.c | 224 +++++++++++++++++++----------------- libnfc/chips/pn53x.h | 92 +++++++-------- libnfc/drivers/acr122.c | 18 ++- libnfc/drivers/arygon.c | 46 ++++---- libnfc/drivers/pn532_uart.c | 48 ++++---- libnfc/drivers/pn53x_usb.c | 43 +++---- libnfc/nfc-device.c | 2 +- libnfc/nfc-internal.h | 86 +++++++++----- libnfc/nfc.c | 2 +- 11 files changed, 329 insertions(+), 314 deletions(-) diff --git a/include/nfc/nfc-types.h b/include/nfc/nfc-types.h index 5975d5b..81a1270 100644 --- a/include/nfc/nfc-types.h +++ b/include/nfc/nfc-types.h @@ -32,42 +32,10 @@ # include # include -# define DEVICE_NAME_LENGTH 256 -# define DEVICE_PORT_LENGTH 64 - /** - * @struct nfc_device - * @brief NFC device information + * NFC device */ -typedef struct { -/** Driver's functions for handling device specific wrapping */ - const struct nfc_driver_t *driver; - void *driver_data; - void *chip_data; - -/** Device name string, including device wrapper firmware */ - char acName[DEVICE_NAME_LENGTH]; -/** Is the crc automaticly added, checked and removed from the frames */ - bool bCrc; -/** Does the chip handle parity bits, all parities are handled as data */ - bool bPar; -/** Should the chip handle frames encapsulation and chaining */ - bool bEasyFraming; -/** Should the chip switch automatically activate ISO14443-4 when - selecting tags supporting it? */ - bool bAutoIso14443_4; -/** Supported modulation encoded in a byte */ - uint8_t btSupportByte; -/** Last error reported by the PCD / encountered by the PCD driver - * MSB LSB - * | 00 | 00 | - * || || - * || ++----- Chip-level error (as reported by the PCD) - * |+---------- Driver-level specific error - * +----------- Driver-level general error (common to all drivers) - */ - int iLastError; -} nfc_device; +typedef struct nfc_device nfc_device; /** * Connection string @@ -103,7 +71,7 @@ typedef enum { * incorrect CRC bytes this option should be disabled. Example frames where * this is useful are the ATQA and UID+BCC that are transmitted without CRC * bytes during the anti-collision phase of the ISO14443-A protocol. */ - NP_HANDLE_CRC = 0x00, + NP_HANDLE_CRC, /** Parity bits in the network layer of ISO14443-A are by default generated and * validated in the PN53X chip. This is a very convenient feature. On certain * times though it is useful to get full control of the transmitted data. The @@ -111,31 +79,31 @@ typedef enum { * parity bits. For interoperability it is required to be completely * compatible, including the arbitrary parity bits. When this option is * disabled, the functions to communicating bits should be used. */ - NP_HANDLE_PARITY = 0x01, + NP_HANDLE_PARITY, /** This option can be used to enable or disable the electronic field of the * NFC device. */ - NP_ACTIVATE_FIELD = 0x10, + NP_ACTIVATE_FIELD, /** The internal CRYPTO1 co-processor can be used to transmit messages * encrypted. This option is automatically activated after a successful MIFARE * Classic authentication. */ - NP_ACTIVATE_CRYPTO1 = 0x11, + NP_ACTIVATE_CRYPTO1, /** The default configuration defines that the PN53X chip will try indefinitely * to invite a tag in the field to respond. This could be desired when it is * certain a tag will enter the field. On the other hand, when this is * uncertain, it will block the application. This option could best be compared * to the (NON)BLOCKING option used by (socket)network programming. */ - NP_INFINITE_SELECT = 0x20, + NP_INFINITE_SELECT, /** If this option is enabled, frames that carry less than 4 bits are allowed. * According to the standards these frames should normally be handles as * invalid frames. */ - NP_ACCEPT_INVALID_FRAMES = 0x30, + NP_ACCEPT_INVALID_FRAMES, /** If the NFC device should only listen to frames, it could be useful to let * it gather multiple frames in a sequence. They will be stored in the internal * FIFO of the PN53X chip. This could be retrieved by using the receive data * functions. Note that if the chip runs out of bytes (FIFO = 64 bytes long), * it will overwrite the first received frames, so quick retrieving of the * received data is desirable. */ - NP_ACCEPT_MULTIPLE_FRAMES = 0x31, + NP_ACCEPT_MULTIPLE_FRAMES, /** This option can be used to enable or disable the auto-switching mode to * ISO14443-4 is device is compliant. * In initiator mode, it means that NFC chip will send RATS automatically when @@ -143,15 +111,15 @@ typedef enum { * requested. * In target mode, with a NFC chip compiliant (ie. PN532), the chip will * emulate a 14443-4 PICC using hardware capability */ - NP_AUTO_ISO14443_4 = 0x40, + NP_AUTO_ISO14443_4, /** Use automatic frames encapsulation and chaining. */ - NP_EASY_FRAMING = 0x41, + NP_EASY_FRAMING, /** Force the chip to switch in ISO14443-A */ - NP_FORCE_ISO14443_A = 0x42, + NP_FORCE_ISO14443_A, /** Force the chip to switch in ISO14443-B */ - NP_FORCE_ISO14443_B = 0x43, + NP_FORCE_ISO14443_B, /** Force the chip to run at 106 kbps */ - NP_FORCE_SPEED_106 = 0x50, + NP_FORCE_SPEED_106, } nfc_property; // Compiler directive, set struct alignment to 1 uint8_t for compatibility diff --git a/include/nfc/nfc.h b/include/nfc/nfc.h index b55c1b8..64833de 100644 --- a/include/nfc/nfc.h +++ b/include/nfc/nfc.h @@ -91,11 +91,11 @@ extern "C" { /* Error reporting */ NFC_EXPORT const char *nfc_strerror (const nfc_device *pnd); - NFC_EXPORT int nfc_strerror_r (const nfc_device *pnd, char *pcStrErrBuf, size_t szBufLen); - NFC_EXPORT void nfc_perror (const nfc_device *pnd, const char *pcString); + NFC_EXPORT int nfc_strerror_r (const nfc_device *pnd, char *buf, size_t buflen); + NFC_EXPORT void nfc_perror (const nfc_device *pnd, const char *s); /* Special data accessors */ - NFC_EXPORT const char *nfc_device_name (nfc_device *pnd); + NFC_EXPORT const char *nfc_device_get_name (nfc_device *pnd); /* Properties accessors */ NFC_EXPORT int nfc_device_set_property_int (nfc_device *pnd, const nfc_property property, const int value); @@ -108,11 +108,17 @@ extern "C" { NFC_EXPORT const char *nfc_version (void); /* Error codes */ -#define NFC_SUCCESS 0 // No error -#define NFC_EIO -1 // Input / output error, device will not be usable anymore -#define NFC_ENOTSUP -2 // Operation not supported -#define NFC_EINVARG -3 // Invalid argument(s) -#define NFC_DEVICE_ERROR -4 //Device error +#define NFC_SUCCESS 0 // No error +#define NFC_EIO -1 // Input / output error, device will not be usable anymore +#define NFC_EINVARG -2 // Invalid argument(s) +#define NFC_ENOTSUCHDEV -3 // No such device +#define NFC_ETIMEOUT -4 // Operation timed out +#define NFC_EOVFLOW -5 // Buffer overflow +#define NFC_EOPABORTED -6 // Operation aborted (by user) +#define NFC_ECHIP -7 // Device's internal chip error +#define NFC_EDEVNOTSUPP -8 // Operation not supported by device +#define NFC_ENOTIMPL -9 // Not (yet) implemented + /* PN53x specific errors */ // TODO: Be not PN53x-specific here diff --git a/libnfc/chips/pn53x.c b/libnfc/chips/pn53x.c index 0a9c2ca..b123d08 100644 --- a/libnfc/chips/pn53x.c +++ b/libnfc/chips/pn53x.c @@ -35,14 +35,14 @@ #include #include -#include #include +#include "nfc/nfc.h" +#include "nfc-internal.h" #include "pn53x.h" #include "pn53x-internal.h" #include "mirror-subr.h" -#include "nfc-internal.h" #define LOG_CATEGORY "libnfc.chip.pn53x" @@ -51,15 +51,15 @@ const uint8_t pn53x_nack_frame[] = { 0x00, 0x00, 0xff, 0xff, 0x00, 0x00 }; static const uint8_t pn53x_error_frame[] = { 0x00, 0x00, 0xff, 0x01, 0xff, 0x7f, 0x81, 0x00 }; /* prototypes */ -bool pn53x_reset_settings (nfc_device *pnd); -bool pn53x_writeback_register (nfc_device *pnd); +bool pn53x_reset_settings (struct nfc_device *pnd); +bool pn53x_writeback_register (struct nfc_device *pnd); nfc_modulation pn53x_ptt_to_nm (const pn53x_target_type ptt); pn53x_modulation pn53x_nm_to_pm (const nfc_modulation nm); pn53x_target_type pn53x_nm_to_ptt (const nfc_modulation nm); bool -pn53x_init(nfc_device *pnd) +pn53x_init(struct nfc_device *pnd) { // GetFirmwareVersion command is used to set PN53x chips type (PN531, PN532 or PN533) char abtFirmwareText[22]; @@ -78,7 +78,9 @@ pn53x_init(nfc_device *pnd) return false; } - pn53x_reset_settings(pnd); + if (!pn53x_reset_settings(pnd)) { + return false; + } // Add the firmware revision to the device name char *pcName; @@ -89,7 +91,7 @@ pn53x_init(nfc_device *pnd) } bool -pn53x_reset_settings(nfc_device *pnd) +pn53x_reset_settings(struct nfc_device *pnd) { // Reset the ending transmission bits register, it is unknown what the last tranmission used there CHIP_DATA (pnd)->ui8TxBits = 0; @@ -100,7 +102,7 @@ pn53x_reset_settings(nfc_device *pnd) } bool -pn53x_transceive (nfc_device *pnd, const uint8_t *pbtTx, const size_t szTx, uint8_t *pbtRx, size_t *pszRx, int timeout) +pn53x_transceive (struct nfc_device *pnd, const uint8_t *pbtTx, const size_t szTx, uint8_t *pbtRx, size_t *pszRx, int timeout) { if (CHIP_DATA (pnd)->wb_trigged) { if (!pn53x_writeback_register (pnd)) { @@ -140,7 +142,7 @@ pn53x_transceive (nfc_device *pnd, const uint8_t *pbtTx, const size_t szTx, uint return false; } - if (pnd->iLastError) + if (CHIP_DATA(pnd)->last_status_byte) return false; if ((CHIP_DATA(pnd)->type == PN532) && (TgInitAsTarget == pbtTx[0])) { // PN532 automatically wakeup on external RF field @@ -165,36 +167,36 @@ pn53x_transceive (nfc_device *pnd, const uint8_t *pbtTx, const size_t szTx, uint case TgSetMetaData: if (pbtRx[0] & 0x80) { abort(); } // NAD detected if (pbtRx[0] & 0x40) { abort(); } // MI detected - pnd->iLastError = pbtRx[0] & 0x3f; + CHIP_DATA(pnd)->last_status_byte = pbtRx[0] & 0x3f; break; case InDeselect: case InRelease: if (CHIP_DATA(pnd)->type == RCS360) { // Error code is in pbtRx[1] but we ignore error code anyway // because other PN53x chips always return 0 on those commands - pnd->iLastError = 0; + CHIP_DATA(pnd)->last_status_byte = 0; break; } - pnd->iLastError = pbtRx[0] & 0x3f; + CHIP_DATA(pnd)->last_status_byte = pbtRx[0] & 0x3f; break; case ReadRegister: case WriteRegister: if (CHIP_DATA(pnd)->type == PN533) { // PN533 prepends its answer by the status byte - pnd->iLastError = pbtRx[0] & 0x3f; + CHIP_DATA(pnd)->last_status_byte = pbtRx[0] & 0x3f; } else { - pnd->iLastError = 0; + CHIP_DATA(pnd)->last_status_byte = 0; } break; default: - pnd->iLastError = 0; + CHIP_DATA(pnd)->last_status_byte = 0; } log_put (LOG_CATEGORY, NFC_PRIORITY_TRACE, "Last command status: %s", pn53x_strerror(pnd)); - return (0 == pnd->iLastError); + return (0 == CHIP_DATA(pnd)->last_status_byte); } bool -pn53x_set_parameters (nfc_device *pnd, const uint8_t ui8Parameter, const bool bEnable) +pn53x_set_parameters (struct nfc_device *pnd, const uint8_t ui8Parameter, const bool bEnable) { uint8_t ui8Value = (bEnable) ? (CHIP_DATA (pnd)->ui8Parameters | ui8Parameter) : (CHIP_DATA (pnd)->ui8Parameters & ~(ui8Parameter)); if (ui8Value != CHIP_DATA (pnd)->ui8Parameters) { @@ -204,7 +206,7 @@ pn53x_set_parameters (nfc_device *pnd, const uint8_t ui8Parameter, const bool bE } bool -pn53x_set_tx_bits (nfc_device *pnd, const uint8_t ui8Bits) +pn53x_set_tx_bits (struct nfc_device *pnd, const uint8_t ui8Bits) { // Test if we need to update the transmission bits register setting if (CHIP_DATA (pnd)->ui8TxBits != ui8Bits) { @@ -466,7 +468,7 @@ pn53x_decode_target_data (const uint8_t *pbtRawData, size_t szRawData, pn53x_typ } bool -pn53x_ReadRegister (nfc_device *pnd, uint16_t ui16RegisterAddress, uint8_t *ui8Value) +pn53x_ReadRegister (struct nfc_device *pnd, uint16_t ui16RegisterAddress, uint8_t *ui8Value) { uint8_t abtCmd[] = { ReadRegister, ui16RegisterAddress >> 8, ui16RegisterAddress & 0xff }; uint8_t abtRegValue[2]; @@ -485,13 +487,13 @@ pn53x_ReadRegister (nfc_device *pnd, uint16_t ui16RegisterAddress, uint8_t *ui8V return true; } -bool pn53x_read_register (nfc_device *pnd, uint16_t ui16RegisterAddress, uint8_t *ui8Value) +bool pn53x_read_register (struct nfc_device *pnd, uint16_t ui16RegisterAddress, uint8_t *ui8Value) { return pn53x_ReadRegister (pnd, ui16RegisterAddress, ui8Value); } bool -pn53x_WriteRegister (nfc_device *pnd, const uint16_t ui16RegisterAddress, const uint8_t ui8Value) +pn53x_WriteRegister (struct nfc_device *pnd, const uint16_t ui16RegisterAddress, const uint8_t ui8Value) { uint8_t abtCmd[] = { WriteRegister, ui16RegisterAddress >> 8, ui16RegisterAddress & 0xff, ui8Value }; PNREG_TRACE (ui16RegisterAddress); @@ -499,7 +501,7 @@ pn53x_WriteRegister (nfc_device *pnd, const uint16_t ui16RegisterAddress, const } bool -pn53x_write_register (nfc_device *pnd, const uint16_t ui16RegisterAddress, const uint8_t ui8SymbolMask, const uint8_t ui8Value) +pn53x_write_register (struct nfc_device *pnd, const uint16_t ui16RegisterAddress, const uint8_t ui8SymbolMask, const uint8_t ui8Value) { if ((ui16RegisterAddress < PN53X_CACHE_REGISTER_MIN_ADDRESS) || (ui16RegisterAddress > PN53X_CACHE_REGISTER_MAX_ADDRESS)) { // Direct write @@ -525,7 +527,7 @@ pn53x_write_register (nfc_device *pnd, const uint16_t ui16RegisterAddress, const } bool -pn53x_writeback_register (nfc_device *pnd) +pn53x_writeback_register (struct nfc_device *pnd) { // TODO Check at each step (ReadRegister, WriteRegister) if we didn't exceed max supported frame length BUFFER_INIT (abtReadRegisterCmd, PN53x_EXTENDED_FRAME__DATA_MAX_LEN); @@ -593,7 +595,7 @@ pn53x_writeback_register (nfc_device *pnd) } bool -pn53x_get_firmware_version (nfc_device *pnd, char abtFirmwareText[22]) +pn53x_get_firmware_version (struct nfc_device *pnd, char abtFirmwareText[22]) { const uint8_t abtCmd[] = { GetFirmwareVersion }; uint8_t abtFw[4]; @@ -659,7 +661,7 @@ pn53x_int_to_timeout (const int ms) } int -pn53x_set_property_int (nfc_device *pnd, const nfc_property property, const int value) +pn53x_set_property_int (struct nfc_device *pnd, const nfc_property property, const int value) { switch (property) { case NP_TIMEOUT_COMMAND: @@ -674,13 +676,13 @@ pn53x_set_property_int (nfc_device *pnd, const nfc_property property, const int return (pn53x_RFConfiguration__Various_timings (pnd, pn53x_int_to_timeout(CHIP_DATA (pnd)->timeout_atr), pn53x_int_to_timeout(CHIP_DATA (pnd)->timeout_communication))) ? NFC_SUCCESS : NFC_EIO; break; default: - return NFC_ENOTSUP; + return NFC_EINVARG; } return NFC_SUCCESS; } int -pn53x_set_property_bool (nfc_device *pnd, const nfc_property property, const bool bEnable) +pn53x_set_property_bool (struct nfc_device *pnd, const nfc_property property, const bool bEnable) { uint8_t btValue; switch (property) { @@ -693,9 +695,9 @@ pn53x_set_property_bool (nfc_device *pnd, const nfc_property property, const boo // TX and RX are both represented by the symbol 0x80 btValue = (bEnable) ? 0x80 : 0x00; if (!pn53x_write_register (pnd, PN53X_REG_CIU_TxMode, SYMBOL_TX_CRC_ENABLE, btValue)) - return NFC_DEVICE_ERROR; + return NFC_ECHIP; if (!pn53x_write_register (pnd, PN53X_REG_CIU_RxMode, SYMBOL_RX_CRC_ENABLE, btValue)) - return NFC_DEVICE_ERROR; + return NFC_ECHIP; pnd->bCrc = bEnable; break; @@ -706,7 +708,7 @@ pn53x_set_property_bool (nfc_device *pnd, const nfc_property property, const boo return NFC_SUCCESS; btValue = (bEnable) ? 0x00 : SYMBOL_PARITY_DISABLE; if (!pn53x_write_register (pnd, PN53X_REG_CIU_ManualRCV, SYMBOL_PARITY_DISABLE, btValue)) - return NFC_DEVICE_ERROR; + return NFC_ECHIP; pnd->bPar = bEnable; break; @@ -723,7 +725,7 @@ pn53x_set_property_bool (nfc_device *pnd, const nfc_property property, const boo case NP_ACTIVATE_CRYPTO1: btValue = (bEnable) ? SYMBOL_MF_CRYPTO1_ON : 0x00; if (!pn53x_write_register (pnd, PN53X_REG_CIU_Status2, SYMBOL_MF_CRYPTO1_ON, btValue)) - return NFC_DEVICE_ERROR; + return NFC_ECHIP; break; case NP_INFINITE_SELECT: @@ -742,13 +744,13 @@ pn53x_set_property_bool (nfc_device *pnd, const nfc_property property, const boo case NP_ACCEPT_INVALID_FRAMES: btValue = (bEnable) ? SYMBOL_RX_NO_ERROR : 0x00; if (!pn53x_write_register (pnd, PN53X_REG_CIU_RxMode, SYMBOL_RX_NO_ERROR, btValue)) - return NFC_DEVICE_ERROR; + return NFC_ECHIP; break; case NP_ACCEPT_MULTIPLE_FRAMES: btValue = (bEnable) ? SYMBOL_RX_MULTIPLE : 0x00; if (!pn53x_write_register (pnd, PN53X_REG_CIU_RxMode, SYMBOL_RX_MULTIPLE, btValue)) - return NFC_DEVICE_ERROR; + return NFC_ECHIP; return NFC_SUCCESS; break; @@ -767,14 +769,14 @@ pn53x_set_property_bool (nfc_device *pnd, const nfc_property property, const boo } // Force pn53x to be in ISO14443-A mode if (!pn53x_write_register (pnd, PN53X_REG_CIU_TxMode, SYMBOL_TX_FRAMING, 0x00)) { - return NFC_DEVICE_ERROR; + return NFC_ECHIP; } if (!pn53x_write_register (pnd, PN53X_REG_CIU_RxMode, SYMBOL_RX_FRAMING, 0x00)) { - return NFC_DEVICE_ERROR; + return NFC_ECHIP; } // Set the PN53X to force 100% ASK Modified miller decoding (default for 14443A cards) if (!pn53x_write_register (pnd, PN53X_REG_CIU_TxAuto, SYMBOL_FORCE_100_ASK, 0x40)) - return NFC_DEVICE_ERROR; + return NFC_ECHIP; return NFC_SUCCESS; break; @@ -786,10 +788,10 @@ pn53x_set_property_bool (nfc_device *pnd, const nfc_property property, const boo } // Force pn53x to be in ISO14443-B mode if (!pn53x_write_register (pnd, PN53X_REG_CIU_TxMode, SYMBOL_TX_FRAMING, 0x03)) { - return NFC_DEVICE_ERROR; + return NFC_ECHIP; } if (!pn53x_write_register (pnd, PN53X_REG_CIU_RxMode, SYMBOL_RX_FRAMING, 0x03)) { - return NFC_DEVICE_ERROR; + return NFC_ECHIP; } return NFC_SUCCESS; @@ -802,22 +804,27 @@ pn53x_set_property_bool (nfc_device *pnd, const nfc_property property, const boo } // Force pn53x to be at 106 kbps if (!pn53x_write_register (pnd, PN53X_REG_CIU_TxMode, SYMBOL_TX_SPEED, 0x00)) { - return NFC_DEVICE_ERROR; + return NFC_ECHIP; } if (!pn53x_write_register (pnd, PN53X_REG_CIU_RxMode, SYMBOL_RX_SPEED, 0x00)) { - return NFC_DEVICE_ERROR; + return NFC_ECHIP; } return NFC_SUCCESS; break; + // Not boolean property + case NP_TIMEOUT_COMMAND: + case NP_TIMEOUT_ATR: + case NP_TIMEOUT_COM: + return NFC_EINVARG; + break; } - // When we reach this, the configuration is completed and successful - return NFC_SUCCESS; + return NFC_EINVARG; } bool -pn53x_idle (nfc_device *pnd) +pn53x_idle (struct nfc_device *pnd) { switch (CHIP_DATA (pnd)->operating_mode) { case TARGET: @@ -865,7 +872,7 @@ pn53x_idle (nfc_device *pnd) } bool -pn53x_check_communication (nfc_device *pnd) +pn53x_check_communication (struct nfc_device *pnd) { const uint8_t abtCmd[] = { Diagnose, 0x00, 'l', 'i', 'b', 'n', 'f', 'c' }; const uint8_t abtExpectedRx[] = { 0x00, 'l', 'i', 'b', 'n', 'f', 'c' }; @@ -879,7 +886,7 @@ pn53x_check_communication (nfc_device *pnd) } int -pn53x_initiator_init (nfc_device *pnd) +pn53x_initiator_init (struct nfc_device *pnd) { pn53x_reset_settings(pnd); @@ -892,7 +899,7 @@ pn53x_initiator_init (nfc_device *pnd) } bool -pn53x_initiator_select_passive_target_ext (nfc_device *pnd, +pn53x_initiator_select_passive_target_ext (struct nfc_device *pnd, const nfc_modulation nm, const uint8_t *pbtInitData, const size_t szInitData, nfc_target *pnt, @@ -904,7 +911,7 @@ pn53x_initiator_select_passive_target_ext (nfc_device *pnd, if (nm.nmt == NMT_ISO14443BI || nm.nmt == NMT_ISO14443B2SR || nm.nmt == NMT_ISO14443B2CT) { if (CHIP_DATA(pnd)->type == RCS360) { // TODO add support for RC-S360, at the moment it refuses to send raw frames without a first select - pnd->iLastError = ENOTIMPL; + pnd->last_error = ENOTIMPL; return false; } // No native support in InListPassiveTarget so we do discovery by hand @@ -979,7 +986,7 @@ pn53x_initiator_select_passive_target_ext (nfc_device *pnd, const pn53x_modulation pm = pn53x_nm_to_pm(nm); if (PM_UNDEFINED == pm) { - pnd->iLastError = EINVALARG; + pnd->last_error = EINVALARG; return false; } @@ -1002,7 +1009,7 @@ pn53x_initiator_select_passive_target_ext (nfc_device *pnd, } bool -pn53x_initiator_select_passive_target (nfc_device *pnd, +pn53x_initiator_select_passive_target (struct nfc_device *pnd, const nfc_modulation nm, const uint8_t *pbtInitData, const size_t szInitData, nfc_target *pnt) @@ -1011,7 +1018,7 @@ pn53x_initiator_select_passive_target (nfc_device *pnd, } bool -pn53x_initiator_poll_target (nfc_device *pnd, +pn53x_initiator_poll_target (struct nfc_device *pnd, const nfc_modulation *pnmModulations, const size_t szModulations, const uint8_t uiPollNr, const uint8_t uiPeriod, nfc_target *pnt) @@ -1022,7 +1029,7 @@ pn53x_initiator_poll_target (nfc_device *pnd, for (size_t n=0; niLastError = EINVALARG; + pnd->last_error = NFC_EINVARG; return false; } apttTargetTypes[szTargetTypes] = ptt; @@ -1062,7 +1069,7 @@ pn53x_initiator_poll_target (nfc_device *pnd, const int timeout_ms = uiPeriod * 150; if (!pn53x_initiator_select_passive_target_ext (pnd, pnmModulations[n], pbtInitiatorData, szInitiatorData, pnt, timeout_ms)) { - if (pnd->iLastError != ECOMTIMEOUT) + if (pnd->last_error != NFC_ETIMEOUT) return false; } else { return true; @@ -1075,7 +1082,7 @@ pn53x_initiator_poll_target (nfc_device *pnd, } bool -pn53x_initiator_select_dep_target(nfc_device *pnd, +pn53x_initiator_select_dep_target(struct nfc_device *pnd, const nfc_dep_mode ndm, const nfc_baud_rate nbr, const nfc_dep_info *pndiInitiator, nfc_target *pnt, @@ -1104,7 +1111,7 @@ pn53x_initiator_select_dep_target(nfc_device *pnd, } bool -pn53x_initiator_transceive_bits (nfc_device *pnd, const uint8_t *pbtTx, const size_t szTxBits, +pn53x_initiator_transceive_bits (struct nfc_device *pnd, const uint8_t *pbtTx, const size_t szTxBits, const uint8_t *pbtTxPar, uint8_t *pbtRx, size_t *pszRxBits, uint8_t *pbtRxPar) { size_t szFrameBits = 0; @@ -1168,7 +1175,7 @@ pn53x_initiator_transceive_bits (nfc_device *pnd, const uint8_t *pbtTx, const si } bool -pn53x_initiator_transceive_bytes (nfc_device *pnd, const uint8_t *pbtTx, const size_t szTx, uint8_t *pbtRx, +pn53x_initiator_transceive_bytes (struct nfc_device *pnd, const uint8_t *pbtTx, const size_t szTx, uint8_t *pbtRx, size_t *pszRx, int timeout) { size_t szExtraTxLen; @@ -1176,7 +1183,7 @@ pn53x_initiator_transceive_bytes (nfc_device *pnd, const uint8_t *pbtTx, const s // We can not just send bytes without parity if while the PN53X expects we handled them if (!pnd->bPar) { - pnd->iLastError = EINVALARG; + pnd->last_error = NFC_EINVARG; return false; } @@ -1214,7 +1221,7 @@ pn53x_initiator_transceive_bytes (nfc_device *pnd, const uint8_t *pbtTx, const s return true; } -void __pn53x_init_timer(nfc_device *pnd, const uint32_t max_cycles) +void __pn53x_init_timer(struct nfc_device *pnd, const uint32_t max_cycles) { // The prescaler will dictate what will be the precision and // the largest delay to measure before saturation. Some examples: @@ -1235,7 +1242,7 @@ void __pn53x_init_timer(nfc_device *pnd, const uint32_t max_cycles) pn53x_write_register (pnd, PN53X_REG_CIU_TReloadVal_lo, 0xFF, reloadval & 0xFF); } -uint32_t __pn53x_get_timer(nfc_device *pnd, const uint8_t last_cmd_byte) +uint32_t __pn53x_get_timer(struct nfc_device *pnd, const uint8_t last_cmd_byte) { uint8_t parity; uint8_t counter_hi, counter_lo; @@ -1296,7 +1303,7 @@ uint32_t __pn53x_get_timer(nfc_device *pnd, const uint8_t last_cmd_byte) } bool -pn53x_initiator_transceive_bits_timed (nfc_device *pnd, const uint8_t *pbtTx, const size_t szTxBits, +pn53x_initiator_transceive_bits_timed (struct nfc_device *pnd, const uint8_t *pbtTx, const size_t szTxBits, const uint8_t *pbtTxPar, uint8_t *pbtRx, size_t *pszRxBits, uint8_t *pbtRxPar, uint32_t *cycles) { // TODO Do something with these bytes... @@ -1307,17 +1314,17 @@ pn53x_initiator_transceive_bits_timed (nfc_device *pnd, const uint8_t *pbtTx, co // Sorry, no arbitrary parity bits support for now if (!pnd->bPar) { - pnd->iLastError = ENOTIMPL; + pnd->last_error = NFC_ENOTIMPL; return false; } // Sorry, no easy framing support if (pnd->bEasyFraming) { - pnd->iLastError = ENOTIMPL; + pnd->last_error = NFC_ENOTIMPL; return false; } // TODO CRC support but it probably doesn't make sense for (szTxBits % 8 != 0) ... if (pnd->bCrc) { - pnd->iLastError = ENOTIMPL; + pnd->last_error = NFC_ENOTIMPL; return false; } @@ -1398,7 +1405,7 @@ pn53x_initiator_transceive_bits_timed (nfc_device *pnd, const uint8_t *pbtTx, co } bool -pn53x_initiator_transceive_bytes_timed (nfc_device *pnd, const uint8_t *pbtTx, const size_t szTx, uint8_t *pbtRx, +pn53x_initiator_transceive_bytes_timed (struct nfc_device *pnd, const uint8_t *pbtTx, const size_t szTx, uint8_t *pbtRx, size_t *pszRx, uint32_t *cycles) { uint16_t i; @@ -1406,13 +1413,13 @@ pn53x_initiator_transceive_bytes_timed (nfc_device *pnd, const uint8_t *pbtTx, c // We can not just send bytes without parity while the PN53X expects we handled them if (!pnd->bPar) { - pnd->iLastError = EINVALARG; + CHIP_DATA(pnd)->last_status_byte = EINVALARG; return false; } // Sorry, no easy framing support // TODO to be changed once we'll provide easy framing support from libnfc itself... if (pnd->bEasyFraming) { - pnd->iLastError = ENOTIMPL; + CHIP_DATA(pnd)->last_status_byte = ENOTIMPL; return false; } @@ -1501,7 +1508,7 @@ pn53x_initiator_transceive_bytes_timed (nfc_device *pnd, const uint8_t *pbtTx, c } bool -pn53x_initiator_deselect_target (nfc_device *pnd) +pn53x_initiator_deselect_target (struct nfc_device *pnd) { return (pn53x_InDeselect (pnd, 0)); // 0 mean deselect all selected targets } @@ -1509,7 +1516,7 @@ pn53x_initiator_deselect_target (nfc_device *pnd) #define SAK_ISO14443_4_COMPLIANT 0x20 #define SAK_ISO18092_COMPLIANT 0x40 bool -pn53x_target_init (nfc_device *pnd, nfc_target *pnt, uint8_t *pbtRx, size_t *pszRx) +pn53x_target_init (struct nfc_device *pnd, nfc_target *pnt, uint8_t *pbtRx, size_t *pszRx) { pn53x_reset_settings(pnd); @@ -1521,7 +1528,7 @@ pn53x_target_init (nfc_device *pnd, nfc_target *pnt, uint8_t *pbtRx, size_t *psz case NMT_ISO14443A: ptm = PTM_PASSIVE_ONLY; if ((pnt->nti.nai.abtUid[0] != 0x08) || (pnt->nti.nai.szUidLen != 4)) { - pnd->iLastError = ETGUIDNOTSUP; + CHIP_DATA(pnd)->last_status_byte = ETGUIDNOTSUP; return false; } pn53x_set_parameters (pnd, PARAM_AUTO_ATR_RES, false); @@ -1550,7 +1557,7 @@ pn53x_target_init (nfc_device *pnd, nfc_target *pnt, uint8_t *pbtRx, size_t *psz case NMT_ISO14443B2SR: case NMT_ISO14443B2CT: case NMT_JEWEL: - pnd->iLastError = EDEVNOTSUP; + CHIP_DATA(pnd)->last_status_byte = EDEVNOTSUP; return false; break; } @@ -1652,7 +1659,7 @@ pn53x_target_init (nfc_device *pnd, nfc_target *pnt, uint8_t *pbtRx, size_t *psz case NMT_ISO14443B2SR: case NMT_ISO14443B2CT: case NMT_JEWEL: - pnd->iLastError = EDEVNOTSUP; + CHIP_DATA(pnd)->last_status_byte = EDEVNOTSUP; return false; break; } @@ -1730,7 +1737,7 @@ pn53x_target_init (nfc_device *pnd, nfc_target *pnt, uint8_t *pbtRx, size_t *psz } bool -pn53x_target_receive_bits (nfc_device *pnd, uint8_t *pbtRx, size_t *pszRxBits, uint8_t *pbtRxPar) +pn53x_target_receive_bits (struct nfc_device *pnd, uint8_t *pbtRx, size_t *pszRxBits, uint8_t *pbtRxPar) { uint8_t abtCmd[] = { TgGetInitiatorCommand }; @@ -1765,7 +1772,7 @@ pn53x_target_receive_bits (nfc_device *pnd, uint8_t *pbtRx, size_t *pszRxBits, u } bool -pn53x_target_receive_bytes (nfc_device *pnd, uint8_t *pbtRx, size_t *pszRx, int timeout) +pn53x_target_receive_bytes (struct nfc_device *pnd, uint8_t *pbtRx, size_t *pszRx, int timeout) { uint8_t abtCmd[1]; @@ -1785,7 +1792,7 @@ pn53x_target_receive_bytes (nfc_device *pnd, uint8_t *pbtRx, size_t *pszRx, int break; } else { // TODO Support EasyFraming for other cases by software - pnd->iLastError = ENOTIMPL; + CHIP_DATA(pnd)->last_status_byte = ENOTIMPL; return false; } } @@ -1814,7 +1821,7 @@ pn53x_target_receive_bytes (nfc_device *pnd, uint8_t *pbtRx, size_t *pszRx, int } bool -pn53x_target_send_bits (nfc_device *pnd, const uint8_t *pbtTx, const size_t szTxBits, const uint8_t *pbtTxPar) +pn53x_target_send_bits (struct nfc_device *pnd, const uint8_t *pbtTx, const size_t szTxBits, const uint8_t *pbtTxPar) { size_t szFrameBits = 0; size_t szFrameBytes = 0; @@ -1852,7 +1859,7 @@ pn53x_target_send_bits (nfc_device *pnd, const uint8_t *pbtTx, const size_t szTx } bool -pn53x_target_send_bytes (nfc_device *pnd, const uint8_t *pbtTx, const size_t szTx, int timeout) +pn53x_target_send_bytes (struct nfc_device *pnd, const uint8_t *pbtTx, const size_t szTx, int timeout) { uint8_t abtCmd[PN53x_EXTENDED_FRAME__DATA_MAX_LEN]; @@ -1876,7 +1883,7 @@ pn53x_target_send_bytes (nfc_device *pnd, const uint8_t *pbtTx, const size_t szT break; } else { // TODO Support EasyFraming for other cases by software - pnd->iLastError = ENOTIMPL; + CHIP_DATA(pnd)->last_status_byte = ENOTIMPL; return false; } } @@ -1951,13 +1958,13 @@ static struct sErrorMessage { }; const char * -pn53x_strerror (const nfc_device *pnd) +pn53x_strerror (const struct nfc_device *pnd) { const char *pcRes = "Unknown error"; size_t i; for (i = 0; i < (sizeof (sErrorMessages) / sizeof (struct sErrorMessage)); i++) { - if (sErrorMessages[i].iErrorCode == pnd->iLastError) { + if (sErrorMessages[i].iErrorCode == CHIP_DATA(pnd)->last_status_byte) { pcRes = sErrorMessages[i].pcErrorMsg; break; } @@ -1967,14 +1974,14 @@ pn53x_strerror (const nfc_device *pnd) } bool -pn53x_RFConfiguration__RF_field (nfc_device *pnd, bool bEnable) +pn53x_RFConfiguration__RF_field (struct nfc_device *pnd, bool bEnable) { uint8_t abtCmd[] = { RFConfiguration, RFCI_FIELD, (bEnable) ? 0x01 : 0x00 }; return pn53x_transceive (pnd, abtCmd, sizeof (abtCmd), NULL, NULL, -1); } bool -pn53x_RFConfiguration__Various_timings (nfc_device *pnd, const uint8_t fATR_RES_Timeout, const uint8_t fRetryTimeout) +pn53x_RFConfiguration__Various_timings (struct nfc_device *pnd, const uint8_t fATR_RES_Timeout, const uint8_t fRetryTimeout) { uint8_t abtCmd[] = { RFConfiguration, @@ -1987,7 +1994,7 @@ pn53x_RFConfiguration__Various_timings (nfc_device *pnd, const uint8_t fATR_RES_ } bool -pn53x_RFConfiguration__MaxRtyCOM (nfc_device *pnd, const uint8_t MaxRtyCOM) +pn53x_RFConfiguration__MaxRtyCOM (struct nfc_device *pnd, const uint8_t MaxRtyCOM) { uint8_t abtCmd[] = { RFConfiguration, @@ -1998,7 +2005,7 @@ pn53x_RFConfiguration__MaxRtyCOM (nfc_device *pnd, const uint8_t MaxRtyCOM) } bool -pn53x_RFConfiguration__MaxRetries (nfc_device *pnd, const uint8_t MxRtyATR, const uint8_t MxRtyPSL, const uint8_t MxRtyPassiveActivation) +pn53x_RFConfiguration__MaxRetries (struct nfc_device *pnd, const uint8_t MxRtyATR, const uint8_t MxRtyPSL, const uint8_t MxRtyPassiveActivation) { // Retry format: 0x00 means only 1 try, 0xff means infinite uint8_t abtCmd[] = { @@ -2012,7 +2019,7 @@ pn53x_RFConfiguration__MaxRetries (nfc_device *pnd, const uint8_t MxRtyATR, cons } bool -pn53x_SetParameters (nfc_device *pnd, const uint8_t ui8Value) +pn53x_SetParameters (struct nfc_device *pnd, const uint8_t ui8Value) { uint8_t abtCmd[] = { SetParameters, ui8Value }; @@ -2025,14 +2032,14 @@ pn53x_SetParameters (nfc_device *pnd, const uint8_t ui8Value) } bool -pn53x_SAMConfiguration (nfc_device *pnd, const pn532_sam_mode ui8Mode, int timeout) +pn53x_SAMConfiguration (struct nfc_device *pnd, const pn532_sam_mode ui8Mode, int timeout) { uint8_t abtCmd[] = { SAMConfiguration, ui8Mode, 0x00, 0x00 }; size_t szCmd = sizeof(abtCmd); if (CHIP_DATA(pnd)->type != PN532) { // This function is not supported by pn531 neither pn533 - pnd->iLastError = EDEVNOTSUP; + CHIP_DATA(pnd)->last_status_byte = EDEVNOTSUP; return false; } @@ -2047,14 +2054,14 @@ pn53x_SAMConfiguration (nfc_device *pnd, const pn532_sam_mode ui8Mode, int timeo szCmd = 3; break; default: - pnd->iLastError = EINVALARG; + CHIP_DATA(pnd)->last_status_byte = EINVALARG; return false; } return (pn53x_transceive (pnd, abtCmd, szCmd, NULL, NULL, timeout)); } bool -pn53x_PowerDown (nfc_device *pnd) +pn53x_PowerDown (struct nfc_device *pnd) { uint8_t abtCmd[] = { PowerDown, 0xf0 }; return (pn53x_transceive (pnd, abtCmd, sizeof (abtCmd), NULL, NULL, -1)); @@ -2064,7 +2071,7 @@ pn53x_PowerDown (nfc_device *pnd) * @brief C wrapper to InListPassiveTarget command * @return true if command is successfully sent * - * @param pnd nfc_device struct pointer that represent currently used device + * @param pnd struct nfc_device struct pointer that represent currently used device * @param pmInitModulation Desired modulation * @param pbtInitiatorData Optional initiator data used for Felica, ISO14443B, Topaz Polling or for ISO14443A selecting a specific UID * @param szInitiatorData Length of initiator data \a pbtInitiatorData @@ -2075,7 +2082,7 @@ pn53x_PowerDown (nfc_device *pnd) * @note To decode theses TargetData[n], there is @fn pn53x_decode_target_data */ bool -pn53x_InListPassiveTarget (nfc_device *pnd, +pn53x_InListPassiveTarget (struct nfc_device *pnd, const pn53x_modulation pmInitModulation, const uint8_t szMaxTargets, const uint8_t *pbtInitiatorData, const size_t szInitiatorData, uint8_t *pbtTargetsData, size_t *pszTargetsData, @@ -2094,14 +2101,14 @@ pn53x_InListPassiveTarget (nfc_device *pnd, case PM_ISO14443B_106: if (!(pnd->btSupportByte & SUPPORT_ISO14443B)) { // Eg. Some PN532 doesn't support type B! - pnd->iLastError = EDEVNOTSUP; + CHIP_DATA(pnd)->last_status_byte = EDEVNOTSUP; return false; } break; case PM_JEWEL_106: if(CHIP_DATA(pnd)->type == PN531) { // These modulations are not supported by pn531 - pnd->iLastError = EDEVNOTSUP; + CHIP_DATA(pnd)->last_status_byte = EDEVNOTSUP; return false; } break; @@ -2110,12 +2117,12 @@ pn53x_InListPassiveTarget (nfc_device *pnd, case PM_ISO14443B_847: if((CHIP_DATA(pnd)->type != PN533) || (!(pnd->btSupportByte & SUPPORT_ISO14443B))) { // These modulations are not supported by pn531 neither pn532 - pnd->iLastError = EDEVNOTSUP; + CHIP_DATA(pnd)->last_status_byte = EDEVNOTSUP; return false; } break; default: - pnd->iLastError = EINVALARG; + CHIP_DATA(pnd)->last_status_byte = EINVALARG; return false; } abtCmd[2] = pmInitModulation; // BrTy, the type of init modulation used for polling a passive tag @@ -2128,7 +2135,7 @@ pn53x_InListPassiveTarget (nfc_device *pnd, } bool -pn53x_InDeselect (nfc_device *pnd, const uint8_t ui8Target) +pn53x_InDeselect (struct nfc_device *pnd, const uint8_t ui8Target) { if (CHIP_DATA(pnd)->type == RCS360) { // We should do act here *only* if a target was previously selected @@ -2150,7 +2157,7 @@ pn53x_InDeselect (nfc_device *pnd, const uint8_t ui8Target) } bool -pn53x_InRelease (nfc_device *pnd, const uint8_t ui8Target) +pn53x_InRelease (struct nfc_device *pnd, const uint8_t ui8Target) { if (CHIP_DATA(pnd)->type == RCS360) { // We should do act here *only* if a target was previously selected @@ -2172,14 +2179,14 @@ pn53x_InRelease (nfc_device *pnd, const uint8_t ui8Target) } bool -pn53x_InAutoPoll (nfc_device *pnd, +pn53x_InAutoPoll (struct nfc_device *pnd, const pn53x_target_type *ppttTargetTypes, const size_t szTargetTypes, const uint8_t btPollNr, const uint8_t btPeriod, nfc_target * pntTargets, size_t *pszTargetFound, const int timeout) { if (CHIP_DATA(pnd)->type != PN532) { // This function is not supported by pn531 neither pn533 - pnd->iLastError = EDEVNOTSUP; + CHIP_DATA(pnd)->last_status_byte = EDEVNOTSUP; return false; } @@ -2235,7 +2242,7 @@ pn53x_InAutoPoll (nfc_device *pnd, * @param[out] pnt \a nfc_target which will be filled by this function */ bool -pn53x_InJumpForDEP (nfc_device *pnd, +pn53x_InJumpForDEP (struct nfc_device *pnd, const nfc_dep_mode ndm, const nfc_baud_rate nbr, const uint8_t *pbtPassiveInitiatorData, @@ -2261,7 +2268,7 @@ pn53x_InJumpForDEP (nfc_device *pnd, break; case NBR_847: case NBR_UNDEFINED: - pnd->iLastError = EINVALARG; + CHIP_DATA(pnd)->last_status_byte = EINVALARG; return false; break; } @@ -2281,7 +2288,7 @@ pn53x_InJumpForDEP (nfc_device *pnd, break; case NBR_847: case NBR_UNDEFINED: - pnd->iLastError = EINVALARG; + CHIP_DATA(pnd)->last_status_byte = EINVALARG; return false; break; } @@ -2331,7 +2338,7 @@ pn53x_InJumpForDEP (nfc_device *pnd, } bool -pn53x_TgInitAsTarget (nfc_device *pnd, pn53x_target_mode ptm, +pn53x_TgInitAsTarget (struct nfc_device *pnd, pn53x_target_mode ptm, const uint8_t *pbtMifareParams, const uint8_t *pbtTkt, size_t szTkt, const uint8_t *pbtFeliCaParams, @@ -2402,7 +2409,7 @@ pn53x_TgInitAsTarget (nfc_device *pnd, pn53x_target_mode ptm, } bool -pn53x_check_ack_frame (nfc_device *pnd, const uint8_t *pbtRxFrame, const size_t szRxFrameLen) +pn53x_check_ack_frame (struct nfc_device *pnd, const uint8_t *pbtRxFrame, const size_t szRxFrameLen) { if (szRxFrameLen >= sizeof (pn53x_ack_frame)) { if (0 == memcmp (pbtRxFrame, pn53x_ack_frame, sizeof (pn53x_ack_frame))) { @@ -2410,18 +2417,18 @@ pn53x_check_ack_frame (nfc_device *pnd, const uint8_t *pbtRxFrame, const size_t return true; } } - pnd->iLastError = EFRAACKMISMATCH; + CHIP_DATA(pnd)->last_status_byte = EFRAACKMISMATCH; log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "Unexpected PN53x reply!"); return false; } bool -pn53x_check_error_frame (nfc_device *pnd, const uint8_t *pbtRxFrame, const size_t szRxFrameLen) +pn53x_check_error_frame (struct nfc_device *pnd, const uint8_t *pbtRxFrame, const size_t szRxFrameLen) { if (szRxFrameLen >= sizeof (pn53x_error_frame)) { if (0 == memcmp (pbtRxFrame, pn53x_error_frame, sizeof (pn53x_error_frame))) { log_put (LOG_CATEGORY, NFC_PRIORITY_TRACE, "PN53x sent an error frame"); - pnd->iLastError = EFRAISERRFRAME; + CHIP_DATA(pnd)->last_status_byte = EFRAISERRFRAME; return false; } } @@ -2651,7 +2658,7 @@ pn53x_nm_to_ptt(const nfc_modulation nm) } void -pn53x_data_new (nfc_device *pnd, const struct pn53x_io *io) +pn53x_data_new (struct nfc_device *pnd, const struct pn53x_io *io) { pnd->chip_data = malloc(sizeof(struct pn53x_data)); @@ -2668,6 +2675,9 @@ pn53x_data_new (nfc_device *pnd, const struct pn53x_io *io) // PN53x starts in initiator mode CHIP_DATA (pnd)->operating_mode = INITIATOR; + // Clear last status byte + CHIP_DATA (pnd)->last_status_byte = 0x00; + // Set current target to NULL CHIP_DATA (pnd)->current_target = NULL; @@ -2686,7 +2696,7 @@ pn53x_data_new (nfc_device *pnd, const struct pn53x_io *io) } void -pn53x_data_free (nfc_device *pnd) +pn53x_data_free (struct nfc_device *pnd) { if (CHIP_DATA (pnd)->current_target) { free (CHIP_DATA (pnd)->current_target); diff --git a/libnfc/chips/pn53x.h b/libnfc/chips/pn53x.h index d5f8ce4..9c30d4c 100644 --- a/libnfc/chips/pn53x.h +++ b/libnfc/chips/pn53x.h @@ -127,8 +127,8 @@ typedef enum { } pn53x_operating_mode; struct pn53x_io { - bool (*send)(nfc_device *pnd, const uint8_t *pbtData, const size_t szData, int timeout); - int (*receive)(nfc_device *pnd, uint8_t *pbtData, const size_t szDataLen, int timeout); + bool (*send)(struct nfc_device *pnd, const uint8_t *pbtData, const size_t szData, int timeout); + int (*receive)(struct nfc_device *pnd, uint8_t *pbtData, const size_t szDataLen, int timeout); }; /* defines */ @@ -146,6 +146,8 @@ struct pn53x_data { nfc_target *current_target; /** PN53x I/O functions stored in struct */ const struct pn53x_io *io; +/** Last status byte returned by PN53x */ + uint8_t last_status_byte; /** Register cache for REG_CIU_BIT_FRAMING, SYMBOL_TX_LAST_BITS: The last TX bits setting, we need to reset this if it does not apply anymore */ uint8_t ui8TxBits; /** Register cache for SetParameters function. */ @@ -261,11 +263,11 @@ typedef enum { extern const uint8_t pn53x_ack_frame[6]; extern const uint8_t pn53x_nack_frame[6]; -bool pn53x_init(nfc_device *pnd); -bool pn53x_transceive (nfc_device *pnd, const uint8_t *pbtTx, const size_t szTx, uint8_t *pbtRx, size_t *pszRx, int timeout); +bool pn53x_init(struct nfc_device *pnd); +bool pn53x_transceive (struct nfc_device *pnd, const uint8_t *pbtTx, const size_t szTx, uint8_t *pbtRx, size_t *pszRx, int timeout); -bool pn53x_set_parameters (nfc_device *pnd, const uint8_t ui8Value, const bool bEnable); -bool pn53x_set_tx_bits (nfc_device *pnd, const uint8_t ui8Bits); +bool pn53x_set_parameters (struct nfc_device *pnd, const uint8_t ui8Value, const bool bEnable); +bool pn53x_set_tx_bits (struct nfc_device *pnd, const uint8_t ui8Bits); bool pn53x_wrap_frame (const uint8_t *pbtTx, const size_t szTxBits, const uint8_t *pbtTxPar, uint8_t *pbtFrame, size_t *pszFrameBits); bool pn53x_unwrap_frame (const uint8_t *pbtFrame, const size_t szFrameBits, uint8_t *pbtRx, size_t *pszRxBits, @@ -273,74 +275,74 @@ bool pn53x_unwrap_frame (const uint8_t *pbtFrame, const size_t szFrameBits, u bool pn53x_decode_target_data (const uint8_t *pbtRawData, size_t szRawData, pn53x_type chip_type, nfc_modulation_type nmt, nfc_target_info *pnti); -bool pn53x_read_register (nfc_device *pnd, uint16_t ui16Reg, uint8_t *ui8Value); -bool pn53x_write_register (nfc_device *pnd, uint16_t ui16Reg, uint8_t ui8SymbolMask, uint8_t ui8Value); -bool pn53x_get_firmware_version (nfc_device *pnd, char abtFirmwareText[22]); -int pn53x_set_property_int (nfc_device *pnd, const nfc_property property, const int value); -int pn53x_set_property_bool (nfc_device *pnd, const nfc_property property, const bool bEnable); +bool pn53x_read_register (struct nfc_device *pnd, uint16_t ui16Reg, uint8_t *ui8Value); +bool pn53x_write_register (struct nfc_device *pnd, uint16_t ui16Reg, uint8_t ui8SymbolMask, uint8_t ui8Value); +bool pn53x_get_firmware_version (struct nfc_device *pnd, char abtFirmwareText[22]); +int pn53x_set_property_int (struct nfc_device *pnd, const nfc_property property, const int value); +int pn53x_set_property_bool (struct nfc_device *pnd, const nfc_property property, const bool bEnable); -bool pn53x_check_communication (nfc_device *pnd); -bool pn53x_idle (nfc_device *pnd); +bool pn53x_check_communication (struct nfc_device *pnd); +bool pn53x_idle (struct nfc_device *pnd); // NFC device as Initiator functions -int pn53x_initiator_init (nfc_device *pnd); -bool pn53x_initiator_select_passive_target (nfc_device *pnd, +int pn53x_initiator_init (struct nfc_device *pnd); +bool pn53x_initiator_select_passive_target (struct nfc_device *pnd, const nfc_modulation nm, const uint8_t *pbtInitData, const size_t szInitData, nfc_target *pnt); -bool pn53x_initiator_poll_target (nfc_device *pnd, +bool pn53x_initiator_poll_target (struct nfc_device *pnd, const nfc_modulation *pnmModulations, const size_t szModulations, const uint8_t uiPollNr, const uint8_t uiPeriod, nfc_target *pnt); -bool pn53x_initiator_select_dep_target (nfc_device *pnd, +bool pn53x_initiator_select_dep_target (struct nfc_device *pnd, const nfc_dep_mode ndm, const nfc_baud_rate nbr, const nfc_dep_info *pndiInitiator, nfc_target *pnt, const int timeout); -bool pn53x_initiator_transceive_bits (nfc_device *pnd, const uint8_t *pbtTx, const size_t szTxBits, +bool pn53x_initiator_transceive_bits (struct nfc_device *pnd, const uint8_t *pbtTx, const size_t szTxBits, const uint8_t *pbtTxPar, uint8_t *pbtRx, size_t *pszRxBits, uint8_t *pbtRxPar); -bool pn53x_initiator_transceive_bytes (nfc_device *pnd, const uint8_t *pbtTx, const size_t szTx, +bool pn53x_initiator_transceive_bytes (struct nfc_device *pnd, const uint8_t *pbtTx, const size_t szTx, uint8_t *pbtRx, size_t *pszRx, int timeout); -bool pn53x_initiator_transceive_bits_timed (nfc_device *pnd, const uint8_t *pbtTx, const size_t szTxBits, +bool pn53x_initiator_transceive_bits_timed (struct nfc_device *pnd, const uint8_t *pbtTx, const size_t szTxBits, const uint8_t *pbtTxPar, uint8_t *pbtRx, size_t *pszRxBits, uint8_t *pbtRxPar, uint32_t *cycles); -bool pn53x_initiator_transceive_bytes_timed (nfc_device *pnd, const uint8_t *pbtTx, const size_t szTx, +bool pn53x_initiator_transceive_bytes_timed (struct nfc_device *pnd, const uint8_t *pbtTx, const size_t szTx, uint8_t *pbtRx, size_t *pszRx, uint32_t *cycles); -bool pn53x_initiator_deselect_target (nfc_device *pnd); +bool pn53x_initiator_deselect_target (struct nfc_device *pnd); // NFC device as Target functions -bool pn53x_target_init (nfc_device *pnd, nfc_target *pnt, uint8_t *pbtRx, size_t *pszRx); -bool pn53x_target_receive_bits (nfc_device *pnd, uint8_t *pbtRx, size_t *pszRxBits, uint8_t *pbtRxPar); -bool pn53x_target_receive_bytes (nfc_device *pnd, uint8_t *pbtRx, size_t *pszRx, int timeout); -bool pn53x_target_send_bits (nfc_device *pnd, const uint8_t *pbtTx, const size_t szTxBits, const uint8_t *pbtTxPar); -bool pn53x_target_send_bytes (nfc_device *pnd, const uint8_t *pbtTx, const size_t szTx, int timeout); +bool pn53x_target_init (struct nfc_device *pnd, nfc_target *pnt, uint8_t *pbtRx, size_t *pszRx); +bool pn53x_target_receive_bits (struct nfc_device *pnd, uint8_t *pbtRx, size_t *pszRxBits, uint8_t *pbtRxPar); +bool pn53x_target_receive_bytes (struct nfc_device *pnd, uint8_t *pbtRx, size_t *pszRx, int timeout); +bool pn53x_target_send_bits (struct nfc_device *pnd, const uint8_t *pbtTx, const size_t szTxBits, const uint8_t *pbtTxPar); +bool pn53x_target_send_bytes (struct nfc_device *pnd, const uint8_t *pbtTx, const size_t szTx, int timeout); // Error handling functions -const char *pn53x_strerror (const nfc_device *pnd); +const char *pn53x_strerror (const struct nfc_device *pnd); // C wrappers for PN53x commands -bool pn53x_SetParameters (nfc_device *pnd, const uint8_t ui8Value); -bool pn53x_SAMConfiguration (nfc_device *pnd, const pn532_sam_mode mode, int timeout); -bool pn53x_PowerDown (nfc_device *pnd); -bool pn53x_InListPassiveTarget (nfc_device *pnd, const pn53x_modulation pmInitModulation, +bool pn53x_SetParameters (struct nfc_device *pnd, const uint8_t ui8Value); +bool pn53x_SAMConfiguration (struct nfc_device *pnd, const pn532_sam_mode mode, int timeout); +bool pn53x_PowerDown (struct nfc_device *pnd); +bool pn53x_InListPassiveTarget (struct nfc_device *pnd, const pn53x_modulation pmInitModulation, const uint8_t szMaxTargets, const uint8_t *pbtInitiatorData, const size_t szInitiatorDataLen, uint8_t *pbtTargetsData, size_t *pszTargetsData, int timeout); -bool pn53x_InDeselect (nfc_device *pnd, const uint8_t ui8Target); -bool pn53x_InRelease (nfc_device *pnd, const uint8_t ui8Target); -bool pn53x_InAutoPoll (nfc_device *pnd, const pn53x_target_type *ppttTargetTypes, const size_t szTargetTypes, +bool pn53x_InDeselect (struct nfc_device *pnd, const uint8_t ui8Target); +bool pn53x_InRelease (struct nfc_device *pnd, const uint8_t ui8Target); +bool pn53x_InAutoPoll (struct nfc_device *pnd, const pn53x_target_type *ppttTargetTypes, const size_t szTargetTypes, const uint8_t btPollNr, const uint8_t btPeriod, nfc_target *pntTargets, size_t *pszTargetFound, const int timeout); -bool pn53x_InJumpForDEP (nfc_device *pnd, +bool pn53x_InJumpForDEP (struct nfc_device *pnd, const nfc_dep_mode ndm, const nfc_baud_rate nbr, const uint8_t *pbtPassiveInitiatorData, const uint8_t *pbtNFCID3i, const uint8_t *pbtGB, const size_t szGB, nfc_target *pnt, const int timeout); -bool pn53x_TgInitAsTarget (nfc_device *pnd, pn53x_target_mode ptm, +bool pn53x_TgInitAsTarget (struct nfc_device *pnd, pn53x_target_mode ptm, const uint8_t *pbtMifareParams, const uint8_t *pbtTkt, size_t szTkt, const uint8_t *pbtFeliCaParams, @@ -348,17 +350,17 @@ bool pn53x_TgInitAsTarget (nfc_device *pnd, pn53x_target_mode ptm, uint8_t *pbtRx, size_t *pszRx, uint8_t *pbtModeByte); // RFConfiguration -bool pn53x_RFConfiguration__RF_field (nfc_device *pnd, bool bEnable); -bool pn53x_RFConfiguration__Various_timings (nfc_device *pnd, const uint8_t fATR_RES_Timeout, const uint8_t fRetryTimeout); -bool pn53x_RFConfiguration__MaxRtyCOM (nfc_device *pnd, const uint8_t MaxRtyCOM); -bool pn53x_RFConfiguration__MaxRetries (nfc_device *pnd, const uint8_t MxRtyATR, const uint8_t MxRtyPSL, const uint8_t MxRtyPassiveActivation); +bool pn53x_RFConfiguration__RF_field (struct nfc_device *pnd, bool bEnable); +bool pn53x_RFConfiguration__Various_timings (struct nfc_device *pnd, const uint8_t fATR_RES_Timeout, const uint8_t fRetryTimeout); +bool pn53x_RFConfiguration__MaxRtyCOM (struct nfc_device *pnd, const uint8_t MaxRtyCOM); +bool pn53x_RFConfiguration__MaxRetries (struct nfc_device *pnd, const uint8_t MxRtyATR, const uint8_t MxRtyPSL, const uint8_t MxRtyPassiveActivation); // Misc -bool pn53x_check_ack_frame (nfc_device *pnd, const uint8_t *pbtRxFrame, const size_t szRxFrameLen); -bool pn53x_check_error_frame (nfc_device *pnd, const uint8_t *pbtRxFrame, const size_t szRxFrameLen); +bool pn53x_check_ack_frame (struct nfc_device *pnd, const uint8_t *pbtRxFrame, const size_t szRxFrameLen); +bool pn53x_check_error_frame (struct nfc_device *pnd, const uint8_t *pbtRxFrame, const size_t szRxFrameLen); bool pn53x_build_frame (uint8_t *pbtFrame, size_t *pszFrame, const uint8_t *pbtData, const size_t szData); -void pn53x_data_new (nfc_device *pnd, const struct pn53x_io *io); -void pn53x_data_free (nfc_device *pnd); +void pn53x_data_new (struct nfc_device *pnd, const struct pn53x_io *io); +void pn53x_data_free (struct nfc_device *pnd); #endif // __NFC_CHIPS_PN53X_H__ diff --git a/libnfc/drivers/acr122.c b/libnfc/drivers/acr122.c index 48d400b..615f69e 100644 --- a/libnfc/drivers/acr122.c +++ b/libnfc/drivers/acr122.c @@ -317,7 +317,7 @@ acr122_send (nfc_device *pnd, const uint8_t *pbtData, const size_t szData, int t // Make sure the command does not overflow the send buffer if (szData > ACR122_COMMAND_LEN) { - pnd->iLastError = EINVALARG; + pnd->last_error = NFC_EINVARG; return false; } @@ -344,7 +344,7 @@ acr122_send (nfc_device *pnd, const uint8_t *pbtData, const size_t szData, int t * field. */ if (SCardControl (DRIVER_DATA (pnd)->hCard, IOCTL_CCID_ESCAPE_SCARD_CTL_CODE, abtTxBuf, szTxBuf, DRIVER_DATA (pnd)->abtRx, ACR122_RESPONSE_LEN, &dwRxLen) != SCARD_S_SUCCESS) { - pnd->iLastError = ECOMIO; + pnd->last_error = NFC_EIO; return false; } } else { @@ -353,7 +353,7 @@ acr122_send (nfc_device *pnd, const uint8_t *pbtData, const size_t szData, int t * receive the response from the PN532. */ if (SCardTransmit (DRIVER_DATA (pnd)->hCard, &(DRIVER_DATA (pnd)->ioCard), abtTxBuf, szTxBuf, NULL, DRIVER_DATA (pnd)->abtRx, &dwRxLen) != SCARD_S_SUCCESS) { - pnd->iLastError = ECOMIO; + pnd->last_error = NFC_EIO; return false; } } @@ -365,12 +365,12 @@ acr122_send (nfc_device *pnd, const uint8_t *pbtData, const size_t szData, int t // Make sure we received the byte-count we expected if (dwRxLen != 2) { - pnd->iLastError = ECOMIO; + pnd->last_error = NFC_EIO; return false; } // Check if the operation was successful, so an answer is available if (DRIVER_DATA (pnd)->abtRx[0] == SCARD_OPERATION_ERROR) { - pnd->iLastError = EFRAISERRFRAME; + pnd->last_error = NFC_EIO; return false; } } else { @@ -396,7 +396,7 @@ acr122_receive (nfc_device *pnd, uint8_t *pbtData, const size_t szData, int time DWORD dwRxLen = sizeof (DRIVER_DATA (pnd)->abtRx); abtRxCmd[4] = DRIVER_DATA (pnd)->abtRx[1]; if (SCardTransmit (DRIVER_DATA (pnd)->hCard, &(DRIVER_DATA (pnd)->ioCard), abtRxCmd, sizeof (abtRxCmd), NULL, DRIVER_DATA (pnd)->abtRx, &dwRxLen) != SCARD_S_SUCCESS) { - pnd->iLastError = ECOMIO; + pnd->last_error = NFC_EIO; return -1; } DRIVER_DATA (pnd)->szRx = dwRxLen; @@ -409,15 +409,13 @@ acr122_receive (nfc_device *pnd, uint8_t *pbtData, const size_t szData, int time // Make sure we have an emulated answer that fits the return buffer if (DRIVER_DATA (pnd)->szRx < 4 || (DRIVER_DATA (pnd)->szRx - 4) > szData) { - pnd->iLastError = ECOMIO; + pnd->last_error = NFC_EIO; return -1; } // Wipe out the 4 APDU emulation bytes: D5 4B .. .. .. 90 00 len = DRIVER_DATA (pnd)->szRx - 4; memcpy (pbtData, DRIVER_DATA (pnd)->abtRx + 2, len); - // Transmission went successful - pnd->iLastError = 0; return len; } @@ -488,7 +486,7 @@ const struct nfc_driver_t acr122_driver = { .target_receive_bits = pn53x_target_receive_bits, .device_set_property_bool = pn53x_set_property_bool, - .device_set_property_int = pn53x_set_property_int, + .device_set_property_int = pn53x_set_property_int, .abort_command = NULL, // FIXME: abort is not supported in this driver .idle = NULL, // FIXME: idle is not supported in this driver diff --git a/libnfc/drivers/arygon.c b/libnfc/drivers/arygon.c index 11b4c5d..17d4ec9 100644 --- a/libnfc/drivers/arygon.c +++ b/libnfc/drivers/arygon.c @@ -315,19 +315,19 @@ arygon_tama_send (nfc_device *pnd, const uint8_t *pbtData, const size_t szData, if (szData > PN53x_NORMAL_FRAME__DATA_MAX_LEN) { // ARYGON Reader with PN532 equipped does not support extended frame (bug in ARYGON firmware?) log_put (LOG_CATEGORY, NFC_PRIORITY_TRACE, "ARYGON device does not support more than %d bytes as payload (requested: %zd)", PN53x_NORMAL_FRAME__DATA_MAX_LEN, szData); - pnd->iLastError = EDEVNOTSUP; + pnd->last_error = NFC_EDEVNOTSUPP; return false; } if (!pn53x_build_frame (abtFrame + 1, &szFrame, pbtData, szData)) { - pnd->iLastError = EINVALARG; + pnd->last_error = NFC_EINVARG; return false; } int res = uart_send (DRIVER_DATA (pnd)->port, abtFrame, szFrame + 1, timeout); if (res != 0) { log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Unable to transmit data. (TX)"); - pnd->iLastError = res; + pnd->last_error = NFC_EIO; return false; } @@ -335,7 +335,7 @@ arygon_tama_send (nfc_device *pnd, const uint8_t *pbtData, const size_t szData, res = uart_receive (DRIVER_DATA (pnd)->port, abtRxBuf, sizeof (abtRxBuf), 0, timeout); if (res != 0) { log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Unable to read ACK"); - pnd->iLastError = res; + pnd->last_error = NFC_EIO; return false; } @@ -378,17 +378,17 @@ arygon_tama_receive (nfc_device *pnd, uint8_t *pbtData, const size_t szDataLen, abort_p = (void*)&(DRIVER_DATA (pnd)->abort_flag); #endif - pnd->iLastError = uart_receive (DRIVER_DATA (pnd)->port, abtRxBuf, 5, abort_p, timeout); + pnd->last_error = uart_receive (DRIVER_DATA (pnd)->port, abtRxBuf, 5, abort_p, timeout); - if (abort_p && (EOPABORT == pnd->iLastError)) { + if (abort_p && (EOPABORT == pnd->last_error)) { arygon_abort (pnd); - /* iLastError got reset by arygon_abort() */ - pnd->iLastError = EOPABORT; + /* last_error got reset by arygon_abort() */ + pnd->last_error = EOPABORT; return -1; } - if (pnd->iLastError != 0) { + if (pnd->last_error != 0) { log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Unable to receive data. (RX)"); return -1; } @@ -396,7 +396,7 @@ arygon_tama_receive (nfc_device *pnd, uint8_t *pbtData, const size_t szDataLen, const uint8_t pn53x_preamble[3] = { 0x00, 0x00, 0xff }; if (0 != (memcmp (abtRxBuf, pn53x_preamble, 3))) { log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Frame preamble+start code mismatch"); - pnd->iLastError = ECOMIO; + pnd->last_error = ECOMIO; return -1; } @@ -404,7 +404,7 @@ arygon_tama_receive (nfc_device *pnd, uint8_t *pbtData, const size_t szDataLen, // Error frame uart_receive (DRIVER_DATA (pnd)->port, abtRxBuf, 3, 0, timeout); log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Application level error detected"); - pnd->iLastError = EFRAISERRFRAME; + pnd->last_error = EFRAISERRFRAME; return -1; } else if ((0xff == abtRxBuf[3]) && (0xff == abtRxBuf[4])) { // Extended frame @@ -415,7 +415,7 @@ arygon_tama_receive (nfc_device *pnd, uint8_t *pbtData, const size_t szDataLen, if (256 != (abtRxBuf[3] + abtRxBuf[4])) { // TODO: Retry log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Length checksum mismatch"); - pnd->iLastError = ECOMIO; + pnd->last_error = ECOMIO; return -1; } @@ -425,39 +425,39 @@ arygon_tama_receive (nfc_device *pnd, uint8_t *pbtData, const size_t szDataLen, if (len > szDataLen) { log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "Unable to receive data: buffer too small. (szDataLen: %zu, len: %zu)", szDataLen, len); - pnd->iLastError = ECOMIO; + pnd->last_error = ECOMIO; return -1; } // TFI + PD0 (CC+1) - pnd->iLastError = uart_receive (DRIVER_DATA (pnd)->port, abtRxBuf, 2, 0, timeout); - if (pnd->iLastError != 0) { + pnd->last_error = uart_receive (DRIVER_DATA (pnd)->port, abtRxBuf, 2, 0, timeout); + if (pnd->last_error != 0) { log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Unable to receive data. (RX)"); return -1; } if (abtRxBuf[0] != 0xD5) { log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "TFI Mismatch"); - pnd->iLastError = ECOMIO; + pnd->last_error = ECOMIO; return -1; } if (abtRxBuf[1] != CHIP_DATA (pnd)->ui8LastCommand + 1) { log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Command Code verification failed"); - pnd->iLastError = ECOMIO; + pnd->last_error = ECOMIO; return -1; } if (len) { - pnd->iLastError = uart_receive (DRIVER_DATA (pnd)->port, pbtData, len, 0, timeout); - if (pnd->iLastError != 0) { + pnd->last_error = uart_receive (DRIVER_DATA (pnd)->port, pbtData, len, 0, timeout); + if (pnd->last_error != 0) { log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Unable to receive data. (RX)"); return -1; } } - pnd->iLastError = uart_receive (DRIVER_DATA (pnd)->port, abtRxBuf, 2, 0, timeout); - if (pnd->iLastError != 0) { + pnd->last_error = uart_receive (DRIVER_DATA (pnd)->port, abtRxBuf, 2, 0, timeout); + if (pnd->last_error != 0) { log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Unable to receive data. (RX)"); return -1; } @@ -470,13 +470,13 @@ arygon_tama_receive (nfc_device *pnd, uint8_t *pbtData, const size_t szDataLen, if (btDCS != abtRxBuf[0]) { log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Data checksum mismatch"); - pnd->iLastError = ECOMIO; + pnd->last_error = ECOMIO; return -1; } if (0x00 != abtRxBuf[1]) { log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Frame postamble mismatch"); - pnd->iLastError = ECOMIO; + pnd->last_error = ECOMIO; return -1; } // The PN53x command is done and we successfully received the reply diff --git a/libnfc/drivers/pn532_uart.c b/libnfc/drivers/pn532_uart.c index f582d8b..2078a6a 100644 --- a/libnfc/drivers/pn532_uart.c +++ b/libnfc/drivers/pn532_uart.c @@ -321,14 +321,14 @@ pn532_uart_send (nfc_device *pnd, const uint8_t *pbtData, const size_t szData, i size_t szFrame = 0; if (!pn53x_build_frame (abtFrame, &szFrame, pbtData, szData)) { - pnd->iLastError = EINVALARG; + pnd->last_error = EINVALARG; return false; } int res = uart_send (DRIVER_DATA(pnd)->port, abtFrame, szFrame, timeout); if (res != 0) { log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Unable to transmit data. (TX)"); - pnd->iLastError = res; + pnd->last_error = res; return false; } @@ -336,7 +336,7 @@ pn532_uart_send (nfc_device *pnd, const uint8_t *pbtData, const size_t szData, i res = uart_receive (DRIVER_DATA(pnd)->port, abtRxBuf, 6, 0, timeout); if (res != 0) { log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Unable to read ACK"); - pnd->iLastError = res; + pnd->last_error = res; return false; } @@ -361,14 +361,14 @@ pn532_uart_receive (nfc_device *pnd, uint8_t *pbtData, const size_t szDataLen, i abort_p = (void*)&(DRIVER_DATA (pnd)->abort_flag); #endif - pnd->iLastError = uart_receive (DRIVER_DATA (pnd)->port, abtRxBuf, 5, abort_p, timeout); + pnd->last_error = uart_receive (DRIVER_DATA (pnd)->port, abtRxBuf, 5, abort_p, timeout); - if (abort_p && (EOPABORT == pnd->iLastError)) { + if (abort_p && (EOPABORT == pnd->last_error)) { pn532_uart_ack (pnd); return -1; } - if (pnd->iLastError != 0) { + if (pnd->last_error != 0) { log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Unable to receive data. (RX)"); return -1; } @@ -376,7 +376,7 @@ pn532_uart_receive (nfc_device *pnd, uint8_t *pbtData, const size_t szDataLen, i const uint8_t pn53x_preamble[3] = { 0x00, 0x00, 0xff }; if (0 != (memcmp (abtRxBuf, pn53x_preamble, 3))) { log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Frame preamble+start code mismatch"); - pnd->iLastError = ECOMIO; + pnd->last_error = ECOMIO; return -1; } @@ -384,19 +384,19 @@ pn532_uart_receive (nfc_device *pnd, uint8_t *pbtData, const size_t szDataLen, i // Error frame uart_receive (DRIVER_DATA (pnd)->port, abtRxBuf, 3, 0, timeout); log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Application level error detected"); - pnd->iLastError = EFRAISERRFRAME; + pnd->last_error = EFRAISERRFRAME; return -1; } else if ((0xff == abtRxBuf[3]) && (0xff == abtRxBuf[4])) { // Extended frame - pnd->iLastError = uart_receive (DRIVER_DATA(pnd)->port, abtRxBuf, 3, 0, timeout); - if (pnd->iLastError) return -1; + pnd->last_error = uart_receive (DRIVER_DATA(pnd)->port, abtRxBuf, 3, 0, timeout); + if (pnd->last_error) return -1; // (abtRxBuf[0] << 8) + abtRxBuf[1] (LEN) include TFI + (CC+1) len = (abtRxBuf[0] << 8) + abtRxBuf[1] - 2; if (((abtRxBuf[0] + abtRxBuf[1] + abtRxBuf[2]) % 256) != 0) { // TODO: Retry log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Length checksum mismatch"); - pnd->iLastError = ECOMIO; + pnd->last_error = ECOMIO; return -1; } } else { @@ -404,7 +404,7 @@ pn532_uart_receive (nfc_device *pnd, uint8_t *pbtData, const size_t szDataLen, i if (256 != (abtRxBuf[3] + abtRxBuf[4])) { // TODO: Retry log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Length checksum mismatch"); - pnd->iLastError = ECOMIO; + pnd->last_error = ECOMIO; return -1; } @@ -414,39 +414,39 @@ pn532_uart_receive (nfc_device *pnd, uint8_t *pbtData, const size_t szDataLen, i if (len > szDataLen) { log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "Unable to receive data: buffer too small. (szDataLen: %zu, len: %zu)", szDataLen, len); - pnd->iLastError = ECOMIO; + pnd->last_error = ECOMIO; return -1; } // TFI + PD0 (CC+1) - pnd->iLastError = uart_receive (DRIVER_DATA (pnd)->port, abtRxBuf, 2, 0, timeout); - if (pnd->iLastError != 0) { + pnd->last_error = uart_receive (DRIVER_DATA (pnd)->port, abtRxBuf, 2, 0, timeout); + if (pnd->last_error != 0) { log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Unable to receive data. (RX)"); return -1; } if (abtRxBuf[0] != 0xD5) { log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "TFI Mismatch"); - pnd->iLastError = ECOMIO; + pnd->last_error = ECOMIO; return -1; } if (abtRxBuf[1] != CHIP_DATA (pnd)->ui8LastCommand + 1) { log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Command Code verification failed"); - pnd->iLastError = ECOMIO; + pnd->last_error = ECOMIO; return -1; } if (len) { - pnd->iLastError = uart_receive (DRIVER_DATA (pnd)->port, pbtData, len, 0, timeout); - if (pnd->iLastError != 0) { + pnd->last_error = uart_receive (DRIVER_DATA (pnd)->port, pbtData, len, 0, timeout); + if (pnd->last_error != 0) { log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Unable to receive data. (RX)"); return -1; } } - pnd->iLastError = uart_receive (DRIVER_DATA (pnd)->port, abtRxBuf, 2, 0, timeout); - if (pnd->iLastError != 0) { + pnd->last_error = uart_receive (DRIVER_DATA (pnd)->port, abtRxBuf, 2, 0, timeout); + if (pnd->last_error != 0) { log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Unable to receive data. (RX)"); return -1; } @@ -459,13 +459,13 @@ pn532_uart_receive (nfc_device *pnd, uint8_t *pbtData, const size_t szDataLen, i if (btDCS != abtRxBuf[0]) { log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Data checksum mismatch"); - pnd->iLastError = ECOMIO; + pnd->last_error = ECOMIO; return -1; } if (0x00 != abtRxBuf[1]) { log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Frame postamble mismatch"); - pnd->iLastError = ECOMIO; + pnd->last_error = ECOMIO; return -1; } // The PN53x command is done and we successfully received the reply @@ -526,7 +526,7 @@ const struct nfc_driver_t pn532_uart_driver = { .target_receive_bits = pn53x_target_receive_bits, .device_set_property_bool = pn53x_set_property_bool, - .device_set_property_int = pn53x_set_property_int, + .device_set_property_int = pn53x_set_property_int, .abort_command = pn532_uart_abort_command, .idle = pn53x_idle, diff --git a/libnfc/drivers/pn53x_usb.c b/libnfc/drivers/pn53x_usb.c index d660e4f..c2e6349 100644 --- a/libnfc/drivers/pn53x_usb.c +++ b/libnfc/drivers/pn53x_usb.c @@ -516,14 +516,14 @@ pn53x_usb_send (nfc_device *pnd, const uint8_t *pbtData, const size_t szData, co int res = pn53x_usb_bulk_write (DRIVER_DATA (pnd), abtFrame, szFrame, timeout); if (res < 0) { - pnd->iLastError = ECOMIO; + pnd->last_error = ECOMIO; return false; } uint8_t abtRxBuf[PN53X_USB_BUFFER_LEN]; res = pn53x_usb_bulk_read (DRIVER_DATA (pnd), abtRxBuf, sizeof (abtRxBuf), timeout); if (res < 0) { - pnd->iLastError = ECOMIO; + pnd->last_error = ECOMIO; // try to interrupt current device state pn53x_usb_ack(pnd); return false; @@ -541,7 +541,7 @@ pn53x_usb_send (nfc_device *pnd, const uint8_t *pbtData, const size_t szData, co // FIXME Sony reader is also affected by this bug but NACK is not supported int res = pn53x_usb_bulk_write (DRIVER_DATA (pnd), (uint8_t *)pn53x_nack_frame, sizeof(pn53x_nack_frame), timeout); if (res < 0) { - pnd->iLastError = ECOMIO; + pnd->last_error = ECOMIO; // try to interrupt current device state pn53x_usb_ack(pnd); return false; @@ -574,7 +574,7 @@ read: // A user-provided timeout is set, we have to cut it in multiple chunk to be able to keep an nfc_abort_command() mecanism remaining_time -= USB_TIMEOUT_PER_PASS; if (remaining_time <= 0) { - pnd->iLastError = ECOMTIMEOUT; + pnd->last_error = ECOMTIMEOUT; return -1; } else { usb_timeout = MIN(remaining_time, USB_TIMEOUT_PER_PASS); @@ -587,7 +587,7 @@ read: if (DRIVER_DATA (pnd)->abort_flag) { DRIVER_DATA (pnd)->abort_flag = false; pn53x_usb_ack (pnd); - pnd->iLastError = EOPABORT; + pnd->last_error = EOPABORT; return -1; } else { goto read; @@ -595,7 +595,7 @@ read: } if (res < 0) { - pnd->iLastError = ECOMIO; + pnd->last_error = ECOMIO; // try to interrupt current device state pn53x_usb_ack(pnd); return -1; @@ -604,7 +604,7 @@ read: const uint8_t pn53x_preamble[3] = { 0x00, 0x00, 0xff }; if (0 != (memcmp (abtRxBuf, pn53x_preamble, 3))) { log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Frame preamble+start code mismatch"); - pnd->iLastError = ECOMIO; + pnd->last_error = ECOMIO; return -1; } offset += 3; @@ -612,7 +612,7 @@ read: if ((0x01 == abtRxBuf[offset]) && (0xff == abtRxBuf[offset + 1])) { // Error frame log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Application level error detected"); - pnd->iLastError = EFRAISERRFRAME; + pnd->last_error = EFRAISERRFRAME; return -1; } else if ((0xff == abtRxBuf[offset]) && (0xff == abtRxBuf[offset + 1])) { // Extended frame @@ -623,7 +623,7 @@ read: if (((abtRxBuf[offset] + abtRxBuf[offset + 1] + abtRxBuf[offset + 2]) % 256) != 0) { // TODO: Retry log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Length checksum mismatch"); - pnd->iLastError = ECOMIO; + pnd->last_error = ECOMIO; return -1; } offset += 3; @@ -632,7 +632,7 @@ read: if (256 != (abtRxBuf[offset] + abtRxBuf[offset + 1])) { // TODO: Retry log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Length checksum mismatch"); - pnd->iLastError = ECOMIO; + pnd->last_error = ECOMIO; return -1; } @@ -643,21 +643,21 @@ read: if (len > szDataLen) { log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "Unable to receive data: buffer too small. (szDataLen: %zu, len: %zu)", szDataLen, len); - pnd->iLastError = ECOMIO; + pnd->last_error = ECOMIO; return -1; } // TFI + PD0 (CC+1) if (abtRxBuf[offset] != 0xD5) { log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "TFI Mismatch"); - pnd->iLastError = ECOMIO; + pnd->last_error = ECOMIO; return -1; } offset += 1; if (abtRxBuf[offset] != CHIP_DATA (pnd)->ui8LastCommand + 1) { log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Command Code verification failed"); - pnd->iLastError = ECOMIO; + pnd->last_error = ECOMIO; return -1; } offset += 1; @@ -673,18 +673,18 @@ read: if (btDCS != abtRxBuf[offset]) { log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Data checksum mismatch"); - pnd->iLastError = ECOMIO; + pnd->last_error = ECOMIO; return -1; } offset += 1; if (0x00 != abtRxBuf[offset]) { log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Frame postamble mismatch"); - pnd->iLastError = ECOMIO; + pnd->last_error = ECOMIO; return -1; } // The PN53x command is done and we successfully received the reply - pnd->iLastError = 0; + pnd->last_error = 0; return len; } @@ -702,7 +702,7 @@ pn53x_usb_init (nfc_device *pnd) const uint8_t abtCmd[] = { GetFirmwareVersion }; pn53x_transceive (pnd, abtCmd, sizeof (abtCmd), NULL, NULL, 0); // ...and we don't care about error - pnd->iLastError = 0; + pnd->last_error = 0; if (SONY_RCS360 == DRIVER_DATA (pnd)->model) { log_put (LOG_CATEGORY, NFC_PRIORITY_TRACE, "SONY RC-S360 initialization."); const uint8_t abtCmd2[] = { 0x18, 0x01 }; @@ -754,8 +754,9 @@ On ASK LoGO hardware: int pn53x_usb_set_property_bool (nfc_device *pnd, const nfc_property property, const bool bEnable) { - if (!pn53x_set_property_bool (pnd, property, bEnable)) - return NFC_DEVICE_ERROR; + int res; + if ((res = pn53x_set_property_bool (pnd, property, bEnable)) < 0) + return res; switch (DRIVER_DATA (pnd)->model) { case ASK_LOGO: @@ -763,14 +764,14 @@ pn53x_usb_set_property_bool (nfc_device *pnd, const nfc_property property, const /* Switch on/off LED2 and Progressive Field GPIO according to ACTIVATE_FIELD option */ log_put (LOG_CATEGORY, NFC_PRIORITY_TRACE, "Switch progressive field %s", bEnable ? "On" : "Off"); if (!pn53x_write_register (pnd, PN53X_SFR_P3, _BV(P31) | _BV(P34), bEnable ? _BV (P34) : _BV (P31))) - return NFC_DEVICE_ERROR; + return NFC_ECHIP; } break; case SCM_SCL3711: if (NP_ACTIVATE_FIELD == property) { // Switch on/off LED according to ACTIVATE_FIELD option if (!pn53x_write_register (pnd, PN53X_SFR_P3, _BV (P32), bEnable ? 0 : _BV (P32))) - return NFC_DEVICE_ERROR; + return NFC_ECHIP; } break; default: diff --git a/libnfc/nfc-device.c b/libnfc/nfc-device.c index edf3a17..aad31d8 100644 --- a/libnfc/nfc-device.c +++ b/libnfc/nfc-device.c @@ -49,7 +49,7 @@ nfc_device_new (void) res->bPar = false; res->bEasyFraming = false; res->bAutoIso14443_4 = false; - res->iLastError = 0; + res->last_error = 0; res->driver_data = NULL; res->chip_data = NULL; diff --git a/libnfc/nfc-internal.h b/libnfc/nfc-internal.h index bfd703e..b67828f 100644 --- a/libnfc/nfc-internal.h +++ b/libnfc/nfc-internal.h @@ -23,11 +23,12 @@ */ #ifndef __NFC_INTERNAL_H__ -# define __NFC_INTERNAL_H__ +#define __NFC_INTERNAL_H__ -# include -# include -# include +#include +#include + +#include "nfc/nfc.h" #include "log.h" @@ -35,11 +36,11 @@ * @macro HAL * @brief Execute corresponding driver function if exists. */ -#define HAL( FUNCTION, ... ) pnd->iLastError = 0; \ +#define HAL( FUNCTION, ... ) pnd->last_error = 0; \ if (pnd->driver->FUNCTION) { \ return pnd->driver->FUNCTION( __VA_ARGS__ ); \ } else { \ - pnd->iLastError = EDEVNOTSUP; \ + pnd->last_error = NFC_EDEVNOTSUPP; \ return false; \ } @@ -127,35 +128,64 @@ struct nfc_driver_t { const char *name; bool (*probe)(nfc_connstring connstrings[], size_t connstrings_len, size_t * pszDeviceFound); - nfc_device *(*connect) (const nfc_connstring connstring); - void (*disconnect) (nfc_device *pnd); - const char *(*strerror) (const nfc_device *pnd); + struct nfc_device *(*connect) (const nfc_connstring connstring); + void (*disconnect) (struct nfc_device *pnd); + const char *(*strerror) (const struct nfc_device *pnd); - int (*initiator_init) (nfc_device *pnd); - bool (*initiator_select_passive_target) (nfc_device *pnd, const nfc_modulation nm, const uint8_t * pbtInitData, const size_t szInitData, nfc_target * pnt); - bool (*initiator_poll_target) (nfc_device *pnd, const nfc_modulation * pnmModulations, const size_t szModulations, const uint8_t uiPollNr, const uint8_t btPeriod, nfc_target * pnt); - bool (*initiator_select_dep_target) (nfc_device *pnd, const nfc_dep_mode ndm, const nfc_baud_rate nbr, const nfc_dep_info * pndiInitiator, nfc_target * pnt, const int timeout); - bool (*initiator_deselect_target) (nfc_device *pnd); - bool (*initiator_transceive_bytes) (nfc_device *pnd, const uint8_t * pbtTx, const size_t szTx, uint8_t * pbtRx, size_t * pszRx, int timeout); - bool (*initiator_transceive_bits) (nfc_device *pnd, const uint8_t * pbtTx, const size_t szTxBits, const uint8_t * pbtTxPar, uint8_t * pbtRx, size_t * pszRxBits, uint8_t * pbtRxPar); - bool (*initiator_transceive_bytes_timed) (nfc_device *pnd, const uint8_t * pbtTx, const size_t szTx, uint8_t * pbtRx, size_t * pszRx, uint32_t * cycles); - bool (*initiator_transceive_bits_timed) (nfc_device *pnd, const uint8_t * pbtTx, const size_t szTxBits, const uint8_t * pbtTxPar, uint8_t * pbtRx, size_t * pszRxBits, uint8_t * pbtRxPar, uint32_t * cycles); + int (*initiator_init) (struct nfc_device *pnd); + bool (*initiator_select_passive_target) (struct nfc_device *pnd, const nfc_modulation nm, const uint8_t * pbtInitData, const size_t szInitData, nfc_target * pnt); + bool (*initiator_poll_target) (struct nfc_device *pnd, const nfc_modulation * pnmModulations, const size_t szModulations, const uint8_t uiPollNr, const uint8_t btPeriod, nfc_target * pnt); + bool (*initiator_select_dep_target) (struct nfc_device *pnd, const nfc_dep_mode ndm, const nfc_baud_rate nbr, const nfc_dep_info * pndiInitiator, nfc_target * pnt, const int timeout); + bool (*initiator_deselect_target) (struct nfc_device *pnd); + bool (*initiator_transceive_bytes) (struct nfc_device *pnd, const uint8_t * pbtTx, const size_t szTx, uint8_t * pbtRx, size_t * pszRx, int timeout); + bool (*initiator_transceive_bits) (struct nfc_device *pnd, const uint8_t * pbtTx, const size_t szTxBits, const uint8_t * pbtTxPar, uint8_t * pbtRx, size_t * pszRxBits, uint8_t * pbtRxPar); + bool (*initiator_transceive_bytes_timed) (struct nfc_device *pnd, const uint8_t * pbtTx, const size_t szTx, uint8_t * pbtRx, size_t * pszRx, uint32_t * cycles); + bool (*initiator_transceive_bits_timed) (struct nfc_device *pnd, const uint8_t * pbtTx, const size_t szTxBits, const uint8_t * pbtTxPar, uint8_t * pbtRx, size_t * pszRxBits, uint8_t * pbtRxPar, uint32_t * cycles); - bool (*target_init) (nfc_device *pnd, nfc_target * pnt, uint8_t * pbtRx, size_t * pszRx); - bool (*target_send_bytes) (nfc_device *pnd, const uint8_t * pbtTx, const size_t szTx, int timeout); - bool (*target_receive_bytes) (nfc_device *pnd, uint8_t * pbtRx, size_t * pszRx, int timeout); - bool (*target_send_bits) (nfc_device *pnd, const uint8_t * pbtTx, const size_t szTxBits, const uint8_t * pbtTxPar); - bool (*target_receive_bits) (nfc_device *pnd, uint8_t * pbtRx, size_t * pszRxBits, uint8_t * pbtRxPar); + bool (*target_init) (struct nfc_device *pnd, nfc_target * pnt, uint8_t * pbtRx, size_t * pszRx); + bool (*target_send_bytes) (struct nfc_device *pnd, const uint8_t * pbtTx, const size_t szTx, int timeout); + bool (*target_receive_bytes) (struct nfc_device *pnd, uint8_t * pbtRx, size_t * pszRx, int timeout); + bool (*target_send_bits) (struct nfc_device *pnd, const uint8_t * pbtTx, const size_t szTxBits, const uint8_t * pbtTxPar); + bool (*target_receive_bits) (struct nfc_device *pnd, uint8_t * pbtRx, size_t * pszRxBits, uint8_t * pbtRxPar); - int (*device_set_property_bool) (nfc_device *pnd, const nfc_property property, const bool bEnable); - int (*device_set_property_int) (nfc_device *pnd, const nfc_property property, const int value); + int (*device_set_property_bool) (struct nfc_device *pnd, const nfc_property property, const bool bEnable); + int (*device_set_property_int) (struct nfc_device *pnd, const nfc_property property, const int value); - bool (*abort_command) (nfc_device *pnd); - bool (*idle) (nfc_device *pnd); + bool (*abort_command) (struct nfc_device *pnd); + bool (*idle) (struct nfc_device *pnd); +}; + +# define DEVICE_NAME_LENGTH 256 +# define DEVICE_PORT_LENGTH 64 + +/** + * @struct nfc_device + * @brief NFC device information + */ +struct nfc_device { + const struct nfc_driver_t *driver; + void *driver_data; + void *chip_data; + +/** Device name string, including device wrapper firmware */ + char acName[DEVICE_NAME_LENGTH]; +/** Is the CRC automaticly added, checked and removed from the frames */ + bool bCrc; +/** Does the chip handle parity bits, all parities are handled as data */ + bool bPar; +/** Should the chip handle frames encapsulation and chaining */ + bool bEasyFraming; +/** Should the chip switch automatically activate ISO14443-4 when + selecting tags supporting it? */ + bool bAutoIso14443_4; +/** Supported modulation encoded in a byte */ + uint8_t btSupportByte; +/** Last reported error */ + int last_error; }; nfc_device *nfc_device_new (void); -void nfc_device_free (nfc_device *nfc_device); +void nfc_device_free (nfc_device *dev); void iso14443_cascade_uid (const uint8_t abtUID[], const size_t szUID, uint8_t * pbtCascadedUID, size_t * pszCascadedUID); diff --git a/libnfc/nfc.c b/libnfc/nfc.c index 48641f3..44d18e2 100644 --- a/libnfc/nfc.c +++ b/libnfc/nfc.c @@ -372,7 +372,7 @@ nfc_initiator_list_passive_targets (nfc_device *pnd, size_t szInitDataLen = 0; int res = 0; - pnd->iLastError = 0; + pnd->last_error = 0; // Let the reader only try once to find a tag if ((res = nfc_device_set_property_bool (pnd, NP_INFINITE_SELECT, false)) < 0) { From bf7c36d9bb81af6e5974742d93fed962a7a9d595 Mon Sep 17 00:00:00 2001 From: Romuald Conty Date: Mon, 19 Dec 2011 00:23:21 +0000 Subject: [PATCH 028/113] less structs and defines publicly exposed - nfc_device is now an opaque type; - PN53x specific errors are not public anymore; - nfc_device_name() renamed to nfc_device_get_name() for the sake of consistency; - examples/*, utils/* uses the new nfc_device_get_name() function instead of access directly to struct's content; - new error defined: NFC_ERFTRANS for notifying about RF transmission error, its used by mifare.c to detect permissions error on mifare; - drivers initiator_transceive_bytes() function now returns libnfc's error code on failure (<0), and received bytes count on success (>=0); - remove some unused errors. --- examples/nfc-anticol.c | 2 +- examples/nfc-dep-initiator.c | 2 +- examples/nfc-dep-target.c | 2 +- examples/nfc-emulate-forum-tag2.c | 2 +- examples/nfc-emulate-tag.c | 2 +- examples/nfc-emulate-uid.c | 2 +- examples/nfc-poll.c | 19 ++++--- examples/nfc-relay.c | 4 +- examples/pn53x-diagnose.c | 2 +- examples/pn53x-sam.c | 2 +- examples/pn53x-tamashell.c | 2 +- include/nfc/nfc.h | 51 ++----------------- libnfc/buses/uart_posix.c | 12 ++--- libnfc/chips/pn53x-internal.h | 28 +++++++++++ libnfc/chips/pn53x.c | 84 +++++++++++++++---------------- libnfc/chips/pn53x.h | 2 +- libnfc/drivers/arygon.c | 30 +++++------ libnfc/drivers/pn532_uart.c | 28 +++++------ libnfc/drivers/pn53x_usb.c | 30 +++++------ libnfc/nfc-internal.h | 2 +- libnfc/nfc.c | 2 +- utils/mifare.c | 14 ++++-- utils/nfc-emulate-forum-tag4.c | 2 +- utils/nfc-list.c | 2 +- utils/nfc-mfclassic.c | 2 +- utils/nfc-mfsetuid.c | 2 +- utils/nfc-mfultralight.c | 2 +- utils/nfc-read-forum-tag3.c | 2 +- utils/nfc-relay-picc.c | 4 +- 29 files changed, 162 insertions(+), 178 deletions(-) diff --git a/examples/nfc-anticol.c b/examples/nfc-anticol.c index d6afe0d..b5d82b2 100644 --- a/examples/nfc-anticol.c +++ b/examples/nfc-anticol.c @@ -175,7 +175,7 @@ main (int argc, char *argv[]) exit (EXIT_FAILURE); } - printf ("Connected to NFC reader: %s\n\n", pnd->acName); + printf ("Connected to NFC reader: %s\n\n", nfc_device_get_name (pnd)); // Send the 7 bits request command specified in ISO 14443A (0x26) if (!transmit_bits (abtReqa, 7)) { diff --git a/examples/nfc-dep-initiator.c b/examples/nfc-dep-initiator.c index 4de2da5..85d836f 100644 --- a/examples/nfc-dep-initiator.c +++ b/examples/nfc-dep-initiator.c @@ -77,7 +77,7 @@ main (int argc, const char *argv[]) printf("Unable to connect to NFC device.\n"); return EXIT_FAILURE; } - printf ("Connected to NFC device: %s\n", pnd->acName); + printf ("Connected to NFC device: %s\n", nfc_device_get_name (pnd)); signal (SIGINT, stop_dep_communication); diff --git a/examples/nfc-dep-target.c b/examples/nfc-dep-target.c index 7ed991e..1d59910 100644 --- a/examples/nfc-dep-target.c +++ b/examples/nfc-dep-target.c @@ -111,7 +111,7 @@ main (int argc, const char *argv[]) printf("Unable to connect to NFC device.\n"); return EXIT_FAILURE; } - printf ("Connected to NFC device: %s\n", pnd->acName); + printf ("Connected to NFC device: %s\n", nfc_device_get_name (pnd)); signal (SIGINT, stop_dep_communication); diff --git a/examples/nfc-emulate-forum-tag2.c b/examples/nfc-emulate-forum-tag2.c index 5289269..c682eb7 100644 --- a/examples/nfc-emulate-forum-tag2.c +++ b/examples/nfc-emulate-forum-tag2.c @@ -189,7 +189,7 @@ main(int argc, char *argv[]) exit (EXIT_FAILURE); } - printf ("Connected to NFC device: %s\n", pnd->acName); + printf ("Connected to NFC device: %s\n", nfc_device_get_name (pnd)); printf ("Emulating NDEF tag now, please touch it with a second NFC device\n"); if (nfc_emulate_target (pnd, &emulator) < 0) { diff --git a/examples/nfc-emulate-tag.c b/examples/nfc-emulate-tag.c index 814d0b5..8a459cc 100644 --- a/examples/nfc-emulate-tag.c +++ b/examples/nfc-emulate-tag.c @@ -191,7 +191,7 @@ main (int argc, char *argv[]) exit (EXIT_FAILURE); } - printf ("Connected to NFC device: %s\n", pnd->acName); + printf ("Connected to NFC device: %s\n", nfc_device_get_name (pnd)); // Notes for ISO14443-A emulated tags: // * Only short UIDs are supported diff --git a/examples/nfc-emulate-uid.c b/examples/nfc-emulate-uid.c index 5e762f1..d536339 100644 --- a/examples/nfc-emulate-uid.c +++ b/examples/nfc-emulate-uid.c @@ -134,7 +134,7 @@ main (int argc, char *argv[]) } printf ("\n"); - printf ("Connected to NFC device: %s\n", pnd->acName); + printf ("Connected to NFC device: %s\n", nfc_device_get_name (pnd)); printf ("[+] Try to break out the auto-emulation, this requires a second NFC device!\n"); printf ("[+] To do this, please send any command after the anti-collision\n"); printf ("[+] For example, send a RATS command or use the \"nfc-anticol\" or \"nfc-list\" tool.\n"); diff --git a/examples/nfc-poll.c b/examples/nfc-poll.c index c3b414c..fbb1893 100644 --- a/examples/nfc-poll.c +++ b/examples/nfc-poll.c @@ -101,19 +101,18 @@ main (int argc, const char *argv[]) nfc_initiator_init (pnd); - printf ("Connected to NFC reader: %s\n", pnd->acName); + printf ("Connected to NFC reader: %s\n", nfc_device_get_name (pnd)); printf ("NFC device will poll during %ld ms (%u pollings of %lu ms for %zd modulations)\n", (unsigned long) uiPollNr * szModulations * uiPeriod * 150, uiPollNr, (unsigned long) uiPeriod * 150, szModulations); - res = nfc_initiator_poll_target (pnd, nmModulations, szModulations, uiPollNr, uiPeriod, &nt); - if (res) { + if ((res = nfc_initiator_poll_target (pnd, nmModulations, szModulations, uiPollNr, uiPeriod, &nt)) < 0) { + nfc_perror (pnd, "nfc_initiator_poll_targets"); + nfc_disconnect (pnd); + exit (EXIT_FAILURE); + } + + if (res > 0) { print_nfc_target ( nt, verbose ); } else { - if (pnd->iLastError) { - nfc_perror (pnd, "nfc_initiator_poll_targets"); - nfc_disconnect (pnd); - exit (EXIT_FAILURE); - } else { - printf ("No target found.\n"); - } + printf ("No target found.\n"); } nfc_disconnect (pnd); exit (EXIT_SUCCESS); diff --git a/examples/nfc-relay.c b/examples/nfc-relay.c index aea0a56..b275628 100644 --- a/examples/nfc-relay.c +++ b/examples/nfc-relay.c @@ -125,7 +125,7 @@ main (int argc, char *argv[]) printf ("Hint: tag <---> initiator (relay) <---> target (relay) <---> original reader\n\n"); - printf ("Connected to the NFC emulator device: %s\n", pndTag->acName); + printf ("Connected to the NFC emulator device: %s\n", nfc_device_get_name (pndTag)); printf ("[+] Try to break out the auto-emulation, this requires a second reader!\n"); printf ("[+] To do this, please send any command after the anti-collision\n"); printf ("[+] For example, send a RATS command or use the \"nfc-anticol\" tool\n"); @@ -162,7 +162,7 @@ main (int argc, char *argv[]) // Try to open the NFC reader pndReader = nfc_connect (connstrings[1]); - printf ("Connected to the NFC reader device: %s", pndReader->acName); + printf ("Connected to the NFC reader device: %s", nfc_device_get_name (pndReader)); printf ("%s", "Configuring NFC reader settings..."); nfc_initiator_init (pndReader); if ((nfc_device_set_property_bool (pndReader, NP_HANDLE_CRC, false) < 0) || diff --git a/examples/pn53x-diagnose.c b/examples/pn53x-diagnose.c index 979e4b9..7f51700 100644 --- a/examples/pn53x-diagnose.c +++ b/examples/pn53x-diagnose.c @@ -86,7 +86,7 @@ main (int argc, const char *argv[]) return EXIT_FAILURE; } - printf ("NFC device [%s] connected.\n", pnd->acName); + printf ("NFC device [%s] connected.\n", nfc_device_get_name (pnd)); result = pn53x_transceive (pnd, pncmd_diagnose_communication_line_test, sizeof (pncmd_diagnose_communication_line_test), abtRx, &szRx, 0); if (result) { diff --git a/examples/pn53x-sam.c b/examples/pn53x-sam.c index ec5e4db..2b19db9 100644 --- a/examples/pn53x-sam.c +++ b/examples/pn53x-sam.c @@ -89,7 +89,7 @@ main (int argc, const char *argv[]) return EXIT_FAILURE; } - printf ("Connected to NFC device: %s\n", pnd->acName); + printf ("Connected to NFC device: %s\n", nfc_device_get_name (pnd)); // Print the example's menu printf ("\nSelect the communication mode:\n"); diff --git a/examples/pn53x-tamashell.c b/examples/pn53x-tamashell.c index 8fe5c61..abaae4a 100644 --- a/examples/pn53x-tamashell.c +++ b/examples/pn53x-tamashell.c @@ -94,7 +94,7 @@ int main(int argc, const char* argv[]) return EXIT_FAILURE; } - printf ("Connected to NFC reader: %s\n", pnd->acName); + printf ("Connected to NFC reader: %s\n", nfc_device_get_name (pnd)); nfc_initiator_init(pnd); char *cmd; diff --git a/include/nfc/nfc.h b/include/nfc/nfc.h index 64833de..4bca672 100644 --- a/include/nfc/nfc.h +++ b/include/nfc/nfc.h @@ -116,54 +116,9 @@ extern "C" { #define NFC_EOVFLOW -5 // Buffer overflow #define NFC_EOPABORTED -6 // Operation aborted (by user) #define NFC_ECHIP -7 // Device's internal chip error -#define NFC_EDEVNOTSUPP -8 // Operation not supported by device -#define NFC_ENOTIMPL -9 // Not (yet) implemented - - -/* PN53x specific errors */ -// TODO: Be not PN53x-specific here -#define ETIMEOUT 0x01 -#define ECRC 0x02 -#define EPARITY 0x03 -#define EBITCOUNT 0x04 -#define EFRAMING 0x05 -#define EBITCOLL 0x06 -#define ESMALLBUF 0x07 -#define EBUFOVF 0x09 -#define ERFTIMEOUT 0x0a -#define ERFPROTO 0x0b -#define EOVHEAT 0x0d -#define EINBUFOVF 0x0e -#define EINVPARAM 0x10 -#define EDEPUNKCMD 0x12 -#define EINVRXFRAM 0x13 -#define EMFAUTH 0x14 -#define ENSECNOTSUPP 0x18 // PN533 only -#define EBCC 0x23 -#define EDEPINVSTATE 0x25 -#define EOPNOTALL 0x26 -#define ECMD 0x27 -#define ETGREL 0x29 -#define ECID 0x2a -#define ECDISCARDED 0x2b -#define ENFCID3 0x2c -#define EOVCURRENT 0x2d -#define ENAD 0x2e - -/* PN53x framing-level errors */ -#define EFRAACKMISMATCH 0x0100 /* Unexpected data */ -#define EFRAISERRFRAME 0x0101 /* Error frame */ - -/* Communication-level errors */ -#define ECOMIO 0x1000 /* Input/output error */ -#define ECOMTIMEOUT 0x1001 /* Operation timeout */ - -/* Software level errors */ -#define ETGUIDNOTSUP 0xFF00 /* Target UID not supported */ -#define EOPABORT 0xFF01 /* Operation aborted */ -#define EINVALARG 0xFF02 /* Invalid argument */ -#define EDEVNOTSUP 0xFF03 /* Not supported by device */ -#define ENOTIMPL 0xFF04 /* Not (yet) implemented in libnfc */ +#define NFC_ERFTRANS -8 // Error while RF transmission +#define NFC_EDEVNOTSUPP -9 // Operation not supported by device +#define NFC_ENOTIMPL -10 // Not (yet) implemented # ifdef __cplusplus } diff --git a/libnfc/buses/uart_posix.c b/libnfc/buses/uart_posix.c index f427cbe..5b5db3b 100644 --- a/libnfc/buses/uart_posix.c +++ b/libnfc/buses/uart_posix.c @@ -284,31 +284,31 @@ select: // Read error if (res < 0) { log_put (LOG_CATEGORY, NFC_PRIORITY_TRACE, "%s", "RX error."); - return ECOMIO; + return NFC_EIO; } // Read time-out if (res == 0) { log_put (LOG_CATEGORY, NFC_PRIORITY_TRACE, "Timeout!"); - return ECOMTIMEOUT; + return NFC_ETIMEOUT; } if (FD_ISSET (iAbortFd, &rfds)) { // Abort requested log_put (LOG_CATEGORY, NFC_PRIORITY_TRACE, "Abort!"); close (iAbortFd); - return EOPABORT; + return NFC_EOPABORTED; } // Retrieve the count of the incoming bytes res = ioctl (UART_DATA(sp)->fd, FIONREAD, &available_bytes_count); if (res != 0) { - return ECOMIO; + return NFC_EIO; } // There is something available, read the data res = read (UART_DATA(sp)->fd, pbtRx + received_bytes_count, MIN(available_bytes_count, (expected_bytes_count - received_bytes_count))); // Stop if the OS has some troubles reading the data if (res <= 0) { - return ECOMIO; + return NFC_EIO; } received_bytes_count += res; @@ -330,7 +330,7 @@ uart_send (serial_port sp, const uint8_t *pbtTx, const size_t szTx, int timeout) if ((int) szTx == write (UART_DATA(sp)->fd, pbtTx, szTx)) return 0; else - return ECOMIO; + return NFC_EIO; } char ** diff --git a/libnfc/chips/pn53x-internal.h b/libnfc/chips/pn53x-internal.h index ceb8a80..7861091 100644 --- a/libnfc/chips/pn53x-internal.h +++ b/libnfc/chips/pn53x-internal.h @@ -299,6 +299,34 @@ typedef struct { #define PN53X_SFR_P7CFGB 0xFFF5 #define PN53X_SFR_P7 0xFFF7 +/* PN53x specific errors */ +#define ETIMEOUT 0x01 +#define ECRC 0x02 +#define EPARITY 0x03 +#define EBITCOUNT 0x04 +#define EFRAMING 0x05 +#define EBITCOLL 0x06 +#define ESMALLBUF 0x07 +#define EBUFOVF 0x09 +#define ERFTIMEOUT 0x0a +#define ERFPROTO 0x0b +#define EOVHEAT 0x0d +#define EINBUFOVF 0x0e +#define EINVPARAM 0x10 +#define EDEPUNKCMD 0x12 +#define EINVRXFRAM 0x13 +#define EMFAUTH 0x14 +#define ENSECNOTSUPP 0x18 // PN533 only +#define EBCC 0x23 +#define EDEPINVSTATE 0x25 +#define EOPNOTALL 0x26 +#define ECMD 0x27 +#define ETGREL 0x29 +#define ECID 0x2a +#define ECDISCARDED 0x2b +#define ENFCID3 0x2c +#define EOVCURRENT 0x2d +#define ENAD 0x2e #ifdef LOGGING static const pn53x_register pn53x_registers[] = { diff --git a/libnfc/chips/pn53x.c b/libnfc/chips/pn53x.c index b123d08..0bc3b0f 100644 --- a/libnfc/chips/pn53x.c +++ b/libnfc/chips/pn53x.c @@ -892,7 +892,8 @@ pn53x_initiator_init (struct nfc_device *pnd) // Configure the PN53X to be an Initiator or Reader/Writer if (!pn53x_write_register (pnd, PN53X_REG_CIU_Control, SYMBOL_INITIATOR, 0x10)) - return NFC_DEVICE_ERROR; + // FIXMES pn53x_write_register() should return integer + return NFC_EIO; CHIP_DATA (pnd)->operating_mode = INITIATOR; return NFC_SUCCESS; @@ -911,7 +912,7 @@ pn53x_initiator_select_passive_target_ext (struct nfc_device *pnd, if (nm.nmt == NMT_ISO14443BI || nm.nmt == NMT_ISO14443B2SR || nm.nmt == NMT_ISO14443B2CT) { if (CHIP_DATA(pnd)->type == RCS360) { // TODO add support for RC-S360, at the moment it refuses to send raw frames without a first select - pnd->last_error = ENOTIMPL; + pnd->last_error = NFC_ENOTIMPL; return false; } // No native support in InListPassiveTarget so we do discovery by hand @@ -986,7 +987,7 @@ pn53x_initiator_select_passive_target_ext (struct nfc_device *pnd, const pn53x_modulation pm = pn53x_nm_to_pm(nm); if (PM_UNDEFINED == pm) { - pnd->last_error = EINVALARG; + pnd->last_error = NFC_EINVARG; return false; } @@ -1174,7 +1175,7 @@ pn53x_initiator_transceive_bits (struct nfc_device *pnd, const uint8_t *pbtTx, c return true; } -bool +int pn53x_initiator_transceive_bytes (struct nfc_device *pnd, const uint8_t *pbtTx, const size_t szTx, uint8_t *pbtRx, size_t *pszRx, int timeout) { @@ -1184,7 +1185,7 @@ pn53x_initiator_transceive_bytes (struct nfc_device *pnd, const uint8_t *pbtTx, // We can not just send bytes without parity if while the PN53X expects we handled them if (!pnd->bPar) { pnd->last_error = NFC_EINVARG; - return false; + return pnd->last_error; } // Copy the data into the command frame @@ -1200,15 +1201,25 @@ pn53x_initiator_transceive_bytes (struct nfc_device *pnd, const uint8_t *pbtTx, } // To transfer command frames bytes we can not have any leading bits, reset this to zero - if (!pn53x_set_tx_bits (pnd, 0)) - return false; + if (!pn53x_set_tx_bits (pnd, 0)) { + pnd->last_error = NFC_EIO; // FIXME pn53x_set_tx_bits should return an integer + return pnd->last_error; + } // Send the frame to the PN53X chip and get the answer // We have to give the amount of bytes + (the two command bytes 0xD4, 0x42) uint8_t abtRx[PN53x_EXTENDED_FRAME__DATA_MAX_LEN]; size_t szRx = sizeof(abtRx); - if (!pn53x_transceive (pnd, abtCmd, szTx + szExtraTxLen, abtRx, &szRx, timeout)) - return false; + if (!pn53x_transceive (pnd, abtCmd, szTx + szExtraTxLen, abtRx, &szRx, timeout)) { + // FIXME pn53x_transceive should return an integer + if (CHIP_DATA (pnd)->last_status_byte == EINVRXFRAM) { + pnd->last_error = NFC_ERFTRANS; + return pnd->last_error; + } else { + pnd->last_error = NFC_EIO; + return pnd->last_error; + } + } if (pbtRx != NULL) { // Save the received byte count @@ -1217,8 +1228,8 @@ pn53x_initiator_transceive_bytes (struct nfc_device *pnd, const uint8_t *pbtTx, // Copy the received bytes memcpy (pbtRx, abtRx + 1, *pszRx); } - // Everything went successful - return true; + // Everything went successful, we return received bytes count + return *pszRx; } void __pn53x_init_timer(struct nfc_device *pnd, const uint32_t max_cycles) @@ -1413,13 +1424,13 @@ pn53x_initiator_transceive_bytes_timed (struct nfc_device *pnd, const uint8_t *p // We can not just send bytes without parity while the PN53X expects we handled them if (!pnd->bPar) { - CHIP_DATA(pnd)->last_status_byte = EINVALARG; + pnd->last_error = NFC_EINVARG; return false; } // Sorry, no easy framing support // TODO to be changed once we'll provide easy framing support from libnfc itself... if (pnd->bEasyFraming) { - CHIP_DATA(pnd)->last_status_byte = ENOTIMPL; + pnd->last_error = NFC_ENOTIMPL; return false; } @@ -1528,7 +1539,7 @@ pn53x_target_init (struct nfc_device *pnd, nfc_target *pnt, uint8_t *pbtRx, size case NMT_ISO14443A: ptm = PTM_PASSIVE_ONLY; if ((pnt->nti.nai.abtUid[0] != 0x08) || (pnt->nti.nai.szUidLen != 4)) { - CHIP_DATA(pnd)->last_status_byte = ETGUIDNOTSUP; + pnd->last_error = NFC_EINVARG; return false; } pn53x_set_parameters (pnd, PARAM_AUTO_ATR_RES, false); @@ -1557,7 +1568,7 @@ pn53x_target_init (struct nfc_device *pnd, nfc_target *pnt, uint8_t *pbtRx, size case NMT_ISO14443B2SR: case NMT_ISO14443B2CT: case NMT_JEWEL: - CHIP_DATA(pnd)->last_status_byte = EDEVNOTSUP; + pnd->last_error = NFC_EDEVNOTSUPP; return false; break; } @@ -1659,7 +1670,7 @@ pn53x_target_init (struct nfc_device *pnd, nfc_target *pnt, uint8_t *pbtRx, size case NMT_ISO14443B2SR: case NMT_ISO14443B2CT: case NMT_JEWEL: - CHIP_DATA(pnd)->last_status_byte = EDEVNOTSUP; + pnd->last_error = NFC_EDEVNOTSUPP; return false; break; } @@ -1792,7 +1803,7 @@ pn53x_target_receive_bytes (struct nfc_device *pnd, uint8_t *pbtRx, size_t *pszR break; } else { // TODO Support EasyFraming for other cases by software - CHIP_DATA(pnd)->last_status_byte = ENOTIMPL; + pnd->last_error = NFC_ENOTIMPL; return false; } } @@ -1883,7 +1894,7 @@ pn53x_target_send_bytes (struct nfc_device *pnd, const uint8_t *pbtTx, const siz break; } else { // TODO Support EasyFraming for other cases by software - CHIP_DATA(pnd)->last_status_byte = ENOTIMPL; + pnd->last_error = NFC_ENOTIMPL; return false; } } @@ -1920,7 +1931,6 @@ static struct sErrorMessage { { EBITCOLL, "Bit-collision" }, // An abnormal bit-collision has been detected during bit wise anti-collision at 106 kbps { ESMALLBUF, "Communication Buffer Too Small" }, // Communication buffer size insufficient { EBUFOVF, "Buffer Overflow" }, // RF Buffer overflow has been detected by the CIU (bit BufferOvfl of the register CIU_Error) - { ERFTIMEOUT, "RF Timeout" }, // In active communication mode, the RF field has not been switched on in time by the counterpart (as defined in NFCIP-1 standard) { ERFPROTO, "RF Protocol Error" }, // RF Protocol error (see PN53x manual) { EOVHEAT, "Chip Overheating" }, // Temperature error: the internal temperature sensor has detected overheating, and therefore has automatically switched off the antenna drivers { EINBUFOVF, "Internal Buffer overflow."}, // Internal buffer overflow @@ -1929,6 +1939,7 @@ static struct sErrorMessage { { ECMD, "Command Not Acceptable" }, // Command is not acceptable due to the current context { EOVCURRENT, "Over Current" }, /* DEP errors */ + { ERFTIMEOUT, "RF Timeout" }, // In active communication mode, the RF field has not been switched on in time by the counterpart (as defined in NFCIP-1 standard) { EDEPUNKCMD, "Unknown DEP Command" }, { EDEPINVSTATE, "Invalid DEP State" }, // DEP Protocol: Invalid device state, the system is in a state which does not allow the operation { ENAD, "NAD Missing in DEP Frame" }, @@ -1942,19 +1953,6 @@ static struct sErrorMessage { { ECID, "Card ID Mismatch" }, // ISO14443 type B: Card ID mismatch, meaning that the expected card has been exchanged with another one. { ECDISCARDED, "Card Discarded" }, // ISO/IEC14443 type B: the card previously activated has disappeared. { ENFCID3, "NFCID3 Mismatch" }, - /* Software level errors */ - { ETGUIDNOTSUP, "Target UID not supported" }, // In target mode, PN53x only support 4 bytes UID and the first byte must start with 0x08 - { EOPABORT, "Operation aborted" }, // Error used to catch a user-requested command abort - { EINVALARG, "Invalid argument" }, // Function called with invalid argument(s) - { ENOTIMPL, "Not (yet) implemented in library" }, - /* Framming-level errors */ - { EFRAACKMISMATCH, "Expected ACK frame" }, - { EFRAISERRFRAME, "Received an error frame" }, - /* Communication-level errors */ - { ECOMIO, "Input/output error" }, // Communication I/O errors: connection broken, read/write failure, unreacheable device, etc. - { ECOMTIMEOUT, "Operation timed-out" }, // A timeout occured while reading device's reply - /* Device-level errors */ - { EDEVNOTSUP, "Operation not supported by device" } // Requested task can not be done by current device }; const char * @@ -2039,7 +2037,7 @@ pn53x_SAMConfiguration (struct nfc_device *pnd, const pn532_sam_mode ui8Mode, in if (CHIP_DATA(pnd)->type != PN532) { // This function is not supported by pn531 neither pn533 - CHIP_DATA(pnd)->last_status_byte = EDEVNOTSUP; + pnd->last_error = NFC_EDEVNOTSUPP; return false; } @@ -2054,7 +2052,7 @@ pn53x_SAMConfiguration (struct nfc_device *pnd, const pn532_sam_mode ui8Mode, in szCmd = 3; break; default: - CHIP_DATA(pnd)->last_status_byte = EINVALARG; + pnd->last_error = NFC_EINVARG; return false; } return (pn53x_transceive (pnd, abtCmd, szCmd, NULL, NULL, timeout)); @@ -2101,14 +2099,14 @@ pn53x_InListPassiveTarget (struct nfc_device *pnd, case PM_ISO14443B_106: if (!(pnd->btSupportByte & SUPPORT_ISO14443B)) { // Eg. Some PN532 doesn't support type B! - CHIP_DATA(pnd)->last_status_byte = EDEVNOTSUP; + pnd->last_error = NFC_EDEVNOTSUPP; return false; } break; case PM_JEWEL_106: if(CHIP_DATA(pnd)->type == PN531) { // These modulations are not supported by pn531 - CHIP_DATA(pnd)->last_status_byte = EDEVNOTSUP; + pnd->last_error = NFC_EDEVNOTSUPP; return false; } break; @@ -2117,12 +2115,12 @@ pn53x_InListPassiveTarget (struct nfc_device *pnd, case PM_ISO14443B_847: if((CHIP_DATA(pnd)->type != PN533) || (!(pnd->btSupportByte & SUPPORT_ISO14443B))) { // These modulations are not supported by pn531 neither pn532 - CHIP_DATA(pnd)->last_status_byte = EDEVNOTSUP; + pnd->last_error = NFC_EDEVNOTSUPP; return false; } break; default: - CHIP_DATA(pnd)->last_status_byte = EINVALARG; + pnd->last_error = NFC_EINVARG; return false; } abtCmd[2] = pmInitModulation; // BrTy, the type of init modulation used for polling a passive tag @@ -2186,7 +2184,7 @@ pn53x_InAutoPoll (struct nfc_device *pnd, { if (CHIP_DATA(pnd)->type != PN532) { // This function is not supported by pn531 neither pn533 - CHIP_DATA(pnd)->last_status_byte = EDEVNOTSUP; + pnd->last_error = NFC_EDEVNOTSUPP; return false; } @@ -2268,7 +2266,7 @@ pn53x_InJumpForDEP (struct nfc_device *pnd, break; case NBR_847: case NBR_UNDEFINED: - CHIP_DATA(pnd)->last_status_byte = EINVALARG; + pnd->last_error = NFC_EINVARG; return false; break; } @@ -2288,7 +2286,7 @@ pn53x_InJumpForDEP (struct nfc_device *pnd, break; case NBR_847: case NBR_UNDEFINED: - CHIP_DATA(pnd)->last_status_byte = EINVALARG; + pnd->last_error = NFC_EINVARG; return false; break; } @@ -2417,7 +2415,7 @@ pn53x_check_ack_frame (struct nfc_device *pnd, const uint8_t *pbtRxFrame, const return true; } } - CHIP_DATA(pnd)->last_status_byte = EFRAACKMISMATCH; + pnd->last_error = NFC_EIO; log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "Unexpected PN53x reply!"); return false; } @@ -2428,7 +2426,7 @@ pn53x_check_error_frame (struct nfc_device *pnd, const uint8_t *pbtRxFrame, cons if (szRxFrameLen >= sizeof (pn53x_error_frame)) { if (0 == memcmp (pbtRxFrame, pn53x_error_frame, sizeof (pn53x_error_frame))) { log_put (LOG_CATEGORY, NFC_PRIORITY_TRACE, "PN53x sent an error frame"); - CHIP_DATA(pnd)->last_status_byte = EFRAISERRFRAME; + pnd->last_error = NFC_EIO; return false; } } diff --git a/libnfc/chips/pn53x.h b/libnfc/chips/pn53x.h index 9c30d4c..91cb353 100644 --- a/libnfc/chips/pn53x.h +++ b/libnfc/chips/pn53x.h @@ -302,7 +302,7 @@ bool pn53x_initiator_select_dep_target (struct nfc_device *pnd, bool pn53x_initiator_transceive_bits (struct nfc_device *pnd, const uint8_t *pbtTx, const size_t szTxBits, const uint8_t *pbtTxPar, uint8_t *pbtRx, size_t *pszRxBits, uint8_t *pbtRxPar); -bool pn53x_initiator_transceive_bytes (struct nfc_device *pnd, const uint8_t *pbtTx, const size_t szTx, +int pn53x_initiator_transceive_bytes (struct nfc_device *pnd, const uint8_t *pbtTx, const size_t szTx, uint8_t *pbtRx, size_t *pszRx, int timeout); bool pn53x_initiator_transceive_bits_timed (struct nfc_device *pnd, const uint8_t *pbtTx, const size_t szTxBits, const uint8_t *pbtTxPar, uint8_t *pbtRx, size_t *pszRxBits, diff --git a/libnfc/drivers/arygon.c b/libnfc/drivers/arygon.c index 17d4ec9..602f144 100644 --- a/libnfc/drivers/arygon.c +++ b/libnfc/drivers/arygon.c @@ -380,32 +380,32 @@ arygon_tama_receive (nfc_device *pnd, uint8_t *pbtData, const size_t szDataLen, pnd->last_error = uart_receive (DRIVER_DATA (pnd)->port, abtRxBuf, 5, abort_p, timeout); - if (abort_p && (EOPABORT == pnd->last_error)) { + if (abort_p && (NFC_EOPABORTED == pnd->last_error)) { arygon_abort (pnd); /* last_error got reset by arygon_abort() */ - pnd->last_error = EOPABORT; - return -1; + pnd->last_error = NFC_EOPABORTED; + return pnd->last_error; } if (pnd->last_error != 0) { log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Unable to receive data. (RX)"); - return -1; + return pnd->last_error; } const uint8_t pn53x_preamble[3] = { 0x00, 0x00, 0xff }; if (0 != (memcmp (abtRxBuf, pn53x_preamble, 3))) { log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Frame preamble+start code mismatch"); - pnd->last_error = ECOMIO; - return -1; + pnd->last_error = NFC_EIO; + return pnd->last_error; } if ((0x01 == abtRxBuf[3]) && (0xff == abtRxBuf[4])) { // Error frame uart_receive (DRIVER_DATA (pnd)->port, abtRxBuf, 3, 0, timeout); log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Application level error detected"); - pnd->last_error = EFRAISERRFRAME; - return -1; + pnd->last_error = NFC_EIO; + return pnd->last_error; } else if ((0xff == abtRxBuf[3]) && (0xff == abtRxBuf[4])) { // Extended frame // ARYGON devices does not support extended frame sending @@ -415,8 +415,8 @@ arygon_tama_receive (nfc_device *pnd, uint8_t *pbtData, const size_t szDataLen, if (256 != (abtRxBuf[3] + abtRxBuf[4])) { // TODO: Retry log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Length checksum mismatch"); - pnd->last_error = ECOMIO; - return -1; + pnd->last_error = NFC_EIO; + return pnd->last_error; } // abtRxBuf[3] (LEN) include TFI + (CC+1) @@ -425,7 +425,7 @@ arygon_tama_receive (nfc_device *pnd, uint8_t *pbtData, const size_t szDataLen, if (len > szDataLen) { log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "Unable to receive data: buffer too small. (szDataLen: %zu, len: %zu)", szDataLen, len); - pnd->last_error = ECOMIO; + pnd->last_error = NFC_EIO; return -1; } @@ -438,13 +438,13 @@ arygon_tama_receive (nfc_device *pnd, uint8_t *pbtData, const size_t szDataLen, if (abtRxBuf[0] != 0xD5) { log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "TFI Mismatch"); - pnd->last_error = ECOMIO; + pnd->last_error = NFC_EIO; return -1; } if (abtRxBuf[1] != CHIP_DATA (pnd)->ui8LastCommand + 1) { log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Command Code verification failed"); - pnd->last_error = ECOMIO; + pnd->last_error = NFC_EIO; return -1; } @@ -470,13 +470,13 @@ arygon_tama_receive (nfc_device *pnd, uint8_t *pbtData, const size_t szDataLen, if (btDCS != abtRxBuf[0]) { log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Data checksum mismatch"); - pnd->last_error = ECOMIO; + pnd->last_error = NFC_EIO; return -1; } if (0x00 != abtRxBuf[1]) { log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Frame postamble mismatch"); - pnd->last_error = ECOMIO; + pnd->last_error = NFC_EIO; return -1; } // The PN53x command is done and we successfully received the reply diff --git a/libnfc/drivers/pn532_uart.c b/libnfc/drivers/pn532_uart.c index 2078a6a..efbd5df 100644 --- a/libnfc/drivers/pn532_uart.c +++ b/libnfc/drivers/pn532_uart.c @@ -321,7 +321,7 @@ pn532_uart_send (nfc_device *pnd, const uint8_t *pbtData, const size_t szData, i size_t szFrame = 0; if (!pn53x_build_frame (abtFrame, &szFrame, pbtData, szData)) { - pnd->last_error = EINVALARG; + pnd->last_error = NFC_EINVARG; return false; } @@ -363,20 +363,20 @@ pn532_uart_receive (nfc_device *pnd, uint8_t *pbtData, const size_t szDataLen, i pnd->last_error = uart_receive (DRIVER_DATA (pnd)->port, abtRxBuf, 5, abort_p, timeout); - if (abort_p && (EOPABORT == pnd->last_error)) { + if (abort_p && (NFC_EOPABORTED == pnd->last_error)) { pn532_uart_ack (pnd); - return -1; + return pnd->last_error; } if (pnd->last_error != 0) { log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Unable to receive data. (RX)"); - return -1; + return pnd->last_error; } const uint8_t pn53x_preamble[3] = { 0x00, 0x00, 0xff }; if (0 != (memcmp (abtRxBuf, pn53x_preamble, 3))) { log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Frame preamble+start code mismatch"); - pnd->last_error = ECOMIO; + pnd->last_error = NFC_EIO; return -1; } @@ -384,8 +384,8 @@ pn532_uart_receive (nfc_device *pnd, uint8_t *pbtData, const size_t szDataLen, i // Error frame uart_receive (DRIVER_DATA (pnd)->port, abtRxBuf, 3, 0, timeout); log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Application level error detected"); - pnd->last_error = EFRAISERRFRAME; - return -1; + pnd->last_error = NFC_EIO; + return pnd->last_error; } else if ((0xff == abtRxBuf[3]) && (0xff == abtRxBuf[4])) { // Extended frame pnd->last_error = uart_receive (DRIVER_DATA(pnd)->port, abtRxBuf, 3, 0, timeout); @@ -396,7 +396,7 @@ pn532_uart_receive (nfc_device *pnd, uint8_t *pbtData, const size_t szDataLen, i if (((abtRxBuf[0] + abtRxBuf[1] + abtRxBuf[2]) % 256) != 0) { // TODO: Retry log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Length checksum mismatch"); - pnd->last_error = ECOMIO; + pnd->last_error = NFC_EIO; return -1; } } else { @@ -404,7 +404,7 @@ pn532_uart_receive (nfc_device *pnd, uint8_t *pbtData, const size_t szDataLen, i if (256 != (abtRxBuf[3] + abtRxBuf[4])) { // TODO: Retry log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Length checksum mismatch"); - pnd->last_error = ECOMIO; + pnd->last_error = NFC_EIO; return -1; } @@ -414,7 +414,7 @@ pn532_uart_receive (nfc_device *pnd, uint8_t *pbtData, const size_t szDataLen, i if (len > szDataLen) { log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "Unable to receive data: buffer too small. (szDataLen: %zu, len: %zu)", szDataLen, len); - pnd->last_error = ECOMIO; + pnd->last_error = NFC_EIO; return -1; } @@ -427,13 +427,13 @@ pn532_uart_receive (nfc_device *pnd, uint8_t *pbtData, const size_t szDataLen, i if (abtRxBuf[0] != 0xD5) { log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "TFI Mismatch"); - pnd->last_error = ECOMIO; + pnd->last_error = NFC_EIO; return -1; } if (abtRxBuf[1] != CHIP_DATA (pnd)->ui8LastCommand + 1) { log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Command Code verification failed"); - pnd->last_error = ECOMIO; + pnd->last_error = NFC_EIO; return -1; } @@ -459,13 +459,13 @@ pn532_uart_receive (nfc_device *pnd, uint8_t *pbtData, const size_t szDataLen, i if (btDCS != abtRxBuf[0]) { log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Data checksum mismatch"); - pnd->last_error = ECOMIO; + pnd->last_error = NFC_EIO; return -1; } if (0x00 != abtRxBuf[1]) { log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Frame postamble mismatch"); - pnd->last_error = ECOMIO; + pnd->last_error = NFC_EIO; return -1; } // The PN53x command is done and we successfully received the reply diff --git a/libnfc/drivers/pn53x_usb.c b/libnfc/drivers/pn53x_usb.c index c2e6349..b5b0ccf 100644 --- a/libnfc/drivers/pn53x_usb.c +++ b/libnfc/drivers/pn53x_usb.c @@ -516,14 +516,14 @@ pn53x_usb_send (nfc_device *pnd, const uint8_t *pbtData, const size_t szData, co int res = pn53x_usb_bulk_write (DRIVER_DATA (pnd), abtFrame, szFrame, timeout); if (res < 0) { - pnd->last_error = ECOMIO; + pnd->last_error = NFC_EIO; return false; } uint8_t abtRxBuf[PN53X_USB_BUFFER_LEN]; res = pn53x_usb_bulk_read (DRIVER_DATA (pnd), abtRxBuf, sizeof (abtRxBuf), timeout); if (res < 0) { - pnd->last_error = ECOMIO; + pnd->last_error = NFC_EIO; // try to interrupt current device state pn53x_usb_ack(pnd); return false; @@ -541,7 +541,7 @@ pn53x_usb_send (nfc_device *pnd, const uint8_t *pbtData, const size_t szData, co // FIXME Sony reader is also affected by this bug but NACK is not supported int res = pn53x_usb_bulk_write (DRIVER_DATA (pnd), (uint8_t *)pn53x_nack_frame, sizeof(pn53x_nack_frame), timeout); if (res < 0) { - pnd->last_error = ECOMIO; + pnd->last_error = NFC_EIO; // try to interrupt current device state pn53x_usb_ack(pnd); return false; @@ -574,7 +574,7 @@ read: // A user-provided timeout is set, we have to cut it in multiple chunk to be able to keep an nfc_abort_command() mecanism remaining_time -= USB_TIMEOUT_PER_PASS; if (remaining_time <= 0) { - pnd->last_error = ECOMTIMEOUT; + pnd->last_error = NFC_ETIMEOUT; return -1; } else { usb_timeout = MIN(remaining_time, USB_TIMEOUT_PER_PASS); @@ -587,7 +587,7 @@ read: if (DRIVER_DATA (pnd)->abort_flag) { DRIVER_DATA (pnd)->abort_flag = false; pn53x_usb_ack (pnd); - pnd->last_error = EOPABORT; + pnd->last_error = NFC_EOPABORTED; return -1; } else { goto read; @@ -595,7 +595,7 @@ read: } if (res < 0) { - pnd->last_error = ECOMIO; + pnd->last_error = NFC_EIO; // try to interrupt current device state pn53x_usb_ack(pnd); return -1; @@ -604,7 +604,7 @@ read: const uint8_t pn53x_preamble[3] = { 0x00, 0x00, 0xff }; if (0 != (memcmp (abtRxBuf, pn53x_preamble, 3))) { log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Frame preamble+start code mismatch"); - pnd->last_error = ECOMIO; + pnd->last_error = NFC_EIO; return -1; } offset += 3; @@ -612,7 +612,7 @@ read: if ((0x01 == abtRxBuf[offset]) && (0xff == abtRxBuf[offset + 1])) { // Error frame log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Application level error detected"); - pnd->last_error = EFRAISERRFRAME; + pnd->last_error = NFC_EIO; return -1; } else if ((0xff == abtRxBuf[offset]) && (0xff == abtRxBuf[offset + 1])) { // Extended frame @@ -623,7 +623,7 @@ read: if (((abtRxBuf[offset] + abtRxBuf[offset + 1] + abtRxBuf[offset + 2]) % 256) != 0) { // TODO: Retry log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Length checksum mismatch"); - pnd->last_error = ECOMIO; + pnd->last_error = NFC_EIO; return -1; } offset += 3; @@ -632,7 +632,7 @@ read: if (256 != (abtRxBuf[offset] + abtRxBuf[offset + 1])) { // TODO: Retry log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Length checksum mismatch"); - pnd->last_error = ECOMIO; + pnd->last_error = NFC_EIO; return -1; } @@ -643,21 +643,21 @@ read: if (len > szDataLen) { log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "Unable to receive data: buffer too small. (szDataLen: %zu, len: %zu)", szDataLen, len); - pnd->last_error = ECOMIO; + pnd->last_error = NFC_EIO; return -1; } // TFI + PD0 (CC+1) if (abtRxBuf[offset] != 0xD5) { log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "TFI Mismatch"); - pnd->last_error = ECOMIO; + pnd->last_error = NFC_EIO; return -1; } offset += 1; if (abtRxBuf[offset] != CHIP_DATA (pnd)->ui8LastCommand + 1) { log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Command Code verification failed"); - pnd->last_error = ECOMIO; + pnd->last_error = NFC_EIO; return -1; } offset += 1; @@ -673,14 +673,14 @@ read: if (btDCS != abtRxBuf[offset]) { log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Data checksum mismatch"); - pnd->last_error = ECOMIO; + pnd->last_error = NFC_EIO; return -1; } offset += 1; if (0x00 != abtRxBuf[offset]) { log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Frame postamble mismatch"); - pnd->last_error = ECOMIO; + pnd->last_error = NFC_EIO; return -1; } // The PN53x command is done and we successfully received the reply diff --git a/libnfc/nfc-internal.h b/libnfc/nfc-internal.h index b67828f..7a5320d 100644 --- a/libnfc/nfc-internal.h +++ b/libnfc/nfc-internal.h @@ -137,7 +137,7 @@ struct nfc_driver_t { bool (*initiator_poll_target) (struct nfc_device *pnd, const nfc_modulation * pnmModulations, const size_t szModulations, const uint8_t uiPollNr, const uint8_t btPeriod, nfc_target * pnt); bool (*initiator_select_dep_target) (struct nfc_device *pnd, const nfc_dep_mode ndm, const nfc_baud_rate nbr, const nfc_dep_info * pndiInitiator, nfc_target * pnt, const int timeout); bool (*initiator_deselect_target) (struct nfc_device *pnd); - bool (*initiator_transceive_bytes) (struct nfc_device *pnd, const uint8_t * pbtTx, const size_t szTx, uint8_t * pbtRx, size_t * pszRx, int timeout); + int (*initiator_transceive_bytes) (struct nfc_device *pnd, const uint8_t * pbtTx, const size_t szTx, uint8_t * pbtRx, size_t * pszRx, int timeout); bool (*initiator_transceive_bits) (struct nfc_device *pnd, const uint8_t * pbtTx, const size_t szTxBits, const uint8_t * pbtTxPar, uint8_t * pbtRx, size_t * pszRxBits, uint8_t * pbtRxPar); bool (*initiator_transceive_bytes_timed) (struct nfc_device *pnd, const uint8_t * pbtTx, const size_t szTx, uint8_t * pbtRx, size_t * pszRx, uint32_t * cycles); bool (*initiator_transceive_bits_timed) (struct nfc_device *pnd, const uint8_t * pbtTx, const size_t szTxBits, const uint8_t * pbtTxPar, uint8_t * pbtRx, size_t * pszRxBits, uint8_t * pbtRxPar, uint32_t * cycles); diff --git a/libnfc/nfc.c b/libnfc/nfc.c index 44d18e2..3957509 100644 --- a/libnfc/nfc.c +++ b/libnfc/nfc.c @@ -799,7 +799,7 @@ nfc_perror (const nfc_device *pnd, const char *pcString) * @return Returns a string with the device name */ const char * -nfc_device_name (nfc_device *pnd) +nfc_device_get_name (nfc_device *pnd) { return pnd->acName; } diff --git a/utils/mifare.c b/utils/mifare.c index 973fff9..4773e45 100644 --- a/utils/mifare.c +++ b/utils/mifare.c @@ -94,28 +94,32 @@ nfc_initiator_mifare_cmd (nfc_device *pnd, const mifare_cmd mc, const uint8_t ui if (szParamLen) memcpy (abtCmd + 2, (uint8_t *) pmp, szParamLen); - bEasyFraming = pnd->bEasyFraming; + // FIXME: Save and restore bEasyFraming + // bEasyFraming = nfc_device_get_property_bool (pnd, NP_EASY_FRAMING, &bEasyFraming); if (nfc_device_set_property_bool (pnd, NP_EASY_FRAMING, true) < 0) { nfc_perror (pnd, "nfc_device_set_property_bool"); return false; } // Fire the mifare command - if (!nfc_initiator_transceive_bytes (pnd, abtCmd, 2 + szParamLen, abtRx, &szRx, 0)) { - if (pnd->iLastError == EINVRXFRAM) { - // "Invalid received frame" AKA EINVRXFRAM, usual means we are + int res; + if ((res = nfc_initiator_transceive_bytes (pnd, abtCmd, 2 + szParamLen, abtRx, &szRx, -1)) < 0) { + if (res == NFC_ERFTRANS) { + // "Invalid received frame", usual means we are // authenticated on a sector but the requested MIFARE cmd (read, write) // is not permitted by current acces bytes; // So there is nothing to do here. } else { nfc_perror (pnd, "nfc_initiator_transceive_bytes"); } - nfc_device_set_property_bool (pnd, NP_EASY_FRAMING, bEasyFraming); + // XXX nfc_device_set_property_bool (pnd, NP_EASY_FRAMING, bEasyFraming); return false; } + /* XXX if (nfc_device_set_property_bool (pnd, NP_EASY_FRAMING, bEasyFraming) < 0) { nfc_perror (pnd, "nfc_device_set_property_bool"); return false; } + */ // When we have executed a read command, copy the received bytes into the param if (mc == MC_READ) { diff --git a/utils/nfc-emulate-forum-tag4.c b/utils/nfc-emulate-forum-tag4.c index b2f4bd1..8f633f7 100644 --- a/utils/nfc-emulate-forum-tag4.c +++ b/utils/nfc-emulate-forum-tag4.c @@ -356,7 +356,7 @@ main (int argc, char *argv[]) signal (SIGINT, stop_emulation); - printf ("Connected to NFC device: %s\n", pnd->acName); + printf ("Connected to NFC device: %s\n", nfc_device_get_name(pnd)); printf ("Emulating NDEF tag now, please touch it with a second NFC device\n"); if (0 != nfc_emulate_target (pnd, &emulator)) { // contains already nfc_target_init() call diff --git a/utils/nfc-list.c b/utils/nfc-list.c index 8172a4e..fd18fc1 100644 --- a/utils/nfc-list.c +++ b/utils/nfc-list.c @@ -117,7 +117,7 @@ main (int argc, const char *argv[]) } nfc_initiator_init (pnd); - printf ("Connected to NFC device: %s\n", pnd->acName); + printf ("Connected to NFC device: %s\n", nfc_device_get_name (pnd)); nfc_modulation nm; diff --git a/utils/nfc-mfclassic.c b/utils/nfc-mfclassic.c index 27a9aff..ec0dc95 100644 --- a/utils/nfc-mfclassic.c +++ b/utils/nfc-mfclassic.c @@ -554,7 +554,7 @@ main (int argc, const char *argv[]) // Disable ISO14443-4 switching in order to read devices that emulate Mifare Classic with ISO14443-4 compliance. nfc_device_set_property_bool (pnd, NP_AUTO_ISO14443_4, false); - printf ("Connected to NFC reader: %s\n", pnd->acName); + printf ("Connected to NFC reader: %s\n", nfc_device_get_name (pnd)); // Try to find a MIFARE Classic tag if (!nfc_initiator_select_passive_target (pnd, nmMifare, NULL, 0, &nt)) { diff --git a/utils/nfc-mfsetuid.c b/utils/nfc-mfsetuid.c index a64f497..9d18dc0 100644 --- a/utils/nfc-mfsetuid.c +++ b/utils/nfc-mfsetuid.c @@ -204,7 +204,7 @@ main (int argc, char *argv[]) exit (EXIT_FAILURE); } - printf ("Connected to NFC reader: %s\n\n", pnd->acName); + printf ("Connected to NFC reader: %s\n", nfc_device_get_name (pnd)); // Send the 7 bits request command specified in ISO 14443A (0x26) if (!transmit_bits (abtReqa, 7)) { diff --git a/utils/nfc-mfultralight.c b/utils/nfc-mfultralight.c index bfad59c..680406d 100644 --- a/utils/nfc-mfultralight.c +++ b/utils/nfc-mfultralight.c @@ -219,7 +219,7 @@ main (int argc, const char *argv[]) exit (EXIT_FAILURE); } - printf ("Connected to NFC device: %s\n", pnd->acName); + printf ("Connected to NFC device: %s\n", nfc_device_get_name (pnd)); // Try to find a MIFARE Ultralight tag if (!nfc_initiator_select_passive_target (pnd, nmMifare, NULL, 0, &nt)) { diff --git a/utils/nfc-read-forum-tag3.c b/utils/nfc-read-forum-tag3.c index d8f8c73..926c435 100644 --- a/utils/nfc-read-forum-tag3.c +++ b/utils/nfc-read-forum-tag3.c @@ -202,7 +202,7 @@ main(int argc, char *argv[]) exit (EXIT_FAILURE); } - fprintf (message_stream, "Connected to NFC device: %s\n", pnd->acName); + fprintf (message_stream, "Connected to NFC device: %s\n", nfc_device_get_name (pnd)); nfc_modulation nm = { .nmt = NMT_FELICA, diff --git a/utils/nfc-relay-picc.c b/utils/nfc-relay-picc.c index c2057b3..06c0433 100644 --- a/utils/nfc-relay-picc.c +++ b/utils/nfc-relay-picc.c @@ -227,7 +227,7 @@ main (int argc, char *argv[]) exit(EXIT_FAILURE); } - printf ("Connected to the NFC reader device: %s\n", pndInitiator->acName); + printf ("Connected to the NFC reader device: %s\n", nfc_device_get_name (pndInitiator)); if (nfc_initiator_init (pndInitiator) < 0) { printf ("Error: fail initializing initiator\n"); @@ -352,7 +352,7 @@ main (int argc, char *argv[]) return EXIT_FAILURE; } - printf ("Connected to the NFC emulator device: %s\n", pndTarget->acName); + printf ("Connected to the NFC emulator device: %s\n", nfc_device_get_name (pndTarget)); if (!nfc_target_init (pndTarget, &ntEmulatedTarget, abtCapdu, &szCapduLen)) { ERR ("%s", "Initialization of NFC emulator failed"); From 6eb2499aa8905ad5368d8d9dace7d17d91200439 Mon Sep 17 00:00:00 2001 From: Audrey Diacre Date: Mon, 19 Dec 2011 09:15:42 +0000 Subject: [PATCH 029/113] test/* uses the new nfc_device_get_name() function. --- test/test_dep_active.c | 6 +++--- test/test_dep_passive.c | 12 ++++++------ 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/test/test_dep_active.c b/test/test_dep_active.c index 0b1cc20..18a0454 100644 --- a/test/test_dep_active.c +++ b/test/test_dep_active.c @@ -60,7 +60,7 @@ target_thread (void *arg) nfc_device *device = ((struct thread_data *) arg)->device; cut_set_current_test_context (((struct thread_data *) arg)->cut_test_context); - printf ("=========== TARGET %s =========\n", nfc_device_name (device)); + printf ("=========== TARGET %s =========\n", nfc_device_get_name (device)); nfc_target nt = { .nm = { .nmt = NMT_DEP, @@ -115,7 +115,7 @@ initiator_thread (void *arg) * Wait some time for the other thread to initialise NFC device as target */ sleep (1); - printf ("=========== INITIATOR %s =========\n", nfc_device_name (device)); + printf ("=========== INITIATOR %s =========\n", nfc_device_get_name (device)); bool res = nfc_initiator_init (device); cut_assert_equal_int (0, res, cut_message ("Can't initialize NFC device as initiator: %s", nfc_strerror (device))); if (!res) { thread_res = -1; return (void*) thread_res; } @@ -123,7 +123,7 @@ initiator_thread (void *arg) nfc_target nt; // Active mode - printf ("=========== INITIATOR %s (Active mode / %s Kbps) =========\n", nfc_device_name (device), str_nfc_baud_rate(nbr)); + printf ("=========== INITIATOR %s (Active mode / %s Kbps) =========\n", nfc_device_get_name (device), str_nfc_baud_rate(nbr)); res = nfc_initiator_select_dep_target (device, NDM_ACTIVE, nbr, NULL, &nt, 1000); cut_assert_true (res, cut_message ("Can't select any DEP target: %s", nfc_strerror (device))); cut_assert_equal_int (NMT_DEP, nt.nm.nmt, cut_message ("Invalid target modulation")); diff --git a/test/test_dep_passive.c b/test/test_dep_passive.c index e17c1f6..162d582 100644 --- a/test/test_dep_passive.c +++ b/test/test_dep_passive.c @@ -58,7 +58,7 @@ target_thread (void *arg) nfc_device *device = ((struct thread_data *) arg)->device; cut_set_current_test_context (((struct thread_data *) arg)->cut_test_context); - printf ("=========== TARGET %s =========\n", nfc_device_name (device)); + printf ("=========== TARGET %s =========\n", nfc_device_get_name (device)); nfc_target nt = { .nm = { .nmt = NMT_DEP, @@ -147,7 +147,7 @@ initiator_thread (void *arg) * Wait some time for the other thread to initialise NFC device as target */ sleep (1); - printf ("=========== INITIATOR %s =========\n", nfc_device_name (device)); + printf ("=========== INITIATOR %s =========\n", nfc_device_get_name (device)); bool res = nfc_initiator_init (device); cut_assert_equal_int (0, res, cut_message ("Can't initialize NFC device as initiator: %s", nfc_strerror (device))); @@ -156,7 +156,7 @@ initiator_thread (void *arg) nfc_target nt; // Passive mode / 106Kbps - printf ("=========== INITIATOR %s (Passive mode / 106Kbps) =========\n", nfc_device_name (device)); + printf ("=========== INITIATOR %s (Passive mode / 106Kbps) =========\n", nfc_device_get_name (device)); res = nfc_initiator_select_dep_target (device, NDM_PASSIVE, NBR_106, NULL, &nt, 5000); cut_assert_true (res, cut_message ("Can't select any DEP target: %s", nfc_strerror (device))); cut_assert_equal_int (NMT_DEP, nt.nm.nmt, cut_message ("Invalid target modulation")); @@ -181,7 +181,7 @@ initiator_thread (void *arg) if (!res) { thread_res = -1; return (void*) thread_res; } // Passive mode / 212Kbps (second pass) - printf ("=========== INITIATOR %s (Passive mode / 212Kbps) =========\n", nfc_device_name (device)); + printf ("=========== INITIATOR %s (Passive mode / 212Kbps) =========\n", nfc_device_get_name (device)); res = nfc_initiator_select_dep_target (device, NDM_PASSIVE, NBR_212, NULL, &nt, 1000); cut_assert_true (res, cut_message ("Can't select any DEP target: %s", nfc_strerror (device))); cut_assert_equal_int (NMT_DEP, nt.nm.nmt, cut_message ("Invalid target modulation")); @@ -203,7 +203,7 @@ initiator_thread (void *arg) if (!res) { thread_res = -1; return (void*) thread_res; } // Passive mode / 212Kbps - printf ("=========== INITIATOR %s (Passive mode / 212Kbps, second pass) =========\n", nfc_device_name (device)); + printf ("=========== INITIATOR %s (Passive mode / 212Kbps, second pass) =========\n", nfc_device_get_name (device)); res = nfc_initiator_select_dep_target (device, NDM_PASSIVE, NBR_212, NULL, &nt, 1000); cut_assert_true (res, cut_message ("Can't select any DEP target: %s", nfc_strerror (device))); cut_assert_equal_int (NMT_DEP, nt.nm.nmt, cut_message ("Invalid target modulation")); @@ -225,7 +225,7 @@ initiator_thread (void *arg) if (!res) { thread_res = -1; return (void*) thread_res; } // Passive mode / 424Kbps - printf ("=========== INITIATOR %s (Passive mode / 424Kbps) =========\n", nfc_device_name (device)); + printf ("=========== INITIATOR %s (Passive mode / 424Kbps) =========\n", nfc_device_get_name (device)); res = nfc_initiator_select_dep_target (device, NDM_PASSIVE, NBR_424, NULL, &nt, 1000); cut_assert_true (res, cut_message ("Can't select any DEP target: %s", nfc_strerror (device))); cut_assert_equal_int (NMT_DEP, nt.nm.nmt, cut_message ("Invalid target modulation")); From 31f67be83eba63fba3feb766649f233bd7c1e00a Mon Sep 17 00:00:00 2001 From: Audrey Diacre Date: Mon, 19 Dec 2011 14:05:02 +0000 Subject: [PATCH 030/113] add some forgotten NFC_SUCCESS returns in pn53x_set_property_bool function and fix some return types in test/ --- libnfc/chips/pn53x.c | 16 ++++++++++++---- test/test_dep_active.c | 8 ++++---- test/test_dep_passive.c | 9 +++++---- 3 files changed, 21 insertions(+), 12 deletions(-) diff --git a/libnfc/chips/pn53x.c b/libnfc/chips/pn53x.c index 0bc3b0f..696f9b1 100644 --- a/libnfc/chips/pn53x.c +++ b/libnfc/chips/pn53x.c @@ -699,6 +699,7 @@ pn53x_set_property_bool (struct nfc_device *pnd, const nfc_property property, co if (!pn53x_write_register (pnd, PN53X_REG_CIU_RxMode, SYMBOL_RX_CRC_ENABLE, btValue)) return NFC_ECHIP; pnd->bCrc = bEnable; + return NFC_SUCCESS; break; case NP_HANDLE_PARITY: @@ -710,15 +711,18 @@ pn53x_set_property_bool (struct nfc_device *pnd, const nfc_property property, co if (!pn53x_write_register (pnd, PN53X_REG_CIU_ManualRCV, SYMBOL_PARITY_DISABLE, btValue)) return NFC_ECHIP; pnd->bPar = bEnable; + return NFC_SUCCESS; break; case NP_EASY_FRAMING: pnd->bEasyFraming = bEnable; + return NFC_SUCCESS; break; case NP_ACTIVATE_FIELD: { - return pn53x_RFConfiguration__RF_field (pnd, bEnable); + if (pn53x_RFConfiguration__RF_field (pnd, bEnable)) + return NFC_SUCCESS; } break; @@ -726,6 +730,7 @@ pn53x_set_property_bool (struct nfc_device *pnd, const nfc_property property, co btValue = (bEnable) ? SYMBOL_MF_CRYPTO1_ON : 0x00; if (!pn53x_write_register (pnd, PN53X_REG_CIU_Status2, SYMBOL_MF_CRYPTO1_ON, btValue)) return NFC_ECHIP; + return NFC_SUCCESS; break; case NP_INFINITE_SELECT: @@ -733,11 +738,12 @@ pn53x_set_property_bool (struct nfc_device *pnd, const nfc_property property, co // TODO Made some research around this point: // timings could be tweak better than this, and maybe we can tweak timings // to "gain" a sort-of hardware polling (ie. like PN532 does) - return pn53x_RFConfiguration__MaxRetries (pnd, + if (pn53x_RFConfiguration__MaxRetries (pnd, (bEnable) ? 0xff : 0x00, // MxRtyATR, default: active = 0xff, passive = 0x02 (bEnable) ? 0xff : 0x00, // MxRtyPSL, default: 0x01 (bEnable) ? 0xff : 0x02 // MxRtyPassiveActivation, default: 0xff (0x00 leads to problems with PN531) - ); + )) + return NFC_SUCCESS; } break; @@ -745,6 +751,7 @@ pn53x_set_property_bool (struct nfc_device *pnd, const nfc_property property, co btValue = (bEnable) ? SYMBOL_RX_NO_ERROR : 0x00; if (!pn53x_write_register (pnd, PN53X_REG_CIU_RxMode, SYMBOL_RX_NO_ERROR, btValue)) return NFC_ECHIP; + return NFC_SUCCESS; break; case NP_ACCEPT_MULTIPLE_FRAMES: @@ -759,7 +766,8 @@ pn53x_set_property_bool (struct nfc_device *pnd, const nfc_property property, co // Nothing to do return NFC_SUCCESS; pnd->bAutoIso14443_4 = bEnable; - return pn53x_set_parameters (pnd, PARAM_AUTO_RATS, bEnable); + if (pn53x_set_parameters (pnd, PARAM_AUTO_RATS, bEnable)) + return NFC_SUCCESS; break; case NP_FORCE_ISO14443_A: diff --git a/test/test_dep_active.c b/test/test_dep_active.c index 18a0454..b9101e8 100644 --- a/test/test_dep_active.c +++ b/test/test_dep_active.c @@ -116,15 +116,15 @@ initiator_thread (void *arg) */ sleep (1); printf ("=========== INITIATOR %s =========\n", nfc_device_get_name (device)); - bool res = nfc_initiator_init (device); - cut_assert_equal_int (0, res, cut_message ("Can't initialize NFC device as initiator: %s", nfc_strerror (device))); - if (!res) { thread_res = -1; return (void*) thread_res; } + int ires = nfc_initiator_init (device); + cut_assert_equal_int (0, ires, cut_message ("Can't initialize NFC device as initiator: %s", nfc_strerror (device))); + if (ires < 0) { thread_res = -1; return (void*) thread_res; } nfc_target nt; // Active mode printf ("=========== INITIATOR %s (Active mode / %s Kbps) =========\n", nfc_device_get_name (device), str_nfc_baud_rate(nbr)); - res = nfc_initiator_select_dep_target (device, NDM_ACTIVE, nbr, NULL, &nt, 1000); + bool res = nfc_initiator_select_dep_target (device, NDM_ACTIVE, nbr, NULL, &nt, 1000); cut_assert_true (res, cut_message ("Can't select any DEP target: %s", nfc_strerror (device))); cut_assert_equal_int (NMT_DEP, nt.nm.nmt, cut_message ("Invalid target modulation")); cut_assert_equal_int (nbr, nt.nm.nbr, cut_message ("Invalid target baud rate")); diff --git a/test/test_dep_passive.c b/test/test_dep_passive.c index 162d582..a071517 100644 --- a/test/test_dep_passive.c +++ b/test/test_dep_passive.c @@ -149,15 +149,16 @@ initiator_thread (void *arg) sleep (1); printf ("=========== INITIATOR %s =========\n", nfc_device_get_name (device)); - bool res = nfc_initiator_init (device); - cut_assert_equal_int (0, res, cut_message ("Can't initialize NFC device as initiator: %s", nfc_strerror (device))); - if (!res) { thread_res = -1; return (void*) thread_res; } + int ires = nfc_initiator_init (device); + printf ("IRES: %d\n", ires); + cut_assert_equal_int (0, ires, cut_message ("Can't initialize NFC device as initiator: %s", nfc_strerror (device))); + if (ires < 0) { thread_res = -1; return (void*) thread_res; } nfc_target nt; // Passive mode / 106Kbps printf ("=========== INITIATOR %s (Passive mode / 106Kbps) =========\n", nfc_device_get_name (device)); - res = nfc_initiator_select_dep_target (device, NDM_PASSIVE, NBR_106, NULL, &nt, 5000); + bool res = nfc_initiator_select_dep_target (device, NDM_PASSIVE, NBR_106, NULL, &nt, 5000); cut_assert_true (res, cut_message ("Can't select any DEP target: %s", nfc_strerror (device))); cut_assert_equal_int (NMT_DEP, nt.nm.nmt, cut_message ("Invalid target modulation")); cut_assert_equal_int (NBR_106, nt.nm.nbr, cut_message ("Invalid target baud rate")); From 145cc4b2ad349c0c9d42655b355f3d6259339c62 Mon Sep 17 00:00:00 2001 From: Audrey Diacre Date: Mon, 19 Dec 2011 14:37:22 +0000 Subject: [PATCH 031/113] nfc_initiator_transceive_bytesfunction now returns libnfc error code on failure and received bytes count on success. --- examples/nfc-anticol.c | 2 +- examples/nfc-dep-initiator.c | 2 +- include/nfc/nfc.h | 2 +- libnfc/chips/pn53x.c | 12 ++++++------ libnfc/nfc.c | 4 ++-- 5 files changed, 11 insertions(+), 11 deletions(-) diff --git a/examples/nfc-anticol.c b/examples/nfc-anticol.c index b5d82b2..6bcc4ed 100644 --- a/examples/nfc-anticol.c +++ b/examples/nfc-anticol.c @@ -105,7 +105,7 @@ transmit_bytes (const uint8_t *pbtTx, const size_t szTx) print_hex (pbtTx, szTx); } // Transmit the command bytes - if (!nfc_initiator_transceive_bytes (pnd, pbtTx, szTx, abtRx, &szRx, 0)) + if (nfc_initiator_transceive_bytes (pnd, pbtTx, szTx, abtRx, &szRx, 0) < 0) return false; // Show received answer diff --git a/examples/nfc-dep-initiator.c b/examples/nfc-dep-initiator.c index 85d836f..fd3d212 100644 --- a/examples/nfc-dep-initiator.c +++ b/examples/nfc-dep-initiator.c @@ -93,7 +93,7 @@ main (int argc, const char *argv[]) print_nfc_target (nt, false); printf ("Sending: %s\n", abtTx); - if (!nfc_initiator_transceive_bytes (pnd, abtTx, sizeof(abtTx), abtRx, &szRx, 0)) { + if (nfc_initiator_transceive_bytes (pnd, abtTx, sizeof(abtTx), abtRx, &szRx, 0) < 0) { nfc_perror(pnd, "nfc_initiator_transceive_bytes"); goto error; } diff --git a/include/nfc/nfc.h b/include/nfc/nfc.h index 4bca672..1fb5ca9 100644 --- a/include/nfc/nfc.h +++ b/include/nfc/nfc.h @@ -77,7 +77,7 @@ extern "C" { NFC_EXPORT bool nfc_initiator_poll_target (nfc_device *pnd, const nfc_modulation *pnmTargetTypes, const size_t szTargetTypes, const uint8_t uiPollNr, const uint8_t uiPeriod, nfc_target *pnt); NFC_EXPORT bool nfc_initiator_select_dep_target (nfc_device *pnd, const nfc_dep_mode ndm, const nfc_baud_rate nbr, const nfc_dep_info *pndiInitiator, nfc_target *pnt, const int timeout); NFC_EXPORT bool nfc_initiator_deselect_target (nfc_device *pnd); - NFC_EXPORT bool nfc_initiator_transceive_bytes (nfc_device *pnd, const uint8_t *pbtTx, const size_t szTx, uint8_t *pbtRx, size_t *pszRx, int timeout); + NFC_EXPORT int nfc_initiator_transceive_bytes (nfc_device *pnd, const uint8_t *pbtTx, const size_t szTx, uint8_t *pbtRx, size_t *pszRx, int timeout); NFC_EXPORT bool nfc_initiator_transceive_bits (nfc_device *pnd, const uint8_t *pbtTx, const size_t szTxBits, const uint8_t *pbtTxPar, uint8_t *pbtRx, size_t *pszRxBits, uint8_t *pbtRxPar); NFC_EXPORT bool nfc_initiator_transceive_bytes_timed (nfc_device *pnd, const uint8_t *pbtTx, const size_t szTx, uint8_t *pbtRx, size_t *pszRx, uint32_t *cycles); NFC_EXPORT bool nfc_initiator_transceive_bits_timed (nfc_device *pnd, const uint8_t *pbtTx, const size_t szTxBits, const uint8_t *pbtTxPar, uint8_t *pbtRx, size_t *pszRxBits, uint8_t *pbtRxPar, uint32_t *cycles); diff --git a/libnfc/chips/pn53x.c b/libnfc/chips/pn53x.c index 696f9b1..34933b0 100644 --- a/libnfc/chips/pn53x.c +++ b/libnfc/chips/pn53x.c @@ -943,11 +943,11 @@ pn53x_initiator_select_passive_target_ext (struct nfc_device *pnd, uint8_t abtRx[1]; size_t szRxLen = 1; // Getting random Chip_ID - if (!pn53x_initiator_transceive_bytes (pnd, abtInitiate, szInitiateLen, abtRx, &szRxLen, timeout)) { + if (pn53x_initiator_transceive_bytes (pnd, abtInitiate, szInitiateLen, abtRx, &szRxLen, timeout) < 0) { return false; } abtSelect[1] = abtRx[0]; - if (!pn53x_initiator_transceive_bytes (pnd, abtSelect, szSelectLen, abtRx, &szRxLen, timeout)) { + if (pn53x_initiator_transceive_bytes (pnd, abtSelect, szSelectLen, abtRx, &szRxLen, timeout) < 0) { return false; } } @@ -956,11 +956,11 @@ pn53x_initiator_select_passive_target_ext (struct nfc_device *pnd, uint8_t abtReqt[]="\x10"; size_t szReqtLen = 1; // Getting product code / fab code & store it in output buffer after the serial nr we'll obtain later - if (!pn53x_initiator_transceive_bytes (pnd, abtReqt, szReqtLen, abtTargetsData+2, &szTargetsData, timeout) || szTargetsData != 2) { + if ((pn53x_initiator_transceive_bytes (pnd, abtReqt, szReqtLen, abtTargetsData+2, &szTargetsData, timeout) < 0) || szTargetsData != 2) { return false; } } - if (!pn53x_initiator_transceive_bytes (pnd, pbtInitData, szInitData, abtTargetsData, &szTargetsData, timeout)) { + if (pn53x_initiator_transceive_bytes (pnd, pbtInitData, szInitData, abtTargetsData, &szTargetsData, timeout) < 0) { return false; } if (nm.nmt == NMT_ISO14443B2CT) { @@ -968,7 +968,7 @@ pn53x_initiator_select_passive_target_ext (struct nfc_device *pnd, return false; uint8_t abtRead[]="\xC4"; // Reading UID_MSB (Read address 4) size_t szReadLen = 1; - if (!pn53x_initiator_transceive_bytes (pnd, abtRead, szReadLen, abtTargetsData+4, &szTargetsData, timeout) || szTargetsData != 2) { + if ((pn53x_initiator_transceive_bytes (pnd, abtRead, szReadLen, abtTargetsData+4, &szTargetsData, timeout) < 0) || szTargetsData != 2) { return false; } szTargetsData = 6; // u16 UID_LSB, u8 prod code, u8 fab code, u16 UID_MSB @@ -986,7 +986,7 @@ pn53x_initiator_select_passive_target_ext (struct nfc_device *pnd, size_t szAttribLen = sizeof(abtAttrib); memcpy(abtAttrib, abtTargetsData, szAttribLen); abtAttrib[1] = 0x0f; // ATTRIB - if (!pn53x_initiator_transceive_bytes (pnd, abtAttrib, szAttribLen, NULL, NULL, timeout)) { + if (pn53x_initiator_transceive_bytes (pnd, abtAttrib, szAttribLen, NULL, NULL, timeout) < 0) { return false; } } diff --git a/libnfc/nfc.c b/libnfc/nfc.c index 3957509..6c5e0fc 100644 --- a/libnfc/nfc.c +++ b/libnfc/nfc.c @@ -474,7 +474,7 @@ nfc_initiator_deselect_target (nfc_device *pnd) /** * @brief Send data to target then retrieve data from target - * @return Returns \c true if action was successfully performed; otherwise returns \c false. + * @return Returns received bytes count on success, otherwise returns libnfc's error code * * @param pbtTx contains a byte array of the frame that needs to be transmitted. * @param szTx contains the length in bytes. @@ -495,7 +495,7 @@ nfc_initiator_deselect_target (nfc_device *pnd) * * @warning The configuration option \a NP_HANDLE_PARITY must be set to \c true (the default value). */ -bool +int nfc_initiator_transceive_bytes (nfc_device *pnd, const uint8_t *pbtTx, const size_t szTx, uint8_t *pbtRx, size_t *pszRx, int timeout) { From ba2a7cfe2e75ae36e26be9af52dc8c483a24c872 Mon Sep 17 00:00:00 2001 From: Audrey Diacre Date: Mon, 19 Dec 2011 15:35:37 +0000 Subject: [PATCH 032/113] nfc_target_init() function returns now 0 on succes and libnfc error code on failure. --- examples/nfc-dep-target.c | 2 +- examples/nfc-emulate-tag.c | 2 +- examples/nfc-emulate-uid.c | 2 +- examples/nfc-relay.c | 2 +- examples/pn53x-sam.c | 2 +- include/nfc/nfc.h | 2 +- libnfc/chips/pn53x.c | 14 +++++++------- libnfc/chips/pn53x.h | 2 +- libnfc/nfc-emulation.c | 2 +- libnfc/nfc-internal.h | 2 +- libnfc/nfc.c | 21 +++++++++++---------- test/test_dep_active.c | 8 ++++---- test/test_dep_passive.c | 9 ++++----- utils/nfc-relay-picc.c | 2 +- 14 files changed, 36 insertions(+), 36 deletions(-) diff --git a/examples/nfc-dep-target.c b/examples/nfc-dep-target.c index 1d59910..28a7fd3 100644 --- a/examples/nfc-dep-target.c +++ b/examples/nfc-dep-target.c @@ -119,7 +119,7 @@ main (int argc, const char *argv[]) print_nfc_target (nt, false); printf ("Waiting for initiator request...\n"); - if(!nfc_target_init (pnd, &nt, abtRx, &szRx)) { + if(nfc_target_init (pnd, &nt, abtRx, &szRx) < 0) { nfc_perror(pnd, "nfc_target_init"); goto error; } diff --git a/examples/nfc-emulate-tag.c b/examples/nfc-emulate-tag.c index 8a459cc..8ef6db9 100644 --- a/examples/nfc-emulate-tag.c +++ b/examples/nfc-emulate-tag.c @@ -140,7 +140,7 @@ nfc_target_emulate_tag(nfc_device *pnd, nfc_target *pnt) uint8_t abtTx[MAX_FRAME_LEN]; bool loop = true; - if (!nfc_target_init (pnd, pnt, abtRx, &szRx)) { + if (nfc_target_init (pnd, pnt, abtRx, &szRx) < 0) { nfc_perror (pnd, "nfc_target_init"); return false; } diff --git a/examples/nfc-emulate-uid.c b/examples/nfc-emulate-uid.c index d536339..0835034 100644 --- a/examples/nfc-emulate-uid.c +++ b/examples/nfc-emulate-uid.c @@ -155,7 +155,7 @@ main (int argc, char *argv[]) }, }, }; - if (!nfc_target_init (pnd, &nt, abtRecv, &szRecvBits)) { + if (nfc_target_init (pnd, &nt, abtRecv, &szRecvBits) < 0) { nfc_perror (pnd, "nfc_target_init"); ERR ("Could not come out of auto-emulation, no command was received"); goto error; diff --git a/examples/nfc-relay.c b/examples/nfc-relay.c index b275628..7b3f803 100644 --- a/examples/nfc-relay.c +++ b/examples/nfc-relay.c @@ -146,7 +146,7 @@ main (int argc, char *argv[]) }, }; - if (!nfc_target_init (pndTag, &nt, abtReaderRx, &szReaderRxBits)) { + if (nfc_target_init (pndTag, &nt, abtReaderRx, &szReaderRxBits) < 0) { ERR ("%s", "Initialization of NFC emulator failed"); nfc_disconnect (pndTag); return EXIT_FAILURE; diff --git a/examples/pn53x-sam.c b/examples/pn53x-sam.c index 2b19db9..9899d6f 100644 --- a/examples/pn53x-sam.c +++ b/examples/pn53x-sam.c @@ -173,7 +173,7 @@ main (int argc, const char *argv[]) }; printf ("Now both, NFC device (configured as target) and SAM are readables from an external NFC initiator.\n"); printf ("Please note that NFC device (configured as target) stay in target mode until it receive RATS, ATR_REQ or proprietary command.\n"); - if (!nfc_target_init (pnd, &nt, abtRx, &szRx)) { + if (nfc_target_init (pnd, &nt, abtRx, &szRx) < 0) { nfc_perror(pnd, "nfc_target_init"); return EXIT_FAILURE; } diff --git a/include/nfc/nfc.h b/include/nfc/nfc.h index 1fb5ca9..4c51604 100644 --- a/include/nfc/nfc.h +++ b/include/nfc/nfc.h @@ -83,7 +83,7 @@ extern "C" { NFC_EXPORT bool nfc_initiator_transceive_bits_timed (nfc_device *pnd, const uint8_t *pbtTx, const size_t szTxBits, const uint8_t *pbtTxPar, uint8_t *pbtRx, size_t *pszRxBits, uint8_t *pbtRxPar, uint32_t *cycles); /* NFC target: act as tag (i.e. MIFARE Classic) or NFC target device. */ - NFC_EXPORT bool nfc_target_init (nfc_device *pnd, nfc_target *pnt, uint8_t *pbtRx, size_t *pszRx); + NFC_EXPORT int nfc_target_init (nfc_device *pnd, nfc_target *pnt, uint8_t *pbtRx, size_t *pszRx); NFC_EXPORT bool nfc_target_send_bytes (nfc_device *pnd, const uint8_t *pbtTx, const size_t szTx, int timeout); NFC_EXPORT bool nfc_target_receive_bytes (nfc_device *pnd, uint8_t *pbtRx, size_t *pszRx, int timeout); NFC_EXPORT bool nfc_target_send_bits (nfc_device *pnd, const uint8_t *pbtTx, const size_t szTxBits, const uint8_t *pbtTxPar); diff --git a/libnfc/chips/pn53x.c b/libnfc/chips/pn53x.c index 34933b0..6c56ec0 100644 --- a/libnfc/chips/pn53x.c +++ b/libnfc/chips/pn53x.c @@ -1534,7 +1534,7 @@ pn53x_initiator_deselect_target (struct nfc_device *pnd) #define SAK_ISO14443_4_COMPLIANT 0x20 #define SAK_ISO18092_COMPLIANT 0x40 -bool +int pn53x_target_init (struct nfc_device *pnd, nfc_target *pnt, uint8_t *pbtRx, size_t *pszRx) { pn53x_reset_settings(pnd); @@ -1548,7 +1548,7 @@ pn53x_target_init (struct nfc_device *pnd, nfc_target *pnt, uint8_t *pbtRx, size ptm = PTM_PASSIVE_ONLY; if ((pnt->nti.nai.abtUid[0] != 0x08) || (pnt->nti.nai.szUidLen != 4)) { pnd->last_error = NFC_EINVARG; - return false; + return pnd->last_error; } pn53x_set_parameters (pnd, PARAM_AUTO_ATR_RES, false); if (CHIP_DATA(pnd)->type == PN532) { // We have a PN532 @@ -1577,13 +1577,13 @@ pn53x_target_init (struct nfc_device *pnd, nfc_target *pnt, uint8_t *pbtRx, size case NMT_ISO14443B2CT: case NMT_JEWEL: pnd->last_error = NFC_EDEVNOTSUPP; - return false; + return pnd->last_error; break; } // Let the PN53X be activated by the RF level detector from power down mode if (!pn53x_write_register (pnd, PN53X_REG_CIU_TxAuto, SYMBOL_INITIAL_RF_ON, 0x04)) - return false; + return NFC_ECHIP; uint8_t abtMifareParams[6]; uint8_t *pbtMifareParams = NULL; @@ -1679,7 +1679,7 @@ pn53x_target_init (struct nfc_device *pnd, nfc_target *pnt, uint8_t *pbtRx, size case NMT_ISO14443B2CT: case NMT_JEWEL: pnd->last_error = NFC_EDEVNOTSUPP; - return false; + return pnd->last_error; break; } @@ -1688,7 +1688,7 @@ pn53x_target_init (struct nfc_device *pnd, nfc_target *pnt, uint8_t *pbtRx, size uint8_t btActivatedMode; if(!pn53x_TgInitAsTarget(pnd, ptm, pbtMifareParams, pbtTkt, szTkt, pbtFeliCaParams, pbtNFCID3t, pbtGBt, szGBt, pbtRx, pszRx, &btActivatedMode)) { - return false; + return NFC_ECHIP; } nfc_modulation nm = { @@ -1752,7 +1752,7 @@ pn53x_target_init (struct nfc_device *pnd, nfc_target *pnt, uint8_t *pbtRx, size } } - return true; + return NFC_SUCCESS; } bool diff --git a/libnfc/chips/pn53x.h b/libnfc/chips/pn53x.h index 91cb353..f453f9a 100644 --- a/libnfc/chips/pn53x.h +++ b/libnfc/chips/pn53x.h @@ -312,7 +312,7 @@ bool pn53x_initiator_transceive_bytes_timed (struct nfc_device *pnd, const ui bool pn53x_initiator_deselect_target (struct nfc_device *pnd); // NFC device as Target functions -bool pn53x_target_init (struct nfc_device *pnd, nfc_target *pnt, uint8_t *pbtRx, size_t *pszRx); +int pn53x_target_init (struct nfc_device *pnd, nfc_target *pnt, uint8_t *pbtRx, size_t *pszRx); bool pn53x_target_receive_bits (struct nfc_device *pnd, uint8_t *pbtRx, size_t *pszRxBits, uint8_t *pbtRxPar); bool pn53x_target_receive_bytes (struct nfc_device *pnd, uint8_t *pbtRx, size_t *pszRx, int timeout); bool pn53x_target_send_bits (struct nfc_device *pnd, const uint8_t *pbtTx, const size_t szTxBits, const uint8_t *pbtTxPar); diff --git a/libnfc/nfc-emulation.c b/libnfc/nfc-emulation.c index c8a7af9..592c555 100644 --- a/libnfc/nfc-emulation.c +++ b/libnfc/nfc-emulation.c @@ -35,7 +35,7 @@ nfc_emulate_target (nfc_device *pnd, struct nfc_emulator *emulator) uint8_t abtTx[ISO7816_SHORT_C_APDU_MAX_LEN]; int res = 0; - if (!nfc_target_init (pnd, emulator->target, abtRx, &szRx)) { + if (nfc_target_init (pnd, emulator->target, abtRx, &szRx) < 0) { return -1; } diff --git a/libnfc/nfc-internal.h b/libnfc/nfc-internal.h index 7a5320d..f5d054b 100644 --- a/libnfc/nfc-internal.h +++ b/libnfc/nfc-internal.h @@ -142,7 +142,7 @@ struct nfc_driver_t { bool (*initiator_transceive_bytes_timed) (struct nfc_device *pnd, const uint8_t * pbtTx, const size_t szTx, uint8_t * pbtRx, size_t * pszRx, uint32_t * cycles); bool (*initiator_transceive_bits_timed) (struct nfc_device *pnd, const uint8_t * pbtTx, const size_t szTxBits, const uint8_t * pbtTxPar, uint8_t * pbtRx, size_t * pszRxBits, uint8_t * pbtRxPar, uint32_t * cycles); - bool (*target_init) (struct nfc_device *pnd, nfc_target * pnt, uint8_t * pbtRx, size_t * pszRx); + int (*target_init) (struct nfc_device *pnd, nfc_target * pnt, uint8_t * pbtRx, size_t * pszRx); bool (*target_send_bytes) (struct nfc_device *pnd, const uint8_t * pbtTx, const size_t szTx, int timeout); bool (*target_receive_bytes) (struct nfc_device *pnd, uint8_t * pbtRx, size_t * pszRx, int timeout); bool (*target_send_bits) (struct nfc_device *pnd, const uint8_t * pbtTx, const size_t szTxBits, const uint8_t * pbtTxPar); diff --git a/libnfc/nfc.c b/libnfc/nfc.c index 6c5e0fc..b92735b 100644 --- a/libnfc/nfc.c +++ b/libnfc/nfc.c @@ -601,7 +601,7 @@ nfc_initiator_transceive_bits_timed (nfc_device *pnd, const uint8_t *pbtTx, cons /** * @brief Initialize NFC device as an emulated tag - * @return Returns \c true if action was successfully performed; otherwise returns \c false. + * @return Returns 0 on success, otherwise returns libnfc's error code * * @param pnd \a nfc_device struct pointer that represent currently used device * @param ntm target mode restriction that you want to emulate (eg. NTM_PASSIVE_ONLY) @@ -629,31 +629,32 @@ nfc_initiator_transceive_bits_timed (nfc_device *pnd, const uint8_t *pbtTx, cons * example would wake up the emulator. After this is received, the send and * receive functions can be used. */ -bool +int nfc_target_init (nfc_device *pnd, nfc_target *pnt, uint8_t *pbtRx, size_t * pszRx) { + int res = 0; // Disallow invalid frame - if (nfc_device_set_property_bool (pnd, NP_ACCEPT_INVALID_FRAMES, false) < 0) + if ((res = nfc_device_set_property_bool (pnd, NP_ACCEPT_INVALID_FRAMES, false)) < 0) return false; // Disallow multiple frames - if (nfc_device_set_property_bool (pnd, NP_ACCEPT_MULTIPLE_FRAMES, false) < 0) + if ((res = nfc_device_set_property_bool (pnd, NP_ACCEPT_MULTIPLE_FRAMES, false)) < 0) return false; // Make sure we reset the CRC and parity to chip handling. - if (nfc_device_set_property_bool (pnd, NP_HANDLE_CRC, true) < 0) + if ((res = nfc_device_set_property_bool (pnd, NP_HANDLE_CRC, true)) < 0) return false; - if (nfc_device_set_property_bool (pnd, NP_HANDLE_PARITY, true) < 0) + if ((res = nfc_device_set_property_bool (pnd, NP_HANDLE_PARITY, true)) < 0) return false; // Activate auto ISO14443-4 switching by default - if (nfc_device_set_property_bool (pnd, NP_AUTO_ISO14443_4, true) < 0) + if ((res = nfc_device_set_property_bool (pnd, NP_AUTO_ISO14443_4, true)) < 0) return false; // Activate "easy framing" feature by default - if (nfc_device_set_property_bool (pnd, NP_EASY_FRAMING, true) < 0) + if ((res = nfc_device_set_property_bool (pnd, NP_EASY_FRAMING, true)) < 0) return false; // Deactivate the CRYPTO1 cipher, it may could cause problems when still active - if (nfc_device_set_property_bool (pnd, NP_ACTIVATE_CRYPTO1, false) < 0) + if ((res = nfc_device_set_property_bool (pnd, NP_ACTIVATE_CRYPTO1, false)) < 0) return false; // Drop explicitely the field - if (nfc_device_set_property_bool (pnd, NP_ACTIVATE_FIELD, false) < 0) + if ((res = nfc_device_set_property_bool (pnd, NP_ACTIVATE_FIELD, false)) < 0) return false; HAL (target_init, pnd, pnt, pbtRx, pszRx); diff --git a/test/test_dep_active.c b/test/test_dep_active.c index b9101e8..a80eefa 100644 --- a/test/test_dep_active.c +++ b/test/test_dep_active.c @@ -84,11 +84,11 @@ target_thread (void *arg) uint8_t abtRx[1024]; size_t szRx = sizeof (abtRx); - bool res = nfc_target_init (device, &nt, abtRx, &szRx); - cut_assert_true (res, cut_message ("Can't initialize NFC device as target: %s", nfc_strerror (device))); - if (!res) { thread_res = -1; return (void*) thread_res; } + int ires = nfc_target_init (device, &nt, abtRx, &szRx); + cut_assert_equal_int (0, ires, cut_message ("Can't initialize NFC device as target: %s", nfc_strerror (device))); + if (ires < 0) { thread_res = -1; return (void*) thread_res; } - res = nfc_target_receive_bytes (device, abtRx, &szRx, 500); + bool res = nfc_target_receive_bytes (device, abtRx, &szRx, 500); cut_assert_true (res, cut_message ("Can't receive bytes from initiator: %s", nfc_strerror (device))); const uint8_t abtAttRx[] = "Hello DEP target!"; diff --git a/test/test_dep_passive.c b/test/test_dep_passive.c index a071517..cc80ece 100644 --- a/test/test_dep_passive.c +++ b/test/test_dep_passive.c @@ -82,13 +82,12 @@ target_thread (void *arg) uint8_t abtRx[1024]; size_t szRx = sizeof (abtRx); - bool res = nfc_target_init (device, &nt, abtRx, &szRx); - cut_assert_true (res, cut_message ("Can't initialize NFC device as target: %s", nfc_strerror (device))); - - if (!res) { thread_res = -1; return (void*) thread_res; } + int ires = nfc_target_init (device, &nt, abtRx, &szRx); + cut_assert_equal_int (0, ires, cut_message ("Can't initialize NFC device as target: %s", nfc_strerror (device))); + if (ires < 0) { thread_res = -1; return (void*) thread_res; } // First pass - res = nfc_target_receive_bytes (device, abtRx, &szRx, 500); + bool res = nfc_target_receive_bytes (device, abtRx, &szRx, 500); cut_assert_true (res, cut_message ("Can't receive bytes from initiator: %s", nfc_strerror (device))); const uint8_t abtAttRx[] = "Hello DEP target!"; diff --git a/utils/nfc-relay-picc.c b/utils/nfc-relay-picc.c index 06c0433..c150825 100644 --- a/utils/nfc-relay-picc.c +++ b/utils/nfc-relay-picc.c @@ -354,7 +354,7 @@ main (int argc, char *argv[]) printf ("Connected to the NFC emulator device: %s\n", nfc_device_get_name (pndTarget)); - if (!nfc_target_init (pndTarget, &ntEmulatedTarget, abtCapdu, &szCapduLen)) { + if (nfc_target_init (pndTarget, &ntEmulatedTarget, abtCapdu, &szCapduLen) < 0) { ERR ("%s", "Initialization of NFC emulator failed"); if (!target_only_mode) { nfc_disconnect (pndInitiator); From 103485518c9c906eccd6d368015e76ea3b8efb5d Mon Sep 17 00:00:00 2001 From: Audrey Diacre Date: Mon, 19 Dec 2011 16:27:50 +0000 Subject: [PATCH 033/113] nfc_initiator_select_passive_target() function returns now libnfc error code. --- examples/doc/quick_start_example1.c | 2 +- examples/pn53x-sam.c | 2 +- include/nfc/nfc.h | 2 +- libnfc/chips/pn53x.c | 51 +++++++++++++++-------------- libnfc/chips/pn53x.h | 2 +- libnfc/nfc-internal.h | 2 +- libnfc/nfc.c | 6 ++-- utils/nfc-mfclassic.c | 6 ++-- utils/nfc-mfultralight.c | 4 +-- utils/nfc-read-forum-tag3.c | 4 +-- utils/nfc-relay-picc.c | 2 +- 11 files changed, 42 insertions(+), 41 deletions(-) diff --git a/examples/doc/quick_start_example1.c b/examples/doc/quick_start_example1.c index 073fe02..65bc8e9 100644 --- a/examples/doc/quick_start_example1.c +++ b/examples/doc/quick_start_example1.c @@ -35,7 +35,7 @@ main (int argc, const char *argv[]) .nmt = NMT_ISO14443A, .nbr = NBR_106, }; - if (nfc_initiator_select_passive_target (pnd, nmMifare, NULL, 0, &nt)) { + if (nfc_initiator_select_passive_target (pnd, nmMifare, NULL, 0, &nt) == 0) { printf ("The following (NFC) ISO14443A tag was found:\n"); printf (" ATQA (SENS_RES): "); print_hex (nt.nti.nai.abtAtqa, 2); diff --git a/examples/pn53x-sam.c b/examples/pn53x-sam.c index 9899d6f..ed185b8 100644 --- a/examples/pn53x-sam.c +++ b/examples/pn53x-sam.c @@ -140,7 +140,7 @@ main (int argc, const char *argv[]) .nmt = NMT_ISO14443A, .nbr = NBR_106, }; - if (!nfc_initiator_select_passive_target (pnd, nmSAM, NULL, 0, &nt)) { + if (nfc_initiator_select_passive_target (pnd, nmSAM, NULL, 0, &nt) < 0) { nfc_perror (pnd, "nfc_initiator_select_passive_target"); ERR ("%s", "Reading of SAM info failed."); exit (EXIT_FAILURE); diff --git a/include/nfc/nfc.h b/include/nfc/nfc.h index 4c51604..61e7410 100644 --- a/include/nfc/nfc.h +++ b/include/nfc/nfc.h @@ -72,7 +72,7 @@ extern "C" { /* NFC initiator: act as "reader" */ NFC_EXPORT int nfc_initiator_init (nfc_device *pnd); - NFC_EXPORT bool nfc_initiator_select_passive_target (nfc_device *pnd, const nfc_modulation nm, const uint8_t *pbtInitData, const size_t szInitData, nfc_target *pnt); + NFC_EXPORT int nfc_initiator_select_passive_target (nfc_device *pnd, const nfc_modulation nm, const uint8_t *pbtInitData, const size_t szInitData, nfc_target *pnt); NFC_EXPORT int nfc_initiator_list_passive_targets (nfc_device *pnd, const nfc_modulation nm, nfc_target ant[], const size_t szTargets); NFC_EXPORT bool nfc_initiator_poll_target (nfc_device *pnd, const nfc_modulation *pnmTargetTypes, const size_t szTargetTypes, const uint8_t uiPollNr, const uint8_t uiPeriod, nfc_target *pnt); NFC_EXPORT bool nfc_initiator_select_dep_target (nfc_device *pnd, const nfc_dep_mode ndm, const nfc_baud_rate nbr, const nfc_dep_info *pndiInitiator, nfc_target *pnt, const int timeout); diff --git a/libnfc/chips/pn53x.c b/libnfc/chips/pn53x.c index 6c56ec0..d55ac2f 100644 --- a/libnfc/chips/pn53x.c +++ b/libnfc/chips/pn53x.c @@ -907,7 +907,7 @@ pn53x_initiator_init (struct nfc_device *pnd) return NFC_SUCCESS; } -bool +int pn53x_initiator_select_passive_target_ext (struct nfc_device *pnd, const nfc_modulation nm, const uint8_t *pbtInitData, const size_t szInitData, @@ -916,22 +916,23 @@ pn53x_initiator_select_passive_target_ext (struct nfc_device *pnd, { uint8_t abtTargetsData[PN53x_EXTENDED_FRAME__DATA_MAX_LEN]; size_t szTargetsData = sizeof(abtTargetsData); + int res = 0; if (nm.nmt == NMT_ISO14443BI || nm.nmt == NMT_ISO14443B2SR || nm.nmt == NMT_ISO14443B2CT) { if (CHIP_DATA(pnd)->type == RCS360) { // TODO add support for RC-S360, at the moment it refuses to send raw frames without a first select pnd->last_error = NFC_ENOTIMPL; - return false; + return pnd->last_error; } // No native support in InListPassiveTarget so we do discovery by hand - if (nfc_device_set_property_bool (pnd, NP_FORCE_ISO14443_B, true) < 0) { - return false; + if ((res = nfc_device_set_property_bool (pnd, NP_FORCE_ISO14443_B, true)) < 0) { + return res; } - if (nfc_device_set_property_bool (pnd, NP_FORCE_SPEED_106, true) < 0) { - return false; + if ((res = nfc_device_set_property_bool (pnd, NP_FORCE_SPEED_106, true)) < 0) { + return res; } - if (nfc_device_set_property_bool (pnd, NP_HANDLE_CRC, true) < 0) { - return false; + if ((res = nfc_device_set_property_bool (pnd, NP_HANDLE_CRC, true)) < 0) { + return res; } pnd->bEasyFraming = false; if (nm.nmt == NMT_ISO14443B2SR) { @@ -943,12 +944,12 @@ pn53x_initiator_select_passive_target_ext (struct nfc_device *pnd, uint8_t abtRx[1]; size_t szRxLen = 1; // Getting random Chip_ID - if (pn53x_initiator_transceive_bytes (pnd, abtInitiate, szInitiateLen, abtRx, &szRxLen, timeout) < 0) { - return false; + if ((res = pn53x_initiator_transceive_bytes (pnd, abtInitiate, szInitiateLen, abtRx, &szRxLen, timeout)) < 0) { + return res; } abtSelect[1] = abtRx[0]; - if (pn53x_initiator_transceive_bytes (pnd, abtSelect, szSelectLen, abtRx, &szRxLen, timeout) < 0) { - return false; + if ((res = pn53x_initiator_transceive_bytes (pnd, abtSelect, szSelectLen, abtRx, &szRxLen, timeout)) < 0) { + return res; } } else if (nm.nmt == NMT_ISO14443B2CT) { @@ -956,20 +957,20 @@ pn53x_initiator_select_passive_target_ext (struct nfc_device *pnd, uint8_t abtReqt[]="\x10"; size_t szReqtLen = 1; // Getting product code / fab code & store it in output buffer after the serial nr we'll obtain later - if ((pn53x_initiator_transceive_bytes (pnd, abtReqt, szReqtLen, abtTargetsData+2, &szTargetsData, timeout) < 0) || szTargetsData != 2) { - return false; + if ((res = pn53x_initiator_transceive_bytes (pnd, abtReqt, szReqtLen, abtTargetsData+2, &szTargetsData, timeout)) < 0) { + return res; } } - if (pn53x_initiator_transceive_bytes (pnd, pbtInitData, szInitData, abtTargetsData, &szTargetsData, timeout) < 0) { - return false; + if ((res = pn53x_initiator_transceive_bytes (pnd, pbtInitData, szInitData, abtTargetsData, &szTargetsData, timeout)) < 0) { + return res; } if (nm.nmt == NMT_ISO14443B2CT) { if (szTargetsData != 2) - return false; + return NFC_ECHIP; uint8_t abtRead[]="\xC4"; // Reading UID_MSB (Read address 4) size_t szReadLen = 1; - if ((pn53x_initiator_transceive_bytes (pnd, abtRead, szReadLen, abtTargetsData+4, &szTargetsData, timeout) < 0) || szTargetsData != 2) { - return false; + if ((res = pn53x_initiator_transceive_bytes (pnd, abtRead, szReadLen, abtTargetsData+4, &szTargetsData, timeout) < 0)) { + return res; } szTargetsData = 6; // u16 UID_LSB, u8 prod code, u8 fab code, u16 UID_MSB } @@ -977,7 +978,7 @@ pn53x_initiator_select_passive_target_ext (struct nfc_device *pnd, pnt->nm = nm; // Fill the tag info struct with the values corresponding to this init modulation if (!pn53x_decode_target_data (abtTargetsData, szTargetsData, CHIP_DATA(pnd)->type, nm.nmt, &(pnt->nti))) { - return false; + return NFC_ECHIP; } } if (nm.nmt == NMT_ISO14443BI) { @@ -986,11 +987,11 @@ pn53x_initiator_select_passive_target_ext (struct nfc_device *pnd, size_t szAttribLen = sizeof(abtAttrib); memcpy(abtAttrib, abtTargetsData, szAttribLen); abtAttrib[1] = 0x0f; // ATTRIB - if (pn53x_initiator_transceive_bytes (pnd, abtAttrib, szAttribLen, NULL, NULL, timeout) < 0) { - return false; + if ((res = pn53x_initiator_transceive_bytes (pnd, abtAttrib, szAttribLen, NULL, NULL, timeout)) < 0) { + return res; } } - return true; + return NFC_SUCCESS; } // else: const pn53x_modulation pm = pn53x_nm_to_pm(nm); @@ -1017,7 +1018,7 @@ pn53x_initiator_select_passive_target_ext (struct nfc_device *pnd, return true; } -bool +int pn53x_initiator_select_passive_target (struct nfc_device *pnd, const nfc_modulation nm, const uint8_t *pbtInitData, const size_t szInitData, @@ -1077,7 +1078,7 @@ pn53x_initiator_poll_target (struct nfc_device *pnd, prepare_initiator_data (pnmModulations[n], &pbtInitiatorData, &szInitiatorData); const int timeout_ms = uiPeriod * 150; - if (!pn53x_initiator_select_passive_target_ext (pnd, pnmModulations[n], pbtInitiatorData, szInitiatorData, pnt, timeout_ms)) { + if (pn53x_initiator_select_passive_target_ext (pnd, pnmModulations[n], pbtInitiatorData, szInitiatorData, pnt, timeout_ms) < 0) { if (pnd->last_error != NFC_ETIMEOUT) return false; } else { diff --git a/libnfc/chips/pn53x.h b/libnfc/chips/pn53x.h index f453f9a..c5adaa9 100644 --- a/libnfc/chips/pn53x.h +++ b/libnfc/chips/pn53x.h @@ -286,7 +286,7 @@ bool pn53x_idle (struct nfc_device *pnd); // NFC device as Initiator functions int pn53x_initiator_init (struct nfc_device *pnd); -bool pn53x_initiator_select_passive_target (struct nfc_device *pnd, +int pn53x_initiator_select_passive_target (struct nfc_device *pnd, const nfc_modulation nm, const uint8_t *pbtInitData, const size_t szInitData, nfc_target *pnt); diff --git a/libnfc/nfc-internal.h b/libnfc/nfc-internal.h index f5d054b..a5846c5 100644 --- a/libnfc/nfc-internal.h +++ b/libnfc/nfc-internal.h @@ -133,7 +133,7 @@ struct nfc_driver_t { const char *(*strerror) (const struct nfc_device *pnd); int (*initiator_init) (struct nfc_device *pnd); - bool (*initiator_select_passive_target) (struct nfc_device *pnd, const nfc_modulation nm, const uint8_t * pbtInitData, const size_t szInitData, nfc_target * pnt); + int (*initiator_select_passive_target) (struct nfc_device *pnd, const nfc_modulation nm, const uint8_t * pbtInitData, const size_t szInitData, nfc_target * pnt); bool (*initiator_poll_target) (struct nfc_device *pnd, const nfc_modulation * pnmModulations, const size_t szModulations, const uint8_t uiPollNr, const uint8_t btPeriod, nfc_target * pnt); bool (*initiator_select_dep_target) (struct nfc_device *pnd, const nfc_dep_mode ndm, const nfc_baud_rate nbr, const nfc_dep_info * pndiInitiator, nfc_target * pnt, const int timeout); bool (*initiator_deselect_target) (struct nfc_device *pnd); diff --git a/libnfc/nfc.c b/libnfc/nfc.c index b92735b..fbbfc40 100644 --- a/libnfc/nfc.c +++ b/libnfc/nfc.c @@ -304,7 +304,7 @@ nfc_initiator_init (nfc_device *pnd) /** * @brief Select a passive or emulated tag - * @return Returns \c true if action was successfully performed; otherwise returns \c false. + * @return Returns 0 on success, otherwise returns libnfc's error code (negative value) * * @param pnd \a nfc_device struct pointer that represent currently used device * @param nm desired modulation @@ -322,7 +322,7 @@ nfc_initiator_init (nfc_device *pnd) * The chip needs to know with what kind of tag it is dealing with, therefore * the initial modulation and speed (106, 212 or 424 kbps) should be supplied. */ -bool +int nfc_initiator_select_passive_target (nfc_device *pnd, const nfc_modulation nm, const uint8_t *pbtInitData, const size_t szInitData, @@ -381,7 +381,7 @@ nfc_initiator_list_passive_targets (nfc_device *pnd, prepare_initiator_data (nm, &pbtInitData, &szInitDataLen); - while (nfc_initiator_select_passive_target (pnd, nm, pbtInitData, szInitDataLen, &nt)) { + while (nfc_initiator_select_passive_target (pnd, nm, pbtInitData, szInitDataLen, &nt) == 0) { nfc_initiator_deselect_target (pnd); if (szTargets == szTargetFound) { break; diff --git a/utils/nfc-mfclassic.c b/utils/nfc-mfclassic.c index ec0dc95..320c73d 100644 --- a/utils/nfc-mfclassic.c +++ b/utils/nfc-mfclassic.c @@ -280,7 +280,7 @@ read_card (int read_unlocked) // Show if the readout went well if (bFailure) { // When a failure occured we need to redo the anti-collision - if (!nfc_initiator_select_passive_target (pnd, nmMifare, NULL, 0, &nt)) { + if (nfc_initiator_select_passive_target (pnd, nmMifare, NULL, 0, &nt) < 0) { printf ("!\nError: tag was removed\n"); return false; } @@ -353,7 +353,7 @@ write_card (int write_block_zero) // Show if the readout went well if (bFailure) { // When a failure occured we need to redo the anti-collision - if (!nfc_initiator_select_passive_target (pnd, nmMifare, NULL, 0, &nt)) { + if (nfc_initiator_select_passive_target (pnd, nmMifare, NULL, 0, &nt) < 0) { printf ("!\nError: tag was removed\n"); return false; } @@ -557,7 +557,7 @@ main (int argc, const char *argv[]) printf ("Connected to NFC reader: %s\n", nfc_device_get_name (pnd)); // Try to find a MIFARE Classic tag - if (!nfc_initiator_select_passive_target (pnd, nmMifare, NULL, 0, &nt)) { + if (nfc_initiator_select_passive_target (pnd, nmMifare, NULL, 0, &nt) < 0) { printf ("Error: no tag was found\n"); nfc_disconnect (pnd); exit (EXIT_FAILURE); diff --git a/utils/nfc-mfultralight.c b/utils/nfc-mfultralight.c index 680406d..3027a18 100644 --- a/utils/nfc-mfultralight.c +++ b/utils/nfc-mfultralight.c @@ -142,7 +142,7 @@ write_card (void) // Show if the readout went well if (bFailure) { // When a failure occured we need to redo the anti-collision - if (!nfc_initiator_select_passive_target (pnd, nmMifare, NULL, 0, &nt)) { + if (nfc_initiator_select_passive_target (pnd, nmMifare, NULL, 0, &nt) < 0) { ERR ("tag was removed"); return false; } @@ -222,7 +222,7 @@ main (int argc, const char *argv[]) printf ("Connected to NFC device: %s\n", nfc_device_get_name (pnd)); // Try to find a MIFARE Ultralight tag - if (!nfc_initiator_select_passive_target (pnd, nmMifare, NULL, 0, &nt)) { + if (nfc_initiator_select_passive_target (pnd, nmMifare, NULL, 0, &nt) < 0) { ERR ("no tag was found\n"); nfc_disconnect (pnd); return 1; diff --git a/utils/nfc-read-forum-tag3.c b/utils/nfc-read-forum-tag3.c index 926c435..e051bb8 100644 --- a/utils/nfc-read-forum-tag3.c +++ b/utils/nfc-read-forum-tag3.c @@ -219,7 +219,7 @@ main(int argc, char *argv[]) int error = EXIT_SUCCESS; // Polling payload (SENSF_REQ) must be present (see NFC Digital Protol) const uint8_t *pbtSensfReq = (uint8_t*)"\x00\xff\xff\x01\x00"; - if (!nfc_initiator_select_passive_target(pnd, nm, pbtSensfReq, 5, &nt)) { + if (nfc_initiator_select_passive_target(pnd, nm, pbtSensfReq, 5, &nt) < 0) { nfc_perror (pnd, "nfc_initiator_select_passive_target"); error = EXIT_FAILURE; goto error; @@ -230,7 +230,7 @@ main(int argc, char *argv[]) if (0 != memcmp (nt.nti.nfi.abtSysCode, abtNfcForumSysCode, 2)) { // Retry with special polling const uint8_t *pbtSensfReqNfcForum = (uint8_t*)"\x00\x12\xfc\x01\x00"; - if (!nfc_initiator_select_passive_target(pnd, nm, pbtSensfReqNfcForum, 5, &nt)) { + if (nfc_initiator_select_passive_target(pnd, nm, pbtSensfReqNfcForum, 5, &nt) < 0) { nfc_perror (pnd, "nfc_initiator_select_passive_target"); error = EXIT_FAILURE; goto error; diff --git a/utils/nfc-relay-picc.c b/utils/nfc-relay-picc.c index c150825..b3cdff3 100644 --- a/utils/nfc-relay-picc.c +++ b/utils/nfc-relay-picc.c @@ -240,7 +240,7 @@ main (int argc, char *argv[]) .nmt = NMT_ISO14443A, .nbr = NBR_106, }; - if (!nfc_initiator_select_passive_target (pndInitiator, nm, NULL, 0, &ntRealTarget)) { + if (nfc_initiator_select_passive_target (pndInitiator, nm, NULL, 0, &ntRealTarget) < 0) { printf ("Error: no tag was found\n"); nfc_disconnect (pndInitiator); exit (EXIT_FAILURE); From 9c5ec3c4e14705b2b16901241dea4ee9a78c42b5 Mon Sep 17 00:00:00 2001 From: Audrey Diacre Date: Tue, 20 Dec 2011 11:25:33 +0000 Subject: [PATCH 034/113] the following functions now return libnfc error code: _pn53x_write_register() _pn53x_WriteRegister() _pn53x_transceive() _pn53x_SAMConfiguration() _pn53x_PowerDown() _pn53x_InListPassiveTarget() _pn53x_InDeselect() _pn53x_InRelease() _pn53x_RFConfiguration__* --- examples/pn53x-diagnose.c | 13 +-- examples/pn53x-sam.c | 2 +- examples/pn53x-tamashell.c | 2 +- libnfc/chips/pn53x.c | 201 +++++++++++++++++------------------- libnfc/chips/pn53x.h | 22 ++-- libnfc/drivers/pn532_uart.c | 2 +- libnfc/drivers/pn53x_usb.c | 8 +- test/test_register_access.c | 4 +- 8 files changed, 121 insertions(+), 133 deletions(-) diff --git a/examples/pn53x-diagnose.c b/examples/pn53x-diagnose.c index 7f51700..d12d6c4 100644 --- a/examples/pn53x-diagnose.c +++ b/examples/pn53x-diagnose.c @@ -57,6 +57,7 @@ main (int argc, const char *argv[]) nfc_device *pnd; const char *acLibnfcVersion; bool result; + int res = 0; uint8_t abtRx[PN53x_EXTENDED_FRAME__DATA_MAX_LEN]; size_t szRx = sizeof(abtRx); @@ -88,8 +89,8 @@ main (int argc, const char *argv[]) printf ("NFC device [%s] connected.\n", nfc_device_get_name (pnd)); - result = pn53x_transceive (pnd, pncmd_diagnose_communication_line_test, sizeof (pncmd_diagnose_communication_line_test), abtRx, &szRx, 0); - if (result) { + res = pn53x_transceive (pnd, pncmd_diagnose_communication_line_test, sizeof (pncmd_diagnose_communication_line_test), abtRx, &szRx, 0); + if (res == 0) { // Result of Diagnose ping for RC-S360 doesn't contain status byte so we've to handle both cases result = (memcmp (pncmd_diagnose_communication_line_test + 1, abtRx, sizeof (pncmd_diagnose_communication_line_test) - 1) == 0) || (memcmp (pncmd_diagnose_communication_line_test + 2, abtRx, sizeof (pncmd_diagnose_communication_line_test) - 2) == 0); @@ -98,16 +99,16 @@ main (int argc, const char *argv[]) } printf (" Communication line test: %s\n", result ? "OK" : "Failed"); - result = pn53x_transceive (pnd, pncmd_diagnose_rom_test, sizeof (pncmd_diagnose_rom_test), abtRx, &szRx, 0); - if (result) { + res = pn53x_transceive (pnd, pncmd_diagnose_rom_test, sizeof (pncmd_diagnose_rom_test), abtRx, &szRx, 0); + if (res == 0) { result = ((szRx == 1) && (abtRx[0] == 0x00)); } else { nfc_perror (pnd, "pn53x_transceive"); } printf (" ROM test: %s\n", result ? "OK" : "Failed"); - result = pn53x_transceive (pnd, pncmd_diagnose_ram_test, sizeof (pncmd_diagnose_ram_test), abtRx, &szRx, 0); - if (result) { + res = pn53x_transceive (pnd, pncmd_diagnose_ram_test, sizeof (pncmd_diagnose_ram_test), abtRx, &szRx, 0); + if (res == 0) { result = ((szRx == 1) && (abtRx[0] == 0x00)); } else { nfc_perror (pnd, "pn53x_transceive"); diff --git a/examples/pn53x-sam.c b/examples/pn53x-sam.c index ed185b8..09b67d7 100644 --- a/examples/pn53x-sam.c +++ b/examples/pn53x-sam.c @@ -110,7 +110,7 @@ main (int argc, const char *argv[]) // Connect with the SAM // FIXME: Its a private pn53x function - if (!pn53x_SAMConfiguration (pnd, mode, 0)) { + if (pn53x_SAMConfiguration (pnd, mode, 0) < 0) { nfc_perror (pnd, "pn53x_SAMConfiguration"); exit (EXIT_FAILURE); } diff --git a/examples/pn53x-tamashell.c b/examples/pn53x-tamashell.c index abaae4a..87cff6f 100644 --- a/examples/pn53x-tamashell.c +++ b/examples/pn53x-tamashell.c @@ -178,7 +178,7 @@ int main(int argc, const char* argv[]) print_hex((uint8_t*)abtTx,szTx); szRx = sizeof(abtRx); - if (!pn53x_transceive (pnd, abtTx, szTx, abtRx, &szRx, 0)) { + if (pn53x_transceive (pnd, abtTx, szTx, abtRx, &szRx, 0) < 0) { free(cmd); nfc_perror (pnd, "Rx"); continue; diff --git a/libnfc/chips/pn53x.c b/libnfc/chips/pn53x.c index d55ac2f..55a5c94 100644 --- a/libnfc/chips/pn53x.c +++ b/libnfc/chips/pn53x.c @@ -95,18 +95,18 @@ pn53x_reset_settings(struct nfc_device *pnd) { // Reset the ending transmission bits register, it is unknown what the last tranmission used there CHIP_DATA (pnd)->ui8TxBits = 0; - if (!pn53x_write_register (pnd, PN53X_REG_CIU_BitFraming, SYMBOL_TX_LAST_BITS, 0x00)) { + if (pn53x_write_register (pnd, PN53X_REG_CIU_BitFraming, SYMBOL_TX_LAST_BITS, 0x00) < 0) { return false; } return true; } -bool +int pn53x_transceive (struct nfc_device *pnd, const uint8_t *pbtTx, const size_t szTx, uint8_t *pbtRx, size_t *pszRx, int timeout) { if (CHIP_DATA (pnd)->wb_trigged) { if (!pn53x_writeback_register (pnd)) { - return false; + return NFC_ECHIP; } } @@ -127,7 +127,7 @@ pn53x_transceive (struct nfc_device *pnd, const uint8_t *pbtTx, const size_t szT // Call the send/receice callback functions of the current driver if (!CHIP_DATA (pnd)->io->send (pnd, pbtTx, szTx, timeout)) - return false; + return NFC_ECHIP; // Command is sent, we store the command CHIP_DATA (pnd)->ui8LastCommand = pbtTx[0]; @@ -139,11 +139,11 @@ pn53x_transceive (struct nfc_device *pnd, const uint8_t *pbtTx, const size_t szT int res = CHIP_DATA(pnd)->io->receive (pnd, pbtRx, *pszRx, timeout); if (res < 0) { - return false; + return NFC_ECHIP; } if (CHIP_DATA(pnd)->last_status_byte) - return false; + 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 @@ -192,7 +192,7 @@ 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)); - return (0 == CHIP_DATA(pnd)->last_status_byte); + return ((0 == CHIP_DATA(pnd)->last_status_byte) ? NFC_SUCCESS : NFC_ECHIP); } bool @@ -211,7 +211,7 @@ pn53x_set_tx_bits (struct nfc_device *pnd, const uint8_t ui8Bits) // Test if we need to update the transmission bits register setting if (CHIP_DATA (pnd)->ui8TxBits != ui8Bits) { // Set the amount of transmission bits in the PN53X chip register - if (!pn53x_write_register (pnd, PN53X_REG_CIU_BitFraming, SYMBOL_TX_LAST_BITS, ui8Bits)) + if (pn53x_write_register (pnd, PN53X_REG_CIU_BitFraming, SYMBOL_TX_LAST_BITS, ui8Bits) < 0) return false; // Store the new setting @@ -475,7 +475,7 @@ pn53x_ReadRegister (struct nfc_device *pnd, uint16_t ui16RegisterAddress, uint8_ size_t szRegValue = sizeof (abtRegValue); PNREG_TRACE (ui16RegisterAddress); - if (!pn53x_transceive (pnd, abtCmd, sizeof (abtCmd), abtRegValue, &szRegValue, -1)) { + if (pn53x_transceive (pnd, abtCmd, sizeof (abtCmd), abtRegValue, &szRegValue, -1) < 0) { return false; } if (CHIP_DATA(pnd)->type == PN533) { @@ -492,7 +492,7 @@ bool pn53x_read_register (struct nfc_device *pnd, uint16_t ui16RegisterAddress, return pn53x_ReadRegister (pnd, ui16RegisterAddress, ui8Value); } -bool +int pn53x_WriteRegister (struct nfc_device *pnd, const uint16_t ui16RegisterAddress, const uint8_t ui8Value) { uint8_t abtCmd[] = { WriteRegister, ui16RegisterAddress >> 8, ui16RegisterAddress & 0xff, ui8Value }; @@ -500,7 +500,7 @@ pn53x_WriteRegister (struct nfc_device *pnd, const uint16_t ui16RegisterAddress, return pn53x_transceive (pnd, abtCmd, sizeof (abtCmd), NULL, NULL, -1); } -bool +int pn53x_write_register (struct nfc_device *pnd, const uint16_t ui16RegisterAddress, const uint8_t ui8SymbolMask, const uint8_t ui8Value) { if ((ui16RegisterAddress < PN53X_CACHE_REGISTER_MIN_ADDRESS) || (ui16RegisterAddress > PN53X_CACHE_REGISTER_MAX_ADDRESS)) { @@ -508,7 +508,7 @@ pn53x_write_register (struct nfc_device *pnd, const uint16_t ui16RegisterAddress if (ui8SymbolMask != 0xff) { uint8_t ui8CurrentValue; if (!pn53x_read_register (pnd, ui16RegisterAddress, &ui8CurrentValue)) - return false; + return NFC_ECHIP; uint8_t ui8NewValue = ((ui8Value & ui8SymbolMask) | (ui8CurrentValue & (~ui8SymbolMask))); if (ui8NewValue != ui8CurrentValue) { return pn53x_WriteRegister (pnd, ui16RegisterAddress, ui8NewValue); @@ -523,7 +523,7 @@ pn53x_write_register (struct nfc_device *pnd, const uint16_t ui16RegisterAddress CHIP_DATA (pnd)->wb_mask[internal_address] = CHIP_DATA (pnd)->wb_mask[internal_address] | ui8SymbolMask; CHIP_DATA (pnd)->wb_trigged = true; } - return true; + return NFC_SUCCESS; } bool @@ -549,7 +549,7 @@ pn53x_writeback_register (struct nfc_device *pnd) uint8_t abtRes[PN53x_EXTENDED_FRAME__DATA_MAX_LEN]; size_t szRes = sizeof(abtRes); // It transceives the previously constructed ReadRegister command - if (!pn53x_transceive (pnd, abtReadRegisterCmd, BUFFER_SIZE (abtReadRegisterCmd), abtRes, &szRes, -1)) { + if (pn53x_transceive (pnd, abtReadRegisterCmd, BUFFER_SIZE (abtReadRegisterCmd), abtRes, &szRes, -1) < 0) { return false; } size_t i = 0; @@ -587,7 +587,7 @@ pn53x_writeback_register (struct nfc_device *pnd) if (BUFFER_SIZE (abtWriteRegisterCmd) > 1) { // We need to write some registers - if (!pn53x_transceive (pnd, abtWriteRegisterCmd, BUFFER_SIZE (abtWriteRegisterCmd), NULL, NULL, -1)) { + if (pn53x_transceive (pnd, abtWriteRegisterCmd, BUFFER_SIZE (abtWriteRegisterCmd), NULL, NULL, -1) < 0) { return false; } } @@ -600,7 +600,7 @@ pn53x_get_firmware_version (struct nfc_device *pnd, char abtFirmwareText[22]) const uint8_t abtCmd[] = { GetFirmwareVersion }; uint8_t abtFw[4]; size_t szFwLen = sizeof (abtFw); - if (!pn53x_transceive (pnd, abtCmd, sizeof (abtCmd), abtFw, &szFwLen, -1)) { + if (pn53x_transceive (pnd, abtCmd, sizeof (abtCmd), abtFw, &szFwLen, -1) < 0) { return false; } // Determine which version of chip it is: PN531 will return only 2 bytes, while others return 4 bytes and have the first to tell the version IC @@ -669,11 +669,11 @@ pn53x_set_property_int (struct nfc_device *pnd, const nfc_property property, con break; case NP_TIMEOUT_ATR: CHIP_DATA (pnd)->timeout_atr = value; - return (pn53x_RFConfiguration__Various_timings (pnd, pn53x_int_to_timeout(CHIP_DATA (pnd)->timeout_atr), pn53x_int_to_timeout(CHIP_DATA (pnd)->timeout_communication))) ? NFC_SUCCESS : NFC_EIO; + return pn53x_RFConfiguration__Various_timings (pnd, pn53x_int_to_timeout(CHIP_DATA (pnd)->timeout_atr), pn53x_int_to_timeout(CHIP_DATA (pnd)->timeout_communication)); break; case NP_TIMEOUT_COM: CHIP_DATA (pnd)->timeout_communication = value; - return (pn53x_RFConfiguration__Various_timings (pnd, pn53x_int_to_timeout(CHIP_DATA (pnd)->timeout_atr), pn53x_int_to_timeout(CHIP_DATA (pnd)->timeout_communication))) ? NFC_SUCCESS : NFC_EIO; + return pn53x_RFConfiguration__Various_timings (pnd, pn53x_int_to_timeout(CHIP_DATA (pnd)->timeout_atr), pn53x_int_to_timeout(CHIP_DATA (pnd)->timeout_communication)); break; default: return NFC_EINVARG; @@ -685,6 +685,7 @@ int pn53x_set_property_bool (struct nfc_device *pnd, const nfc_property property, const bool bEnable) { uint8_t btValue; + int res = 0; switch (property) { case NP_HANDLE_CRC: // Enable or disable automatic receiving/sending of CRC bytes @@ -694,10 +695,10 @@ pn53x_set_property_bool (struct nfc_device *pnd, const nfc_property property, co } // TX and RX are both represented by the symbol 0x80 btValue = (bEnable) ? 0x80 : 0x00; - if (!pn53x_write_register (pnd, PN53X_REG_CIU_TxMode, SYMBOL_TX_CRC_ENABLE, btValue)) - return NFC_ECHIP; - if (!pn53x_write_register (pnd, PN53X_REG_CIU_RxMode, SYMBOL_RX_CRC_ENABLE, btValue)) - return NFC_ECHIP; + if ((res = pn53x_write_register (pnd, PN53X_REG_CIU_TxMode, SYMBOL_TX_CRC_ENABLE, btValue)) < 0) + return res; + if ((res = pn53x_write_register (pnd, PN53X_REG_CIU_RxMode, SYMBOL_RX_CRC_ENABLE, btValue)) < 0) + return res; pnd->bCrc = bEnable; return NFC_SUCCESS; break; @@ -708,8 +709,8 @@ pn53x_set_property_bool (struct nfc_device *pnd, const nfc_property property, co // Nothing to do return NFC_SUCCESS; btValue = (bEnable) ? 0x00 : SYMBOL_PARITY_DISABLE; - if (!pn53x_write_register (pnd, PN53X_REG_CIU_ManualRCV, SYMBOL_PARITY_DISABLE, btValue)) - return NFC_ECHIP; + if ((res = pn53x_write_register (pnd, PN53X_REG_CIU_ManualRCV, SYMBOL_PARITY_DISABLE, btValue)) < 0) + return res; pnd->bPar = bEnable; return NFC_SUCCESS; break; @@ -721,16 +722,14 @@ pn53x_set_property_bool (struct nfc_device *pnd, const nfc_property property, co case NP_ACTIVATE_FIELD: { - if (pn53x_RFConfiguration__RF_field (pnd, bEnable)) + if (pn53x_RFConfiguration__RF_field (pnd, bEnable) == 0) return NFC_SUCCESS; } break; case NP_ACTIVATE_CRYPTO1: btValue = (bEnable) ? SYMBOL_MF_CRYPTO1_ON : 0x00; - if (!pn53x_write_register (pnd, PN53X_REG_CIU_Status2, SYMBOL_MF_CRYPTO1_ON, btValue)) - return NFC_ECHIP; - return NFC_SUCCESS; + return pn53x_write_register (pnd, PN53X_REG_CIU_Status2, SYMBOL_MF_CRYPTO1_ON, btValue); break; case NP_INFINITE_SELECT: @@ -742,23 +741,19 @@ pn53x_set_property_bool (struct nfc_device *pnd, const nfc_property property, co (bEnable) ? 0xff : 0x00, // MxRtyATR, default: active = 0xff, passive = 0x02 (bEnable) ? 0xff : 0x00, // MxRtyPSL, default: 0x01 (bEnable) ? 0xff : 0x02 // MxRtyPassiveActivation, default: 0xff (0x00 leads to problems with PN531) - )) + ) == 0) return NFC_SUCCESS; } break; case NP_ACCEPT_INVALID_FRAMES: btValue = (bEnable) ? SYMBOL_RX_NO_ERROR : 0x00; - if (!pn53x_write_register (pnd, PN53X_REG_CIU_RxMode, SYMBOL_RX_NO_ERROR, btValue)) - return NFC_ECHIP; - return NFC_SUCCESS; + return pn53x_write_register (pnd, PN53X_REG_CIU_RxMode, SYMBOL_RX_NO_ERROR, btValue); break; case NP_ACCEPT_MULTIPLE_FRAMES: btValue = (bEnable) ? SYMBOL_RX_MULTIPLE : 0x00; - if (!pn53x_write_register (pnd, PN53X_REG_CIU_RxMode, SYMBOL_RX_MULTIPLE, btValue)) - return NFC_ECHIP; - return NFC_SUCCESS; + return pn53x_write_register (pnd, PN53X_REG_CIU_RxMode, SYMBOL_RX_MULTIPLE, btValue); break; case NP_AUTO_ISO14443_4: @@ -776,17 +771,14 @@ pn53x_set_property_bool (struct nfc_device *pnd, const nfc_property property, co return NFC_SUCCESS; } // Force pn53x to be in ISO14443-A mode - if (!pn53x_write_register (pnd, PN53X_REG_CIU_TxMode, SYMBOL_TX_FRAMING, 0x00)) { - return NFC_ECHIP; + if ((res = pn53x_write_register (pnd, PN53X_REG_CIU_TxMode, SYMBOL_TX_FRAMING, 0x00)) < 0) { + return res; } - if (!pn53x_write_register (pnd, PN53X_REG_CIU_RxMode, SYMBOL_RX_FRAMING, 0x00)) { - return NFC_ECHIP; + if ((res = pn53x_write_register (pnd, PN53X_REG_CIU_RxMode, SYMBOL_RX_FRAMING, 0x00)) < 0) { + return res; } // Set the PN53X to force 100% ASK Modified miller decoding (default for 14443A cards) - if (!pn53x_write_register (pnd, PN53X_REG_CIU_TxAuto, SYMBOL_FORCE_100_ASK, 0x40)) - return NFC_ECHIP; - - return NFC_SUCCESS; + return pn53x_write_register (pnd, PN53X_REG_CIU_TxAuto, SYMBOL_FORCE_100_ASK, 0x40); break; case NP_FORCE_ISO14443_B: @@ -795,14 +787,10 @@ pn53x_set_property_bool (struct nfc_device *pnd, const nfc_property property, co return NFC_SUCCESS; } // Force pn53x to be in ISO14443-B mode - if (!pn53x_write_register (pnd, PN53X_REG_CIU_TxMode, SYMBOL_TX_FRAMING, 0x03)) { - return NFC_ECHIP; + if ((res = pn53x_write_register (pnd, PN53X_REG_CIU_TxMode, SYMBOL_TX_FRAMING, 0x03)) < 0) { + return res; } - if (!pn53x_write_register (pnd, PN53X_REG_CIU_RxMode, SYMBOL_RX_FRAMING, 0x03)) { - return NFC_ECHIP; - } - - return NFC_SUCCESS; + return pn53x_write_register (pnd, PN53X_REG_CIU_RxMode, SYMBOL_RX_FRAMING, 0x03); break; case NP_FORCE_SPEED_106: @@ -811,14 +799,10 @@ pn53x_set_property_bool (struct nfc_device *pnd, const nfc_property property, co return NFC_SUCCESS; } // Force pn53x to be at 106 kbps - if (!pn53x_write_register (pnd, PN53X_REG_CIU_TxMode, SYMBOL_TX_SPEED, 0x00)) { - return NFC_ECHIP; + if ((res = pn53x_write_register (pnd, PN53X_REG_CIU_TxMode, SYMBOL_TX_SPEED, 0x00)) < 0) { + return res; } - if (!pn53x_write_register (pnd, PN53X_REG_CIU_RxMode, SYMBOL_RX_SPEED, 0x00)) { - return NFC_ECHIP; - } - - return NFC_SUCCESS; + return pn53x_write_register (pnd, PN53X_REG_CIU_RxMode, SYMBOL_RX_SPEED, 0x00); break; // Not boolean property case NP_TIMEOUT_COMMAND: @@ -838,12 +822,12 @@ pn53x_idle (struct nfc_device *pnd) case TARGET: // InRelease used in target mode stops the target emulation and no more // tag are seen from external initiator - if (!pn53x_InRelease (pnd, 0)) { + if (pn53x_InRelease (pnd, 0) < 0) { return false; } if (CHIP_DATA (pnd)->type == PN532) { // Use PowerDown to go in "Low VBat" power mode - if (!pn53x_PowerDown (pnd)) { + if (pn53x_PowerDown (pnd) < 0) { return false; } CHIP_DATA (pnd)->power_mode = LOWVBAT; @@ -851,7 +835,7 @@ pn53x_idle (struct nfc_device *pnd) break; case INITIATOR: // Deselect all active communications - if (!pn53x_InDeselect (pnd, 0)) { + if (pn53x_InDeselect (pnd, 0) < 0) { return false; } // Disable RF field to avoid heating @@ -860,13 +844,13 @@ pn53x_idle (struct nfc_device *pnd) } if (CHIP_DATA (pnd)->type == PN532) { // Use PowerDown to go in "Low VBat" power mode - if (!pn53x_PowerDown (pnd)) { + if (pn53x_PowerDown (pnd) < 0) { return false; } CHIP_DATA (pnd)->power_mode = LOWVBAT; } else { // Use InRelease to go in "Standby mode" - if (!pn53x_InRelease (pnd, 0)) { + if (pn53x_InRelease (pnd, 0) < 0) { return false; } } @@ -887,7 +871,7 @@ pn53x_check_communication (struct nfc_device *pnd) uint8_t abtRx[sizeof(abtExpectedRx)]; size_t szRx = sizeof (abtRx); - if (!pn53x_transceive (pnd, abtCmd, sizeof (abtCmd), abtRx, &szRx, 1000)) + if (pn53x_transceive (pnd, abtCmd, sizeof (abtCmd), abtRx, &szRx, 1000) < 0) return false; return ((sizeof(abtExpectedRx) == szRx) && (0 == memcmp (abtRx, abtExpectedRx, sizeof(abtExpectedRx)))); @@ -897,11 +881,11 @@ int pn53x_initiator_init (struct nfc_device *pnd) { pn53x_reset_settings(pnd); + int res = 0; // Configure the PN53X to be an Initiator or Reader/Writer - if (!pn53x_write_register (pnd, PN53X_REG_CIU_Control, SYMBOL_INITIATOR, 0x10)) - // FIXMES pn53x_write_register() should return integer - return NFC_EIO; + if ((res = pn53x_write_register (pnd, PN53X_REG_CIU_Control, SYMBOL_INITIATOR, 0x10)) < 0) + return res; CHIP_DATA (pnd)->operating_mode = INITIATOR; return NFC_SUCCESS; @@ -1000,7 +984,7 @@ pn53x_initiator_select_passive_target_ext (struct nfc_device *pnd, return false; } - if (!pn53x_InListPassiveTarget (pnd, pm, 1, pbtInitData, szInitData, abtTargetsData, &szTargetsData, timeout)) + if (pn53x_InListPassiveTarget (pnd, pm, 1, pbtInitData, szInitData, abtTargetsData, &szTargetsData, timeout) < 0) return false; // Make sure one tag has been found, the PN53X returns 0x00 if none was available @@ -1156,7 +1140,7 @@ pn53x_initiator_transceive_bits (struct nfc_device *pnd, const uint8_t *pbtTx, c // We have to give the amount of bytes + (the command byte 0x42) uint8_t abtRx[PN53x_EXTENDED_FRAME__DATA_MAX_LEN]; size_t szRx = sizeof(abtRx); - if (!pn53x_transceive (pnd, abtCmd, szFrameBytes + 1, abtRx, &szRx, -1)) + if (pn53x_transceive (pnd, abtCmd, szFrameBytes + 1, abtRx, &szRx, -1) < 0) return false; // Get the last bit-count that is stored in the received byte @@ -1219,7 +1203,7 @@ pn53x_initiator_transceive_bytes (struct nfc_device *pnd, const uint8_t *pbtTx, // We have to give the amount of bytes + (the two command bytes 0xD4, 0x42) uint8_t abtRx[PN53x_EXTENDED_FRAME__DATA_MAX_LEN]; size_t szRx = sizeof(abtRx); - if (!pn53x_transceive (pnd, abtCmd, szTx + szExtraTxLen, abtRx, &szRx, timeout)) { + if (pn53x_transceive (pnd, abtCmd, szTx + szExtraTxLen, abtRx, &szRx, timeout) < 0) { // FIXME pn53x_transceive should return an integer if (CHIP_DATA (pnd)->last_status_byte == EINVRXFRAM) { pnd->last_error = NFC_ERFTRANS; @@ -1283,7 +1267,7 @@ uint32_t __pn53x_get_timer(struct nfc_device *pnd, const uint8_t last_cmd_byte) uint8_t abtRes[PN53x_EXTENDED_FRAME__DATA_MAX_LEN]; size_t szRes = sizeof(abtRes); // Let's send the previously constructed ReadRegister command - if (!pn53x_transceive (pnd, abtReadRegisterCmd, BUFFER_SIZE (abtReadRegisterCmd), abtRes, &szRes, -1)) { + if (pn53x_transceive (pnd, abtReadRegisterCmd, BUFFER_SIZE (abtReadRegisterCmd), abtRes, &szRes, -1) < 0) { return false; } counter_hi = abtRes[off]; @@ -1373,7 +1357,7 @@ pn53x_initiator_transceive_bits_timed (struct nfc_device *pnd, const uint8_t *pb BUFFER_APPEND (abtWriteRegisterCmd, PN53X_REG_CIU_BitFraming & 0xff); BUFFER_APPEND (abtWriteRegisterCmd, SYMBOL_START_SEND | ((szTxBits % 8) & SYMBOL_TX_LAST_BITS)); // Let's send the previously constructed WriteRegister command - if (!pn53x_transceive (pnd, abtWriteRegisterCmd, BUFFER_SIZE (abtWriteRegisterCmd), NULL, NULL, -1)) { + if (pn53x_transceive (pnd, abtWriteRegisterCmd, BUFFER_SIZE (abtWriteRegisterCmd), NULL, NULL, -1) < 0) { return false; } @@ -1405,7 +1389,7 @@ pn53x_initiator_transceive_bits_timed (struct nfc_device *pnd, const uint8_t *pb uint8_t abtRes[PN53x_EXTENDED_FRAME__DATA_MAX_LEN]; size_t szRes = sizeof(abtRes); // Let's send the previously constructed ReadRegister command - if (!pn53x_transceive (pnd, abtReadRegisterCmd, BUFFER_SIZE (abtReadRegisterCmd), abtRes, &szRes, -1)) { + if (pn53x_transceive (pnd, abtReadRegisterCmd, BUFFER_SIZE (abtReadRegisterCmd), abtRes, &szRes, -1) < 0) { return false; } for (i = 0; i < sz; i++) { @@ -1468,7 +1452,7 @@ pn53x_initiator_transceive_bytes_timed (struct nfc_device *pnd, const uint8_t *p BUFFER_APPEND (abtWriteRegisterCmd, PN53X_REG_CIU_BitFraming & 0xff); BUFFER_APPEND (abtWriteRegisterCmd, SYMBOL_START_SEND); // Let's send the previously constructed WriteRegister command - if (!pn53x_transceive (pnd, abtWriteRegisterCmd, BUFFER_SIZE (abtWriteRegisterCmd), NULL, NULL, -1)) { + if (pn53x_transceive (pnd, abtWriteRegisterCmd, BUFFER_SIZE (abtWriteRegisterCmd), NULL, NULL, -1) < 0) { return false; } @@ -1500,7 +1484,7 @@ pn53x_initiator_transceive_bytes_timed (struct nfc_device *pnd, const uint8_t *p uint8_t abtRes[PN53x_EXTENDED_FRAME__DATA_MAX_LEN]; size_t szRes = sizeof(abtRes); // Let's send the previously constructed ReadRegister command - if (!pn53x_transceive (pnd, abtReadRegisterCmd, BUFFER_SIZE (abtReadRegisterCmd), abtRes, &szRes, -1)) { + if (pn53x_transceive (pnd, abtReadRegisterCmd, BUFFER_SIZE (abtReadRegisterCmd), abtRes, &szRes, -1) < 0) { return false; } for (i = 0; i < sz; i++) { @@ -1530,7 +1514,7 @@ pn53x_initiator_transceive_bytes_timed (struct nfc_device *pnd, const uint8_t *p bool pn53x_initiator_deselect_target (struct nfc_device *pnd) { - return (pn53x_InDeselect (pnd, 0)); // 0 mean deselect all selected targets + return ((pn53x_InDeselect (pnd, 0) < 0 ) ? 0 : 1 ); // 0 mean deselect all selected targets } #define SAK_ISO14443_4_COMPLIANT 0x20 @@ -1543,6 +1527,7 @@ pn53x_target_init (struct nfc_device *pnd, nfc_target *pnt, uint8_t *pbtRx, size CHIP_DATA (pnd)->operating_mode = TARGET; pn53x_target_mode ptm = PTM_NORMAL; + int res = 0; switch (pnt->nm.nmt) { case NMT_ISO14443A: @@ -1583,8 +1568,8 @@ pn53x_target_init (struct nfc_device *pnd, nfc_target *pnt, uint8_t *pbtRx, size } // Let the PN53X be activated by the RF level detector from power down mode - if (!pn53x_write_register (pnd, PN53X_REG_CIU_TxAuto, SYMBOL_INITIAL_RF_ON, 0x04)) - return NFC_ECHIP; + if ((res = pn53x_write_register (pnd, PN53X_REG_CIU_TxAuto, SYMBOL_INITIAL_RF_ON, 0x04)) < 0) + return res; uint8_t abtMifareParams[6]; uint8_t *pbtMifareParams = NULL; @@ -1764,7 +1749,7 @@ pn53x_target_receive_bits (struct nfc_device *pnd, uint8_t *pbtRx, size_t *pszRx uint8_t abtRx[PN53x_EXTENDED_FRAME__DATA_MAX_LEN]; size_t szRx = sizeof (abtRx); // Try to gather a received frame from the reader - if (!pn53x_transceive (pnd, abtCmd, sizeof (abtCmd), abtRx, &szRx, -1)) + if (pn53x_transceive (pnd, abtCmd, sizeof (abtCmd), abtRx, &szRx, -1) < 0) return false; // Get the last bit-count that is stored in the received byte @@ -1827,7 +1812,7 @@ pn53x_target_receive_bytes (struct nfc_device *pnd, uint8_t *pbtRx, size_t *pszR // Try to gather a received frame from the reader uint8_t abtRx[PN53x_EXTENDED_FRAME__DATA_MAX_LEN]; size_t szRx = sizeof (abtRx); - if (!pn53x_transceive (pnd, abtCmd, sizeof (abtCmd), abtRx, &szRx, timeout)) + if (pn53x_transceive (pnd, abtCmd, sizeof (abtCmd), abtRx, &szRx, timeout) < 0) return false; // Save the received byte count @@ -1871,7 +1856,7 @@ pn53x_target_send_bits (struct nfc_device *pnd, const uint8_t *pbtTx, const size return false; // Try to send the bits to the reader - if (!pn53x_transceive (pnd, abtCmd, szFrameBytes + 1, NULL, NULL, -1)) + if (pn53x_transceive (pnd, abtCmd, szFrameBytes + 1, NULL, NULL, -1) < 0) return false; // Everyting seems ok, return true @@ -1919,7 +1904,7 @@ pn53x_target_send_bytes (struct nfc_device *pnd, const uint8_t *pbtTx, const siz memcpy (abtCmd + 1, pbtTx, szTx); // Try to send the bits to the reader - if (!pn53x_transceive (pnd, abtCmd, szTx + 1, NULL, NULL, timeout)) + if (pn53x_transceive (pnd, abtCmd, szTx + 1, NULL, NULL, timeout) < 0) return false; // Everyting seems ok, return true @@ -1980,14 +1965,14 @@ pn53x_strerror (const struct nfc_device *pnd) return pcRes; } -bool +int pn53x_RFConfiguration__RF_field (struct nfc_device *pnd, bool bEnable) { uint8_t abtCmd[] = { RFConfiguration, RFCI_FIELD, (bEnable) ? 0x01 : 0x00 }; return pn53x_transceive (pnd, abtCmd, sizeof (abtCmd), NULL, NULL, -1); } -bool +int pn53x_RFConfiguration__Various_timings (struct nfc_device *pnd, const uint8_t fATR_RES_Timeout, const uint8_t fRetryTimeout) { uint8_t abtCmd[] = { @@ -2000,7 +1985,7 @@ pn53x_RFConfiguration__Various_timings (struct nfc_device *pnd, const uint8_t fA return pn53x_transceive (pnd, abtCmd, sizeof (abtCmd), NULL, NULL, -1); } -bool +int pn53x_RFConfiguration__MaxRtyCOM (struct nfc_device *pnd, const uint8_t MaxRtyCOM) { uint8_t abtCmd[] = { @@ -2011,7 +1996,7 @@ pn53x_RFConfiguration__MaxRtyCOM (struct nfc_device *pnd, const uint8_t MaxRtyCO return pn53x_transceive (pnd, abtCmd, sizeof (abtCmd), NULL, NULL, -1); } -bool +int pn53x_RFConfiguration__MaxRetries (struct nfc_device *pnd, const uint8_t MxRtyATR, const uint8_t MxRtyPSL, const uint8_t MxRtyPassiveActivation) { // Retry format: 0x00 means only 1 try, 0xff means infinite @@ -2030,7 +2015,7 @@ pn53x_SetParameters (struct nfc_device *pnd, const uint8_t ui8Value) { uint8_t abtCmd[] = { SetParameters, ui8Value }; - if(!pn53x_transceive (pnd, abtCmd, sizeof (abtCmd), NULL, NULL, -1)) { + if(pn53x_transceive (pnd, abtCmd, sizeof (abtCmd), NULL, NULL, -1) < 0) { return false; } // We save last parameters in register cache @@ -2038,7 +2023,7 @@ pn53x_SetParameters (struct nfc_device *pnd, const uint8_t ui8Value) return true; } -bool +int pn53x_SAMConfiguration (struct nfc_device *pnd, const pn532_sam_mode ui8Mode, int timeout) { uint8_t abtCmd[] = { SAMConfiguration, ui8Mode, 0x00, 0x00 }; @@ -2047,7 +2032,7 @@ pn53x_SAMConfiguration (struct nfc_device *pnd, const pn532_sam_mode ui8Mode, in if (CHIP_DATA(pnd)->type != PN532) { // This function is not supported by pn531 neither pn533 pnd->last_error = NFC_EDEVNOTSUPP; - return false; + return pnd->last_error; } switch (ui8Mode) { @@ -2062,12 +2047,12 @@ pn53x_SAMConfiguration (struct nfc_device *pnd, const pn532_sam_mode ui8Mode, in break; default: pnd->last_error = NFC_EINVARG; - return false; + return pnd->last_error; } return (pn53x_transceive (pnd, abtCmd, szCmd, NULL, NULL, timeout)); } -bool +int pn53x_PowerDown (struct nfc_device *pnd) { uint8_t abtCmd[] = { PowerDown, 0xf0 }; @@ -2088,7 +2073,7 @@ pn53x_PowerDown (struct nfc_device *pnd) * @note Selected targets count can be found in \a pbtTargetsData[0] if available (i.e. \a pszTargetsData content is more than 0) * @note To decode theses TargetData[n], there is @fn pn53x_decode_target_data */ -bool +int pn53x_InListPassiveTarget (struct nfc_device *pnd, const pn53x_modulation pmInitModulation, const uint8_t szMaxTargets, const uint8_t *pbtInitiatorData, const size_t szInitiatorData, @@ -2109,14 +2094,14 @@ pn53x_InListPassiveTarget (struct nfc_device *pnd, if (!(pnd->btSupportByte & SUPPORT_ISO14443B)) { // Eg. Some PN532 doesn't support type B! pnd->last_error = NFC_EDEVNOTSUPP; - return false; + return pnd->last_error; } break; case PM_JEWEL_106: if(CHIP_DATA(pnd)->type == PN531) { // These modulations are not supported by pn531 pnd->last_error = NFC_EDEVNOTSUPP; - return false; + return pnd->last_error; } break; case PM_ISO14443B_212: @@ -2125,12 +2110,12 @@ pn53x_InListPassiveTarget (struct nfc_device *pnd, if((CHIP_DATA(pnd)->type != PN533) || (!(pnd->btSupportByte & SUPPORT_ISO14443B))) { // These modulations are not supported by pn531 neither pn532 pnd->last_error = NFC_EDEVNOTSUPP; - return false; + return pnd->last_error; } break; default: pnd->last_error = NFC_EINVARG; - return false; + return pnd->last_error; } abtCmd[2] = pmInitModulation; // BrTy, the type of init modulation used for polling a passive tag @@ -2141,7 +2126,7 @@ pn53x_InListPassiveTarget (struct nfc_device *pnd, return pn53x_transceive (pnd, abtCmd, 3 + szInitiatorData, pbtTargetsData, pszTargetsData, timeout); } -bool +int pn53x_InDeselect (struct nfc_device *pnd, const uint8_t ui8Target) { if (CHIP_DATA(pnd)->type == RCS360) { @@ -2149,11 +2134,12 @@ pn53x_InDeselect (struct nfc_device *pnd, const uint8_t ui8Target) uint8_t abtStatus[PN53x_EXTENDED_FRAME__DATA_MAX_LEN]; size_t szStatus = sizeof(abtStatus); uint8_t abtCmdGetStatus[] = { GetGeneralStatus }; - if (!pn53x_transceive (pnd, abtCmdGetStatus, sizeof (abtCmdGetStatus), abtStatus, &szStatus, -1)) { - return false; + int res = 0; + if ((res = pn53x_transceive (pnd, abtCmdGetStatus, sizeof (abtCmdGetStatus), abtStatus, &szStatus, -1)) < 0) { + return res; } if ((szStatus < 3) || (abtStatus[2] == 0)) { - return true; + return NFC_SUCCESS; } // No much choice what to deselect actually... uint8_t abtCmdRcs360[] = { InDeselect, 0x01, 0x01 }; @@ -2163,7 +2149,7 @@ pn53x_InDeselect (struct nfc_device *pnd, const uint8_t ui8Target) return (pn53x_transceive (pnd, abtCmd, sizeof (abtCmd), NULL, NULL, -1)); } -bool +int pn53x_InRelease (struct nfc_device *pnd, const uint8_t ui8Target) { if (CHIP_DATA(pnd)->type == RCS360) { @@ -2171,11 +2157,12 @@ pn53x_InRelease (struct nfc_device *pnd, const uint8_t ui8Target) uint8_t abtStatus[PN53x_EXTENDED_FRAME__DATA_MAX_LEN]; size_t szStatus = sizeof(abtStatus); uint8_t abtCmdGetStatus[] = { GetGeneralStatus }; - if (!pn53x_transceive (pnd, abtCmdGetStatus, sizeof (abtCmdGetStatus), abtStatus, &szStatus, -1)) { - return false; + int res = 0; + if ((res = pn53x_transceive (pnd, abtCmdGetStatus, sizeof (abtCmdGetStatus), abtStatus, &szStatus, -1)) < 0) { + return res; } if ((szStatus < 3) || (abtStatus[2] == 0)) { - return true; + return NFC_SUCCESS; } // No much choice what to release actually... uint8_t abtCmdRcs360[] = { InRelease, 0x01, 0x01 }; @@ -2206,9 +2193,9 @@ pn53x_InAutoPoll (struct nfc_device *pnd, uint8_t abtRx[PN53x_EXTENDED_FRAME__DATA_MAX_LEN]; size_t szRx = sizeof(abtRx); - bool res = pn53x_transceive (pnd, abtCmd, szTxInAutoPoll, abtRx, &szRx, timeout); + int res = pn53x_transceive (pnd, abtCmd, szTxInAutoPoll, abtRx, &szRx, timeout); - if (res == false) { + if (res < 0) { return false; } else if (szRx > 0) { *pszTargetFound = abtRx[0]; @@ -2316,7 +2303,7 @@ pn53x_InJumpForDEP (struct nfc_device *pnd, uint8_t abtRx[PN53x_EXTENDED_FRAME__DATA_MAX_LEN]; size_t szRx = sizeof (abtRx); // Try to find a target, call the transceive callback function of the current device - if (!pn53x_transceive (pnd, abtCmd, offset, abtRx, &szRx, timeout)) + if (pn53x_transceive (pnd, abtCmd, offset, abtRx, &szRx, timeout) < 0) return false; // Make sure one target has been found, the PN53X returns 0x00 if none was available @@ -2398,7 +2385,7 @@ pn53x_TgInitAsTarget (struct nfc_device *pnd, pn53x_target_mode ptm, // Request the initialization as a target uint8_t abtRx[PN53x_EXTENDED_FRAME__DATA_MAX_LEN]; size_t szRx = sizeof (abtRx); - if (!pn53x_transceive (pnd, abtCmd, 36 + szOptionalBytes, abtRx, &szRx, -1)) + if (pn53x_transceive (pnd, abtCmd, 36 + szOptionalBytes, abtRx, &szRx, -1) < 0) return false; // Note: the first byte is skip: diff --git a/libnfc/chips/pn53x.h b/libnfc/chips/pn53x.h index c5adaa9..df1b846 100644 --- a/libnfc/chips/pn53x.h +++ b/libnfc/chips/pn53x.h @@ -264,7 +264,7 @@ extern const uint8_t pn53x_ack_frame[6]; extern const uint8_t pn53x_nack_frame[6]; bool pn53x_init(struct nfc_device *pnd); -bool pn53x_transceive (struct nfc_device *pnd, const uint8_t *pbtTx, const size_t szTx, uint8_t *pbtRx, size_t *pszRx, int timeout); +int pn53x_transceive (struct nfc_device *pnd, const uint8_t *pbtTx, const size_t szTx, uint8_t *pbtRx, size_t *pszRx, int timeout); bool pn53x_set_parameters (struct nfc_device *pnd, const uint8_t ui8Value, const bool bEnable); bool pn53x_set_tx_bits (struct nfc_device *pnd, const uint8_t ui8Bits); @@ -276,7 +276,7 @@ bool pn53x_decode_target_data (const uint8_t *pbtRawData, size_t szRawData, pn53x_type chip_type, nfc_modulation_type nmt, nfc_target_info *pnti); bool pn53x_read_register (struct nfc_device *pnd, uint16_t ui16Reg, uint8_t *ui8Value); -bool pn53x_write_register (struct nfc_device *pnd, uint16_t ui16Reg, uint8_t ui8SymbolMask, uint8_t ui8Value); +int pn53x_write_register (struct nfc_device *pnd, uint16_t ui16Reg, uint8_t ui8SymbolMask, uint8_t ui8Value); bool pn53x_get_firmware_version (struct nfc_device *pnd, char abtFirmwareText[22]); int pn53x_set_property_int (struct nfc_device *pnd, const nfc_property property, const int value); int pn53x_set_property_bool (struct nfc_device *pnd, const nfc_property property, const bool bEnable); @@ -323,14 +323,14 @@ const char *pn53x_strerror (const struct nfc_device *pnd); // C wrappers for PN53x commands bool pn53x_SetParameters (struct nfc_device *pnd, const uint8_t ui8Value); -bool pn53x_SAMConfiguration (struct nfc_device *pnd, const pn532_sam_mode mode, int timeout); -bool pn53x_PowerDown (struct nfc_device *pnd); -bool pn53x_InListPassiveTarget (struct nfc_device *pnd, const pn53x_modulation pmInitModulation, +int pn53x_SAMConfiguration (struct nfc_device *pnd, const pn532_sam_mode mode, int timeout); +int pn53x_PowerDown (struct nfc_device *pnd); +int pn53x_InListPassiveTarget (struct nfc_device *pnd, const pn53x_modulation pmInitModulation, const uint8_t szMaxTargets, const uint8_t *pbtInitiatorData, const size_t szInitiatorDataLen, uint8_t *pbtTargetsData, size_t *pszTargetsData, int timeout); -bool pn53x_InDeselect (struct nfc_device *pnd, const uint8_t ui8Target); -bool pn53x_InRelease (struct nfc_device *pnd, const uint8_t ui8Target); +int pn53x_InDeselect (struct nfc_device *pnd, const uint8_t ui8Target); +int pn53x_InRelease (struct nfc_device *pnd, const uint8_t ui8Target); bool pn53x_InAutoPoll (struct nfc_device *pnd, const pn53x_target_type *ppttTargetTypes, const size_t szTargetTypes, const uint8_t btPollNr, const uint8_t btPeriod, nfc_target *pntTargets, size_t *pszTargetFound, @@ -350,10 +350,10 @@ bool pn53x_TgInitAsTarget (struct nfc_device *pnd, pn53x_target_mode ptm, uint8_t *pbtRx, size_t *pszRx, uint8_t *pbtModeByte); // RFConfiguration -bool pn53x_RFConfiguration__RF_field (struct nfc_device *pnd, bool bEnable); -bool pn53x_RFConfiguration__Various_timings (struct nfc_device *pnd, const uint8_t fATR_RES_Timeout, const uint8_t fRetryTimeout); -bool pn53x_RFConfiguration__MaxRtyCOM (struct nfc_device *pnd, const uint8_t MaxRtyCOM); -bool pn53x_RFConfiguration__MaxRetries (struct nfc_device *pnd, const uint8_t MxRtyATR, const uint8_t MxRtyPSL, const uint8_t MxRtyPassiveActivation); +int pn53x_RFConfiguration__RF_field (struct nfc_device *pnd, bool bEnable); +int pn53x_RFConfiguration__Various_timings (struct nfc_device *pnd, const uint8_t fATR_RES_Timeout, const uint8_t fRetryTimeout); +int pn53x_RFConfiguration__MaxRtyCOM (struct nfc_device *pnd, const uint8_t MaxRtyCOM); +int pn53x_RFConfiguration__MaxRetries (struct nfc_device *pnd, const uint8_t MxRtyATR, const uint8_t MxRtyPSL, const uint8_t MxRtyPassiveActivation); // Misc bool pn53x_check_ack_frame (struct nfc_device *pnd, const uint8_t *pbtRxFrame, const size_t szRxFrameLen); diff --git a/libnfc/drivers/pn532_uart.c b/libnfc/drivers/pn532_uart.c index efbd5df..5906eba 100644 --- a/libnfc/drivers/pn532_uart.c +++ b/libnfc/drivers/pn532_uart.c @@ -301,7 +301,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)) { + if (pn53x_SAMConfiguration (pnd, 0x01, 1000) < 0) { return false; } } diff --git a/libnfc/drivers/pn53x_usb.c b/libnfc/drivers/pn53x_usb.c index b5b0ccf..cfb0296 100644 --- a/libnfc/drivers/pn53x_usb.c +++ b/libnfc/drivers/pn53x_usb.c @@ -754,7 +754,7 @@ On ASK LoGO hardware: int pn53x_usb_set_property_bool (nfc_device *pnd, const nfc_property property, const bool bEnable) { - int res; + int res = 0; if ((res = pn53x_set_property_bool (pnd, property, bEnable)) < 0) return res; @@ -763,15 +763,15 @@ pn53x_usb_set_property_bool (nfc_device *pnd, const nfc_property property, const if (NP_ACTIVATE_FIELD == property) { /* Switch on/off LED2 and Progressive Field GPIO according to ACTIVATE_FIELD option */ log_put (LOG_CATEGORY, NFC_PRIORITY_TRACE, "Switch progressive field %s", bEnable ? "On" : "Off"); - if (!pn53x_write_register (pnd, PN53X_SFR_P3, _BV(P31) | _BV(P34), bEnable ? _BV (P34) : _BV (P31))) + if ((res = pn53x_write_register (pnd, PN53X_SFR_P3, _BV(P31) | _BV(P34), bEnable ? _BV (P34) : _BV (P31))) < 0) return NFC_ECHIP; } break; case SCM_SCL3711: if (NP_ACTIVATE_FIELD == property) { // Switch on/off LED according to ACTIVATE_FIELD option - if (!pn53x_write_register (pnd, PN53X_SFR_P3, _BV (P32), bEnable ? 0 : _BV (P32))) - return NFC_ECHIP; + if ((res = pn53x_write_register (pnd, PN53X_SFR_P3, _BV (P32), bEnable ? 0 : _BV (P32))) < 0) + return res; } break; default: diff --git a/test/test_register_access.c b/test/test_register_access.c index 8bcab33..324ffc3 100644 --- a/test/test_register_access.c +++ b/test/test_register_access.c @@ -26,7 +26,7 @@ test_register_endianness (void) /* Set a 0xAA test value in writable register memory to test register access */ res = pn53x_write_register (device, PN53X_REG_CIU_TxMode, 0xFF, 0xAA); - cut_assert_true (res, cut_message ("write register value to 0xAA")); + cut_assert_equal_int (0, res, cut_message ("write register value to 0xAA")); /* Get test value from register memory */ res = pn53x_read_register (device, PN53X_REG_CIU_TxMode, &value); @@ -35,7 +35,7 @@ test_register_endianness (void) /* Set a 0x55 test value in writable register memory to test register access */ res = pn53x_write_register (device, PN53X_REG_CIU_TxMode, 0xFF, 0x55); - cut_assert_true (res, cut_message ("write register value to 0x55")); + cut_assert_equal_int (0, res, cut_message ("write register value to 0x55")); /* Get test value from register memory */ res = pn53x_read_register (device, PN53X_REG_CIU_TxMode, &value); From c5f05f05927068f80aa2d2a4e47238704c3d7233 Mon Sep 17 00:00:00 2001 From: Audrey Diacre Date: Tue, 20 Dec 2011 13:37:54 +0000 Subject: [PATCH 035/113] pn53x_set_parameters() function returns now libnfc error code. --- libnfc/chips/pn53x.c | 17 +++++++++-------- libnfc/chips/pn53x.h | 4 ++-- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/libnfc/chips/pn53x.c b/libnfc/chips/pn53x.c index 55a5c94..8d2495c 100644 --- a/libnfc/chips/pn53x.c +++ b/libnfc/chips/pn53x.c @@ -74,7 +74,7 @@ pn53x_init(struct nfc_device *pnd) // We can't read these parameters, so we set a default config by using the SetParameters wrapper // Note: pn53x_SetParameters() will save the sent value in pnd->ui8Parameters cache - if(!pn53x_SetParameters(pnd, PARAM_AUTO_ATR_RES | PARAM_AUTO_RATS)) { + if(pn53x_SetParameters(pnd, PARAM_AUTO_ATR_RES | PARAM_AUTO_RATS) < 0) { return false; } @@ -195,14 +195,14 @@ pn53x_transceive (struct nfc_device *pnd, const uint8_t *pbtTx, const size_t szT return ((0 == CHIP_DATA(pnd)->last_status_byte) ? NFC_SUCCESS : NFC_ECHIP); } -bool +int pn53x_set_parameters (struct nfc_device *pnd, const uint8_t ui8Parameter, const bool bEnable) { uint8_t ui8Value = (bEnable) ? (CHIP_DATA (pnd)->ui8Parameters | ui8Parameter) : (CHIP_DATA (pnd)->ui8Parameters & ~(ui8Parameter)); if (ui8Value != CHIP_DATA (pnd)->ui8Parameters) { return pn53x_SetParameters(pnd, ui8Value); } - return true; + return NFC_SUCCESS; } bool @@ -761,7 +761,7 @@ pn53x_set_property_bool (struct nfc_device *pnd, const nfc_property property, co // Nothing to do return NFC_SUCCESS; pnd->bAutoIso14443_4 = bEnable; - if (pn53x_set_parameters (pnd, PARAM_AUTO_RATS, bEnable)) + if (pn53x_set_parameters (pnd, PARAM_AUTO_RATS, bEnable) == 0) return NFC_SUCCESS; break; @@ -2010,17 +2010,18 @@ pn53x_RFConfiguration__MaxRetries (struct nfc_device *pnd, const uint8_t MxRtyAT return pn53x_transceive (pnd, abtCmd, sizeof (abtCmd), NULL, NULL, -1); } -bool +int pn53x_SetParameters (struct nfc_device *pnd, const uint8_t ui8Value) { uint8_t abtCmd[] = { SetParameters, ui8Value }; + int res = 0; - if(pn53x_transceive (pnd, abtCmd, sizeof (abtCmd), NULL, NULL, -1) < 0) { - return false; + if((res = pn53x_transceive (pnd, abtCmd, sizeof (abtCmd), NULL, NULL, -1)) < 0) { + return res; } // We save last parameters in register cache CHIP_DATA (pnd)->ui8Parameters = ui8Value; - return true; + return NFC_SUCCESS; } int diff --git a/libnfc/chips/pn53x.h b/libnfc/chips/pn53x.h index df1b846..30ea7ba 100644 --- a/libnfc/chips/pn53x.h +++ b/libnfc/chips/pn53x.h @@ -266,7 +266,7 @@ extern const uint8_t pn53x_nack_frame[6]; bool pn53x_init(struct nfc_device *pnd); int pn53x_transceive (struct nfc_device *pnd, const uint8_t *pbtTx, const size_t szTx, uint8_t *pbtRx, size_t *pszRx, int timeout); -bool pn53x_set_parameters (struct nfc_device *pnd, const uint8_t ui8Value, const bool bEnable); +int pn53x_set_parameters (struct nfc_device *pnd, const uint8_t ui8Value, const bool bEnable); bool pn53x_set_tx_bits (struct nfc_device *pnd, const uint8_t ui8Bits); bool pn53x_wrap_frame (const uint8_t *pbtTx, const size_t szTxBits, const uint8_t *pbtTxPar, uint8_t *pbtFrame, size_t *pszFrameBits); @@ -322,7 +322,7 @@ bool pn53x_target_send_bytes (struct nfc_device *pnd, const uint8_t *pbtTx, c const char *pn53x_strerror (const struct nfc_device *pnd); // C wrappers for PN53x commands -bool pn53x_SetParameters (struct nfc_device *pnd, const uint8_t ui8Value); +int pn53x_SetParameters (struct nfc_device *pnd, const uint8_t ui8Value); int pn53x_SAMConfiguration (struct nfc_device *pnd, const pn532_sam_mode mode, int timeout); int pn53x_PowerDown (struct nfc_device *pnd); int pn53x_InListPassiveTarget (struct nfc_device *pnd, const pn53x_modulation pmInitModulation, From 42276ccd14d5474bbdc162fd653de52b97821293 Mon Sep 17 00:00:00 2001 From: Audrey Diacre Date: Tue, 20 Dec 2011 14:41:17 +0000 Subject: [PATCH 036/113] the following functions now return libnfc error code: _ pn53x_set_tx_bits _ pn53x_read_register _ pn53x_InAutoPoll _ pn53x_TgInitAsTarget _pn53x_init --- libnfc/chips/pn53x.c | 84 ++++++++++++++++++--------------- libnfc/chips/pn53x.h | 10 ++-- libnfc/drivers/pn53x_usb.c | 2 +- test/test_register_access.c | 6 +-- test/test_register_endianness.c | 6 +-- 5 files changed, 57 insertions(+), 51 deletions(-) diff --git a/libnfc/chips/pn53x.c b/libnfc/chips/pn53x.c index 8d2495c..9ddce62 100644 --- a/libnfc/chips/pn53x.c +++ b/libnfc/chips/pn53x.c @@ -51,20 +51,21 @@ const uint8_t pn53x_nack_frame[] = { 0x00, 0x00, 0xff, 0xff, 0x00, 0x00 }; static const uint8_t pn53x_error_frame[] = { 0x00, 0x00, 0xff, 0x01, 0xff, 0x7f, 0x81, 0x00 }; /* prototypes */ -bool pn53x_reset_settings (struct nfc_device *pnd); +int pn53x_reset_settings (struct nfc_device *pnd); bool pn53x_writeback_register (struct nfc_device *pnd); nfc_modulation pn53x_ptt_to_nm (const pn53x_target_type ptt); pn53x_modulation pn53x_nm_to_pm (const nfc_modulation nm); pn53x_target_type pn53x_nm_to_ptt (const nfc_modulation nm); -bool +int pn53x_init(struct nfc_device *pnd) { + int res = 0; // GetFirmwareVersion command is used to set PN53x chips type (PN531, PN532 or PN533) char abtFirmwareText[22]; if (!pn53x_get_firmware_version (pnd, abtFirmwareText)) { - return false; + return NFC_ECHIP; } // CRC handling should be enabled by default as declared in nfc_device_new @@ -74,12 +75,12 @@ pn53x_init(struct nfc_device *pnd) // We can't read these parameters, so we set a default config by using the SetParameters wrapper // Note: pn53x_SetParameters() will save the sent value in pnd->ui8Parameters cache - if(pn53x_SetParameters(pnd, PARAM_AUTO_ATR_RES | PARAM_AUTO_RATS) < 0) { - return false; + if((res = pn53x_SetParameters(pnd, PARAM_AUTO_ATR_RES | PARAM_AUTO_RATS)) < 0) { + return res; } - if (!pn53x_reset_settings(pnd)) { - return false; + if ((res = pn53x_reset_settings(pnd)) < 0) { + return res; } // Add the firmware revision to the device name @@ -87,18 +88,19 @@ pn53x_init(struct nfc_device *pnd) pcName = strdup (pnd->acName); snprintf (pnd->acName, DEVICE_NAME_LENGTH - 1, "%s - %s", pcName, abtFirmwareText); free (pcName); - return true; + return NFC_SUCCESS; } -bool +int pn53x_reset_settings(struct nfc_device *pnd) { + int res = 0; // Reset the ending transmission bits register, it is unknown what the last tranmission used there CHIP_DATA (pnd)->ui8TxBits = 0; - if (pn53x_write_register (pnd, PN53X_REG_CIU_BitFraming, SYMBOL_TX_LAST_BITS, 0x00) < 0) { - return false; + if ((res = pn53x_write_register (pnd, PN53X_REG_CIU_BitFraming, SYMBOL_TX_LAST_BITS, 0x00)) < 0) { + return res; } - return true; + return NFC_SUCCESS; } int @@ -205,19 +207,20 @@ pn53x_set_parameters (struct nfc_device *pnd, const uint8_t ui8Parameter, const return NFC_SUCCESS; } -bool +int pn53x_set_tx_bits (struct nfc_device *pnd, const uint8_t ui8Bits) { + int res = 0; // Test if we need to update the transmission bits register setting if (CHIP_DATA (pnd)->ui8TxBits != ui8Bits) { // Set the amount of transmission bits in the PN53X chip register - if (pn53x_write_register (pnd, PN53X_REG_CIU_BitFraming, SYMBOL_TX_LAST_BITS, ui8Bits) < 0) - return false; + if ((res = pn53x_write_register (pnd, PN53X_REG_CIU_BitFraming, SYMBOL_TX_LAST_BITS, ui8Bits)) < 0) + return res; // Store the new setting CHIP_DATA (pnd)->ui8TxBits = ui8Bits; } - return true; + return NFC_SUCCESS; } bool @@ -467,16 +470,17 @@ pn53x_decode_target_data (const uint8_t *pbtRawData, size_t szRawData, pn53x_typ return true; } -bool +int pn53x_ReadRegister (struct nfc_device *pnd, uint16_t ui16RegisterAddress, uint8_t *ui8Value) { uint8_t abtCmd[] = { ReadRegister, ui16RegisterAddress >> 8, ui16RegisterAddress & 0xff }; uint8_t abtRegValue[2]; size_t szRegValue = sizeof (abtRegValue); + int res = 0; PNREG_TRACE (ui16RegisterAddress); - if (pn53x_transceive (pnd, abtCmd, sizeof (abtCmd), abtRegValue, &szRegValue, -1) < 0) { - return false; + if ((res = pn53x_transceive (pnd, abtCmd, sizeof (abtCmd), abtRegValue, &szRegValue, -1)) < 0) { + return res; } if (CHIP_DATA(pnd)->type == PN533) { // PN533 prepends its answer by a status byte @@ -484,10 +488,10 @@ pn53x_ReadRegister (struct nfc_device *pnd, uint16_t ui16RegisterAddress, uint8_ } else { *ui8Value = abtRegValue[0]; } - return true; + return NFC_SUCCESS; } -bool pn53x_read_register (struct nfc_device *pnd, uint16_t ui16RegisterAddress, uint8_t *ui8Value) +int pn53x_read_register (struct nfc_device *pnd, uint16_t ui16RegisterAddress, uint8_t *ui8Value) { return pn53x_ReadRegister (pnd, ui16RegisterAddress, ui8Value); } @@ -503,12 +507,13 @@ pn53x_WriteRegister (struct nfc_device *pnd, const uint16_t ui16RegisterAddress, int pn53x_write_register (struct nfc_device *pnd, const uint16_t ui16RegisterAddress, const uint8_t ui8SymbolMask, const uint8_t ui8Value) { + int res = 0; if ((ui16RegisterAddress < PN53X_CACHE_REGISTER_MIN_ADDRESS) || (ui16RegisterAddress > PN53X_CACHE_REGISTER_MAX_ADDRESS)) { // Direct write if (ui8SymbolMask != 0xff) { uint8_t ui8CurrentValue; - if (!pn53x_read_register (pnd, ui16RegisterAddress, &ui8CurrentValue)) - return NFC_ECHIP; + if ((res = pn53x_read_register (pnd, ui16RegisterAddress, &ui8CurrentValue)) < 0) + return res; uint8_t ui8NewValue = ((ui8Value & ui8SymbolMask) | (ui8CurrentValue & (~ui8SymbolMask))); if (ui8NewValue != ui8CurrentValue) { return pn53x_WriteRegister (pnd, ui16RegisterAddress, ui8NewValue); @@ -1036,7 +1041,7 @@ pn53x_initiator_poll_target (struct nfc_device *pnd, } size_t szTargetFound = 0; nfc_target ntTargets[2]; - if (!pn53x_InAutoPoll (pnd, apttTargetTypes, szTargetTypes, uiPollNr, uiPeriod, ntTargets, &szTargetFound, 0)) + if (pn53x_InAutoPoll (pnd, apttTargetTypes, szTargetTypes, uiPollNr, uiPeriod, ntTargets, &szTargetFound, 0) < 0) return false; switch (szTargetFound) { case 1: @@ -1133,7 +1138,7 @@ pn53x_initiator_transceive_bits (struct nfc_device *pnd, const uint8_t *pbtTx, c memcpy (abtCmd + 1, pbtTx, szFrameBytes); // Set the amount of transmission bits in the PN53X chip register - if (!pn53x_set_tx_bits (pnd, ui8Bits)) + if (pn53x_set_tx_bits (pnd, ui8Bits) < 0) return false; // Send the frame to the PN53X chip and get the answer @@ -1144,7 +1149,7 @@ pn53x_initiator_transceive_bits (struct nfc_device *pnd, const uint8_t *pbtTx, c return false; // Get the last bit-count that is stored in the received byte - if (!pn53x_read_register (pnd, PN53X_REG_CIU_Control, &ui8rcc)) + if (pn53x_read_register (pnd, PN53X_REG_CIU_Control, &ui8rcc) < 0) return false; ui8Bits = ui8rcc & SYMBOL_RX_LAST_BITS; @@ -1194,7 +1199,7 @@ pn53x_initiator_transceive_bytes (struct nfc_device *pnd, const uint8_t *pbtTx, } // To transfer command frames bytes we can not have any leading bits, reset this to zero - if (!pn53x_set_tx_bits (pnd, 0)) { + if (pn53x_set_tx_bits (pnd, 0) < 0) { pnd->last_error = NFC_EIO; // FIXME pn53x_set_tx_bits should return an integer return pnd->last_error; } @@ -1673,8 +1678,8 @@ pn53x_target_init (struct nfc_device *pnd, nfc_target *pnt, uint8_t *pbtRx, size while (!targetActivated) { uint8_t btActivatedMode; - if(!pn53x_TgInitAsTarget(pnd, ptm, pbtMifareParams, pbtTkt, szTkt, pbtFeliCaParams, pbtNFCID3t, pbtGBt, szGBt, pbtRx, pszRx, &btActivatedMode)) { - return NFC_ECHIP; + if((res = pn53x_TgInitAsTarget(pnd, ptm, pbtMifareParams, pbtTkt, szTkt, pbtFeliCaParams, pbtNFCID3t, pbtGBt, szGBt, pbtRx, pszRx, &btActivatedMode)) < 0) { + return res; } nfc_modulation nm = { @@ -1754,7 +1759,7 @@ pn53x_target_receive_bits (struct nfc_device *pnd, uint8_t *pbtRx, size_t *pszRx // Get the last bit-count that is stored in the received byte uint8_t ui8rcc; - if (!pn53x_read_register (pnd, PN53X_REG_CIU_Control, &ui8rcc)) + if (pn53x_read_register (pnd, PN53X_REG_CIU_Control, &ui8rcc) < 0) return false; uint8_t ui8Bits = ui8rcc & SYMBOL_RX_LAST_BITS; @@ -1852,7 +1857,7 @@ pn53x_target_send_bits (struct nfc_device *pnd, const uint8_t *pbtTx, const size memcpy (abtCmd + 1, pbtTx, szFrameBytes); // Set the amount of transmission bits in the PN53X chip register - if (!pn53x_set_tx_bits (pnd, ui8Bits)) + if (pn53x_set_tx_bits (pnd, ui8Bits) < 0) return false; // Try to send the bits to the reader @@ -2173,7 +2178,7 @@ pn53x_InRelease (struct nfc_device *pnd, const uint8_t ui8Target) return (pn53x_transceive (pnd, abtCmd, sizeof (abtCmd), NULL, NULL, -1)); } -bool +int pn53x_InAutoPoll (struct nfc_device *pnd, const pn53x_target_type *ppttTargetTypes, const size_t szTargetTypes, const uint8_t btPollNr, const uint8_t btPeriod, nfc_target * pntTargets, size_t *pszTargetFound, @@ -2182,7 +2187,7 @@ pn53x_InAutoPoll (struct nfc_device *pnd, if (CHIP_DATA(pnd)->type != PN532) { // This function is not supported by pn531 neither pn533 pnd->last_error = NFC_EDEVNOTSUPP; - return false; + return pnd->last_error; } // InAutoPoll frame looks like this { 0xd4, 0x60, 0x0f, 0x01, 0x00 } => { direction, command, pollnr, period, types... } @@ -2197,7 +2202,7 @@ pn53x_InAutoPoll (struct nfc_device *pnd, int res = pn53x_transceive (pnd, abtCmd, szTxInAutoPoll, abtRx, &szRx, timeout); if (res < 0) { - return false; + return res; } else if (szRx > 0) { *pszTargetFound = abtRx[0]; if (*pszTargetFound) { @@ -2223,7 +2228,7 @@ pn53x_InAutoPoll (struct nfc_device *pnd, } } } - return true; + return NFC_SUCCESS; } /** @@ -2332,7 +2337,7 @@ pn53x_InJumpForDEP (struct nfc_device *pnd, return true; } -bool +int pn53x_TgInitAsTarget (struct nfc_device *pnd, pn53x_target_mode ptm, const uint8_t *pbtMifareParams, const uint8_t *pbtTkt, size_t szTkt, @@ -2342,6 +2347,7 @@ pn53x_TgInitAsTarget (struct nfc_device *pnd, pn53x_target_mode ptm, { uint8_t abtCmd[39 + 47 + 48] = { TgInitAsTarget }; // Worst case: 39-byte base, 47 bytes max. for General Bytes, 48 bytes max. for Historical Bytes size_t szOptionalBytes = 0; + int res = 0; // Clear the target init struct, reset to all zeros memset (abtCmd + 1, 0x00, sizeof (abtCmd) - 1); @@ -2386,8 +2392,8 @@ pn53x_TgInitAsTarget (struct nfc_device *pnd, pn53x_target_mode ptm, // Request the initialization as a target uint8_t abtRx[PN53x_EXTENDED_FRAME__DATA_MAX_LEN]; size_t szRx = sizeof (abtRx); - if (pn53x_transceive (pnd, abtCmd, 36 + szOptionalBytes, abtRx, &szRx, -1) < 0) - return false; + if ((res = pn53x_transceive (pnd, abtCmd, 36 + szOptionalBytes, abtRx, &szRx, -1)) < 0) + return res; // Note: the first byte is skip: // its the "mode" byte which contains baudrate, DEP and Framing type (Mifare, active or FeliCa) datas. @@ -2400,7 +2406,7 @@ pn53x_TgInitAsTarget (struct nfc_device *pnd, pn53x_target_mode ptm, // Copy the received bytes memcpy (pbtRx, abtRx + 1, *pszRx); - return true; + return NFC_SUCCESS; } bool diff --git a/libnfc/chips/pn53x.h b/libnfc/chips/pn53x.h index 30ea7ba..39597eb 100644 --- a/libnfc/chips/pn53x.h +++ b/libnfc/chips/pn53x.h @@ -263,11 +263,11 @@ typedef enum { extern const uint8_t pn53x_ack_frame[6]; extern const uint8_t pn53x_nack_frame[6]; -bool pn53x_init(struct nfc_device *pnd); +int pn53x_init(struct nfc_device *pnd); int pn53x_transceive (struct nfc_device *pnd, const uint8_t *pbtTx, const size_t szTx, uint8_t *pbtRx, size_t *pszRx, int timeout); int pn53x_set_parameters (struct nfc_device *pnd, const uint8_t ui8Value, const bool bEnable); -bool pn53x_set_tx_bits (struct nfc_device *pnd, const uint8_t ui8Bits); +int pn53x_set_tx_bits (struct nfc_device *pnd, const uint8_t ui8Bits); bool pn53x_wrap_frame (const uint8_t *pbtTx, const size_t szTxBits, const uint8_t *pbtTxPar, uint8_t *pbtFrame, size_t *pszFrameBits); bool pn53x_unwrap_frame (const uint8_t *pbtFrame, const size_t szFrameBits, uint8_t *pbtRx, size_t *pszRxBits, @@ -275,7 +275,7 @@ bool pn53x_unwrap_frame (const uint8_t *pbtFrame, const size_t szFrameBits, u bool pn53x_decode_target_data (const uint8_t *pbtRawData, size_t szRawData, pn53x_type chip_type, nfc_modulation_type nmt, nfc_target_info *pnti); -bool pn53x_read_register (struct nfc_device *pnd, uint16_t ui16Reg, uint8_t *ui8Value); +int pn53x_read_register (struct nfc_device *pnd, uint16_t ui16Reg, uint8_t *ui8Value); int pn53x_write_register (struct nfc_device *pnd, uint16_t ui16Reg, uint8_t ui8SymbolMask, uint8_t ui8Value); bool pn53x_get_firmware_version (struct nfc_device *pnd, char abtFirmwareText[22]); int pn53x_set_property_int (struct nfc_device *pnd, const nfc_property property, const int value); @@ -331,7 +331,7 @@ int pn53x_InListPassiveTarget (struct nfc_device *pnd, const pn53x_modulation int timeout); int pn53x_InDeselect (struct nfc_device *pnd, const uint8_t ui8Target); int pn53x_InRelease (struct nfc_device *pnd, const uint8_t ui8Target); -bool pn53x_InAutoPoll (struct nfc_device *pnd, const pn53x_target_type *ppttTargetTypes, const size_t szTargetTypes, +int pn53x_InAutoPoll (struct nfc_device *pnd, const pn53x_target_type *ppttTargetTypes, const size_t szTargetTypes, const uint8_t btPollNr, const uint8_t btPeriod, nfc_target *pntTargets, size_t *pszTargetFound, const int timeout); @@ -342,7 +342,7 @@ bool pn53x_InJumpForDEP (struct nfc_device *pnd, const uint8_t *pbtGB, const size_t szGB, nfc_target *pnt, const int timeout); -bool pn53x_TgInitAsTarget (struct nfc_device *pnd, pn53x_target_mode ptm, +int pn53x_TgInitAsTarget (struct nfc_device *pnd, pn53x_target_mode ptm, const uint8_t *pbtMifareParams, const uint8_t *pbtTkt, size_t szTkt, const uint8_t *pbtFeliCaParams, diff --git a/libnfc/drivers/pn53x_usb.c b/libnfc/drivers/pn53x_usb.c index cfb0296..1ec7524 100644 --- a/libnfc/drivers/pn53x_usb.c +++ b/libnfc/drivers/pn53x_usb.c @@ -710,7 +710,7 @@ pn53x_usb_init (nfc_device *pnd) pn53x_usb_ack (pnd); } - if (!pn53x_init (pnd)) + if (pn53x_init (pnd) < 0) return false; if (ASK_LOGO == DRIVER_DATA (pnd)->model) { diff --git a/test/test_register_access.c b/test/test_register_access.c index 324ffc3..9b57222 100644 --- a/test/test_register_access.c +++ b/test/test_register_access.c @@ -11,7 +11,7 @@ test_register_endianness (void) { nfc_connstring connstrings[MAX_DEVICE_COUNT]; size_t device_count; - bool res; + int res = 0; nfc_list_devices (connstrings, MAX_DEVICE_COUNT, &device_count); if (!device_count) @@ -30,7 +30,7 @@ test_register_endianness (void) /* Get test value from register memory */ res = pn53x_read_register (device, PN53X_REG_CIU_TxMode, &value); - cut_assert_true (res, cut_message ("read register value")); + cut_assert_equal_int (0, res, cut_message ("read register value")); cut_assert_equal_uint (0xAA, value, cut_message ("check register value")); /* Set a 0x55 test value in writable register memory to test register access */ @@ -39,7 +39,7 @@ test_register_endianness (void) /* Get test value from register memory */ res = pn53x_read_register (device, PN53X_REG_CIU_TxMode, &value); - cut_assert_true (res, cut_message ("read register value")); + cut_assert_equal_int (0, res, cut_message ("read register value")); cut_assert_equal_uint (0x55, value, cut_message ("check register value")); nfc_disconnect (device); diff --git a/test/test_register_endianness.c b/test/test_register_endianness.c index 479837b..150c840 100644 --- a/test/test_register_endianness.c +++ b/test/test_register_endianness.c @@ -12,7 +12,7 @@ test_register_endianness (void) { nfc_connstring connstrings[MAX_DEVICE_COUNT]; size_t device_count; - bool res; + int res = 0; nfc_list_devices (connstrings, MAX_DEVICE_COUNT, &device_count); if (!device_count) @@ -27,11 +27,11 @@ test_register_endianness (void) /* Read valid XRAM memory */ res = pn53x_read_register (device, 0xF0FF, &value); - cut_assert_true (res, cut_message ("read register 0xF0FF")); + cut_assert_equal_int (0, res, cut_message ("read register 0xF0FF")); /* Read invalid SFR register */ res = pn53x_read_register (device, 0xFFF0, &value); - cut_assert_false (res, cut_message ("read register 0xFFF0")); + cut_assert_equal_int (0, res, cut_message ("read register 0xFFF0")); nfc_disconnect (device); } From 0f5cc5683d63bedeb9d2381acbf87dbcda4e79f7 Mon Sep 17 00:00:00 2001 From: Audrey Diacre Date: Tue, 20 Dec 2011 15:46:35 +0000 Subject: [PATCH 037/113] nfc_initiator_poll_target() function returns now libnfc error code. --- examples/nfc-poll.c | 4 ++-- include/nfc/nfc.h | 2 +- libnfc/chips/pn53x.c | 22 ++++++++++++---------- libnfc/chips/pn53x.h | 4 ++-- libnfc/nfc-internal.h | 6 +++--- libnfc/nfc.c | 4 ++-- 6 files changed, 22 insertions(+), 20 deletions(-) diff --git a/examples/nfc-poll.c b/examples/nfc-poll.c index fbb1893..7c7d206 100644 --- a/examples/nfc-poll.c +++ b/examples/nfc-poll.c @@ -90,7 +90,7 @@ main (int argc, const char *argv[]) const size_t szModulations = 5; nfc_target nt; - bool res; + int res = 0; pnd = nfc_connect (NULL); @@ -109,7 +109,7 @@ main (int argc, const char *argv[]) exit (EXIT_FAILURE); } - if (res > 0) { + if (res == 0) { print_nfc_target ( nt, verbose ); } else { printf ("No target found.\n"); diff --git a/include/nfc/nfc.h b/include/nfc/nfc.h index 61e7410..9ef9bd0 100644 --- a/include/nfc/nfc.h +++ b/include/nfc/nfc.h @@ -74,7 +74,7 @@ extern "C" { NFC_EXPORT int nfc_initiator_init (nfc_device *pnd); NFC_EXPORT int nfc_initiator_select_passive_target (nfc_device *pnd, const nfc_modulation nm, const uint8_t *pbtInitData, const size_t szInitData, nfc_target *pnt); NFC_EXPORT int nfc_initiator_list_passive_targets (nfc_device *pnd, const nfc_modulation nm, nfc_target ant[], const size_t szTargets); - NFC_EXPORT bool nfc_initiator_poll_target (nfc_device *pnd, const nfc_modulation *pnmTargetTypes, const size_t szTargetTypes, const uint8_t uiPollNr, const uint8_t uiPeriod, nfc_target *pnt); + NFC_EXPORT int nfc_initiator_poll_target (nfc_device *pnd, const nfc_modulation *pnmTargetTypes, const size_t szTargetTypes, const uint8_t uiPollNr, const uint8_t uiPeriod, nfc_target *pnt); NFC_EXPORT bool nfc_initiator_select_dep_target (nfc_device *pnd, const nfc_dep_mode ndm, const nfc_baud_rate nbr, const nfc_dep_info *pndiInitiator, nfc_target *pnt, const int timeout); NFC_EXPORT bool nfc_initiator_deselect_target (nfc_device *pnd); NFC_EXPORT int nfc_initiator_transceive_bytes (nfc_device *pnd, const uint8_t *pbtTx, const size_t szTx, uint8_t *pbtRx, size_t *pszRx, int timeout); diff --git a/libnfc/chips/pn53x.c b/libnfc/chips/pn53x.c index 9ddce62..82b8943 100644 --- a/libnfc/chips/pn53x.c +++ b/libnfc/chips/pn53x.c @@ -1016,12 +1016,14 @@ pn53x_initiator_select_passive_target (struct nfc_device *pnd, return pn53x_initiator_select_passive_target_ext (pnd, nm, pbtInitData, szInitData, pnt, 0); } -bool +int pn53x_initiator_poll_target (struct nfc_device *pnd, const nfc_modulation *pnmModulations, const size_t szModulations, const uint8_t uiPollNr, const uint8_t uiPeriod, nfc_target *pnt) { + int res = 0; + if (CHIP_DATA(pnd)->type == PN532) { size_t szTargetTypes = 0; pn53x_target_type apttTargetTypes[32]; @@ -1029,7 +1031,7 @@ pn53x_initiator_poll_target (struct nfc_device *pnd, const pn53x_target_type ptt = pn53x_nm_to_ptt(pnmModulations[n]); if (PTT_UNDEFINED == ptt) { pnd->last_error = NFC_EINVARG; - return false; + return pnd->last_error; } apttTargetTypes[szTargetTypes] = ptt; if ((pnd->bAutoIso14443_4) && (ptt == PTT_MIFARE)) { // Hack to have ATS @@ -1041,19 +1043,19 @@ pn53x_initiator_poll_target (struct nfc_device *pnd, } size_t szTargetFound = 0; nfc_target ntTargets[2]; - if (pn53x_InAutoPoll (pnd, apttTargetTypes, szTargetTypes, uiPollNr, uiPeriod, ntTargets, &szTargetFound, 0) < 0) - return false; + if ((res = pn53x_InAutoPoll (pnd, apttTargetTypes, szTargetTypes, uiPollNr, uiPeriod, ntTargets, &szTargetFound, 0)) < 0) + return res; switch (szTargetFound) { case 1: *pnt = ntTargets[0]; - return true; + return NFC_SUCCESS; break; case 2: *pnt = ntTargets[1]; // We keep the selected one - return true; + return NFC_SUCCESS; break; default: - return false; + return NFC_ECHIP; break; } } else { @@ -1069,15 +1071,15 @@ pn53x_initiator_poll_target (struct nfc_device *pnd, if (pn53x_initiator_select_passive_target_ext (pnd, pnmModulations[n], pbtInitiatorData, szInitiatorData, pnt, timeout_ms) < 0) { if (pnd->last_error != NFC_ETIMEOUT) - return false; + return pnd->last_error; } else { - return true; + return NFC_SUCCESS; } } } } while (uiPollNr==0xff); // uiPollNr==0xff means infinite polling } - return false; + return NFC_ECHIP; } bool diff --git a/libnfc/chips/pn53x.h b/libnfc/chips/pn53x.h index 39597eb..4010a77 100644 --- a/libnfc/chips/pn53x.h +++ b/libnfc/chips/pn53x.h @@ -285,12 +285,12 @@ bool pn53x_check_communication (struct nfc_device *pnd); bool pn53x_idle (struct nfc_device *pnd); // NFC device as Initiator functions -int pn53x_initiator_init (struct nfc_device *pnd); +int pn53x_initiator_init (struct nfc_device *pnd); int pn53x_initiator_select_passive_target (struct nfc_device *pnd, const nfc_modulation nm, const uint8_t *pbtInitData, const size_t szInitData, nfc_target *pnt); -bool pn53x_initiator_poll_target (struct nfc_device *pnd, +int pn53x_initiator_poll_target (struct nfc_device *pnd, const nfc_modulation *pnmModulations, const size_t szModulations, const uint8_t uiPollNr, const uint8_t uiPeriod, nfc_target *pnt); diff --git a/libnfc/nfc-internal.h b/libnfc/nfc-internal.h index a5846c5..3f2919f 100644 --- a/libnfc/nfc-internal.h +++ b/libnfc/nfc-internal.h @@ -132,12 +132,12 @@ struct nfc_driver_t { void (*disconnect) (struct nfc_device *pnd); const char *(*strerror) (const struct nfc_device *pnd); - int (*initiator_init) (struct nfc_device *pnd); + int (*initiator_init) (struct nfc_device *pnd); int (*initiator_select_passive_target) (struct nfc_device *pnd, const nfc_modulation nm, const uint8_t * pbtInitData, const size_t szInitData, nfc_target * pnt); - bool (*initiator_poll_target) (struct nfc_device *pnd, const nfc_modulation * pnmModulations, const size_t szModulations, const uint8_t uiPollNr, const uint8_t btPeriod, nfc_target * pnt); + int (*initiator_poll_target) (struct nfc_device *pnd, const nfc_modulation * pnmModulations, const size_t szModulations, const uint8_t uiPollNr, const uint8_t btPeriod, nfc_target * pnt); bool (*initiator_select_dep_target) (struct nfc_device *pnd, const nfc_dep_mode ndm, const nfc_baud_rate nbr, const nfc_dep_info * pndiInitiator, nfc_target * pnt, const int timeout); bool (*initiator_deselect_target) (struct nfc_device *pnd); - int (*initiator_transceive_bytes) (struct nfc_device *pnd, const uint8_t * pbtTx, const size_t szTx, uint8_t * pbtRx, size_t * pszRx, int timeout); + int (*initiator_transceive_bytes) (struct nfc_device *pnd, const uint8_t * pbtTx, const size_t szTx, uint8_t * pbtRx, size_t * pszRx, int timeout); bool (*initiator_transceive_bits) (struct nfc_device *pnd, const uint8_t * pbtTx, const size_t szTxBits, const uint8_t * pbtTxPar, uint8_t * pbtRx, size_t * pszRxBits, uint8_t * pbtRxPar); bool (*initiator_transceive_bytes_timed) (struct nfc_device *pnd, const uint8_t * pbtTx, const size_t szTx, uint8_t * pbtRx, size_t * pszRx, uint32_t * cycles); bool (*initiator_transceive_bits_timed) (struct nfc_device *pnd, const uint8_t * pbtTx, const size_t szTxBits, const uint8_t * pbtTxPar, uint8_t * pbtRx, size_t * pszRxBits, uint8_t * pbtRxPar, uint32_t * cycles); diff --git a/libnfc/nfc.c b/libnfc/nfc.c index fbbfc40..e2eb1cd 100644 --- a/libnfc/nfc.c +++ b/libnfc/nfc.c @@ -410,7 +410,7 @@ nfc_initiator_list_passive_targets (nfc_device *pnd, /** * @brief Polling for NFC targets - * @return Returns \c true if action was successfully performed; otherwise returns \c false. + * @return Returns 0 on success, otherwise returns libnfc's error code (negative value). * * @param pnd \a nfc_device struct pointer that represent currently used device * @param ppttTargetTypes array of desired target types @@ -421,7 +421,7 @@ nfc_initiator_list_passive_targets (nfc_device *pnd, * @note e.g. if uiPeriod=10, it will poll each desired target type during 1.5s * @param[out] pnt pointer on \a nfc_target (over)writable struct */ -bool +int nfc_initiator_poll_target (nfc_device *pnd, const nfc_modulation *pnmModulations, const size_t szModulations, const uint8_t uiPollNr, const uint8_t uiPeriod, From c41d7de8cae92a2e6ba612a99c1da3e0a1a64066 Mon Sep 17 00:00:00 2001 From: Audrey Diacre Date: Wed, 21 Dec 2011 09:15:44 +0000 Subject: [PATCH 038/113] nfc_initiator_select_dep_target() function returns nox libnf error code and fix some uses of nfc_initiator_transceive_bytes() function. --- examples/nfc-dep-initiator.c | 2 +- include/nfc/nfc.h | 2 +- libnfc/chips/pn53x.c | 17 ++++++----- libnfc/chips/pn53x.h | 4 +-- libnfc/nfc-internal.h | 2 +- libnfc/nfc.c | 4 +-- test/test_dep_active.c | 16 +++++----- test/test_dep_passive.c | 58 ++++++++++++++++++------------------ utils/nfc-mfclassic.c | 2 +- utils/nfc-mfsetuid.c | 2 +- utils/nfc-read-forum-tag3.c | 2 +- utils/nfc-relay-picc.c | 4 +-- 12 files changed, 58 insertions(+), 57 deletions(-) diff --git a/examples/nfc-dep-initiator.c b/examples/nfc-dep-initiator.c index fd3d212..2373f59 100644 --- a/examples/nfc-dep-initiator.c +++ b/examples/nfc-dep-initiator.c @@ -86,7 +86,7 @@ main (int argc, const char *argv[]) return EXIT_FAILURE; } - if(!nfc_initiator_select_dep_target (pnd, NDM_PASSIVE, NBR_212, NULL, &nt, 1000)) { + if(nfc_initiator_select_dep_target (pnd, NDM_PASSIVE, NBR_212, NULL, &nt, 1000) < 0) { nfc_perror(pnd, "nfc_initiator_select_dep_target"); goto error; } diff --git a/include/nfc/nfc.h b/include/nfc/nfc.h index 9ef9bd0..a0d75db 100644 --- a/include/nfc/nfc.h +++ b/include/nfc/nfc.h @@ -75,7 +75,7 @@ extern "C" { NFC_EXPORT int nfc_initiator_select_passive_target (nfc_device *pnd, const nfc_modulation nm, const uint8_t *pbtInitData, const size_t szInitData, nfc_target *pnt); NFC_EXPORT int nfc_initiator_list_passive_targets (nfc_device *pnd, const nfc_modulation nm, nfc_target ant[], const size_t szTargets); NFC_EXPORT int nfc_initiator_poll_target (nfc_device *pnd, const nfc_modulation *pnmTargetTypes, const size_t szTargetTypes, const uint8_t uiPollNr, const uint8_t uiPeriod, nfc_target *pnt); - NFC_EXPORT bool nfc_initiator_select_dep_target (nfc_device *pnd, const nfc_dep_mode ndm, const nfc_baud_rate nbr, const nfc_dep_info *pndiInitiator, nfc_target *pnt, const int timeout); + NFC_EXPORT int nfc_initiator_select_dep_target (nfc_device *pnd, const nfc_dep_mode ndm, const nfc_baud_rate nbr, const nfc_dep_info *pndiInitiator, nfc_target *pnt, const int timeout); NFC_EXPORT bool nfc_initiator_deselect_target (nfc_device *pnd); NFC_EXPORT int nfc_initiator_transceive_bytes (nfc_device *pnd, const uint8_t *pbtTx, const size_t szTx, uint8_t *pbtRx, size_t *pszRx, int timeout); NFC_EXPORT bool nfc_initiator_transceive_bits (nfc_device *pnd, const uint8_t *pbtTx, const size_t szTxBits, const uint8_t *pbtTxPar, uint8_t *pbtRx, size_t *pszRxBits, uint8_t *pbtRxPar); diff --git a/libnfc/chips/pn53x.c b/libnfc/chips/pn53x.c index 82b8943..e623b09 100644 --- a/libnfc/chips/pn53x.c +++ b/libnfc/chips/pn53x.c @@ -1082,7 +1082,7 @@ pn53x_initiator_poll_target (struct nfc_device *pnd, return NFC_ECHIP; } -bool +int pn53x_initiator_select_dep_target(struct nfc_device *pnd, const nfc_dep_mode ndm, const nfc_baud_rate nbr, const nfc_dep_info *pndiInitiator, @@ -2243,7 +2243,7 @@ pn53x_InAutoPoll (struct nfc_device *pnd, * @param szGBi count of General Bytes * @param[out] pnt \a nfc_target which will be filled by this function */ -bool +int pn53x_InJumpForDEP (struct nfc_device *pnd, const nfc_dep_mode ndm, const nfc_baud_rate nbr, @@ -2271,7 +2271,7 @@ pn53x_InJumpForDEP (struct nfc_device *pnd, case NBR_847: case NBR_UNDEFINED: pnd->last_error = NFC_EINVARG; - return false; + return pnd->last_error; break; } @@ -2291,7 +2291,7 @@ pn53x_InJumpForDEP (struct nfc_device *pnd, case NBR_847: case NBR_UNDEFINED: pnd->last_error = NFC_EINVARG; - return false; + return pnd->last_error; break; } } @@ -2310,13 +2310,14 @@ pn53x_InJumpForDEP (struct nfc_device *pnd, uint8_t abtRx[PN53x_EXTENDED_FRAME__DATA_MAX_LEN]; size_t szRx = sizeof (abtRx); + int res = 0; // Try to find a target, call the transceive callback function of the current device - if (pn53x_transceive (pnd, abtCmd, offset, abtRx, &szRx, timeout) < 0) - return false; + if ((res = pn53x_transceive (pnd, abtCmd, offset, abtRx, &szRx, timeout)) < 0) + return res; // Make sure one target has been found, the PN53X returns 0x00 if none was available if (abtRx[1] != 1) - return false; + return NFC_ECHIP; // Is a target struct available if (pnt) { @@ -2336,7 +2337,7 @@ pn53x_InJumpForDEP (struct nfc_device *pnd, pnt->nti.ndi.szGB = 0; } } - return true; + return NFC_SUCCESS; } int diff --git a/libnfc/chips/pn53x.h b/libnfc/chips/pn53x.h index 4010a77..33ee19b 100644 --- a/libnfc/chips/pn53x.h +++ b/libnfc/chips/pn53x.h @@ -294,7 +294,7 @@ int pn53x_initiator_poll_target (struct nfc_device *pnd, const nfc_modulation *pnmModulations, const size_t szModulations, const uint8_t uiPollNr, const uint8_t uiPeriod, nfc_target *pnt); -bool pn53x_initiator_select_dep_target (struct nfc_device *pnd, +int pn53x_initiator_select_dep_target (struct nfc_device *pnd, const nfc_dep_mode ndm, const nfc_baud_rate nbr, const nfc_dep_info *pndiInitiator, nfc_target *pnt, @@ -335,7 +335,7 @@ int pn53x_InAutoPoll (struct nfc_device *pnd, const pn53x_target_type *ppttTa const uint8_t btPollNr, const uint8_t btPeriod, nfc_target *pntTargets, size_t *pszTargetFound, const int timeout); -bool pn53x_InJumpForDEP (struct nfc_device *pnd, +int pn53x_InJumpForDEP (struct nfc_device *pnd, const nfc_dep_mode ndm, const nfc_baud_rate nbr, const uint8_t *pbtPassiveInitiatorData, const uint8_t *pbtNFCID3i, diff --git a/libnfc/nfc-internal.h b/libnfc/nfc-internal.h index 3f2919f..6ebe21c 100644 --- a/libnfc/nfc-internal.h +++ b/libnfc/nfc-internal.h @@ -135,7 +135,7 @@ struct nfc_driver_t { int (*initiator_init) (struct nfc_device *pnd); int (*initiator_select_passive_target) (struct nfc_device *pnd, const nfc_modulation nm, const uint8_t * pbtInitData, const size_t szInitData, nfc_target * pnt); int (*initiator_poll_target) (struct nfc_device *pnd, const nfc_modulation * pnmModulations, const size_t szModulations, const uint8_t uiPollNr, const uint8_t btPeriod, nfc_target * pnt); - bool (*initiator_select_dep_target) (struct nfc_device *pnd, const nfc_dep_mode ndm, const nfc_baud_rate nbr, const nfc_dep_info * pndiInitiator, nfc_target * pnt, const int timeout); + int (*initiator_select_dep_target) (struct nfc_device *pnd, const nfc_dep_mode ndm, const nfc_baud_rate nbr, const nfc_dep_info * pndiInitiator, nfc_target * pnt, const int timeout); bool (*initiator_deselect_target) (struct nfc_device *pnd); int (*initiator_transceive_bytes) (struct nfc_device *pnd, const uint8_t * pbtTx, const size_t szTx, uint8_t * pbtRx, size_t * pszRx, int timeout); bool (*initiator_transceive_bits) (struct nfc_device *pnd, const uint8_t * pbtTx, const size_t szTxBits, const uint8_t * pbtTxPar, uint8_t * pbtRx, size_t * pszRxBits, uint8_t * pbtRxPar); diff --git a/libnfc/nfc.c b/libnfc/nfc.c index e2eb1cd..46ad4ed 100644 --- a/libnfc/nfc.c +++ b/libnfc/nfc.c @@ -433,7 +433,7 @@ nfc_initiator_poll_target (nfc_device *pnd, /** * @brief Select a target and request active or passive mode for D.E.P. (Data Exchange Protocol) - * @return Returns \c true if action was successfully performed; otherwise returns \c false. + * @return Returns 0 on success, otherwise returns libnfc's error code (negative value). * * @param pnd \a nfc_device struct pointer that represent currently used device * @param ndm desired D.E.P. mode (\a NDM_ACTIVE or \a NDM_PASSIVE for active, respectively passive mode) @@ -446,7 +446,7 @@ nfc_initiator_poll_target (nfc_device *pnd, * * @note \a nfc_dep_info will be returned when the target was acquired successfully. */ -bool +int nfc_initiator_select_dep_target (nfc_device *pnd, const nfc_dep_mode ndm, const nfc_baud_rate nbr, const nfc_dep_info *pndiInitiator, nfc_target *pnt, const int timeout) diff --git a/test/test_dep_active.c b/test/test_dep_active.c index a80eefa..852939e 100644 --- a/test/test_dep_active.c +++ b/test/test_dep_active.c @@ -124,27 +124,27 @@ initiator_thread (void *arg) // Active mode printf ("=========== INITIATOR %s (Active mode / %s Kbps) =========\n", nfc_device_get_name (device), str_nfc_baud_rate(nbr)); - bool res = nfc_initiator_select_dep_target (device, NDM_ACTIVE, nbr, NULL, &nt, 1000); - cut_assert_true (res, cut_message ("Can't select any DEP target: %s", nfc_strerror (device))); + int res = nfc_initiator_select_dep_target (device, NDM_ACTIVE, nbr, NULL, &nt, 1000); + cut_assert_equal_int (0, res, cut_message ("Can't select any DEP target: %s", nfc_strerror (device))); cut_assert_equal_int (NMT_DEP, nt.nm.nmt, cut_message ("Invalid target modulation")); cut_assert_equal_int (nbr, nt.nm.nbr, cut_message ("Invalid target baud rate")); cut_assert_equal_memory ("\x11\x22\x33\x44\x55\x66\x77\x88\x99\xAA", 10, nt.nti.ndi.abtNFCID3, 10, cut_message ("Invalid target NFCID3")); cut_assert_equal_int (NDM_ACTIVE, nt.nti.ndi.ndm, cut_message ("Invalid target DEP mode")); cut_assert_equal_memory ("\x12\x34\x56\x78", 4, nt.nti.ndi.abtGB, nt.nti.ndi.szGB, cut_message ("Invalid target general bytes")); - if (!res) { thread_res = -1; return (void*) thread_res; } + if (res < 0) { thread_res = -1; return (void*) thread_res; } const uint8_t abtTx[] = "Hello DEP target!"; uint8_t abtRx[1024]; size_t szRx = sizeof (abtRx); res = nfc_initiator_transceive_bytes (device, abtTx, sizeof (abtTx), abtRx, &szRx, 5000); - cut_assert_true (res, cut_message ("Can't transceive bytes to target: %s", nfc_strerror (device))); + cut_assert_operator_int (res, >=, 0, cut_message ("Can't transceive bytes to target: %s", nfc_strerror (device))); const uint8_t abtAttRx[] = "Hello DEP initiator!"; cut_assert_equal_memory (abtAttRx, sizeof (abtAttRx), abtRx, szRx, cut_message ("Invalid received data")); - if (!res) { thread_res = -1; return (void*) thread_res; } - res = nfc_initiator_deselect_target (device); - cut_assert_true (res, cut_message ("Can't deselect target: %s", nfc_strerror (device))); - if (!res) { thread_res = -1; return (void*) thread_res; } + if (res < 0) { thread_res = -1; return (void*) thread_res; } + bool bres = nfc_initiator_deselect_target (device); + cut_assert_true (bres, cut_message ("Can't deselect target: %s", nfc_strerror (device))); + if (!bres) { thread_res = -1; return (void*) thread_res; } return (void *) thread_res; } diff --git a/test/test_dep_passive.c b/test/test_dep_passive.c index cc80ece..3bf13e5 100644 --- a/test/test_dep_passive.c +++ b/test/test_dep_passive.c @@ -157,94 +157,94 @@ initiator_thread (void *arg) // Passive mode / 106Kbps printf ("=========== INITIATOR %s (Passive mode / 106Kbps) =========\n", nfc_device_get_name (device)); - bool res = nfc_initiator_select_dep_target (device, NDM_PASSIVE, NBR_106, NULL, &nt, 5000); - cut_assert_true (res, cut_message ("Can't select any DEP target: %s", nfc_strerror (device))); + int res = nfc_initiator_select_dep_target (device, NDM_PASSIVE, NBR_106, NULL, &nt, 5000); + cut_assert_equal_int (0, res, cut_message ("Can't select any DEP target: %s", nfc_strerror (device))); cut_assert_equal_int (NMT_DEP, nt.nm.nmt, cut_message ("Invalid target modulation")); cut_assert_equal_int (NBR_106, nt.nm.nbr, cut_message ("Invalid target baud rate")); cut_assert_equal_memory ("\x11\x22\x33\x44\x55\x66\x77\x88\x99\xAA", 10, nt.nti.ndi.abtNFCID3, 10, cut_message ("Invalid target NFCID3")); cut_assert_equal_int (NDM_PASSIVE, nt.nti.ndi.ndm, cut_message ("Invalid target DEP mode")); cut_assert_equal_memory ("\x12\x34\x56\x78", 4, nt.nti.ndi.abtGB, nt.nti.ndi.szGB, cut_message ("Invalid target general bytes")); - if (!res) { thread_res = -1; return (void*) thread_res; } + if (res < 0) { thread_res = -1; return (void*) thread_res; } const uint8_t abtTx[] = "Hello DEP target!"; uint8_t abtRx[1024]; size_t szRx = sizeof (abtRx); res = nfc_initiator_transceive_bytes (device, abtTx, sizeof (abtTx), abtRx, &szRx, 500); - cut_assert_true (res, cut_message ("Can't transceive bytes to target: %s", nfc_strerror (device))); + cut_assert_operator_int (res, >=, 0, cut_message ("Can't transceive bytes to target: %s", nfc_strerror (device))); const uint8_t abtAttRx[] = "Hello DEP initiator!"; cut_assert_equal_memory (abtAttRx, sizeof (abtAttRx), abtRx, szRx, cut_message ("Invalid received data")); - if (!res) { thread_res = -1; return (void*) thread_res; } + if (res < 0) { thread_res = -1; return (void*) thread_res; } - res = nfc_initiator_deselect_target (device); - cut_assert_true (res, cut_message ("Can't deselect target: %s", nfc_strerror (device))); - if (!res) { thread_res = -1; return (void*) thread_res; } + bool bres = nfc_initiator_deselect_target (device); + cut_assert_true (bres, cut_message ("Can't deselect target: %s", nfc_strerror (device))); + if (!bres) { thread_res = -1; return (void*) thread_res; } // Passive mode / 212Kbps (second pass) printf ("=========== INITIATOR %s (Passive mode / 212Kbps) =========\n", nfc_device_get_name (device)); res = nfc_initiator_select_dep_target (device, NDM_PASSIVE, NBR_212, NULL, &nt, 1000); - cut_assert_true (res, cut_message ("Can't select any DEP target: %s", nfc_strerror (device))); + cut_assert_equal_int (0, res, cut_message ("Can't select any DEP target: %s", nfc_strerror (device))); cut_assert_equal_int (NMT_DEP, nt.nm.nmt, cut_message ("Invalid target modulation")); cut_assert_equal_int (NBR_212, nt.nm.nbr, cut_message ("Invalid target baud rate")); cut_assert_equal_memory ("\x11\x22\x33\x44\x55\x66\x77\x88\x99\xAA", 10, nt.nti.ndi.abtNFCID3, 10, cut_message ("Invalid target NFCID3")); cut_assert_equal_int (NDM_PASSIVE, nt.nti.ndi.ndm, cut_message ("Invalid target DEP mode")); cut_assert_equal_memory ("\x12\x34\x56\x78", 4, nt.nti.ndi.abtGB, nt.nti.ndi.szGB, cut_message ("Invalid target general bytes")); - if (!res) { thread_res = -1; return (void*) thread_res; } + if (res < 0) { thread_res = -1; return (void*) thread_res; } szRx = sizeof (abtRx); res = nfc_initiator_transceive_bytes (device, abtTx, sizeof (abtTx), abtRx, &szRx, 1000); - cut_assert_true (res, cut_message ("Can't transceive bytes to target: %s", nfc_strerror (device))); + cut_assert_operator_int (res, >=, 0, cut_message ("Can't transceive bytes to target: %s", nfc_strerror (device))); cut_assert_equal_memory (abtAttRx, sizeof (abtAttRx), abtRx, szRx, cut_message ("Invalid received data")); - if (!res) { thread_res = -1; return (void*) thread_res; } + if (res < 0) { thread_res = -1; return (void*) thread_res; } - res = nfc_initiator_deselect_target (device); - cut_assert_true (res, cut_message ("Can't deselect target: %s", nfc_strerror (device))); - if (!res) { thread_res = -1; return (void*) thread_res; } + bres = nfc_initiator_deselect_target (device); + cut_assert_true (bres, cut_message ("Can't deselect target: %s", nfc_strerror (device))); + if (!bres) { thread_res = -1; return (void*) thread_res; } // Passive mode / 212Kbps printf ("=========== INITIATOR %s (Passive mode / 212Kbps, second pass) =========\n", nfc_device_get_name (device)); res = nfc_initiator_select_dep_target (device, NDM_PASSIVE, NBR_212, NULL, &nt, 1000); - cut_assert_true (res, cut_message ("Can't select any DEP target: %s", nfc_strerror (device))); + cut_assert_equal_int (0, res, cut_message ("Can't select any DEP target: %s", nfc_strerror (device))); cut_assert_equal_int (NMT_DEP, nt.nm.nmt, cut_message ("Invalid target modulation")); cut_assert_equal_int (NBR_212, nt.nm.nbr, cut_message ("Invalid target baud rate")); cut_assert_equal_memory ("\x11\x22\x33\x44\x55\x66\x77\x88\x99\xAA", 10, nt.nti.ndi.abtNFCID3, 10, cut_message ("Invalid target NFCID3")); cut_assert_equal_int (NDM_PASSIVE, nt.nti.ndi.ndm, cut_message ("Invalid target DEP mode")); cut_assert_equal_memory ("\x12\x34\x56\x78", 4, nt.nti.ndi.abtGB, nt.nti.ndi.szGB, cut_message ("Invalid target general bytes")); - if (!res) { thread_res = -1; return (void*) thread_res; } + if (res < 0) { thread_res = -1; return (void*) thread_res; } szRx = sizeof (abtRx); res = nfc_initiator_transceive_bytes (device, abtTx, sizeof (abtTx), abtRx, &szRx, 5000); - cut_assert_true (res, cut_message ("Can't transceive bytes to target: %s", nfc_strerror (device))); + cut_assert_operator_int (res, >=, 0, cut_message ("Can't transceive bytes to target: %s", nfc_strerror (device))); cut_assert_equal_memory (abtAttRx, sizeof (abtAttRx), abtRx, szRx, cut_message ("Invalid received data")); - if (!res) { thread_res = -1; return (void*) thread_res; } + if (res < 0) { thread_res = -1; return (void*) thread_res; } - res = nfc_initiator_deselect_target (device); - cut_assert_true (res, cut_message ("Can't deselect target: %s", nfc_strerror (device))); - if (!res) { thread_res = -1; return (void*) thread_res; } + bres = nfc_initiator_deselect_target (device); + cut_assert_true (bres, cut_message ("Can't deselect target: %s", nfc_strerror (device))); + if (!bres) { thread_res = -1; return (void*) thread_res; } // Passive mode / 424Kbps printf ("=========== INITIATOR %s (Passive mode / 424Kbps) =========\n", nfc_device_get_name (device)); res = nfc_initiator_select_dep_target (device, NDM_PASSIVE, NBR_424, NULL, &nt, 1000); - cut_assert_true (res, cut_message ("Can't select any DEP target: %s", nfc_strerror (device))); + cut_assert_equal_int (0, res, cut_message ("Can't select any DEP target: %s", nfc_strerror (device))); cut_assert_equal_int (NMT_DEP, nt.nm.nmt, cut_message ("Invalid target modulation")); cut_assert_equal_int (NBR_424, nt.nm.nbr, cut_message ("Invalid target baud rate")); cut_assert_equal_memory ("\x11\x22\x33\x44\x55\x66\x77\x88\x99\xAA", 10, nt.nti.ndi.abtNFCID3, 10, cut_message ("Invalid target NFCID3")); cut_assert_equal_int (NDM_PASSIVE, nt.nti.ndi.ndm, cut_message ("Invalid target DEP mode")); cut_assert_equal_memory ("\x12\x34\x56\x78", 4, nt.nti.ndi.abtGB, nt.nti.ndi.szGB, cut_message ("Invalid target general bytes")); - if (!res) { thread_res = -1; return (void*) thread_res; } + if (res < 0) { thread_res = -1; return (void*) thread_res; } szRx = sizeof (abtRx); res = nfc_initiator_transceive_bytes (device, abtTx, sizeof (abtTx), abtRx, &szRx, 5000); - cut_assert_true (res, cut_message ("Can't transceive bytes to target: %s", nfc_strerror (device))); + cut_assert_operator_int (res, >=, 0, cut_message ("Can't transceive bytes to target: %s", nfc_strerror (device))); cut_assert_equal_memory (abtAttRx, sizeof (abtAttRx), abtRx, szRx, cut_message ("Invalid received data")); - if (!res) { thread_res = -1; return (void*) thread_res; } + if (res < 0) { thread_res = -1; return (void*) thread_res; } - res = nfc_initiator_deselect_target (device); - cut_assert_true (res, cut_message ("Can't deselect target: %s", nfc_strerror (device))); - if (!res) { thread_res = -1; return (void*) thread_res; } + bres = nfc_initiator_deselect_target (device); + cut_assert_true (bres, cut_message ("Can't deselect target: %s", nfc_strerror (device))); + if (!bres) { thread_res = -1; return (void*) thread_res; } return (void *) thread_res; } diff --git a/utils/nfc-mfclassic.c b/utils/nfc-mfclassic.c index 320c73d..c2ae78c 100644 --- a/utils/nfc-mfclassic.c +++ b/utils/nfc-mfclassic.c @@ -116,7 +116,7 @@ transmit_bytes (const uint8_t *pbtTx, const size_t szTx) printf ("Sent bits: "); print_hex (pbtTx, szTx); // Transmit the command bytes - if (!nfc_initiator_transceive_bytes (pnd, pbtTx, szTx, abtRx, &szRx, 0)) + if (nfc_initiator_transceive_bytes (pnd, pbtTx, szTx, abtRx, &szRx, 0) < 0) return false; // Show received answer diff --git a/utils/nfc-mfsetuid.c b/utils/nfc-mfsetuid.c index 9d18dc0..ea0af5c 100644 --- a/utils/nfc-mfsetuid.c +++ b/utils/nfc-mfsetuid.c @@ -118,7 +118,7 @@ transmit_bytes (const uint8_t *pbtTx, const size_t szTx) print_hex (pbtTx, szTx); } // Transmit the command bytes - if (!nfc_initiator_transceive_bytes (pnd, pbtTx, szTx, abtRx, &szRx, 0)) + if (nfc_initiator_transceive_bytes (pnd, pbtTx, szTx, abtRx, &szRx, 0) < 0) return false; // Show received answer diff --git a/utils/nfc-read-forum-tag3.c b/utils/nfc-read-forum-tag3.c index e051bb8..5b3db96 100644 --- a/utils/nfc-read-forum-tag3.c +++ b/utils/nfc-read-forum-tag3.c @@ -116,7 +116,7 @@ nfc_forum_tag_type3_check (nfc_device *pnd, const nfc_target nt, const uint16_t uint8_t res[1024]; size_t res_len; - if (!nfc_initiator_transceive_bytes (pnd, frame, frame_len, res, &res_len, 0)) { + if (nfc_initiator_transceive_bytes (pnd, frame, frame_len, res, &res_len, 0) < 0) { return -1; } const size_t res_overhead = 1 + 1 + 8 + 2; // 1+1+8+2: LEN + CMD + NFCID2 + STATUS diff --git a/utils/nfc-relay-picc.c b/utils/nfc-relay-picc.c index b3cdff3..ec10084 100644 --- a/utils/nfc-relay-picc.c +++ b/utils/nfc-relay-picc.c @@ -400,8 +400,8 @@ main (int argc, char *argv[]) if (!target_only_mode) { // Forward the frame to the original tag - ret = nfc_initiator_transceive_bytes - (pndInitiator, abtCapdu, szCapduLen, abtRapdu, &szRapduLen, 0); + ret = (nfc_initiator_transceive_bytes + (pndInitiator, abtCapdu, szCapduLen, abtRapdu, &szRapduLen, 0) < 0) ? 0 : 1; } else { if (scan_hex_fd3(abtRapdu, &szRapduLen, "R-APDU") != EXIT_SUCCESS) { fprintf (stderr, "Error while scanning R-APDU from FD3\n"); From ff066e394ddcaca6766a6e9d0fbfa23fe95ee5dc Mon Sep 17 00:00:00 2001 From: Audrey Diacre Date: Wed, 21 Dec 2011 09:53:16 +0000 Subject: [PATCH 039/113] nfc_initiator_deselect__target() function returns now libnf error code. --- include/nfc/nfc.h | 2 +- libnfc/chips/pn53x.c | 4 ++-- libnfc/chips/pn53x.h | 2 +- libnfc/nfc-internal.h | 2 +- libnfc/nfc.c | 4 ++-- test/test_dep_active.c | 6 +++--- test/test_dep_passive.c | 24 ++++++++++++------------ 7 files changed, 22 insertions(+), 22 deletions(-) diff --git a/include/nfc/nfc.h b/include/nfc/nfc.h index a0d75db..579a98f 100644 --- a/include/nfc/nfc.h +++ b/include/nfc/nfc.h @@ -76,7 +76,7 @@ extern "C" { NFC_EXPORT int nfc_initiator_list_passive_targets (nfc_device *pnd, const nfc_modulation nm, nfc_target ant[], const size_t szTargets); NFC_EXPORT int nfc_initiator_poll_target (nfc_device *pnd, const nfc_modulation *pnmTargetTypes, const size_t szTargetTypes, const uint8_t uiPollNr, const uint8_t uiPeriod, nfc_target *pnt); NFC_EXPORT int nfc_initiator_select_dep_target (nfc_device *pnd, const nfc_dep_mode ndm, const nfc_baud_rate nbr, const nfc_dep_info *pndiInitiator, nfc_target *pnt, const int timeout); - NFC_EXPORT bool nfc_initiator_deselect_target (nfc_device *pnd); + NFC_EXPORT int nfc_initiator_deselect_target (nfc_device *pnd); NFC_EXPORT int nfc_initiator_transceive_bytes (nfc_device *pnd, const uint8_t *pbtTx, const size_t szTx, uint8_t *pbtRx, size_t *pszRx, int timeout); NFC_EXPORT bool nfc_initiator_transceive_bits (nfc_device *pnd, const uint8_t *pbtTx, const size_t szTxBits, const uint8_t *pbtTxPar, uint8_t *pbtRx, size_t *pszRxBits, uint8_t *pbtRxPar); NFC_EXPORT bool nfc_initiator_transceive_bytes_timed (nfc_device *pnd, const uint8_t *pbtTx, const size_t szTx, uint8_t *pbtRx, size_t *pszRx, uint32_t *cycles); diff --git a/libnfc/chips/pn53x.c b/libnfc/chips/pn53x.c index e623b09..694b798 100644 --- a/libnfc/chips/pn53x.c +++ b/libnfc/chips/pn53x.c @@ -1518,10 +1518,10 @@ pn53x_initiator_transceive_bytes_timed (struct nfc_device *pnd, const uint8_t *p return true; } -bool +int pn53x_initiator_deselect_target (struct nfc_device *pnd) { - return ((pn53x_InDeselect (pnd, 0) < 0 ) ? 0 : 1 ); // 0 mean deselect all selected targets + return pn53x_InDeselect (pnd, 0); // 0 mean deselect all selected targets } #define SAK_ISO14443_4_COMPLIANT 0x20 diff --git a/libnfc/chips/pn53x.h b/libnfc/chips/pn53x.h index 33ee19b..f18192a 100644 --- a/libnfc/chips/pn53x.h +++ b/libnfc/chips/pn53x.h @@ -309,7 +309,7 @@ bool pn53x_initiator_transceive_bits_timed (struct nfc_device *pnd, const uin uint8_t *pbtRxPar, uint32_t *cycles); bool pn53x_initiator_transceive_bytes_timed (struct nfc_device *pnd, const uint8_t *pbtTx, const size_t szTx, uint8_t *pbtRx, size_t *pszRx, uint32_t *cycles); -bool pn53x_initiator_deselect_target (struct nfc_device *pnd); +int pn53x_initiator_deselect_target (struct nfc_device *pnd); // NFC device as Target functions int pn53x_target_init (struct nfc_device *pnd, nfc_target *pnt, uint8_t *pbtRx, size_t *pszRx); diff --git a/libnfc/nfc-internal.h b/libnfc/nfc-internal.h index 6ebe21c..808d861 100644 --- a/libnfc/nfc-internal.h +++ b/libnfc/nfc-internal.h @@ -136,7 +136,7 @@ struct nfc_driver_t { int (*initiator_select_passive_target) (struct nfc_device *pnd, const nfc_modulation nm, const uint8_t * pbtInitData, const size_t szInitData, nfc_target * pnt); int (*initiator_poll_target) (struct nfc_device *pnd, const nfc_modulation * pnmModulations, const size_t szModulations, const uint8_t uiPollNr, const uint8_t btPeriod, nfc_target * pnt); int (*initiator_select_dep_target) (struct nfc_device *pnd, const nfc_dep_mode ndm, const nfc_baud_rate nbr, const nfc_dep_info * pndiInitiator, nfc_target * pnt, const int timeout); - bool (*initiator_deselect_target) (struct nfc_device *pnd); + int (*initiator_deselect_target) (struct nfc_device *pnd); int (*initiator_transceive_bytes) (struct nfc_device *pnd, const uint8_t * pbtTx, const size_t szTx, uint8_t * pbtRx, size_t * pszRx, int timeout); bool (*initiator_transceive_bits) (struct nfc_device *pnd, const uint8_t * pbtTx, const size_t szTxBits, const uint8_t * pbtTxPar, uint8_t * pbtRx, size_t * pszRxBits, uint8_t * pbtRxPar); bool (*initiator_transceive_bytes_timed) (struct nfc_device *pnd, const uint8_t * pbtTx, const size_t szTx, uint8_t * pbtRx, size_t * pszRx, uint32_t * cycles); diff --git a/libnfc/nfc.c b/libnfc/nfc.c index 46ad4ed..74e8a92 100644 --- a/libnfc/nfc.c +++ b/libnfc/nfc.c @@ -456,7 +456,7 @@ nfc_initiator_select_dep_target (nfc_device *pnd, /** * @brief Deselect a selected passive or emulated tag - * @return Returns \c true if action was successfully performed; otherwise returns \c false. + * @return Returns 0 on success, otherwise returns libnfc's error code (negative value). * @param pnd \a nfc_device struct pointer that represents currently used device * * After selecting and communicating with a passive tag, this function could be @@ -466,7 +466,7 @@ nfc_initiator_select_dep_target (nfc_device *pnd, * tag, test it for the available features and support, deselect it and skip to * the next tag until the correct tag is found. */ -bool +int nfc_initiator_deselect_target (nfc_device *pnd) { HAL (initiator_deselect_target, pnd); diff --git a/test/test_dep_active.c b/test/test_dep_active.c index 852939e..146efe6 100644 --- a/test/test_dep_active.c +++ b/test/test_dep_active.c @@ -142,9 +142,9 @@ initiator_thread (void *arg) const uint8_t abtAttRx[] = "Hello DEP initiator!"; cut_assert_equal_memory (abtAttRx, sizeof (abtAttRx), abtRx, szRx, cut_message ("Invalid received data")); if (res < 0) { thread_res = -1; return (void*) thread_res; } - bool bres = nfc_initiator_deselect_target (device); - cut_assert_true (bres, cut_message ("Can't deselect target: %s", nfc_strerror (device))); - if (!bres) { thread_res = -1; return (void*) thread_res; } + res = nfc_initiator_deselect_target (device); + cut_assert_equal_int (0, res, cut_message ("Can't deselect target: %s", nfc_strerror (device))); + if (res < 0) { thread_res = -1; return (void*) thread_res; } return (void *) thread_res; } diff --git a/test/test_dep_passive.c b/test/test_dep_passive.c index 3bf13e5..ce755c7 100644 --- a/test/test_dep_passive.c +++ b/test/test_dep_passive.c @@ -176,9 +176,9 @@ initiator_thread (void *arg) cut_assert_equal_memory (abtAttRx, sizeof (abtAttRx), abtRx, szRx, cut_message ("Invalid received data")); if (res < 0) { thread_res = -1; return (void*) thread_res; } - bool bres = nfc_initiator_deselect_target (device); - cut_assert_true (bres, cut_message ("Can't deselect target: %s", nfc_strerror (device))); - if (!bres) { thread_res = -1; return (void*) thread_res; } + res = nfc_initiator_deselect_target (device); + cut_assert_equal_int (0, res, cut_message ("Can't deselect target: %s", nfc_strerror (device))); + if (res < 0) { thread_res = -1; return (void*) thread_res; } // Passive mode / 212Kbps (second pass) printf ("=========== INITIATOR %s (Passive mode / 212Kbps) =========\n", nfc_device_get_name (device)); @@ -198,9 +198,9 @@ initiator_thread (void *arg) cut_assert_equal_memory (abtAttRx, sizeof (abtAttRx), abtRx, szRx, cut_message ("Invalid received data")); if (res < 0) { thread_res = -1; return (void*) thread_res; } - bres = nfc_initiator_deselect_target (device); - cut_assert_true (bres, cut_message ("Can't deselect target: %s", nfc_strerror (device))); - if (!bres) { thread_res = -1; return (void*) thread_res; } + res = nfc_initiator_deselect_target (device); + cut_assert_equal_int (0, res, cut_message ("Can't deselect target: %s", nfc_strerror (device))); + if (res < 0) { thread_res = -1; return (void*) thread_res; } // Passive mode / 212Kbps printf ("=========== INITIATOR %s (Passive mode / 212Kbps, second pass) =========\n", nfc_device_get_name (device)); @@ -220,9 +220,9 @@ initiator_thread (void *arg) cut_assert_equal_memory (abtAttRx, sizeof (abtAttRx), abtRx, szRx, cut_message ("Invalid received data")); if (res < 0) { thread_res = -1; return (void*) thread_res; } - bres = nfc_initiator_deselect_target (device); - cut_assert_true (bres, cut_message ("Can't deselect target: %s", nfc_strerror (device))); - if (!bres) { thread_res = -1; return (void*) thread_res; } + res = nfc_initiator_deselect_target (device); + cut_assert_equal_int (0, res, cut_message ("Can't deselect target: %s", nfc_strerror (device))); + if (res < 0) { thread_res = -1; return (void*) thread_res; } // Passive mode / 424Kbps printf ("=========== INITIATOR %s (Passive mode / 424Kbps) =========\n", nfc_device_get_name (device)); @@ -242,9 +242,9 @@ initiator_thread (void *arg) cut_assert_equal_memory (abtAttRx, sizeof (abtAttRx), abtRx, szRx, cut_message ("Invalid received data")); if (res < 0) { thread_res = -1; return (void*) thread_res; } - bres = nfc_initiator_deselect_target (device); - cut_assert_true (bres, cut_message ("Can't deselect target: %s", nfc_strerror (device))); - if (!bres) { thread_res = -1; return (void*) thread_res; } + res = nfc_initiator_deselect_target (device); + cut_assert_equal_int (0, res, cut_message ("Can't deselect target: %s", nfc_strerror (device))); + if (res < 0) { thread_res = -1; return (void*) thread_res; } return (void *) thread_res; } From f93bc59504584a4805517c7fb5f44b48b3231e04 Mon Sep 17 00:00:00 2001 From: Audrey Diacre Date: Wed, 21 Dec 2011 11:33:21 +0000 Subject: [PATCH 040/113] nfc_initiator_select_passive_target() function returns now the selected passive targets count on success. --- examples/doc/quick_start_example1.c | 2 +- libnfc/chips/pn53x.c | 25 ++++++++++++++----------- libnfc/nfc.c | 4 ++-- utils/nfc-list.c | 14 +++++++------- 4 files changed, 24 insertions(+), 21 deletions(-) diff --git a/examples/doc/quick_start_example1.c b/examples/doc/quick_start_example1.c index 65bc8e9..db48c10 100644 --- a/examples/doc/quick_start_example1.c +++ b/examples/doc/quick_start_example1.c @@ -35,7 +35,7 @@ main (int argc, const char *argv[]) .nmt = NMT_ISO14443A, .nbr = NBR_106, }; - if (nfc_initiator_select_passive_target (pnd, nmMifare, NULL, 0, &nt) == 0) { + if (nfc_initiator_select_passive_target (pnd, nmMifare, NULL, 0, &nt) > 0) { printf ("The following (NFC) ISO14443A tag was found:\n"); printf (" ATQA (SENS_RES): "); print_hex (nt.nti.nai.abtAtqa, 2); diff --git a/libnfc/chips/pn53x.c b/libnfc/chips/pn53x.c index 694b798..233ca01 100644 --- a/libnfc/chips/pn53x.c +++ b/libnfc/chips/pn53x.c @@ -980,31 +980,31 @@ pn53x_initiator_select_passive_target_ext (struct nfc_device *pnd, return res; } } - return NFC_SUCCESS; + return abtTargetsData[0]; } // else: const pn53x_modulation pm = pn53x_nm_to_pm(nm); if (PM_UNDEFINED == pm) { pnd->last_error = NFC_EINVARG; - return false; + return pnd->last_error; } - if (pn53x_InListPassiveTarget (pnd, pm, 1, pbtInitData, szInitData, abtTargetsData, &szTargetsData, timeout) < 0) - return false; + if ((res = pn53x_InListPassiveTarget (pnd, pm, 1, pbtInitData, szInitData, abtTargetsData, &szTargetsData, timeout)) < 0) + return res; // Make sure one tag has been found, the PN53X returns 0x00 if none was available - if (abtTargetsData[0] == 0) - return false; + if (res == 0) + return NFC_ECHIP; // Is a tag info struct available if (pnt) { pnt->nm = nm; // Fill the tag info struct with the values corresponding to this init modulation if (!pn53x_decode_target_data (abtTargetsData + 1, szTargetsData - 1, CHIP_DATA(pnd)->type, nm.nmt, &(pnt->nti))) { - return false; + return NFC_ECHIP; } } - return true; + return abtTargetsData[0]; } int @@ -2069,7 +2069,7 @@ pn53x_PowerDown (struct nfc_device *pnd) /** * @brief C wrapper to InListPassiveTarget command - * @return true if command is successfully sent + * @return Returns selected targets count on success, otherwise returns libnfc's error code (negative value) * * @param pnd struct nfc_device struct pointer that represent currently used device * @param pmInitModulation Desired modulation @@ -2130,8 +2130,11 @@ pn53x_InListPassiveTarget (struct nfc_device *pnd, // Set the optional initiator data (used for Felica, ISO14443B, Topaz Polling or for ISO14443A selecting a specific UID). if (pbtInitiatorData) memcpy (abtCmd + 3, pbtInitiatorData, szInitiatorData); - - return pn53x_transceive (pnd, abtCmd, 3 + szInitiatorData, pbtTargetsData, pszTargetsData, timeout); + int res = 0; + if ((res = pn53x_transceive (pnd, abtCmd, 3 + szInitiatorData, pbtTargetsData, pszTargetsData, timeout)) < 0) { + return res; + } + return pbtTargetsData[0]; } int diff --git a/libnfc/nfc.c b/libnfc/nfc.c index 74e8a92..6bf4a74 100644 --- a/libnfc/nfc.c +++ b/libnfc/nfc.c @@ -304,7 +304,7 @@ nfc_initiator_init (nfc_device *pnd) /** * @brief Select a passive or emulated tag - * @return Returns 0 on success, otherwise returns libnfc's error code (negative value) + * @return Returns selected passive target count on success, otherwise returns libnfc's error code (negative value) * * @param pnd \a nfc_device struct pointer that represent currently used device * @param nm desired modulation @@ -381,7 +381,7 @@ nfc_initiator_list_passive_targets (nfc_device *pnd, prepare_initiator_data (nm, &pbtInitData, &szInitDataLen); - while (nfc_initiator_select_passive_target (pnd, nm, pbtInitData, szInitDataLen, &nt) == 0) { + while (nfc_initiator_select_passive_target (pnd, nm, pbtInitData, szInitDataLen, &nt) > 0) { nfc_initiator_deselect_target (pnd); if (szTargets == szTargetFound) { break; diff --git a/utils/nfc-list.c b/utils/nfc-list.c index fd18fc1..a12a4a6 100644 --- a/utils/nfc-list.c +++ b/utils/nfc-list.c @@ -138,7 +138,7 @@ main (int argc, const char *argv[]) nm.nmt = NMT_FELICA; nm.nbr = NBR_212; // List Felica tags - if (nfc_initiator_list_passive_targets (pnd, nm, ant, MAX_TARGET_COUNT) >= 0) { + if ((res = nfc_initiator_list_passive_targets (pnd, nm, ant, MAX_TARGET_COUNT)) >= 0) { int n; if (verbose) { printf ("%d Felica (212 kbps) passive target(s) found%s\n", res, (res == 0) ? ".\n" : ":"); @@ -150,7 +150,7 @@ main (int argc, const char *argv[]) } nm.nbr = NBR_424; - if (nfc_initiator_list_passive_targets (pnd, nm, ant, MAX_TARGET_COUNT) >= 0) { + if ((res = nfc_initiator_list_passive_targets (pnd, nm, ant, MAX_TARGET_COUNT)) >= 0) { int n; if (verbose) { printf ("%d Felica (424 kbps) passive target(s) found%s\n", res, (res == 0) ? ".\n" : ":"); @@ -164,7 +164,7 @@ main (int argc, const char *argv[]) nm.nmt = NMT_ISO14443B; nm.nbr = NBR_106; // List ISO14443B targets - if (nfc_initiator_list_passive_targets (pnd, nm, ant, MAX_TARGET_COUNT) >= 0) { + if ((res = nfc_initiator_list_passive_targets (pnd, nm, ant, MAX_TARGET_COUNT)) >= 0) { int n; if (verbose) { printf ("%d ISO14443B passive target(s) found%s\n", res, (res == 0) ? ".\n" : ":"); @@ -178,7 +178,7 @@ main (int argc, const char *argv[]) nm.nmt = NMT_ISO14443BI; nm.nbr = NBR_106; // List ISO14443B' targets - if (nfc_initiator_list_passive_targets (pnd, nm, ant, MAX_TARGET_COUNT) >= 0) { + if ((res = nfc_initiator_list_passive_targets (pnd, nm, ant, MAX_TARGET_COUNT)) >= 0) { int n; if (verbose) { printf ("%d ISO14443B' passive target(s) found%s\n", res, (res == 0) ? ".\n" : ":"); @@ -192,7 +192,7 @@ main (int argc, const char *argv[]) nm.nmt = NMT_ISO14443B2SR; nm.nbr = NBR_106; // List ISO14443B-2 ST SRx family targets - if (nfc_initiator_list_passive_targets (pnd, nm, ant, MAX_TARGET_COUNT) >= 0) { + if ((res = nfc_initiator_list_passive_targets (pnd, nm, ant, MAX_TARGET_COUNT)) >= 0) { int n; if (verbose) { printf ("%d ISO14443B-2 ST SRx passive target(s) found%s\n", res, (res == 0) ? ".\n" : ":"); @@ -206,7 +206,7 @@ main (int argc, const char *argv[]) nm.nmt = NMT_ISO14443B2CT; nm.nbr = NBR_106; // List ISO14443B-2 ASK CTx family targets - if (nfc_initiator_list_passive_targets (pnd, nm, ant, MAX_TARGET_COUNT) >= 0) { + if ((res = nfc_initiator_list_passive_targets (pnd, nm, ant, MAX_TARGET_COUNT)) >= 0) { int n; if (verbose) { printf ("%d ISO14443B-2 ASK CTx passive target(s) found%s\n", res, (res == 0) ? ".\n" : ":"); @@ -220,7 +220,7 @@ main (int argc, const char *argv[]) nm.nmt = NMT_JEWEL; nm.nbr = NBR_106; // List Jewel targets - if (nfc_initiator_list_passive_targets(pnd, nm, ant, MAX_TARGET_COUNT) >= 0) { + if ((res = nfc_initiator_list_passive_targets(pnd, nm, ant, MAX_TARGET_COUNT)) >= 0) { int n; if (verbose) { printf("%d Jewel passive target(s) found%s\n", res, (res == 0)?".\n":":"); From 340e80d7a8fe6df8e3a86f0a4e68f72ddbba0c9c Mon Sep 17 00:00:00 2001 From: Audrey Diacre Date: Wed, 21 Dec 2011 11:52:02 +0000 Subject: [PATCH 041/113] pn53x_initiator_select_passive_target_ext() function now does not return error when there is no target. --- libnfc/chips/pn53x.c | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/libnfc/chips/pn53x.c b/libnfc/chips/pn53x.c index 233ca01..c80f67a 100644 --- a/libnfc/chips/pn53x.c +++ b/libnfc/chips/pn53x.c @@ -989,13 +989,9 @@ pn53x_initiator_select_passive_target_ext (struct nfc_device *pnd, return pnd->last_error; } - if ((res = pn53x_InListPassiveTarget (pnd, pm, 1, pbtInitData, szInitData, abtTargetsData, &szTargetsData, timeout)) < 0) + if ((res = pn53x_InListPassiveTarget (pnd, pm, 1, pbtInitData, szInitData, abtTargetsData, &szTargetsData, timeout)) <= 0) return res; - // Make sure one tag has been found, the PN53X returns 0x00 if none was available - if (res == 0) - return NFC_ECHIP; - // Is a tag info struct available if (pnt) { pnt->nm = nm; From b864215d632e712f6b117e37f95e05f8786193ff Mon Sep 17 00:00:00 2001 From: Audrey Diacre Date: Thu, 22 Dec 2011 11:16:27 +0000 Subject: [PATCH 042/113] pn53x_initiator_poll_target() function returns now polled targets count on success. --- examples/nfc-poll.c | 2 +- libnfc/chips/pn53x.c | 26 +++++++++++++------------- libnfc/chips/pn53x.h | 1 - libnfc/nfc.c | 2 +- 4 files changed, 15 insertions(+), 16 deletions(-) diff --git a/examples/nfc-poll.c b/examples/nfc-poll.c index 7c7d206..820b26e 100644 --- a/examples/nfc-poll.c +++ b/examples/nfc-poll.c @@ -109,7 +109,7 @@ main (int argc, const char *argv[]) exit (EXIT_FAILURE); } - if (res == 0) { + if (res > 0) { print_nfc_target ( nt, verbose ); } else { printf ("No target found.\n"); diff --git a/libnfc/chips/pn53x.c b/libnfc/chips/pn53x.c index c80f67a..cc170a2 100644 --- a/libnfc/chips/pn53x.c +++ b/libnfc/chips/pn53x.c @@ -1037,18 +1037,17 @@ pn53x_initiator_poll_target (struct nfc_device *pnd, } szTargetTypes++; } - size_t szTargetFound = 0; nfc_target ntTargets[2]; - if ((res = pn53x_InAutoPoll (pnd, apttTargetTypes, szTargetTypes, uiPollNr, uiPeriod, ntTargets, &szTargetFound, 0)) < 0) + if ((res = pn53x_InAutoPoll (pnd, apttTargetTypes, szTargetTypes, uiPollNr, uiPeriod, ntTargets, 0)) < 0) return res; - switch (szTargetFound) { + switch (res) { case 1: *pnt = ntTargets[0]; - return NFC_SUCCESS; + return res; break; case 2: *pnt = ntTargets[1]; // We keep the selected one - return NFC_SUCCESS; + return res; break; default: return NFC_ECHIP; @@ -1065,11 +1064,12 @@ pn53x_initiator_poll_target (struct nfc_device *pnd, prepare_initiator_data (pnmModulations[n], &pbtInitiatorData, &szInitiatorData); const int timeout_ms = uiPeriod * 150; - if (pn53x_initiator_select_passive_target_ext (pnd, pnmModulations[n], pbtInitiatorData, szInitiatorData, pnt, timeout_ms) < 0) { - if (pnd->last_error != NFC_ETIMEOUT) + if ((res = pn53x_initiator_select_passive_target_ext (pnd, pnmModulations[n], pbtInitiatorData, szInitiatorData, pnt, timeout_ms)) < 0) { + if (pnd->last_error != NFC_ETIMEOUT) { return pnd->last_error; + } } else { - return NFC_SUCCESS; + return res; } } } @@ -2182,9 +2182,9 @@ pn53x_InRelease (struct nfc_device *pnd, const uint8_t ui8Target) int pn53x_InAutoPoll (struct nfc_device *pnd, const pn53x_target_type *ppttTargetTypes, const size_t szTargetTypes, - const uint8_t btPollNr, const uint8_t btPeriod, nfc_target * pntTargets, size_t *pszTargetFound, - const int timeout) + const uint8_t btPollNr, const uint8_t btPeriod, nfc_target * pntTargets, const int timeout) { + size_t szTargetFound = 0; if (CHIP_DATA(pnd)->type != PN532) { // This function is not supported by pn531 neither pn533 pnd->last_error = NFC_EDEVNOTSUPP; @@ -2205,8 +2205,8 @@ pn53x_InAutoPoll (struct nfc_device *pnd, if (res < 0) { return res; } else if (szRx > 0) { - *pszTargetFound = abtRx[0]; - if (*pszTargetFound) { + szTargetFound = abtRx[0]; + if (szTargetFound > 0) { uint8_t ln; uint8_t *pbt = abtRx + 1; /* 1st target */ @@ -2229,7 +2229,7 @@ pn53x_InAutoPoll (struct nfc_device *pnd, } } } - return NFC_SUCCESS; + return szTargetFound; } /** diff --git a/libnfc/chips/pn53x.h b/libnfc/chips/pn53x.h index f18192a..dd651c0 100644 --- a/libnfc/chips/pn53x.h +++ b/libnfc/chips/pn53x.h @@ -333,7 +333,6 @@ int pn53x_InDeselect (struct nfc_device *pnd, const uint8_t ui8Target); int pn53x_InRelease (struct nfc_device *pnd, const uint8_t ui8Target); int pn53x_InAutoPoll (struct nfc_device *pnd, const pn53x_target_type *ppttTargetTypes, const size_t szTargetTypes, const uint8_t btPollNr, const uint8_t btPeriod, nfc_target *pntTargets, - size_t *pszTargetFound, const int timeout); int pn53x_InJumpForDEP (struct nfc_device *pnd, const nfc_dep_mode ndm, const nfc_baud_rate nbr, diff --git a/libnfc/nfc.c b/libnfc/nfc.c index 6bf4a74..723ceef 100644 --- a/libnfc/nfc.c +++ b/libnfc/nfc.c @@ -410,7 +410,7 @@ nfc_initiator_list_passive_targets (nfc_device *pnd, /** * @brief Polling for NFC targets - * @return Returns 0 on success, otherwise returns libnfc's error code (negative value). + * @return Returns polled targets count, otherwise returns libnfc's error code (negative value). * * @param pnd \a nfc_device struct pointer that represent currently used device * @param ppttTargetTypes array of desired target types From 658fceb7fcbb2ca7fe73b4720de0445ceb23e250 Mon Sep 17 00:00:00 2001 From: Audrey Diacre Date: Thu, 22 Dec 2011 13:28:41 +0000 Subject: [PATCH 043/113] nfc_initiator_select_dep_target() function returns now selected D.E.P targets count on success. --- libnfc/chips/pn53x.c | 39 +++++++++++++++++++-------------------- libnfc/nfc.c | 2 +- test/test_dep_active.c | 10 +++++----- test/test_dep_passive.c | 17 ++++++++--------- 4 files changed, 33 insertions(+), 35 deletions(-) diff --git a/libnfc/chips/pn53x.c b/libnfc/chips/pn53x.c index cc170a2..dfae861 100644 --- a/libnfc/chips/pn53x.c +++ b/libnfc/chips/pn53x.c @@ -2315,28 +2315,27 @@ pn53x_InJumpForDEP (struct nfc_device *pnd, return res; // Make sure one target has been found, the PN53X returns 0x00 if none was available - if (abtRx[1] != 1) - return NFC_ECHIP; - - // Is a target struct available - if (pnt) { - pnt->nm.nmt = NMT_DEP; - pnt->nm.nbr = nbr; - pnt->nti.ndi.ndm = ndm; - memcpy (pnt->nti.ndi.abtNFCID3, abtRx + 2, 10); - pnt->nti.ndi.btDID = abtRx[12]; - pnt->nti.ndi.btBS = abtRx[13]; - pnt->nti.ndi.btBR = abtRx[14]; - pnt->nti.ndi.btTO = abtRx[15]; - pnt->nti.ndi.btPP = abtRx[16]; - if(szRx > 17) { - pnt->nti.ndi.szGB = szRx - 17; - memcpy (pnt->nti.ndi.abtGB, abtRx + 17, pnt->nti.ndi.szGB); - } else { - pnt->nti.ndi.szGB = 0; + if (abtRx[1] >= 1) { + // Is a target struct available + if (pnt) { + pnt->nm.nmt = NMT_DEP; + pnt->nm.nbr = nbr; + pnt->nti.ndi.ndm = ndm; + memcpy (pnt->nti.ndi.abtNFCID3, abtRx + 2, 10); + pnt->nti.ndi.btDID = abtRx[12]; + pnt->nti.ndi.btBS = abtRx[13]; + pnt->nti.ndi.btBR = abtRx[14]; + pnt->nti.ndi.btTO = abtRx[15]; + pnt->nti.ndi.btPP = abtRx[16]; + if(szRx > 17) { + pnt->nti.ndi.szGB = szRx - 17; + memcpy (pnt->nti.ndi.abtGB, abtRx + 17, pnt->nti.ndi.szGB); + } else { + pnt->nti.ndi.szGB = 0; + } } } - return NFC_SUCCESS; + return abtRx[1]; } int diff --git a/libnfc/nfc.c b/libnfc/nfc.c index 723ceef..9de8049 100644 --- a/libnfc/nfc.c +++ b/libnfc/nfc.c @@ -433,7 +433,7 @@ nfc_initiator_poll_target (nfc_device *pnd, /** * @brief Select a target and request active or passive mode for D.E.P. (Data Exchange Protocol) - * @return Returns 0 on success, otherwise returns libnfc's error code (negative value). + * @return Returns selected D.E.P tagets count on success, otherwise returns libnfc's error code (negative value). * * @param pnd \a nfc_device struct pointer that represent currently used device * @param ndm desired D.E.P. mode (\a NDM_ACTIVE or \a NDM_PASSIVE for active, respectively passive mode) diff --git a/test/test_dep_active.c b/test/test_dep_active.c index 146efe6..e25aa88 100644 --- a/test/test_dep_active.c +++ b/test/test_dep_active.c @@ -116,16 +116,16 @@ initiator_thread (void *arg) */ sleep (1); printf ("=========== INITIATOR %s =========\n", nfc_device_get_name (device)); - int ires = nfc_initiator_init (device); - cut_assert_equal_int (0, ires, cut_message ("Can't initialize NFC device as initiator: %s", nfc_strerror (device))); - if (ires < 0) { thread_res = -1; return (void*) thread_res; } + int res = nfc_initiator_init (device); + cut_assert_equal_int (0, res, cut_message ("Can't initialize NFC device as initiator: %s", nfc_strerror (device))); + if (res < 0) { thread_res = -1; return (void*) thread_res; } nfc_target nt; // Active mode printf ("=========== INITIATOR %s (Active mode / %s Kbps) =========\n", nfc_device_get_name (device), str_nfc_baud_rate(nbr)); - int res = nfc_initiator_select_dep_target (device, NDM_ACTIVE, nbr, NULL, &nt, 1000); - cut_assert_equal_int (0, res, cut_message ("Can't select any DEP target: %s", nfc_strerror (device))); + res = nfc_initiator_select_dep_target (device, NDM_ACTIVE, nbr, NULL, &nt, 1000); + cut_assert_operator_int (res, >=, 0, cut_message ("Can't select any DEP target: %s", nfc_strerror (device))); cut_assert_equal_int (NMT_DEP, nt.nm.nmt, cut_message ("Invalid target modulation")); cut_assert_equal_int (nbr, nt.nm.nbr, cut_message ("Invalid target baud rate")); cut_assert_equal_memory ("\x11\x22\x33\x44\x55\x66\x77\x88\x99\xAA", 10, nt.nti.ndi.abtNFCID3, 10, cut_message ("Invalid target NFCID3")); diff --git a/test/test_dep_passive.c b/test/test_dep_passive.c index ce755c7..49f2936 100644 --- a/test/test_dep_passive.c +++ b/test/test_dep_passive.c @@ -148,17 +148,16 @@ initiator_thread (void *arg) sleep (1); printf ("=========== INITIATOR %s =========\n", nfc_device_get_name (device)); - int ires = nfc_initiator_init (device); - printf ("IRES: %d\n", ires); - cut_assert_equal_int (0, ires, cut_message ("Can't initialize NFC device as initiator: %s", nfc_strerror (device))); - if (ires < 0) { thread_res = -1; return (void*) thread_res; } + int res = nfc_initiator_init (device); + cut_assert_equal_int (0, res, cut_message ("Can't initialize NFC device as initiator: %s", nfc_strerror (device))); + if (res < 0) { thread_res = -1; return (void*) thread_res; } nfc_target nt; // Passive mode / 106Kbps printf ("=========== INITIATOR %s (Passive mode / 106Kbps) =========\n", nfc_device_get_name (device)); - int res = nfc_initiator_select_dep_target (device, NDM_PASSIVE, NBR_106, NULL, &nt, 5000); - cut_assert_equal_int (0, res, cut_message ("Can't select any DEP target: %s", nfc_strerror (device))); + res = nfc_initiator_select_dep_target (device, NDM_PASSIVE, NBR_106, NULL, &nt, 5000); + cut_assert_operator_int (res, >=, 0, cut_message ("Can't select any DEP target: %s", nfc_strerror (device))); cut_assert_equal_int (NMT_DEP, nt.nm.nmt, cut_message ("Invalid target modulation")); cut_assert_equal_int (NBR_106, nt.nm.nbr, cut_message ("Invalid target baud rate")); cut_assert_equal_memory ("\x11\x22\x33\x44\x55\x66\x77\x88\x99\xAA", 10, nt.nti.ndi.abtNFCID3, 10, cut_message ("Invalid target NFCID3")); @@ -183,7 +182,7 @@ initiator_thread (void *arg) // Passive mode / 212Kbps (second pass) printf ("=========== INITIATOR %s (Passive mode / 212Kbps) =========\n", nfc_device_get_name (device)); res = nfc_initiator_select_dep_target (device, NDM_PASSIVE, NBR_212, NULL, &nt, 1000); - cut_assert_equal_int (0, res, cut_message ("Can't select any DEP target: %s", nfc_strerror (device))); + cut_assert_operator_int (res, >=, 0, cut_message ("Can't select any DEP target: %s", nfc_strerror (device))); cut_assert_equal_int (NMT_DEP, nt.nm.nmt, cut_message ("Invalid target modulation")); cut_assert_equal_int (NBR_212, nt.nm.nbr, cut_message ("Invalid target baud rate")); cut_assert_equal_memory ("\x11\x22\x33\x44\x55\x66\x77\x88\x99\xAA", 10, nt.nti.ndi.abtNFCID3, 10, cut_message ("Invalid target NFCID3")); @@ -205,7 +204,7 @@ initiator_thread (void *arg) // Passive mode / 212Kbps printf ("=========== INITIATOR %s (Passive mode / 212Kbps, second pass) =========\n", nfc_device_get_name (device)); res = nfc_initiator_select_dep_target (device, NDM_PASSIVE, NBR_212, NULL, &nt, 1000); - cut_assert_equal_int (0, res, cut_message ("Can't select any DEP target: %s", nfc_strerror (device))); + cut_assert_operator_int (res, >=, 0, cut_message ("Can't select any DEP target: %s", nfc_strerror (device))); cut_assert_equal_int (NMT_DEP, nt.nm.nmt, cut_message ("Invalid target modulation")); cut_assert_equal_int (NBR_212, nt.nm.nbr, cut_message ("Invalid target baud rate")); cut_assert_equal_memory ("\x11\x22\x33\x44\x55\x66\x77\x88\x99\xAA", 10, nt.nti.ndi.abtNFCID3, 10, cut_message ("Invalid target NFCID3")); @@ -227,7 +226,7 @@ initiator_thread (void *arg) // Passive mode / 424Kbps printf ("=========== INITIATOR %s (Passive mode / 424Kbps) =========\n", nfc_device_get_name (device)); res = nfc_initiator_select_dep_target (device, NDM_PASSIVE, NBR_424, NULL, &nt, 1000); - cut_assert_equal_int (0, res, cut_message ("Can't select any DEP target: %s", nfc_strerror (device))); + cut_assert_operator_int (res, >=, 0, cut_message ("Can't select any DEP target: %s", nfc_strerror (device))); cut_assert_equal_int (NMT_DEP, nt.nm.nmt, cut_message ("Invalid target modulation")); cut_assert_equal_int (NBR_424, nt.nm.nbr, cut_message ("Invalid target baud rate")); cut_assert_equal_memory ("\x11\x22\x33\x44\x55\x66\x77\x88\x99\xAA", 10, nt.nti.ndi.abtNFCID3, 10, cut_message ("Invalid target NFCID3")); From a40e63ab9d04944b3ed6702019d215e04f25c03f Mon Sep 17 00:00:00 2001 From: Audrey Diacre Date: Thu, 22 Dec 2011 14:16:12 +0000 Subject: [PATCH 044/113] test/tes_dep_*: now check there is one or more selected dep target. --- test/test_dep_active.c | 2 +- test/test_dep_passive.c | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/test/test_dep_active.c b/test/test_dep_active.c index e25aa88..a6b5749 100644 --- a/test/test_dep_active.c +++ b/test/test_dep_active.c @@ -125,7 +125,7 @@ initiator_thread (void *arg) // Active mode printf ("=========== INITIATOR %s (Active mode / %s Kbps) =========\n", nfc_device_get_name (device), str_nfc_baud_rate(nbr)); res = nfc_initiator_select_dep_target (device, NDM_ACTIVE, nbr, NULL, &nt, 1000); - cut_assert_operator_int (res, >=, 0, cut_message ("Can't select any DEP target: %s", nfc_strerror (device))); + cut_assert_operator_int (res, >, 0, cut_message ("Can't select any DEP target: %s", nfc_strerror (device))); cut_assert_equal_int (NMT_DEP, nt.nm.nmt, cut_message ("Invalid target modulation")); cut_assert_equal_int (nbr, nt.nm.nbr, cut_message ("Invalid target baud rate")); cut_assert_equal_memory ("\x11\x22\x33\x44\x55\x66\x77\x88\x99\xAA", 10, nt.nti.ndi.abtNFCID3, 10, cut_message ("Invalid target NFCID3")); diff --git a/test/test_dep_passive.c b/test/test_dep_passive.c index 49f2936..fdc91c8 100644 --- a/test/test_dep_passive.c +++ b/test/test_dep_passive.c @@ -157,7 +157,7 @@ initiator_thread (void *arg) // Passive mode / 106Kbps printf ("=========== INITIATOR %s (Passive mode / 106Kbps) =========\n", nfc_device_get_name (device)); res = nfc_initiator_select_dep_target (device, NDM_PASSIVE, NBR_106, NULL, &nt, 5000); - cut_assert_operator_int (res, >=, 0, cut_message ("Can't select any DEP target: %s", nfc_strerror (device))); + cut_assert_operator_int (res, >, 0, cut_message ("Can't select any DEP target: %s", nfc_strerror (device))); cut_assert_equal_int (NMT_DEP, nt.nm.nmt, cut_message ("Invalid target modulation")); cut_assert_equal_int (NBR_106, nt.nm.nbr, cut_message ("Invalid target baud rate")); cut_assert_equal_memory ("\x11\x22\x33\x44\x55\x66\x77\x88\x99\xAA", 10, nt.nti.ndi.abtNFCID3, 10, cut_message ("Invalid target NFCID3")); @@ -182,7 +182,7 @@ initiator_thread (void *arg) // Passive mode / 212Kbps (second pass) printf ("=========== INITIATOR %s (Passive mode / 212Kbps) =========\n", nfc_device_get_name (device)); res = nfc_initiator_select_dep_target (device, NDM_PASSIVE, NBR_212, NULL, &nt, 1000); - cut_assert_operator_int (res, >=, 0, cut_message ("Can't select any DEP target: %s", nfc_strerror (device))); + cut_assert_operator_int (res, >, 0, cut_message ("Can't select any DEP target: %s", nfc_strerror (device))); cut_assert_equal_int (NMT_DEP, nt.nm.nmt, cut_message ("Invalid target modulation")); cut_assert_equal_int (NBR_212, nt.nm.nbr, cut_message ("Invalid target baud rate")); cut_assert_equal_memory ("\x11\x22\x33\x44\x55\x66\x77\x88\x99\xAA", 10, nt.nti.ndi.abtNFCID3, 10, cut_message ("Invalid target NFCID3")); @@ -204,7 +204,7 @@ initiator_thread (void *arg) // Passive mode / 212Kbps printf ("=========== INITIATOR %s (Passive mode / 212Kbps, second pass) =========\n", nfc_device_get_name (device)); res = nfc_initiator_select_dep_target (device, NDM_PASSIVE, NBR_212, NULL, &nt, 1000); - cut_assert_operator_int (res, >=, 0, cut_message ("Can't select any DEP target: %s", nfc_strerror (device))); + cut_assert_operator_int (res, >, 0, cut_message ("Can't select any DEP target: %s", nfc_strerror (device))); cut_assert_equal_int (NMT_DEP, nt.nm.nmt, cut_message ("Invalid target modulation")); cut_assert_equal_int (NBR_212, nt.nm.nbr, cut_message ("Invalid target baud rate")); cut_assert_equal_memory ("\x11\x22\x33\x44\x55\x66\x77\x88\x99\xAA", 10, nt.nti.ndi.abtNFCID3, 10, cut_message ("Invalid target NFCID3")); @@ -226,7 +226,7 @@ initiator_thread (void *arg) // Passive mode / 424Kbps printf ("=========== INITIATOR %s (Passive mode / 424Kbps) =========\n", nfc_device_get_name (device)); res = nfc_initiator_select_dep_target (device, NDM_PASSIVE, NBR_424, NULL, &nt, 1000); - cut_assert_operator_int (res, >=, 0, cut_message ("Can't select any DEP target: %s", nfc_strerror (device))); + cut_assert_operator_int (res, >, 0, cut_message ("Can't select any DEP target: %s", nfc_strerror (device))); cut_assert_equal_int (NMT_DEP, nt.nm.nmt, cut_message ("Invalid target modulation")); cut_assert_equal_int (NBR_424, nt.nm.nbr, cut_message ("Invalid target baud rate")); cut_assert_equal_memory ("\x11\x22\x33\x44\x55\x66\x77\x88\x99\xAA", 10, nt.nti.ndi.abtNFCID3, 10, cut_message ("Invalid target NFCID3")); From ac6f6523681e829c4a59a7d55ad1d40f37c2859c Mon Sep 17 00:00:00 2001 From: Audrey Diacre Date: Thu, 22 Dec 2011 15:39:51 +0000 Subject: [PATCH 045/113] nfc_target_receive_bytes() function returns now received bytes count on success and libnfc error code on failure. --- examples/nfc-dep-target.c | 2 +- examples/nfc-emulate-tag.c | 2 +- include/nfc/nfc.h | 2 +- libnfc/chips/pn53x.c | 8 ++++---- libnfc/chips/pn53x.h | 2 +- libnfc/nfc-emulation.c | 2 +- libnfc/nfc-internal.h | 2 +- libnfc/nfc.c | 4 ++-- test/test_dep_active.c | 10 +++++----- test/test_dep_passive.c | 34 +++++++++++++++++----------------- utils/nfc-relay-picc.c | 2 +- 11 files changed, 35 insertions(+), 35 deletions(-) diff --git a/examples/nfc-dep-target.c b/examples/nfc-dep-target.c index 28a7fd3..9ec6cf9 100644 --- a/examples/nfc-dep-target.c +++ b/examples/nfc-dep-target.c @@ -125,7 +125,7 @@ main (int argc, const char *argv[]) } printf("Initiator request received. Waiting for data...\n"); - if (!nfc_target_receive_bytes (pnd, abtRx, &szRx, 0)) { + if (nfc_target_receive_bytes (pnd, abtRx, &szRx, 0) < 0) { nfc_perror(pnd, "nfc_target_receive_bytes"); goto error; } diff --git a/examples/nfc-emulate-tag.c b/examples/nfc-emulate-tag.c index 8ef6db9..e69c77c 100644 --- a/examples/nfc-emulate-tag.c +++ b/examples/nfc-emulate-tag.c @@ -158,7 +158,7 @@ nfc_target_emulate_tag(nfc_device *pnd, nfc_target *pnt) nfc_device_set_property_bool (pnd, NP_HANDLE_CRC, false); init_mfc_auth = false; } - if (!nfc_target_receive_bytes(pnd, abtRx, &szRx, 0)) { + if (nfc_target_receive_bytes(pnd, abtRx, &szRx, 0) < 0) { nfc_perror (pnd, "nfc_target_receive_bytes"); return false; } diff --git a/include/nfc/nfc.h b/include/nfc/nfc.h index 579a98f..3c61e36 100644 --- a/include/nfc/nfc.h +++ b/include/nfc/nfc.h @@ -85,7 +85,7 @@ extern "C" { /* 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, size_t *pszRx); NFC_EXPORT bool nfc_target_send_bytes (nfc_device *pnd, const uint8_t *pbtTx, const size_t szTx, int timeout); - NFC_EXPORT bool nfc_target_receive_bytes (nfc_device *pnd, uint8_t *pbtRx, size_t *pszRx, int timeout); + NFC_EXPORT int nfc_target_receive_bytes (nfc_device *pnd, uint8_t *pbtRx, size_t *pszRx, int timeout); NFC_EXPORT bool nfc_target_send_bits (nfc_device *pnd, const uint8_t *pbtTx, const size_t szTxBits, const uint8_t *pbtTxPar); NFC_EXPORT bool nfc_target_receive_bits (nfc_device *pnd, uint8_t *pbtRx, size_t *pszRxBits, uint8_t *pbtRxPar); diff --git a/libnfc/chips/pn53x.c b/libnfc/chips/pn53x.c index dfae861..71a4065 100644 --- a/libnfc/chips/pn53x.c +++ b/libnfc/chips/pn53x.c @@ -1779,7 +1779,7 @@ pn53x_target_receive_bits (struct nfc_device *pnd, uint8_t *pbtRx, size_t *pszRx return true; } -bool +int pn53x_target_receive_bytes (struct nfc_device *pnd, uint8_t *pbtRx, size_t *pszRx, int timeout) { uint8_t abtCmd[1]; @@ -1801,7 +1801,7 @@ pn53x_target_receive_bytes (struct nfc_device *pnd, uint8_t *pbtRx, size_t *pszR } else { // TODO Support EasyFraming for other cases by software pnd->last_error = NFC_ENOTIMPL; - return false; + return pnd->last_error; } } default: @@ -1816,7 +1816,7 @@ pn53x_target_receive_bytes (struct nfc_device *pnd, uint8_t *pbtRx, size_t *pszR uint8_t abtRx[PN53x_EXTENDED_FRAME__DATA_MAX_LEN]; size_t szRx = sizeof (abtRx); if (pn53x_transceive (pnd, abtCmd, sizeof (abtCmd), abtRx, &szRx, timeout) < 0) - return false; + return pnd->last_error; // Save the received byte count *pszRx = szRx - 1; @@ -1825,7 +1825,7 @@ pn53x_target_receive_bytes (struct nfc_device *pnd, uint8_t *pbtRx, size_t *pszR memcpy (pbtRx, abtRx + 1, *pszRx); // Everyting seems ok, return true - return true; + return *pszRx; } bool diff --git a/libnfc/chips/pn53x.h b/libnfc/chips/pn53x.h index dd651c0..6628d9e 100644 --- a/libnfc/chips/pn53x.h +++ b/libnfc/chips/pn53x.h @@ -314,7 +314,7 @@ int pn53x_initiator_deselect_target (struct nfc_device *pnd); // NFC device as Target functions int pn53x_target_init (struct nfc_device *pnd, nfc_target *pnt, uint8_t *pbtRx, size_t *pszRx); bool pn53x_target_receive_bits (struct nfc_device *pnd, uint8_t *pbtRx, size_t *pszRxBits, uint8_t *pbtRxPar); -bool pn53x_target_receive_bytes (struct nfc_device *pnd, uint8_t *pbtRx, size_t *pszRx, int timeout); +int pn53x_target_receive_bytes (struct nfc_device *pnd, uint8_t *pbtRx, size_t *pszRx, int timeout); bool pn53x_target_send_bits (struct nfc_device *pnd, const uint8_t *pbtTx, const size_t szTxBits, const uint8_t *pbtTxPar); bool pn53x_target_send_bytes (struct nfc_device *pnd, const uint8_t *pbtTx, const size_t szTx, int timeout); diff --git a/libnfc/nfc-emulation.c b/libnfc/nfc-emulation.c index 592c555..a5ed90c 100644 --- a/libnfc/nfc-emulation.c +++ b/libnfc/nfc-emulation.c @@ -47,7 +47,7 @@ nfc_emulate_target (nfc_device *pnd, struct nfc_emulator *emulator) } } if (res >= 0) { - if (!nfc_target_receive_bytes(pnd, abtRx, &szRx, 0)) { + if (nfc_target_receive_bytes(pnd, abtRx, &szRx, 0) < 0) { return -1; } } diff --git a/libnfc/nfc-internal.h b/libnfc/nfc-internal.h index 808d861..2304b2c 100644 --- a/libnfc/nfc-internal.h +++ b/libnfc/nfc-internal.h @@ -144,7 +144,7 @@ struct nfc_driver_t { int (*target_init) (struct nfc_device *pnd, nfc_target * pnt, uint8_t * pbtRx, size_t * pszRx); bool (*target_send_bytes) (struct nfc_device *pnd, const uint8_t * pbtTx, const size_t szTx, int timeout); - bool (*target_receive_bytes) (struct nfc_device *pnd, uint8_t * pbtRx, size_t * pszRx, int timeout); + int (*target_receive_bytes) (struct nfc_device *pnd, uint8_t * pbtRx, size_t * pszRx, int timeout); bool (*target_send_bits) (struct nfc_device *pnd, const uint8_t * pbtTx, const size_t szTxBits, const uint8_t * pbtTxPar); bool (*target_receive_bits) (struct nfc_device *pnd, uint8_t * pbtRx, size_t * pszRxBits, uint8_t * pbtRxPar); diff --git a/libnfc/nfc.c b/libnfc/nfc.c index 9de8049..1cbad87 100644 --- a/libnfc/nfc.c +++ b/libnfc/nfc.c @@ -716,7 +716,7 @@ nfc_target_send_bytes (nfc_device *pnd, const uint8_t *pbtTx, const size_t szTx, /** * @brief Receive bytes and APDU frames - * @return Returns \c true if action was successfully performed; otherwise returns \c false. + * @return Returns received bytes count on success, otherwise returns libnfc's error code * * @param pnd \a nfc_device struct pointer that represent currently used device * @param[out] pbtRx pointer to Rx buffer @@ -728,7 +728,7 @@ nfc_target_send_bytes (nfc_device *pnd, const uint8_t *pbtTx, const size_t szTx, * If timeout is not a null pointer, it specifies the maximum interval to wait for the function to be executed. * If timeout is a null pointer, the function blocks indefinitely (until an error is raised or function is completed). */ -bool +int nfc_target_receive_bytes (nfc_device *pnd, uint8_t *pbtRx, size_t *pszRx, int timeout) { HAL (target_receive_bytes, pnd, pbtRx, pszRx, timeout); diff --git a/test/test_dep_active.c b/test/test_dep_active.c index a6b5749..1bc5cae 100644 --- a/test/test_dep_active.c +++ b/test/test_dep_active.c @@ -88,15 +88,15 @@ target_thread (void *arg) cut_assert_equal_int (0, ires, cut_message ("Can't initialize NFC device as target: %s", nfc_strerror (device))); if (ires < 0) { thread_res = -1; return (void*) thread_res; } - bool res = nfc_target_receive_bytes (device, abtRx, &szRx, 500); - cut_assert_true (res, cut_message ("Can't receive bytes from initiator: %s", nfc_strerror (device))); + ires = nfc_target_receive_bytes (device, abtRx, &szRx, 500); + cut_assert_operator_int (ires, >, 0, cut_message ("Can't receive bytes from initiator: %s", nfc_strerror (device))); const uint8_t abtAttRx[] = "Hello DEP target!"; cut_assert_equal_memory (abtAttRx, sizeof (abtAttRx), abtRx, szRx, cut_message ("Invalid received data")); - if (!res) { thread_res = -1; return (void*) thread_res; } + if (ires <= 0) { thread_res = -1; return (void*) thread_res; } const uint8_t abtTx[] = "Hello DEP initiator!"; - res = nfc_target_send_bytes (device, abtTx, sizeof(abtTx), 500); + bool res = nfc_target_send_bytes (device, abtTx, sizeof(abtTx), 500); cut_assert_true (res, cut_message ("Can't send bytes to initiator: %s", nfc_strerror (device))); if (!res) { thread_res = -1; return (void*) thread_res; } @@ -131,7 +131,7 @@ initiator_thread (void *arg) cut_assert_equal_memory ("\x11\x22\x33\x44\x55\x66\x77\x88\x99\xAA", 10, nt.nti.ndi.abtNFCID3, 10, cut_message ("Invalid target NFCID3")); cut_assert_equal_int (NDM_ACTIVE, nt.nti.ndi.ndm, cut_message ("Invalid target DEP mode")); cut_assert_equal_memory ("\x12\x34\x56\x78", 4, nt.nti.ndi.abtGB, nt.nti.ndi.szGB, cut_message ("Invalid target general bytes")); - if (res < 0) { thread_res = -1; return (void*) thread_res; } + if (res <= 0) { thread_res = -1; return (void*) thread_res; } const uint8_t abtTx[] = "Hello DEP target!"; uint8_t abtRx[1024]; diff --git a/test/test_dep_passive.c b/test/test_dep_passive.c index fdc91c8..32d4653 100644 --- a/test/test_dep_passive.c +++ b/test/test_dep_passive.c @@ -87,46 +87,46 @@ target_thread (void *arg) if (ires < 0) { thread_res = -1; return (void*) thread_res; } // First pass - bool res = nfc_target_receive_bytes (device, abtRx, &szRx, 500); - cut_assert_true (res, cut_message ("Can't receive bytes from initiator: %s", nfc_strerror (device))); + ires = nfc_target_receive_bytes (device, abtRx, &szRx, 500); + cut_assert_operator_int (ires, >, 0, cut_message ("Can't receive bytes from initiator: %s", nfc_strerror (device))); const uint8_t abtAttRx[] = "Hello DEP target!"; cut_assert_equal_memory (abtAttRx, sizeof (abtAttRx), abtRx, szRx, cut_message ("Invalid received data")); - if (!res) { thread_res = -1; return (void*) thread_res; } + if (ires <= 0) { thread_res = -1; return (void*) thread_res; } const uint8_t abtTx[] = "Hello DEP initiator!"; - res = nfc_target_send_bytes (device, abtTx, sizeof(abtTx), 500); + bool res = nfc_target_send_bytes (device, abtTx, sizeof(abtTx), 500); cut_assert_true (res, cut_message ("Can't send bytes to initiator: %s", nfc_strerror (device))); if (!res) { thread_res = -1; return (void*) thread_res; } // Second pass - res = nfc_target_receive_bytes (device, abtRx, &szRx, 500); - cut_assert_true (res, cut_message ("Can't receive bytes from initiator: %s", nfc_strerror (device))); + ires = nfc_target_receive_bytes (device, abtRx, &szRx, 500); + cut_assert_operator_int (ires, >, 0, cut_message ("Can't receive bytes from initiator: %s", nfc_strerror (device))); cut_assert_equal_memory (abtAttRx, sizeof (abtAttRx), abtRx, szRx, cut_message ("Invalid received data")); - if (!res) { thread_res = -1; return (void*) thread_res; } + if (ires <= 0) { thread_res = -1; return (void*) thread_res; } res = nfc_target_send_bytes (device, abtTx, sizeof(abtTx), 500); cut_assert_true (res, cut_message ("Can't send bytes to initiator: %s", nfc_strerror (device))); if (!res) { thread_res = -1; return (void*) thread_res; } // Third pass - res = nfc_target_receive_bytes (device, abtRx, &szRx, 500); - cut_assert_true (res, cut_message ("Can't receive bytes from initiator: %s", nfc_strerror (device))); + ires = nfc_target_receive_bytes (device, abtRx, &szRx, 500); + cut_assert_operator_int (ires, >, 0, cut_message ("Can't receive bytes from initiator: %s", nfc_strerror (device))); cut_assert_equal_memory (abtAttRx, sizeof (abtAttRx), abtRx, szRx, cut_message ("Invalid received data")); - if (!res) { thread_res = -1; return (void*) thread_res; } + if (ires <= 0) { thread_res = -1; return (void*) thread_res; } res = nfc_target_send_bytes (device, abtTx, sizeof(abtTx), 500); cut_assert_true (res, cut_message ("Can't send bytes to initiator: %s", nfc_strerror (device))); if (!res) { thread_res = -1; return (void*) thread_res; } // Fourth pass - res = nfc_target_receive_bytes (device, abtRx, &szRx, 500); - cut_assert_true (res, cut_message ("Can't receive bytes from initiator: %s", nfc_strerror (device))); + ires = nfc_target_receive_bytes (device, abtRx, &szRx, 500); + cut_assert_operator_int (ires, >, 0, cut_message ("Can't receive bytes from initiator: %s", nfc_strerror (device))); cut_assert_equal_memory (abtAttRx, sizeof (abtAttRx), abtRx, szRx, cut_message ("Invalid received data")); - if (!res) { thread_res = -1; return (void*) thread_res; } + if (ires <= 0) { thread_res = -1; return (void*) thread_res; } res = nfc_target_send_bytes (device, abtTx, sizeof(abtTx), 500); cut_assert_true (res, cut_message ("Can't send bytes to initiator: %s", nfc_strerror (device))); @@ -163,7 +163,7 @@ initiator_thread (void *arg) cut_assert_equal_memory ("\x11\x22\x33\x44\x55\x66\x77\x88\x99\xAA", 10, nt.nti.ndi.abtNFCID3, 10, cut_message ("Invalid target NFCID3")); cut_assert_equal_int (NDM_PASSIVE, nt.nti.ndi.ndm, cut_message ("Invalid target DEP mode")); cut_assert_equal_memory ("\x12\x34\x56\x78", 4, nt.nti.ndi.abtGB, nt.nti.ndi.szGB, cut_message ("Invalid target general bytes")); - if (res < 0) { thread_res = -1; return (void*) thread_res; } + if (res <= 0) { thread_res = -1; return (void*) thread_res; } const uint8_t abtTx[] = "Hello DEP target!"; uint8_t abtRx[1024]; @@ -188,7 +188,7 @@ initiator_thread (void *arg) cut_assert_equal_memory ("\x11\x22\x33\x44\x55\x66\x77\x88\x99\xAA", 10, nt.nti.ndi.abtNFCID3, 10, cut_message ("Invalid target NFCID3")); cut_assert_equal_int (NDM_PASSIVE, nt.nti.ndi.ndm, cut_message ("Invalid target DEP mode")); cut_assert_equal_memory ("\x12\x34\x56\x78", 4, nt.nti.ndi.abtGB, nt.nti.ndi.szGB, cut_message ("Invalid target general bytes")); - if (res < 0) { thread_res = -1; return (void*) thread_res; } + if (res <= 0) { thread_res = -1; return (void*) thread_res; } szRx = sizeof (abtRx); res = nfc_initiator_transceive_bytes (device, abtTx, sizeof (abtTx), abtRx, &szRx, 1000); @@ -210,7 +210,7 @@ initiator_thread (void *arg) cut_assert_equal_memory ("\x11\x22\x33\x44\x55\x66\x77\x88\x99\xAA", 10, nt.nti.ndi.abtNFCID3, 10, cut_message ("Invalid target NFCID3")); cut_assert_equal_int (NDM_PASSIVE, nt.nti.ndi.ndm, cut_message ("Invalid target DEP mode")); cut_assert_equal_memory ("\x12\x34\x56\x78", 4, nt.nti.ndi.abtGB, nt.nti.ndi.szGB, cut_message ("Invalid target general bytes")); - if (res < 0) { thread_res = -1; return (void*) thread_res; } + if (res <= 0) { thread_res = -1; return (void*) thread_res; } szRx = sizeof (abtRx); res = nfc_initiator_transceive_bytes (device, abtTx, sizeof (abtTx), abtRx, &szRx, 5000); @@ -232,7 +232,7 @@ initiator_thread (void *arg) cut_assert_equal_memory ("\x11\x22\x33\x44\x55\x66\x77\x88\x99\xAA", 10, nt.nti.ndi.abtNFCID3, 10, cut_message ("Invalid target NFCID3")); cut_assert_equal_int (NDM_PASSIVE, nt.nti.ndi.ndm, cut_message ("Invalid target DEP mode")); cut_assert_equal_memory ("\x12\x34\x56\x78", 4, nt.nti.ndi.abtGB, nt.nti.ndi.szGB, cut_message ("Invalid target general bytes")); - if (res < 0) { thread_res = -1; return (void*) thread_res; } + if (res <= 0) { thread_res = -1; return (void*) thread_res; } szRx = sizeof (abtRx); res = nfc_initiator_transceive_bytes (device, abtTx, sizeof (abtTx), abtRx, &szRx, 5000); diff --git a/utils/nfc-relay-picc.c b/utils/nfc-relay-picc.c index ec10084..18e3544 100644 --- a/utils/nfc-relay-picc.c +++ b/utils/nfc-relay-picc.c @@ -370,7 +370,7 @@ main (int argc, char *argv[]) bool ret; if (!initiator_only_mode) { // Receive external reader command through target - if (!nfc_target_receive_bytes(pndTarget,abtCapdu,&szCapduLen, 0)) { + if (nfc_target_receive_bytes(pndTarget,abtCapdu,&szCapduLen, 0) < 0) { nfc_perror (pndTarget, "nfc_target_receive_bytes"); if (!target_only_mode) { nfc_disconnect (pndInitiator); From 9c1371dcca5202f1abdfd99a31599b85c057f13c Mon Sep 17 00:00:00 2001 From: Audrey Diacre Date: Thu, 22 Dec 2011 15:59:08 +0000 Subject: [PATCH 046/113] nfc_target_send_bytes() function returns now sent bytes count on success and libnfc error code on failure. --- examples/nfc-dep-target.c | 2 +- examples/nfc-emulate-tag.c | 2 +- include/nfc/nfc.h | 2 +- libnfc/chips/pn53x.c | 17 +++++++------- libnfc/chips/pn53x.h | 2 +- libnfc/nfc-emulation.c | 2 +- libnfc/nfc-internal.h | 2 +- libnfc/nfc.c | 4 ++-- test/test_dep_active.c | 18 +++++++------- test/test_dep_passive.c | 48 +++++++++++++++++++------------------- utils/nfc-relay-picc.c | 2 +- 11 files changed, 51 insertions(+), 50 deletions(-) diff --git a/examples/nfc-dep-target.c b/examples/nfc-dep-target.c index 9ec6cf9..0597706 100644 --- a/examples/nfc-dep-target.c +++ b/examples/nfc-dep-target.c @@ -133,7 +133,7 @@ main (int argc, const char *argv[]) printf ("Received: %s\n", abtRx); printf ("Sending: %s\n", abtTx); - if (!nfc_target_send_bytes (pnd, abtTx, sizeof(abtTx), 0)) { + if (nfc_target_send_bytes (pnd, abtTx, sizeof(abtTx), 0) < 0) { nfc_perror(pnd, "nfc_target_send_bytes"); goto error; } diff --git a/examples/nfc-emulate-tag.c b/examples/nfc-emulate-tag.c index e69c77c..828bc86 100644 --- a/examples/nfc-emulate-tag.c +++ b/examples/nfc-emulate-tag.c @@ -148,7 +148,7 @@ nfc_target_emulate_tag(nfc_device *pnd, nfc_target *pnt) while ( loop ) { loop = target_io( pnt, abtRx, szRx, abtTx, &szTx ); if (szTx) { - if (!nfc_target_send_bytes(pnd, abtTx, szTx, 0)) { + if (nfc_target_send_bytes(pnd, abtTx, szTx, 0) < 0) { nfc_perror (pnd, "nfc_target_send_bytes"); return false; } diff --git a/include/nfc/nfc.h b/include/nfc/nfc.h index 3c61e36..d02f451 100644 --- a/include/nfc/nfc.h +++ b/include/nfc/nfc.h @@ -84,7 +84,7 @@ extern "C" { /* 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, size_t *pszRx); - NFC_EXPORT bool nfc_target_send_bytes (nfc_device *pnd, const uint8_t *pbtTx, const size_t szTx, int timeout); + NFC_EXPORT int nfc_target_send_bytes (nfc_device *pnd, const uint8_t *pbtTx, const size_t szTx, int timeout); NFC_EXPORT int nfc_target_receive_bytes (nfc_device *pnd, uint8_t *pbtRx, size_t *pszRx, int timeout); NFC_EXPORT bool nfc_target_send_bits (nfc_device *pnd, const uint8_t *pbtTx, const size_t szTxBits, const uint8_t *pbtTxPar); NFC_EXPORT bool nfc_target_receive_bits (nfc_device *pnd, uint8_t *pbtRx, size_t *pszRxBits, uint8_t *pbtRxPar); diff --git a/libnfc/chips/pn53x.c b/libnfc/chips/pn53x.c index 71a4065..1ef18d5 100644 --- a/libnfc/chips/pn53x.c +++ b/libnfc/chips/pn53x.c @@ -1866,14 +1866,15 @@ pn53x_target_send_bits (struct nfc_device *pnd, const uint8_t *pbtTx, const size return true; } -bool +int pn53x_target_send_bytes (struct nfc_device *pnd, const uint8_t *pbtTx, const size_t szTx, int timeout) { uint8_t abtCmd[PN53x_EXTENDED_FRAME__DATA_MAX_LEN]; + int res = 0; // We can not just send bytes without parity if while the PN53X expects we handled them if (!pnd->bPar) - return false; + return NFC_ECHIP; // XXX I think this is not a clean way to provide some kind of "EasyFraming" // but at the moment I have no more better than this @@ -1892,7 +1893,7 @@ pn53x_target_send_bytes (struct nfc_device *pnd, const uint8_t *pbtTx, const siz } else { // TODO Support EasyFraming for other cases by software pnd->last_error = NFC_ENOTIMPL; - return false; + return pnd->last_error; } } default: @@ -1905,13 +1906,13 @@ pn53x_target_send_bytes (struct nfc_device *pnd, const uint8_t *pbtTx, const siz // Copy the data into the command frame memcpy (abtCmd + 1, pbtTx, szTx); - + // Try to send the bits to the reader - if (pn53x_transceive (pnd, abtCmd, szTx + 1, NULL, NULL, timeout) < 0) - return false; + if ((res = pn53x_transceive (pnd, abtCmd, szTx + 1, NULL, NULL, timeout)) < 0) + return res; - // Everyting seems ok, return true - return true; + // Everyting seems ok, return sent byte count + return szTx; } static struct sErrorMessage { diff --git a/libnfc/chips/pn53x.h b/libnfc/chips/pn53x.h index 6628d9e..aa33453 100644 --- a/libnfc/chips/pn53x.h +++ b/libnfc/chips/pn53x.h @@ -316,7 +316,7 @@ int pn53x_target_init (struct nfc_device *pnd, nfc_target *pnt, uint8_t *pbtR bool pn53x_target_receive_bits (struct nfc_device *pnd, uint8_t *pbtRx, size_t *pszRxBits, uint8_t *pbtRxPar); int pn53x_target_receive_bytes (struct nfc_device *pnd, uint8_t *pbtRx, size_t *pszRx, int timeout); bool pn53x_target_send_bits (struct nfc_device *pnd, const uint8_t *pbtTx, const size_t szTxBits, const uint8_t *pbtTxPar); -bool pn53x_target_send_bytes (struct nfc_device *pnd, const uint8_t *pbtTx, const size_t szTx, int timeout); +int pn53x_target_send_bytes (struct nfc_device *pnd, const uint8_t *pbtTx, const size_t szTx, int timeout); // Error handling functions const char *pn53x_strerror (const struct nfc_device *pnd); diff --git a/libnfc/nfc-emulation.c b/libnfc/nfc-emulation.c index a5ed90c..287632c 100644 --- a/libnfc/nfc-emulation.c +++ b/libnfc/nfc-emulation.c @@ -42,7 +42,7 @@ nfc_emulate_target (nfc_device *pnd, struct nfc_emulator *emulator) while (res >= 0) { res = emulator->state_machine->io (emulator, abtRx, szRx, abtTx, sizeof (abtTx)); if (res > 0) { - if (!nfc_target_send_bytes(pnd, abtTx, res, 0)) { + if (nfc_target_send_bytes(pnd, abtTx, res, 0) < 0) { return -1; } } diff --git a/libnfc/nfc-internal.h b/libnfc/nfc-internal.h index 2304b2c..359140b 100644 --- a/libnfc/nfc-internal.h +++ b/libnfc/nfc-internal.h @@ -143,7 +143,7 @@ struct nfc_driver_t { bool (*initiator_transceive_bits_timed) (struct nfc_device *pnd, const uint8_t * pbtTx, const size_t szTxBits, const uint8_t * pbtTxPar, uint8_t * pbtRx, size_t * pszRxBits, uint8_t * pbtRxPar, uint32_t * cycles); int (*target_init) (struct nfc_device *pnd, nfc_target * pnt, uint8_t * pbtRx, size_t * pszRx); - bool (*target_send_bytes) (struct nfc_device *pnd, const uint8_t * pbtTx, const size_t szTx, int timeout); + int (*target_send_bytes) (struct nfc_device *pnd, const uint8_t * pbtTx, const size_t szTx, int timeout); int (*target_receive_bytes) (struct nfc_device *pnd, uint8_t * pbtRx, size_t * pszRx, int timeout); bool (*target_send_bits) (struct nfc_device *pnd, const uint8_t * pbtTx, const size_t szTxBits, const uint8_t * pbtTxPar); bool (*target_receive_bits) (struct nfc_device *pnd, uint8_t * pbtRx, size_t * pszRxBits, uint8_t * pbtRxPar); diff --git a/libnfc/nfc.c b/libnfc/nfc.c index 1cbad87..c6d40d4 100644 --- a/libnfc/nfc.c +++ b/libnfc/nfc.c @@ -695,7 +695,7 @@ nfc_abort_command (nfc_device *pnd) /** * @brief Send bytes and APDU frames - * @return Returns \c true if action was successfully performed; otherwise returns \c false. + * @return Returns sent bytes count on success, otherwise returns libnfc's error code * * @param pnd \a nfc_device struct pointer that represent currently used device * @param pbtTx pointer to Tx buffer @@ -708,7 +708,7 @@ nfc_abort_command (nfc_device *pnd) * If timeout is not a null pointer, it specifies the maximum interval to wait for the function to be executed. * If timeout is a null pointer, the function blocks indefinitely (until an error is raised or function is completed). */ -bool +int nfc_target_send_bytes (nfc_device *pnd, const uint8_t *pbtTx, const size_t szTx, int timeout) { HAL (target_send_bytes, pnd, pbtTx, szTx, timeout); diff --git a/test/test_dep_active.c b/test/test_dep_active.c index 1bc5cae..c2af320 100644 --- a/test/test_dep_active.c +++ b/test/test_dep_active.c @@ -84,21 +84,21 @@ target_thread (void *arg) uint8_t abtRx[1024]; size_t szRx = sizeof (abtRx); - int ires = nfc_target_init (device, &nt, abtRx, &szRx); - cut_assert_equal_int (0, ires, cut_message ("Can't initialize NFC device as target: %s", nfc_strerror (device))); - if (ires < 0) { thread_res = -1; return (void*) thread_res; } + int res = nfc_target_init (device, &nt, abtRx, &szRx); + cut_assert_equal_int (0, res, cut_message ("Can't initialize NFC device as target: %s", nfc_strerror (device))); + if (res < 0) { thread_res = -1; return (void*) thread_res; } - ires = nfc_target_receive_bytes (device, abtRx, &szRx, 500); - cut_assert_operator_int (ires, >, 0, cut_message ("Can't receive bytes from initiator: %s", nfc_strerror (device))); + res = nfc_target_receive_bytes (device, abtRx, &szRx, 500); + cut_assert_operator_int (res, >, 0, cut_message ("Can't receive bytes from initiator: %s", nfc_strerror (device))); const uint8_t abtAttRx[] = "Hello DEP target!"; cut_assert_equal_memory (abtAttRx, sizeof (abtAttRx), abtRx, szRx, cut_message ("Invalid received data")); - if (ires <= 0) { thread_res = -1; return (void*) thread_res; } + if (res <= 0) { thread_res = -1; return (void*) thread_res; } const uint8_t abtTx[] = "Hello DEP initiator!"; - bool res = nfc_target_send_bytes (device, abtTx, sizeof(abtTx), 500); - cut_assert_true (res, cut_message ("Can't send bytes to initiator: %s", nfc_strerror (device))); - if (!res) { thread_res = -1; return (void*) thread_res; } + res = nfc_target_send_bytes (device, abtTx, sizeof(abtTx), 500); + cut_assert_operator_int (res, >, 0, cut_message ("Can't send bytes to initiator: %s", nfc_strerror (device))); + if (res <= 0) { thread_res = -1; return (void*) thread_res; } return (void *) thread_res; } diff --git a/test/test_dep_passive.c b/test/test_dep_passive.c index 32d4653..442c911 100644 --- a/test/test_dep_passive.c +++ b/test/test_dep_passive.c @@ -82,55 +82,55 @@ target_thread (void *arg) uint8_t abtRx[1024]; size_t szRx = sizeof (abtRx); - int ires = nfc_target_init (device, &nt, abtRx, &szRx); - cut_assert_equal_int (0, ires, cut_message ("Can't initialize NFC device as target: %s", nfc_strerror (device))); - if (ires < 0) { thread_res = -1; return (void*) thread_res; } + int res = nfc_target_init (device, &nt, abtRx, &szRx); + cut_assert_equal_int (0, res, cut_message ("Can't initialize NFC device as target: %s", nfc_strerror (device))); + if (res < 0) { thread_res = -1; return (void*) thread_res; } // First pass - ires = nfc_target_receive_bytes (device, abtRx, &szRx, 500); - cut_assert_operator_int (ires, >, 0, cut_message ("Can't receive bytes from initiator: %s", nfc_strerror (device))); + res = nfc_target_receive_bytes (device, abtRx, &szRx, 500); + cut_assert_operator_int (res, >, 0, cut_message ("Can't receive bytes from initiator: %s", nfc_strerror (device))); const uint8_t abtAttRx[] = "Hello DEP target!"; cut_assert_equal_memory (abtAttRx, sizeof (abtAttRx), abtRx, szRx, cut_message ("Invalid received data")); - if (ires <= 0) { thread_res = -1; return (void*) thread_res; } + if (res <= 0) { thread_res = -1; return (void*) thread_res; } const uint8_t abtTx[] = "Hello DEP initiator!"; - bool res = nfc_target_send_bytes (device, abtTx, sizeof(abtTx), 500); - cut_assert_true (res, cut_message ("Can't send bytes to initiator: %s", nfc_strerror (device))); - if (!res) { thread_res = -1; return (void*) thread_res; } + res = nfc_target_send_bytes (device, abtTx, sizeof(abtTx), 500); + cut_assert_operator_int (res, >, 0, cut_message ("Can't send bytes to initiator: %s", nfc_strerror (device))); + if (res <= 0) { thread_res = -1; return (void*) thread_res; } // Second pass - ires = nfc_target_receive_bytes (device, abtRx, &szRx, 500); - cut_assert_operator_int (ires, >, 0, cut_message ("Can't receive bytes from initiator: %s", nfc_strerror (device))); + res = nfc_target_receive_bytes (device, abtRx, &szRx, 500); + cut_assert_operator_int (res, >, 0, cut_message ("Can't receive bytes from initiator: %s", nfc_strerror (device))); cut_assert_equal_memory (abtAttRx, sizeof (abtAttRx), abtRx, szRx, cut_message ("Invalid received data")); - if (ires <= 0) { thread_res = -1; return (void*) thread_res; } + if (res <= 0) { thread_res = -1; return (void*) thread_res; } res = nfc_target_send_bytes (device, abtTx, sizeof(abtTx), 500); - cut_assert_true (res, cut_message ("Can't send bytes to initiator: %s", nfc_strerror (device))); - if (!res) { thread_res = -1; return (void*) thread_res; } + cut_assert_operator_int (res, >, 0, cut_message ("Can't send bytes to initiator: %s", nfc_strerror (device))); + if (res <= 0) { thread_res = -1; return (void*) thread_res; } // Third pass - ires = nfc_target_receive_bytes (device, abtRx, &szRx, 500); - cut_assert_operator_int (ires, >, 0, cut_message ("Can't receive bytes from initiator: %s", nfc_strerror (device))); + res = nfc_target_receive_bytes (device, abtRx, &szRx, 500); + cut_assert_operator_int (res, >, 0, cut_message ("Can't receive bytes from initiator: %s", nfc_strerror (device))); cut_assert_equal_memory (abtAttRx, sizeof (abtAttRx), abtRx, szRx, cut_message ("Invalid received data")); - if (ires <= 0) { thread_res = -1; return (void*) thread_res; } + if (res <= 0) { thread_res = -1; return (void*) thread_res; } res = nfc_target_send_bytes (device, abtTx, sizeof(abtTx), 500); - cut_assert_true (res, cut_message ("Can't send bytes to initiator: %s", nfc_strerror (device))); - if (!res) { thread_res = -1; return (void*) thread_res; } + cut_assert_operator_int (res, >, 0, cut_message ("Can't send bytes to initiator: %s", nfc_strerror (device))); + if (res <= 0) { thread_res = -1; return (void*) thread_res; } // Fourth pass - ires = nfc_target_receive_bytes (device, abtRx, &szRx, 500); - cut_assert_operator_int (ires, >, 0, cut_message ("Can't receive bytes from initiator: %s", nfc_strerror (device))); + res = nfc_target_receive_bytes (device, abtRx, &szRx, 500); + cut_assert_operator_int (res, >, 0, cut_message ("Can't receive bytes from initiator: %s", nfc_strerror (device))); cut_assert_equal_memory (abtAttRx, sizeof (abtAttRx), abtRx, szRx, cut_message ("Invalid received data")); - if (ires <= 0) { thread_res = -1; return (void*) thread_res; } + if (res <= 0) { thread_res = -1; return (void*) thread_res; } res = nfc_target_send_bytes (device, abtTx, sizeof(abtTx), 500); - cut_assert_true (res, cut_message ("Can't send bytes to initiator: %s", nfc_strerror (device))); - if (!res) { thread_res = -1; return (void*) thread_res; } + cut_assert_operator_int (res, >, 0, cut_message ("Can't send bytes to initiator: %s", nfc_strerror (device))); + if (res <= 0) { thread_res = -1; return (void*) thread_res; } return (void *) thread_res; } diff --git a/utils/nfc-relay-picc.c b/utils/nfc-relay-picc.c index 18e3544..234ea73 100644 --- a/utils/nfc-relay-picc.c +++ b/utils/nfc-relay-picc.c @@ -425,7 +425,7 @@ main (int argc, char *argv[]) } if (!initiator_only_mode) { // Transmit the response bytes - if (!nfc_target_send_bytes(pndTarget, abtRapdu, szRapduLen, 0)) { + if (nfc_target_send_bytes(pndTarget, abtRapdu, szRapduLen, 0) < 0) { nfc_perror (pndTarget, "nfc_target_send_bytes"); if (!target_only_mode) { nfc_disconnect (pndInitiator); From d6477df7a66606f2ac8c146230562cb75dc1e9da Mon Sep 17 00:00:00 2001 From: Romuald Conty Date: Thu, 22 Dec 2011 17:40:22 +0000 Subject: [PATCH 047/113] doc: quick start example minor fix (make distcheck OK) --- examples/doc/quick_start_example1.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/doc/quick_start_example1.c b/examples/doc/quick_start_example1.c index db48c10..448a8aa 100644 --- a/examples/doc/quick_start_example1.c +++ b/examples/doc/quick_start_example1.c @@ -28,7 +28,7 @@ main (int argc, const char *argv[]) // Set connected NFC device to initiator mode nfc_initiator_init (pnd); - printf ("Connected to NFC reader: %s\n", pnd->acName); + printf ("Connected to NFC reader: %s\n", nfc_device_get_name (pnd)); // Poll for a ISO14443A (MIFARE) tag const nfc_modulation nmMifare = { From 61074f3497167b3b2442ca9c9a9b87a31146e385 Mon Sep 17 00:00:00 2001 From: Audrey Diacre Date: Wed, 4 Jan 2012 11:46:07 +0000 Subject: [PATCH 048/113] nfc_initiator_transceive_bits() function returns now received bits count on success and libnfc error code on failure. --- examples/nfc-anticol.c | 2 +- examples/nfc-relay.c | 2 +- include/nfc/nfc.h | 2 +- libnfc/chips/pn53x.c | 17 +++++++++-------- libnfc/chips/pn53x.h | 2 +- libnfc/nfc-internal.h | 2 +- libnfc/nfc.c | 4 ++-- utils/nfc-mfclassic.c | 3 ++- utils/nfc-mfsetuid.c | 2 +- 9 files changed, 19 insertions(+), 17 deletions(-) diff --git a/examples/nfc-anticol.c b/examples/nfc-anticol.c index 6bcc4ed..20377e5 100644 --- a/examples/nfc-anticol.c +++ b/examples/nfc-anticol.c @@ -83,7 +83,7 @@ transmit_bits (const uint8_t *pbtTx, const size_t szTxBits) print_hex_bits (pbtTx, szTxBits); } // Transmit the bit frame command, we don't use the arbitrary parity feature - if (!nfc_initiator_transceive_bits (pnd, pbtTx, szTxBits, NULL, abtRx, &szRxBits, NULL)) + if (nfc_initiator_transceive_bits (pnd, pbtTx, szTxBits, NULL, abtRx, &szRxBits, NULL) < 0) return false; // Show received answer diff --git a/examples/nfc-relay.c b/examples/nfc-relay.c index 7b3f803..7f4597a 100644 --- a/examples/nfc-relay.c +++ b/examples/nfc-relay.c @@ -197,7 +197,7 @@ main (int argc, char *argv[]) } // Forward the frame to the original tag if (nfc_initiator_transceive_bits - (pndReader, abtReaderRx, szReaderRxBits, abtReaderRxPar, abtTagRx, &szTagRxBits, abtTagRxPar)) { + (pndReader, abtReaderRx, szReaderRxBits, abtReaderRxPar, abtTagRx, &szTagRxBits, abtTagRxPar) > 0) { // Redirect the answer back to the reader if (!nfc_target_send_bits (pndTag, abtTagRx, szTagRxBits, abtTagRxPar)) { nfc_perror (pndTag, "nfc_target_send_bits"); diff --git a/include/nfc/nfc.h b/include/nfc/nfc.h index d02f451..8d6309d 100644 --- a/include/nfc/nfc.h +++ b/include/nfc/nfc.h @@ -78,7 +78,7 @@ extern "C" { NFC_EXPORT int nfc_initiator_select_dep_target (nfc_device *pnd, const nfc_dep_mode ndm, const nfc_baud_rate nbr, const nfc_dep_info *pndiInitiator, nfc_target *pnt, const int timeout); NFC_EXPORT int nfc_initiator_deselect_target (nfc_device *pnd); NFC_EXPORT int nfc_initiator_transceive_bytes (nfc_device *pnd, const uint8_t *pbtTx, const size_t szTx, uint8_t *pbtRx, size_t *pszRx, int timeout); - NFC_EXPORT bool nfc_initiator_transceive_bits (nfc_device *pnd, const uint8_t *pbtTx, const size_t szTxBits, const uint8_t *pbtTxPar, uint8_t *pbtRx, size_t *pszRxBits, uint8_t *pbtRxPar); + 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, size_t *pszRxBits, uint8_t *pbtRxPar); NFC_EXPORT bool nfc_initiator_transceive_bytes_timed (nfc_device *pnd, const uint8_t *pbtTx, const size_t szTx, uint8_t *pbtRx, size_t *pszRx, uint32_t *cycles); NFC_EXPORT bool nfc_initiator_transceive_bits_timed (nfc_device *pnd, const uint8_t *pbtTx, const size_t szTxBits, const uint8_t *pbtTxPar, uint8_t *pbtRx, size_t *pszRxBits, uint8_t *pbtRxPar, uint32_t *cycles); diff --git a/libnfc/chips/pn53x.c b/libnfc/chips/pn53x.c index 1ef18d5..c129985 100644 --- a/libnfc/chips/pn53x.c +++ b/libnfc/chips/pn53x.c @@ -1107,10 +1107,11 @@ pn53x_initiator_select_dep_target(struct nfc_device *pnd, } } -bool +int pn53x_initiator_transceive_bits (struct nfc_device *pnd, const uint8_t *pbtTx, const size_t szTxBits, const uint8_t *pbtTxPar, uint8_t *pbtRx, size_t *pszRxBits, uint8_t *pbtRxPar) { + int res = 0; size_t szFrameBits = 0; size_t szFrameBytes = 0; uint8_t ui8rcc; @@ -1136,19 +1137,19 @@ pn53x_initiator_transceive_bits (struct nfc_device *pnd, const uint8_t *pbtTx, c memcpy (abtCmd + 1, pbtTx, szFrameBytes); // Set the amount of transmission bits in the PN53X chip register - if (pn53x_set_tx_bits (pnd, ui8Bits) < 0) - return false; + if ((res = pn53x_set_tx_bits (pnd, ui8Bits)) < 0) + return res; // Send the frame to the PN53X chip and get the answer // We have to give the amount of bytes + (the command byte 0x42) uint8_t abtRx[PN53x_EXTENDED_FRAME__DATA_MAX_LEN]; size_t szRx = sizeof(abtRx); - if (pn53x_transceive (pnd, abtCmd, szFrameBytes + 1, abtRx, &szRx, -1) < 0) - return false; + if ((res = pn53x_transceive (pnd, abtCmd, szFrameBytes + 1, abtRx, &szRx, -1)) < 0) + return res; // Get the last bit-count that is stored in the received byte - if (pn53x_read_register (pnd, PN53X_REG_CIU_Control, &ui8rcc) < 0) - return false; + if ((res = pn53x_read_register (pnd, PN53X_REG_CIU_Control, &ui8rcc)) < 0) + return res; ui8Bits = ui8rcc & SYMBOL_RX_LAST_BITS; // Recover the real frame length in bits @@ -1168,7 +1169,7 @@ pn53x_initiator_transceive_bits (struct nfc_device *pnd, const uint8_t *pbtTx, c } } // Everything went successful - return true; + return *pszRxBits; } int diff --git a/libnfc/chips/pn53x.h b/libnfc/chips/pn53x.h index aa33453..10bca76 100644 --- a/libnfc/chips/pn53x.h +++ b/libnfc/chips/pn53x.h @@ -299,7 +299,7 @@ int pn53x_initiator_select_dep_target (struct nfc_device *pnd, const nfc_dep_info *pndiInitiator, nfc_target *pnt, const int timeout); -bool pn53x_initiator_transceive_bits (struct nfc_device *pnd, const uint8_t *pbtTx, const size_t szTxBits, +int pn53x_initiator_transceive_bits (struct nfc_device *pnd, const uint8_t *pbtTx, const size_t szTxBits, const uint8_t *pbtTxPar, uint8_t *pbtRx, size_t *pszRxBits, uint8_t *pbtRxPar); int pn53x_initiator_transceive_bytes (struct nfc_device *pnd, const uint8_t *pbtTx, const size_t szTx, diff --git a/libnfc/nfc-internal.h b/libnfc/nfc-internal.h index 359140b..1a20465 100644 --- a/libnfc/nfc-internal.h +++ b/libnfc/nfc-internal.h @@ -138,7 +138,7 @@ struct nfc_driver_t { int (*initiator_select_dep_target) (struct nfc_device *pnd, const nfc_dep_mode ndm, const nfc_baud_rate nbr, const nfc_dep_info * pndiInitiator, nfc_target * pnt, const int timeout); int (*initiator_deselect_target) (struct nfc_device *pnd); int (*initiator_transceive_bytes) (struct nfc_device *pnd, const uint8_t * pbtTx, const size_t szTx, uint8_t * pbtRx, size_t * pszRx, int timeout); - bool (*initiator_transceive_bits) (struct nfc_device *pnd, const uint8_t * pbtTx, const size_t szTxBits, const uint8_t * pbtTxPar, uint8_t * pbtRx, size_t * pszRxBits, uint8_t * pbtRxPar); + int (*initiator_transceive_bits) (struct nfc_device *pnd, const uint8_t * pbtTx, const size_t szTxBits, const uint8_t * pbtTxPar, uint8_t * pbtRx, size_t * pszRxBits, uint8_t * pbtRxPar); bool (*initiator_transceive_bytes_timed) (struct nfc_device *pnd, const uint8_t * pbtTx, const size_t szTx, uint8_t * pbtRx, size_t * pszRx, uint32_t * cycles); bool (*initiator_transceive_bits_timed) (struct nfc_device *pnd, const uint8_t * pbtTx, const size_t szTxBits, const uint8_t * pbtTxPar, uint8_t * pbtRx, size_t * pszRxBits, uint8_t * pbtRxPar, uint32_t * cycles); diff --git a/libnfc/nfc.c b/libnfc/nfc.c index c6d40d4..d006986 100644 --- a/libnfc/nfc.c +++ b/libnfc/nfc.c @@ -504,7 +504,7 @@ nfc_initiator_transceive_bytes (nfc_device *pnd, const uint8_t *pbtTx, const siz /** * @brief Transceive raw bit-frames to a target - * @return Returns \c true if action was successfully performed; otherwise returns \c false. + * @return Returns received bits count on success, otherwise returns libnfc's error code * * @param pbtTx contains a byte array of the frame that needs to be transmitted. * @param szTxBits contains the length in bits. @@ -537,7 +537,7 @@ nfc_initiator_transceive_bytes (nfc_device *pnd, const uint8_t *pbtTx, const siz * require to violate the ISO14443-A standard by sending incorrect parity and * CRC bytes. Using this feature you are able to simulate these frames. */ -bool +int nfc_initiator_transceive_bits (nfc_device *pnd, const uint8_t *pbtTx, const size_t szTxBits, const uint8_t *pbtTxPar, uint8_t *pbtRx, size_t *pszRxBits, uint8_t *pbtRxPar) { diff --git a/utils/nfc-mfclassic.c b/utils/nfc-mfclassic.c index c2ae78c..c585d47 100644 --- a/utils/nfc-mfclassic.c +++ b/utils/nfc-mfclassic.c @@ -98,7 +98,8 @@ transmit_bits (const uint8_t *pbtTx, const size_t szTxBits) printf ("Sent bits: "); print_hex_bits (pbtTx, szTxBits); // Transmit the bit frame command, we don't use the arbitrary parity feature - if (!nfc_initiator_transceive_bits (pnd, pbtTx, szTxBits, NULL, abtRx, &szRxBits, NULL)) + int res = 0; + if ((res = nfc_initiator_transceive_bits (pnd, pbtTx, szTxBits, NULL, abtRx, &szRxBits, NULL)) < 0) return false; // Show received answer diff --git a/utils/nfc-mfsetuid.c b/utils/nfc-mfsetuid.c index ea0af5c..521dada 100644 --- a/utils/nfc-mfsetuid.c +++ b/utils/nfc-mfsetuid.c @@ -96,7 +96,7 @@ transmit_bits (const uint8_t *pbtTx, const size_t szTxBits) print_hex_bits (pbtTx, szTxBits); } // Transmit the bit frame command, we don't use the arbitrary parity feature - if (!nfc_initiator_transceive_bits (pnd, pbtTx, szTxBits, NULL, abtRx, &szRxBits, NULL)) + if (nfc_initiator_transceive_bits (pnd, pbtTx, szTxBits, NULL, abtRx, &szRxBits, NULL) < 0) return false; // Show received answer From d02da0db64724622f28b3689842eaef6a0efb5e0 Mon Sep 17 00:00:00 2001 From: Audrey Diacre Date: Wed, 4 Jan 2012 11:54:55 +0000 Subject: [PATCH 049/113] nfc_initiator_transceive_bytes_timed() function returns now received bytes count on success and libnfc error code on failure. --- include/nfc/nfc.h | 2 +- libnfc/chips/pn53x.c | 17 +++++++++-------- libnfc/chips/pn53x.h | 2 +- libnfc/nfc-internal.h | 2 +- libnfc/nfc.c | 4 ++-- 5 files changed, 14 insertions(+), 13 deletions(-) diff --git a/include/nfc/nfc.h b/include/nfc/nfc.h index 8d6309d..cb58418 100644 --- a/include/nfc/nfc.h +++ b/include/nfc/nfc.h @@ -79,7 +79,7 @@ extern "C" { NFC_EXPORT int nfc_initiator_deselect_target (nfc_device *pnd); NFC_EXPORT int nfc_initiator_transceive_bytes (nfc_device *pnd, const uint8_t *pbtTx, const size_t szTx, uint8_t *pbtRx, size_t *pszRx, int timeout); 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, size_t *pszRxBits, uint8_t *pbtRxPar); - NFC_EXPORT bool nfc_initiator_transceive_bytes_timed (nfc_device *pnd, const uint8_t *pbtTx, const size_t szTx, uint8_t *pbtRx, size_t *pszRx, uint32_t *cycles); + NFC_EXPORT int nfc_initiator_transceive_bytes_timed (nfc_device *pnd, const uint8_t *pbtTx, const size_t szTx, uint8_t *pbtRx, size_t *pszRx, uint32_t *cycles); NFC_EXPORT bool nfc_initiator_transceive_bits_timed (nfc_device *pnd, const uint8_t *pbtTx, const size_t szTxBits, const uint8_t *pbtTxPar, uint8_t *pbtRx, size_t *pszRxBits, uint8_t *pbtRxPar, uint32_t *cycles); /* NFC target: act as tag (i.e. MIFARE Classic) or NFC target device. */ diff --git a/libnfc/chips/pn53x.c b/libnfc/chips/pn53x.c index c129985..fc42e28 100644 --- a/libnfc/chips/pn53x.c +++ b/libnfc/chips/pn53x.c @@ -1412,23 +1412,24 @@ pn53x_initiator_transceive_bits_timed (struct nfc_device *pnd, const uint8_t *pb return true; } -bool +int pn53x_initiator_transceive_bytes_timed (struct nfc_device *pnd, const uint8_t *pbtTx, const size_t szTx, uint8_t *pbtRx, size_t *pszRx, uint32_t *cycles) { uint16_t i; uint8_t sz; + int res = 0; // We can not just send bytes without parity while the PN53X expects we handled them if (!pnd->bPar) { pnd->last_error = NFC_EINVARG; - return false; + return pnd->last_error; } // Sorry, no easy framing support // TODO to be changed once we'll provide easy framing support from libnfc itself... if (pnd->bEasyFraming) { pnd->last_error = NFC_ENOTIMPL; - return false; + return pnd->last_error; } __pn53x_init_timer(pnd, *cycles); @@ -1456,8 +1457,8 @@ pn53x_initiator_transceive_bytes_timed (struct nfc_device *pnd, const uint8_t *p BUFFER_APPEND (abtWriteRegisterCmd, PN53X_REG_CIU_BitFraming & 0xff); BUFFER_APPEND (abtWriteRegisterCmd, SYMBOL_START_SEND); // Let's send the previously constructed WriteRegister command - if (pn53x_transceive (pnd, abtWriteRegisterCmd, BUFFER_SIZE (abtWriteRegisterCmd), NULL, NULL, -1) < 0) { - return false; + if ((res = pn53x_transceive (pnd, abtWriteRegisterCmd, BUFFER_SIZE (abtWriteRegisterCmd), NULL, NULL, -1)) < 0) { + return res; } // Recv data @@ -1488,8 +1489,8 @@ pn53x_initiator_transceive_bytes_timed (struct nfc_device *pnd, const uint8_t *p uint8_t abtRes[PN53x_EXTENDED_FRAME__DATA_MAX_LEN]; size_t szRes = sizeof(abtRes); // Let's send the previously constructed ReadRegister command - if (pn53x_transceive (pnd, abtReadRegisterCmd, BUFFER_SIZE (abtReadRegisterCmd), abtRes, &szRes, -1) < 0) { - return false; + if ((res = pn53x_transceive (pnd, abtReadRegisterCmd, BUFFER_SIZE (abtReadRegisterCmd), abtRes, &szRes, -1)) < 0) { + return res; } for (i = 0; i < sz; i++) { pbtRx[i+*pszRx] = abtRes[i+off]; @@ -1512,7 +1513,7 @@ pn53x_initiator_transceive_bytes_timed (struct nfc_device *pnd, const uint8_t *p } else { *cycles = __pn53x_get_timer (pnd, pbtTx[szTx -1]); } - return true; + return *pszRx; } int diff --git a/libnfc/chips/pn53x.h b/libnfc/chips/pn53x.h index 10bca76..18e7108 100644 --- a/libnfc/chips/pn53x.h +++ b/libnfc/chips/pn53x.h @@ -307,7 +307,7 @@ int pn53x_initiator_transceive_bytes (struct nfc_device *pnd, const uint8_t bool pn53x_initiator_transceive_bits_timed (struct nfc_device *pnd, const uint8_t *pbtTx, const size_t szTxBits, const uint8_t *pbtTxPar, uint8_t *pbtRx, size_t *pszRxBits, uint8_t *pbtRxPar, uint32_t *cycles); -bool pn53x_initiator_transceive_bytes_timed (struct nfc_device *pnd, const uint8_t *pbtTx, const size_t szTx, +int pn53x_initiator_transceive_bytes_timed (struct nfc_device *pnd, const uint8_t *pbtTx, const size_t szTx, uint8_t *pbtRx, size_t *pszRx, uint32_t *cycles); int pn53x_initiator_deselect_target (struct nfc_device *pnd); diff --git a/libnfc/nfc-internal.h b/libnfc/nfc-internal.h index 1a20465..0946fb0 100644 --- a/libnfc/nfc-internal.h +++ b/libnfc/nfc-internal.h @@ -139,7 +139,7 @@ struct nfc_driver_t { int (*initiator_deselect_target) (struct nfc_device *pnd); int (*initiator_transceive_bytes) (struct nfc_device *pnd, const uint8_t * pbtTx, const size_t szTx, uint8_t * pbtRx, size_t * pszRx, int timeout); int (*initiator_transceive_bits) (struct nfc_device *pnd, const uint8_t * pbtTx, const size_t szTxBits, const uint8_t * pbtTxPar, uint8_t * pbtRx, size_t * pszRxBits, uint8_t * pbtRxPar); - bool (*initiator_transceive_bytes_timed) (struct nfc_device *pnd, const uint8_t * pbtTx, const size_t szTx, uint8_t * pbtRx, size_t * pszRx, uint32_t * cycles); + int (*initiator_transceive_bytes_timed) (struct nfc_device *pnd, const uint8_t * pbtTx, const size_t szTx, uint8_t * pbtRx, size_t * pszRx, uint32_t * cycles); bool (*initiator_transceive_bits_timed) (struct nfc_device *pnd, const uint8_t * pbtTx, const size_t szTxBits, const uint8_t * pbtTxPar, uint8_t * pbtRx, size_t * pszRxBits, uint8_t * pbtRxPar, uint32_t * cycles); int (*target_init) (struct nfc_device *pnd, nfc_target * pnt, uint8_t * pbtRx, size_t * pszRx); diff --git a/libnfc/nfc.c b/libnfc/nfc.c index d006986..8d60e95 100644 --- a/libnfc/nfc.c +++ b/libnfc/nfc.c @@ -546,7 +546,7 @@ nfc_initiator_transceive_bits (nfc_device *pnd, const uint8_t *pbtTx, const size /** * @brief Send data to target then retrieve data from target - * @return Returns \c true if action was successfully performed; otherwise returns \c false. + * @return Returns received bytes count on success, otherwise returns libnfc's error code. * * This function is similar to nfc_initiator_transceive_bytes() with the following differences: * - A precise cycles counter will indicate the number of cycles between emission & reception of frames. @@ -564,7 +564,7 @@ nfc_initiator_transceive_bits (nfc_device *pnd, const uint8_t *pbtTx, const size * @warning The configuration option \a NP_EASY_FRAMING must be set to \c false. * @warning The configuration option \a NP_HANDLE_PARITY must be set to \c true (the default value). */ -bool +int nfc_initiator_transceive_bytes_timed (nfc_device *pnd, const uint8_t *pbtTx, const size_t szTx, uint8_t *pbtRx, size_t *pszRx, uint32_t *cycles) { From 1c49329ffad1de30b299383183cc3f7548d243be Mon Sep 17 00:00:00 2001 From: Audrey Diacre Date: Wed, 4 Jan 2012 13:18:11 +0000 Subject: [PATCH 050/113] nfc_initiator_transceive_bits_timed() function returns now received bits count on success and libnfc error code on failure. --- include/nfc/nfc.h | 2 +- libnfc/chips/pn53x.c | 19 ++++++++++--------- libnfc/chips/pn53x.h | 2 +- libnfc/nfc-internal.h | 2 +- libnfc/nfc.c | 4 ++-- 5 files changed, 15 insertions(+), 14 deletions(-) diff --git a/include/nfc/nfc.h b/include/nfc/nfc.h index cb58418..b471a38 100644 --- a/include/nfc/nfc.h +++ b/include/nfc/nfc.h @@ -80,7 +80,7 @@ extern "C" { NFC_EXPORT int nfc_initiator_transceive_bytes (nfc_device *pnd, const uint8_t *pbtTx, const size_t szTx, uint8_t *pbtRx, size_t *pszRx, int timeout); 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, size_t *pszRxBits, 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, size_t *pszRx, uint32_t *cycles); - NFC_EXPORT bool nfc_initiator_transceive_bits_timed (nfc_device *pnd, const uint8_t *pbtTx, const size_t szTxBits, const uint8_t *pbtTxPar, uint8_t *pbtRx, size_t *pszRxBits, uint8_t *pbtRxPar, 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, size_t *pszRxBits, uint8_t *pbtRxPar, uint32_t *cycles); /* 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, size_t *pszRx); diff --git a/libnfc/chips/pn53x.c b/libnfc/chips/pn53x.c index fc42e28..e232824 100644 --- a/libnfc/chips/pn53x.c +++ b/libnfc/chips/pn53x.c @@ -1310,7 +1310,7 @@ uint32_t __pn53x_get_timer(struct nfc_device *pnd, const uint8_t last_cmd_byte) return u32cycles; } -bool +int pn53x_initiator_transceive_bits_timed (struct nfc_device *pnd, const uint8_t *pbtTx, const size_t szTxBits, const uint8_t *pbtTxPar, uint8_t *pbtRx, size_t *pszRxBits, uint8_t *pbtRxPar, uint32_t *cycles) { @@ -1319,21 +1319,22 @@ pn53x_initiator_transceive_bits_timed (struct nfc_device *pnd, const uint8_t *pb (void) pbtRxPar; uint16_t i; uint8_t sz; + int res = 0; // Sorry, no arbitrary parity bits support for now if (!pnd->bPar) { pnd->last_error = NFC_ENOTIMPL; - return false; + return pnd->last_error; } // Sorry, no easy framing support if (pnd->bEasyFraming) { pnd->last_error = NFC_ENOTIMPL; - return false; + return pnd->last_error; } // TODO CRC support but it probably doesn't make sense for (szTxBits % 8 != 0) ... if (pnd->bCrc) { pnd->last_error = NFC_ENOTIMPL; - return false; + return pnd->last_error; } __pn53x_init_timer(pnd, *cycles); @@ -1361,8 +1362,8 @@ pn53x_initiator_transceive_bits_timed (struct nfc_device *pnd, const uint8_t *pb BUFFER_APPEND (abtWriteRegisterCmd, PN53X_REG_CIU_BitFraming & 0xff); BUFFER_APPEND (abtWriteRegisterCmd, SYMBOL_START_SEND | ((szTxBits % 8) & SYMBOL_TX_LAST_BITS)); // Let's send the previously constructed WriteRegister command - if (pn53x_transceive (pnd, abtWriteRegisterCmd, BUFFER_SIZE (abtWriteRegisterCmd), NULL, NULL, -1) < 0) { - return false; + if ((res = pn53x_transceive (pnd, abtWriteRegisterCmd, BUFFER_SIZE (abtWriteRegisterCmd), NULL, NULL, -1)) < 0) { + return res; } // Recv data @@ -1393,8 +1394,8 @@ pn53x_initiator_transceive_bits_timed (struct nfc_device *pnd, const uint8_t *pb uint8_t abtRes[PN53x_EXTENDED_FRAME__DATA_MAX_LEN]; size_t szRes = sizeof(abtRes); // Let's send the previously constructed ReadRegister command - if (pn53x_transceive (pnd, abtReadRegisterCmd, BUFFER_SIZE (abtReadRegisterCmd), abtRes, &szRes, -1) < 0) { - return false; + if ((res = pn53x_transceive (pnd, abtReadRegisterCmd, BUFFER_SIZE (abtReadRegisterCmd), abtRes, &szRes, -1)) < 0) { + return res; } for (i = 0; i < sz; i++) { pbtRx[i+*pszRxBits] = abtRes[i+off]; @@ -1409,7 +1410,7 @@ pn53x_initiator_transceive_bits_timed (struct nfc_device *pnd, const uint8_t *pb // Recv corrected timer value *cycles = __pn53x_get_timer (pnd, pbtTx[szTxBits / 8]); - return true; + return *pszRxBits; } int diff --git a/libnfc/chips/pn53x.h b/libnfc/chips/pn53x.h index 18e7108..be02599 100644 --- a/libnfc/chips/pn53x.h +++ b/libnfc/chips/pn53x.h @@ -304,7 +304,7 @@ int pn53x_initiator_transceive_bits (struct nfc_device *pnd, const uint8_t *p uint8_t *pbtRxPar); int pn53x_initiator_transceive_bytes (struct nfc_device *pnd, const uint8_t *pbtTx, const size_t szTx, uint8_t *pbtRx, size_t *pszRx, int timeout); -bool pn53x_initiator_transceive_bits_timed (struct nfc_device *pnd, const uint8_t *pbtTx, const size_t szTxBits, +int pn53x_initiator_transceive_bits_timed (struct nfc_device *pnd, const uint8_t *pbtTx, const size_t szTxBits, const uint8_t *pbtTxPar, uint8_t *pbtRx, size_t *pszRxBits, uint8_t *pbtRxPar, uint32_t *cycles); int pn53x_initiator_transceive_bytes_timed (struct nfc_device *pnd, const uint8_t *pbtTx, const size_t szTx, diff --git a/libnfc/nfc-internal.h b/libnfc/nfc-internal.h index 0946fb0..0afbf2a 100644 --- a/libnfc/nfc-internal.h +++ b/libnfc/nfc-internal.h @@ -140,7 +140,7 @@ struct nfc_driver_t { int (*initiator_transceive_bytes) (struct nfc_device *pnd, const uint8_t * pbtTx, const size_t szTx, uint8_t * pbtRx, size_t * pszRx, int timeout); int (*initiator_transceive_bits) (struct nfc_device *pnd, const uint8_t * pbtTx, const size_t szTxBits, const uint8_t * pbtTxPar, uint8_t * pbtRx, size_t * pszRxBits, uint8_t * pbtRxPar); int (*initiator_transceive_bytes_timed) (struct nfc_device *pnd, const uint8_t * pbtTx, const size_t szTx, uint8_t * pbtRx, size_t * pszRx, uint32_t * cycles); - bool (*initiator_transceive_bits_timed) (struct nfc_device *pnd, const uint8_t * pbtTx, const size_t szTxBits, const uint8_t * pbtTxPar, uint8_t * pbtRx, size_t * pszRxBits, uint8_t * pbtRxPar, 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, size_t * pszRxBits, uint8_t * pbtRxPar, uint32_t * cycles); int (*target_init) (struct nfc_device *pnd, nfc_target * pnt, uint8_t * pbtRx, size_t * pszRx); 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 8d60e95..ee22f7a 100644 --- a/libnfc/nfc.c +++ b/libnfc/nfc.c @@ -573,7 +573,7 @@ nfc_initiator_transceive_bytes_timed (nfc_device *pnd, const uint8_t *pbtTx, con /** * @brief Transceive raw bit-frames to a target - * @return Returns \c true if action was successfully performed; otherwise returns \c false. + * @return Returns received bits count on success, otherwise returns libnfc's error code * * This function is similar to nfc_initiator_transceive_bits() with the following differences: * - A precise cycles counter will indicate the number of cycles between emission & reception of frames. @@ -592,7 +592,7 @@ nfc_initiator_transceive_bytes_timed (nfc_device *pnd, const uint8_t *pbtTx, con * @warning The configuration option \a NP_HANDLE_CRC must be set to \c false. * @warning The configuration option \a NP_HANDLE_PARITY must be set to \c true (the default value). */ -bool +int nfc_initiator_transceive_bits_timed (nfc_device *pnd, const uint8_t *pbtTx, const size_t szTxBits, const uint8_t *pbtTxPar, uint8_t *pbtRx, size_t *pszRxBits, uint8_t *pbtRxPar, uint32_t *cycles) { From 951dde8143b672ab324d3e0dc94850b9350766c7 Mon Sep 17 00:00:00 2001 From: Audrey Diacre Date: Wed, 4 Jan 2012 13:27:15 +0000 Subject: [PATCH 051/113] nfc_target_send_bits() function returns now sent bits count on success and libnfc error code on failure. --- examples/nfc-emulate-uid.c | 2 +- examples/nfc-relay.c | 2 +- include/nfc/nfc.h | 2 +- libnfc/chips/pn53x.c | 15 ++++++++------- libnfc/chips/pn53x.h | 2 +- libnfc/nfc-internal.h | 2 +- libnfc/nfc.c | 4 ++-- 7 files changed, 15 insertions(+), 14 deletions(-) diff --git a/examples/nfc-emulate-uid.c b/examples/nfc-emulate-uid.c index 0835034..80339bf 100644 --- a/examples/nfc-emulate-uid.c +++ b/examples/nfc-emulate-uid.c @@ -205,7 +205,7 @@ main (int argc, char *argv[]) // Test if we know how to respond if (szTxBits) { // Send and print the command to the screen - if (!nfc_target_send_bits (pnd, pbtTx, szTxBits, NULL)) { + if (nfc_target_send_bits (pnd, pbtTx, szTxBits, NULL) < 0) { nfc_perror (pnd, "nfc_target_send_bits"); goto error; } diff --git a/examples/nfc-relay.c b/examples/nfc-relay.c index 7f4597a..fc7230d 100644 --- a/examples/nfc-relay.c +++ b/examples/nfc-relay.c @@ -199,7 +199,7 @@ main (int argc, char *argv[]) if (nfc_initiator_transceive_bits (pndReader, abtReaderRx, szReaderRxBits, abtReaderRxPar, abtTagRx, &szTagRxBits, abtTagRxPar) > 0) { // Redirect the answer back to the reader - if (!nfc_target_send_bits (pndTag, abtTagRx, szTagRxBits, abtTagRxPar)) { + if (nfc_target_send_bits (pndTag, abtTagRx, szTagRxBits, abtTagRxPar) < 0) { nfc_perror (pndTag, "nfc_target_send_bits"); exit (EXIT_FAILURE); } diff --git a/include/nfc/nfc.h b/include/nfc/nfc.h index b471a38..29b3199 100644 --- a/include/nfc/nfc.h +++ b/include/nfc/nfc.h @@ -86,7 +86,7 @@ extern "C" { NFC_EXPORT int nfc_target_init (nfc_device *pnd, nfc_target *pnt, uint8_t *pbtRx, size_t *pszRx); NFC_EXPORT int nfc_target_send_bytes (nfc_device *pnd, const uint8_t *pbtTx, const size_t szTx, int timeout); NFC_EXPORT int nfc_target_receive_bytes (nfc_device *pnd, uint8_t *pbtRx, size_t *pszRx, int timeout); - NFC_EXPORT bool nfc_target_send_bits (nfc_device *pnd, const uint8_t *pbtTx, const size_t szTxBits, const uint8_t *pbtTxPar); + NFC_EXPORT int nfc_target_send_bits (nfc_device *pnd, const uint8_t *pbtTx, const size_t szTxBits, const uint8_t *pbtTxPar); NFC_EXPORT bool nfc_target_receive_bits (nfc_device *pnd, uint8_t *pbtRx, size_t *pszRxBits, uint8_t *pbtRxPar); /* Error reporting */ diff --git a/libnfc/chips/pn53x.c b/libnfc/chips/pn53x.c index e232824..9267b03 100644 --- a/libnfc/chips/pn53x.c +++ b/libnfc/chips/pn53x.c @@ -1831,13 +1831,14 @@ pn53x_target_receive_bytes (struct nfc_device *pnd, uint8_t *pbtRx, size_t *pszR return *pszRx; } -bool +int pn53x_target_send_bits (struct nfc_device *pnd, const uint8_t *pbtTx, const size_t szTxBits, const uint8_t *pbtTxPar) { size_t szFrameBits = 0; size_t szFrameBytes = 0; uint8_t ui8Bits = 0; uint8_t abtCmd[PN53x_EXTENDED_FRAME__DATA_MAX_LEN] = { TgResponseToInitiator }; + int res = 0; // Check if we should prepare the parity bits ourself if (!pnd->bPar) { @@ -1858,15 +1859,15 @@ pn53x_target_send_bits (struct nfc_device *pnd, const uint8_t *pbtTx, const size memcpy (abtCmd + 1, pbtTx, szFrameBytes); // Set the amount of transmission bits in the PN53X chip register - if (pn53x_set_tx_bits (pnd, ui8Bits) < 0) - return false; + if ((res = pn53x_set_tx_bits (pnd, ui8Bits)) < 0) + return res; // Try to send the bits to the reader - if (pn53x_transceive (pnd, abtCmd, szFrameBytes + 1, NULL, NULL, -1) < 0) - return false; + if ((res = pn53x_transceive (pnd, abtCmd, szFrameBytes + 1, NULL, NULL, -1)) < 0) + return res; - // Everyting seems ok, return true - return true; + // Everyting seems ok, return return sent bits count + return szTxBits; } int diff --git a/libnfc/chips/pn53x.h b/libnfc/chips/pn53x.h index be02599..42eeb9c 100644 --- a/libnfc/chips/pn53x.h +++ b/libnfc/chips/pn53x.h @@ -315,7 +315,7 @@ int pn53x_initiator_deselect_target (struct nfc_device *pnd); int pn53x_target_init (struct nfc_device *pnd, nfc_target *pnt, uint8_t *pbtRx, size_t *pszRx); bool pn53x_target_receive_bits (struct nfc_device *pnd, uint8_t *pbtRx, size_t *pszRxBits, uint8_t *pbtRxPar); int pn53x_target_receive_bytes (struct nfc_device *pnd, uint8_t *pbtRx, size_t *pszRx, int timeout); -bool pn53x_target_send_bits (struct nfc_device *pnd, const uint8_t *pbtTx, const size_t szTxBits, const uint8_t *pbtTxPar); +int pn53x_target_send_bits (struct nfc_device *pnd, const uint8_t *pbtTx, const size_t szTxBits, const uint8_t *pbtTxPar); int pn53x_target_send_bytes (struct nfc_device *pnd, const uint8_t *pbtTx, const size_t szTx, int timeout); // Error handling functions diff --git a/libnfc/nfc-internal.h b/libnfc/nfc-internal.h index 0afbf2a..51ac12c 100644 --- a/libnfc/nfc-internal.h +++ b/libnfc/nfc-internal.h @@ -145,7 +145,7 @@ struct nfc_driver_t { int (*target_init) (struct nfc_device *pnd, nfc_target * pnt, uint8_t * pbtRx, size_t * pszRx); int (*target_send_bytes) (struct nfc_device *pnd, const uint8_t * pbtTx, const size_t szTx, int timeout); int (*target_receive_bytes) (struct nfc_device *pnd, uint8_t * pbtRx, size_t * pszRx, int timeout); - bool (*target_send_bits) (struct nfc_device *pnd, const uint8_t * pbtTx, const size_t szTxBits, const uint8_t * pbtTxPar); + int (*target_send_bits) (struct nfc_device *pnd, const uint8_t * pbtTx, const size_t szTxBits, const uint8_t * pbtTxPar); bool (*target_receive_bits) (struct nfc_device *pnd, uint8_t * pbtRx, size_t * pszRxBits, uint8_t * pbtRxPar); int (*device_set_property_bool) (struct nfc_device *pnd, const nfc_property property, const bool bEnable); diff --git a/libnfc/nfc.c b/libnfc/nfc.c index ee22f7a..439d795 100644 --- a/libnfc/nfc.c +++ b/libnfc/nfc.c @@ -736,12 +736,12 @@ nfc_target_receive_bytes (nfc_device *pnd, uint8_t *pbtRx, size_t *pszRx, int ti /** * @brief Send raw bit-frames - * @return Returns \c true if action was successfully performed; otherwise returns \c false. + * @return Returns sent bits count on success, otherwise returns libnfc's error code. * * This function can be used to transmit (raw) bit-frames to the \e initiator * using the specified NFC device (configured as \e target). */ -bool +int nfc_target_send_bits (nfc_device *pnd, const uint8_t *pbtTx, const size_t szTxBits, const uint8_t *pbtTxPar) { HAL (target_send_bits, pnd, pbtTx, szTxBits, pbtTxPar); From 0de1136037cada317e9f716b66332592966f3baf Mon Sep 17 00:00:00 2001 From: Audrey Diacre Date: Wed, 4 Jan 2012 13:33:53 +0000 Subject: [PATCH 052/113] nfc_target_receive_bits() function returns now received bits count on success and libnfc error code on failure. --- examples/nfc-emulate-uid.c | 2 +- examples/nfc-relay.c | 2 +- include/nfc/nfc.h | 2 +- libnfc/chips/pn53x.c | 20 +++++++++++--------- libnfc/chips/pn53x.h | 2 +- libnfc/nfc-internal.h | 2 +- libnfc/nfc.c | 4 ++-- 7 files changed, 18 insertions(+), 16 deletions(-) diff --git a/examples/nfc-emulate-uid.c b/examples/nfc-emulate-uid.c index 80339bf..90c01aa 100644 --- a/examples/nfc-emulate-uid.c +++ b/examples/nfc-emulate-uid.c @@ -172,7 +172,7 @@ main (int argc, char *argv[]) while (true) { // Test if we received a frame - if (nfc_target_receive_bits (pnd, abtRecv, &szRecvBits, NULL)) { + if (nfc_target_receive_bits (pnd, abtRecv, &szRecvBits, NULL) > 0) { // Prepare the command to send back for the anti-collision request switch (szRecvBits) { case 7: // Request or Wakeup diff --git a/examples/nfc-relay.c b/examples/nfc-relay.c index fc7230d..865ff60 100644 --- a/examples/nfc-relay.c +++ b/examples/nfc-relay.c @@ -175,7 +175,7 @@ main (int argc, char *argv[]) while (!quitting) { // Test if we received a frame from the reader - if (nfc_target_receive_bits (pndTag, abtReaderRx, &szReaderRxBits, abtReaderRxPar)) { + if (nfc_target_receive_bits (pndTag, abtReaderRx, &szReaderRxBits, abtReaderRxPar) > 0) { // Drop down the field before sending a REQA command and start a new session if (szReaderRxBits == 7 && abtReaderRx[0] == 0x26) { // Drop down field for a very short time (original tag will reboot) diff --git a/include/nfc/nfc.h b/include/nfc/nfc.h index 29b3199..006cfe1 100644 --- a/include/nfc/nfc.h +++ b/include/nfc/nfc.h @@ -87,7 +87,7 @@ extern "C" { NFC_EXPORT int nfc_target_send_bytes (nfc_device *pnd, const uint8_t *pbtTx, const size_t szTx, int timeout); NFC_EXPORT int nfc_target_receive_bytes (nfc_device *pnd, uint8_t *pbtRx, size_t *pszRx, int timeout); NFC_EXPORT int nfc_target_send_bits (nfc_device *pnd, const uint8_t *pbtTx, const size_t szTxBits, const uint8_t *pbtTxPar); - NFC_EXPORT bool nfc_target_receive_bits (nfc_device *pnd, uint8_t *pbtRx, size_t *pszRxBits, uint8_t *pbtRxPar); + NFC_EXPORT int nfc_target_receive_bits (nfc_device *pnd, uint8_t *pbtRx, size_t *pszRxBits, uint8_t *pbtRxPar); /* Error reporting */ NFC_EXPORT const char *nfc_strerror (const nfc_device *pnd); diff --git a/libnfc/chips/pn53x.c b/libnfc/chips/pn53x.c index 9267b03..5ef63f2 100644 --- a/libnfc/chips/pn53x.c +++ b/libnfc/chips/pn53x.c @@ -1747,21 +1747,23 @@ pn53x_target_init (struct nfc_device *pnd, nfc_target *pnt, uint8_t *pbtRx, size return NFC_SUCCESS; } -bool +int pn53x_target_receive_bits (struct nfc_device *pnd, uint8_t *pbtRx, size_t *pszRxBits, uint8_t *pbtRxPar) { uint8_t abtCmd[] = { TgGetInitiatorCommand }; uint8_t abtRx[PN53x_EXTENDED_FRAME__DATA_MAX_LEN]; size_t szRx = sizeof (abtRx); + int res = 0; + // Try to gather a received frame from the reader - if (pn53x_transceive (pnd, abtCmd, sizeof (abtCmd), abtRx, &szRx, -1) < 0) - return false; + if ((res = pn53x_transceive (pnd, abtCmd, sizeof (abtCmd), abtRx, &szRx, -1)) < 0) + return res; // Get the last bit-count that is stored in the received byte uint8_t ui8rcc; - if (pn53x_read_register (pnd, PN53X_REG_CIU_Control, &ui8rcc) < 0) - return false; + if ((res = pn53x_read_register (pnd, PN53X_REG_CIU_Control, &ui8rcc)) < 0) + return res; uint8_t ui8Bits = ui8rcc & SYMBOL_RX_LAST_BITS; // Recover the real frame length in bits @@ -1778,8 +1780,8 @@ pn53x_target_receive_bits (struct nfc_device *pnd, uint8_t *pbtRx, size_t *pszRx // Copy the received bytes memcpy (pbtRx, abtRx + 1, szRx - 1); } - // Everyting seems ok, return true - return true; + // Everyting seems ok, return received bits count + return *pszRxBits; } int @@ -1821,13 +1823,13 @@ pn53x_target_receive_bytes (struct nfc_device *pnd, uint8_t *pbtRx, size_t *pszR if (pn53x_transceive (pnd, abtCmd, sizeof (abtCmd), abtRx, &szRx, timeout) < 0) return pnd->last_error; - // Save the received byte count + // Save the received bytes count *pszRx = szRx - 1; // Copy the received bytes memcpy (pbtRx, abtRx + 1, *pszRx); - // Everyting seems ok, return true + // Everyting seems ok, return received bytes count return *pszRx; } diff --git a/libnfc/chips/pn53x.h b/libnfc/chips/pn53x.h index 42eeb9c..8fd96ab 100644 --- a/libnfc/chips/pn53x.h +++ b/libnfc/chips/pn53x.h @@ -313,7 +313,7 @@ int pn53x_initiator_deselect_target (struct nfc_device *pnd); // NFC device as Target functions int pn53x_target_init (struct nfc_device *pnd, nfc_target *pnt, uint8_t *pbtRx, size_t *pszRx); -bool pn53x_target_receive_bits (struct nfc_device *pnd, uint8_t *pbtRx, size_t *pszRxBits, uint8_t *pbtRxPar); +int pn53x_target_receive_bits (struct nfc_device *pnd, uint8_t *pbtRx, size_t *pszRxBits, uint8_t *pbtRxPar); int pn53x_target_receive_bytes (struct nfc_device *pnd, uint8_t *pbtRx, size_t *pszRx, int timeout); int pn53x_target_send_bits (struct nfc_device *pnd, const uint8_t *pbtTx, const size_t szTxBits, const uint8_t *pbtTxPar); int pn53x_target_send_bytes (struct nfc_device *pnd, const uint8_t *pbtTx, const size_t szTx, int timeout); diff --git a/libnfc/nfc-internal.h b/libnfc/nfc-internal.h index 51ac12c..adff35c 100644 --- a/libnfc/nfc-internal.h +++ b/libnfc/nfc-internal.h @@ -146,7 +146,7 @@ struct nfc_driver_t { int (*target_send_bytes) (struct nfc_device *pnd, const uint8_t * pbtTx, const size_t szTx, int timeout); int (*target_receive_bytes) (struct nfc_device *pnd, uint8_t * pbtRx, size_t * pszRx, int timeout); int (*target_send_bits) (struct nfc_device *pnd, const uint8_t * pbtTx, const size_t szTxBits, const uint8_t * pbtTxPar); - bool (*target_receive_bits) (struct nfc_device *pnd, uint8_t * pbtRx, size_t * pszRxBits, uint8_t * pbtRxPar); + int (*target_receive_bits) (struct nfc_device *pnd, uint8_t * pbtRx, size_t * pszRxBits, uint8_t * pbtRxPar); int (*device_set_property_bool) (struct nfc_device *pnd, const nfc_property property, const bool bEnable); int (*device_set_property_int) (struct nfc_device *pnd, const nfc_property property, const int value); diff --git a/libnfc/nfc.c b/libnfc/nfc.c index 439d795..371331d 100644 --- a/libnfc/nfc.c +++ b/libnfc/nfc.c @@ -749,7 +749,7 @@ nfc_target_send_bits (nfc_device *pnd, const uint8_t *pbtTx, const size_t szTxBi /** * @brief Receive bit-frames - * @return Returns \c true if action was successfully performed; otherwise returns \c false. + * @return Returns received bits count on success, otherwise returns libnfc's error code * * This function makes it possible to receive (raw) bit-frames. It returns all * the messages that are stored in the FIFO buffer of the \e PN53x chip. It @@ -758,7 +758,7 @@ nfc_target_send_bits (nfc_device *pnd, const uint8_t *pbtTx, const size_t szTxBi * NP_ACCEPT_MULTIPLE_FRAMES configuration option to avoid losing transmitted * frames. */ -bool +int nfc_target_receive_bits (nfc_device *pnd, uint8_t *pbtRx, size_t *pszRxBits, uint8_t *pbtRxPar) { HAL (target_receive_bits, pnd, pbtRx, pszRxBits, pbtRxPar); From 61c3e5b8145ce5e50affdaf58a3fa5182eb0064c Mon Sep 17 00:00:00 2001 From: Audrey Diacre Date: Wed, 4 Jan 2012 14:59:16 +0000 Subject: [PATCH 053/113] pn53x_wrap_frame() and pn53x_unwrap_frame() functions return now frame length in bits on success and libnfc error code on failure. --- libnfc/chips/pn53x.c | 47 ++++++++++++++++++++++++++------------------ libnfc/chips/pn53x.h | 6 ++---- 2 files changed, 30 insertions(+), 23 deletions(-) diff --git a/libnfc/chips/pn53x.c b/libnfc/chips/pn53x.c index 5ef63f2..8be568b 100644 --- a/libnfc/chips/pn53x.c +++ b/libnfc/chips/pn53x.c @@ -223,28 +223,29 @@ pn53x_set_tx_bits (struct nfc_device *pnd, const uint8_t ui8Bits) return NFC_SUCCESS; } -bool +int pn53x_wrap_frame (const uint8_t *pbtTx, const size_t szTxBits, const uint8_t *pbtTxPar, - uint8_t *pbtFrame, size_t *pszFrameBits) + uint8_t *pbtFrame) { uint8_t btFrame; uint8_t btData; uint32_t uiBitPos; uint32_t uiDataPos = 0; size_t szBitsLeft = szTxBits; + size_t szFrameBits = 0; // Make sure we should frame at least something if (szBitsLeft == 0) - return false; + return NFC_ECHIP; // Handle a short response (1byte) as a special case if (szBitsLeft < 9) { *pbtFrame = *pbtTx; - *pszFrameBits = szTxBits; - return true; + szFrameBits = szTxBits; + return szFrameBits; } // We start by calculating the frame length in bits - *pszFrameBits = szTxBits + (szTxBits / 8); + szFrameBits = szTxBits + (szTxBits / 8); // Parse the data bytes and add the parity bits // This is really a sensitive process, mirror the frame bytes and append parity bits @@ -271,7 +272,7 @@ pn53x_wrap_frame (const uint8_t *pbtTx, const size_t szTxBits, const uint8_t *pb uiDataPos++; // Test if we are done if (szBitsLeft < 9) - return true; + return szFrameBits; szBitsLeft -= 8; } // Every 8 data bytes we lose one frame byte to the parities @@ -279,9 +280,8 @@ pn53x_wrap_frame (const uint8_t *pbtTx, const size_t szTxBits, const uint8_t *pb } } -bool -pn53x_unwrap_frame (const uint8_t *pbtFrame, const size_t szFrameBits, uint8_t *pbtRx, size_t *pszRxBits, - uint8_t *pbtRxPar) +int +pn53x_unwrap_frame (const uint8_t *pbtFrame, const size_t szFrameBits, uint8_t *pbtRx, uint8_t *pbtRxPar) { uint8_t btFrame; uint8_t btData; @@ -289,19 +289,20 @@ pn53x_unwrap_frame (const uint8_t *pbtFrame, const size_t szFrameBits, uint8_t * uint32_t uiDataPos = 0; uint8_t *pbtFramePos = (uint8_t *) pbtFrame; size_t szBitsLeft = szFrameBits; + size_t szRxBits = 0; // Make sure we should frame at least something if (szBitsLeft == 0) - return false; + return NFC_ECHIP; // Handle a short response (1byte) as a special case if (szBitsLeft < 9) { *pbtRx = *pbtFrame; - *pszRxBits = szFrameBits; - return true; + szRxBits = szFrameBits; + return szRxBits; } // Calculate the data length in bits - *pszRxBits = szFrameBits - (szFrameBits / 9); + szRxBits = szFrameBits - (szFrameBits / 9); // Parse the frame bytes, remove the parity bits and store them in the parity array // This process is the reverse of WrapFrame(), look there for more info @@ -318,7 +319,7 @@ pn53x_unwrap_frame (const uint8_t *pbtFrame, const size_t szFrameBits, uint8_t * uiDataPos++; // Test if we are done if (szBitsLeft < 9) - return true; + return szRxBits; szBitsLeft -= 9; } // Every 8 data bytes we lose one frame byte to the parities @@ -1121,7 +1122,9 @@ pn53x_initiator_transceive_bits (struct nfc_device *pnd, const uint8_t *pbtTx, c // Check if we should prepare the parity bits ourself if (!pnd->bPar) { // Convert data with parity to a frame - pn53x_wrap_frame (pbtTx, szTxBits, pbtTxPar, abtCmd + 1, &szFrameBits); + if ((res = pn53x_wrap_frame (pbtTx, szTxBits, pbtTxPar, abtCmd + 1)) < 0) + return res; + szFrameBits = res; } else { szFrameBits = szTxBits; } @@ -1160,7 +1163,9 @@ pn53x_initiator_transceive_bits (struct nfc_device *pnd, const uint8_t *pbtTx, c // Check if we should recover the parity bits ourself if (!pnd->bPar) { // Unwrap the response frame - pn53x_unwrap_frame (abtRx + 1, szFrameBits, pbtRx, pszRxBits, pbtRxPar); + if ((res = pn53x_unwrap_frame (abtRx + 1, szFrameBits, pbtRx, pbtRxPar)) < 0) + return res; + *pszRxBits = res; } else { // Save the received bits *pszRxBits = szFrameBits; @@ -1773,7 +1778,9 @@ pn53x_target_receive_bits (struct nfc_device *pnd, uint8_t *pbtRx, size_t *pszRx // Check if we should recover the parity bits ourself if (!pnd->bPar) { // Unwrap the response frame - pn53x_unwrap_frame (abtRx + 1, szFrameBits, pbtRx, pszRxBits, pbtRxPar); + if ((res = pn53x_unwrap_frame (abtRx + 1, szFrameBits, pbtRx, pbtRxPar)) < 0) + return res; + *pszRxBits = res; } else { // Save the received bits *pszRxBits = szFrameBits; @@ -1845,7 +1852,9 @@ pn53x_target_send_bits (struct nfc_device *pnd, const uint8_t *pbtTx, const size // Check if we should prepare the parity bits ourself if (!pnd->bPar) { // Convert data with parity to a frame - pn53x_wrap_frame (pbtTx, szTxBits, pbtTxPar, abtCmd + 1, &szFrameBits); + if ((res = pn53x_wrap_frame (pbtTx, szTxBits, pbtTxPar, abtCmd + 1)) < 0) + return res; + szFrameBits = res; } else { szFrameBits = szTxBits; } diff --git a/libnfc/chips/pn53x.h b/libnfc/chips/pn53x.h index 8fd96ab..85b233c 100644 --- a/libnfc/chips/pn53x.h +++ b/libnfc/chips/pn53x.h @@ -268,10 +268,8 @@ int pn53x_transceive (struct nfc_device *pnd, const uint8_t *pbtTx, const siz int pn53x_set_parameters (struct nfc_device *pnd, const uint8_t ui8Value, const bool bEnable); int pn53x_set_tx_bits (struct nfc_device *pnd, const uint8_t ui8Bits); -bool pn53x_wrap_frame (const uint8_t *pbtTx, const size_t szTxBits, const uint8_t *pbtTxPar, uint8_t *pbtFrame, - size_t *pszFrameBits); -bool pn53x_unwrap_frame (const uint8_t *pbtFrame, const size_t szFrameBits, uint8_t *pbtRx, size_t *pszRxBits, - uint8_t *pbtRxPar); +int pn53x_wrap_frame (const uint8_t *pbtTx, const size_t szTxBits, const uint8_t *pbtTxPar, uint8_t *pbtFrame); +int pn53x_unwrap_frame (const uint8_t *pbtFrame, const size_t szFrameBits, uint8_t *pbtRx, uint8_t *pbtRxPar); bool pn53x_decode_target_data (const uint8_t *pbtRawData, size_t szRawData, pn53x_type chip_type, nfc_modulation_type nmt, nfc_target_info *pnti); From 6e7092b16033eb551e8f2194949033d66e29987f Mon Sep 17 00:00:00 2001 From: Audrey Diacre Date: Wed, 4 Jan 2012 15:30:42 +0000 Subject: [PATCH 054/113] nfc_initiator_transceive_bits() function does not now use pszRxBits as parameter because this function returns it. --- examples/nfc-anticol.c | 2 +- examples/nfc-relay.c | 6 +++--- include/nfc/nfc.h | 2 +- libnfc/chips/pn53x.c | 9 +++++---- libnfc/chips/pn53x.h | 3 +-- libnfc/nfc-internal.h | 2 +- libnfc/nfc.c | 5 ++--- utils/nfc-mfclassic.c | 5 ++--- utils/nfc-mfsetuid.c | 4 ++-- 9 files changed, 18 insertions(+), 20 deletions(-) diff --git a/examples/nfc-anticol.c b/examples/nfc-anticol.c index 20377e5..1080802 100644 --- a/examples/nfc-anticol.c +++ b/examples/nfc-anticol.c @@ -83,7 +83,7 @@ transmit_bits (const uint8_t *pbtTx, const size_t szTxBits) print_hex_bits (pbtTx, szTxBits); } // Transmit the bit frame command, we don't use the arbitrary parity feature - if (nfc_initiator_transceive_bits (pnd, pbtTx, szTxBits, NULL, abtRx, &szRxBits, NULL) < 0) + if ((szRxBits = nfc_initiator_transceive_bits (pnd, pbtTx, szTxBits, NULL, abtRx, NULL)) < 0) return false; // Show received answer diff --git a/examples/nfc-relay.c b/examples/nfc-relay.c index 865ff60..4402fa2 100644 --- a/examples/nfc-relay.c +++ b/examples/nfc-relay.c @@ -55,7 +55,7 @@ static uint8_t abtReaderRxPar[MAX_FRAME_LEN]; static size_t szReaderRxBits; static uint8_t abtTagRx[MAX_FRAME_LEN]; static uint8_t abtTagRxPar[MAX_FRAME_LEN]; -static size_t szTagRxBits; +static int szTagRxBits; static nfc_device *pndReader; static nfc_device *pndTag; static bool quitting = false; @@ -196,8 +196,8 @@ main (int argc, char *argv[]) print_hex_par (abtReaderRx, szReaderRxBits, abtReaderRxPar); } // Forward the frame to the original tag - if (nfc_initiator_transceive_bits - (pndReader, abtReaderRx, szReaderRxBits, abtReaderRxPar, abtTagRx, &szTagRxBits, abtTagRxPar) > 0) { + if ((szTagRxBits = nfc_initiator_transceive_bits + (pndReader, abtReaderRx, szReaderRxBits, abtReaderRxPar, abtTagRx, abtTagRxPar)) > 0) { // Redirect the answer back to the reader if (nfc_target_send_bits (pndTag, abtTagRx, szTagRxBits, abtTagRxPar) < 0) { nfc_perror (pndTag, "nfc_target_send_bits"); diff --git a/include/nfc/nfc.h b/include/nfc/nfc.h index 006cfe1..f7c2640 100644 --- a/include/nfc/nfc.h +++ b/include/nfc/nfc.h @@ -78,7 +78,7 @@ extern "C" { NFC_EXPORT int nfc_initiator_select_dep_target (nfc_device *pnd, const nfc_dep_mode ndm, const nfc_baud_rate nbr, const nfc_dep_info *pndiInitiator, nfc_target *pnt, const int timeout); NFC_EXPORT int nfc_initiator_deselect_target (nfc_device *pnd); NFC_EXPORT int nfc_initiator_transceive_bytes (nfc_device *pnd, const uint8_t *pbtTx, const size_t szTx, uint8_t *pbtRx, size_t *pszRx, int timeout); - 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, size_t *pszRxBits, uint8_t *pbtRxPar); + 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, size_t *pszRx, 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, size_t *pszRxBits, uint8_t *pbtRxPar, uint32_t *cycles); diff --git a/libnfc/chips/pn53x.c b/libnfc/chips/pn53x.c index 8be568b..aaeb8be 100644 --- a/libnfc/chips/pn53x.c +++ b/libnfc/chips/pn53x.c @@ -1110,11 +1110,12 @@ pn53x_initiator_select_dep_target(struct nfc_device *pnd, int pn53x_initiator_transceive_bits (struct nfc_device *pnd, const uint8_t *pbtTx, const size_t szTxBits, - const uint8_t *pbtTxPar, uint8_t *pbtRx, size_t *pszRxBits, uint8_t *pbtRxPar) + const uint8_t *pbtTxPar, uint8_t *pbtRx, uint8_t *pbtRxPar) { int res = 0; size_t szFrameBits = 0; size_t szFrameBytes = 0; + size_t szRxBits = 0; uint8_t ui8rcc; uint8_t ui8Bits = 0; uint8_t abtCmd[PN53x_EXTENDED_FRAME__DATA_MAX_LEN] = { InCommunicateThru }; @@ -1165,16 +1166,16 @@ pn53x_initiator_transceive_bits (struct nfc_device *pnd, const uint8_t *pbtTx, c // Unwrap the response frame if ((res = pn53x_unwrap_frame (abtRx + 1, szFrameBits, pbtRx, pbtRxPar)) < 0) return res; - *pszRxBits = res; + szRxBits = res; } else { // Save the received bits - *pszRxBits = szFrameBits; + szRxBits = szFrameBits; // Copy the received bytes memcpy (pbtRx, abtRx + 1, szRx - 1); } } // Everything went successful - return *pszRxBits; + return szRxBits; } int diff --git a/libnfc/chips/pn53x.h b/libnfc/chips/pn53x.h index 85b233c..d193612 100644 --- a/libnfc/chips/pn53x.h +++ b/libnfc/chips/pn53x.h @@ -298,8 +298,7 @@ int pn53x_initiator_select_dep_target (struct nfc_device *pnd, nfc_target *pnt, const int timeout); int pn53x_initiator_transceive_bits (struct nfc_device *pnd, const uint8_t *pbtTx, const size_t szTxBits, - const uint8_t *pbtTxPar, uint8_t *pbtRx, size_t *pszRxBits, - uint8_t *pbtRxPar); + const uint8_t *pbtTxPar, uint8_t *pbtRx, uint8_t *pbtRxPar); int pn53x_initiator_transceive_bytes (struct nfc_device *pnd, const uint8_t *pbtTx, const size_t szTx, uint8_t *pbtRx, size_t *pszRx, int timeout); int pn53x_initiator_transceive_bits_timed (struct nfc_device *pnd, const uint8_t *pbtTx, const size_t szTxBits, diff --git a/libnfc/nfc-internal.h b/libnfc/nfc-internal.h index adff35c..abebdd3 100644 --- a/libnfc/nfc-internal.h +++ b/libnfc/nfc-internal.h @@ -138,7 +138,7 @@ struct nfc_driver_t { int (*initiator_select_dep_target) (struct nfc_device *pnd, const nfc_dep_mode ndm, const nfc_baud_rate nbr, const nfc_dep_info * pndiInitiator, nfc_target * pnt, const int timeout); int (*initiator_deselect_target) (struct nfc_device *pnd); int (*initiator_transceive_bytes) (struct nfc_device *pnd, const uint8_t * pbtTx, const size_t szTx, uint8_t * pbtRx, size_t * pszRx, int timeout); - int (*initiator_transceive_bits) (struct nfc_device *pnd, const uint8_t * pbtTx, const size_t szTxBits, const uint8_t * pbtTxPar, uint8_t * pbtRx, size_t * pszRxBits, uint8_t * pbtRxPar); + 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, size_t * pszRx, 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, size_t * pszRxBits, uint8_t * pbtRxPar, uint32_t * cycles); diff --git a/libnfc/nfc.c b/libnfc/nfc.c index 371331d..1f620f4 100644 --- a/libnfc/nfc.c +++ b/libnfc/nfc.c @@ -527,7 +527,6 @@ nfc_initiator_transceive_bytes (nfc_device *pnd, const uint8_t *pbtTx, const siz * nfc_initiator_transceive_bytes() function. * * @param[out] pbtRx response from the tag - * @param[out] pszRxBits \a pbtRx length in bits * @param[out] pbtRxPar parameter contains a byte array of the corresponding parity bits * * The NFC device (configured as \e initiator) will transmit low-level messages @@ -539,9 +538,9 @@ nfc_initiator_transceive_bytes (nfc_device *pnd, const uint8_t *pbtTx, const siz */ int nfc_initiator_transceive_bits (nfc_device *pnd, const uint8_t *pbtTx, const size_t szTxBits, const uint8_t *pbtTxPar, - uint8_t *pbtRx, size_t *pszRxBits, uint8_t *pbtRxPar) + uint8_t *pbtRx, uint8_t *pbtRxPar) { - HAL (initiator_transceive_bits, pnd, pbtTx, szTxBits, pbtTxPar, pbtRx, pszRxBits, pbtRxPar); + HAL (initiator_transceive_bits, pnd, pbtTx, szTxBits, pbtTxPar, pbtRx, pbtRxPar); } /** diff --git a/utils/nfc-mfclassic.c b/utils/nfc-mfclassic.c index c585d47..c784a7c 100644 --- a/utils/nfc-mfclassic.c +++ b/utils/nfc-mfclassic.c @@ -82,7 +82,7 @@ static size_t num_keys = sizeof (keys) / 6; #define MAX_FRAME_LEN 264 static uint8_t abtRx[MAX_FRAME_LEN]; -static size_t szRxBits; +static int szRxBits; static size_t szRx = sizeof(abtRx); uint8_t abtHalt[4] = { 0x50, 0x00, 0x00, 0x00 }; @@ -98,8 +98,7 @@ transmit_bits (const uint8_t *pbtTx, const size_t szTxBits) printf ("Sent bits: "); print_hex_bits (pbtTx, szTxBits); // Transmit the bit frame command, we don't use the arbitrary parity feature - int res = 0; - if ((res = nfc_initiator_transceive_bits (pnd, pbtTx, szTxBits, NULL, abtRx, &szRxBits, NULL)) < 0) + if ((szRxBits = nfc_initiator_transceive_bits (pnd, pbtTx, szTxBits, NULL, abtRx, NULL)) < 0) return false; // Show received answer diff --git a/utils/nfc-mfsetuid.c b/utils/nfc-mfsetuid.c index 521dada..5ef33e4 100644 --- a/utils/nfc-mfsetuid.c +++ b/utils/nfc-mfsetuid.c @@ -57,7 +57,7 @@ #define MAX_FRAME_LEN 264 static uint8_t abtRx[MAX_FRAME_LEN]; -static size_t szRxBits; +static int szRxBits; static size_t szRx = sizeof(abtRx); static uint8_t abtRawUid[12]; static uint8_t abtAtqa[2]; @@ -96,7 +96,7 @@ transmit_bits (const uint8_t *pbtTx, const size_t szTxBits) print_hex_bits (pbtTx, szTxBits); } // Transmit the bit frame command, we don't use the arbitrary parity feature - if (nfc_initiator_transceive_bits (pnd, pbtTx, szTxBits, NULL, abtRx, &szRxBits, NULL) < 0) + if ((szRxBits = nfc_initiator_transceive_bits (pnd, pbtTx, szTxBits, NULL, abtRx, NULL)) < 0) return false; // Show received answer From b69974397366a57f6f7d420c9ef2dcd2d046e6cb Mon Sep 17 00:00:00 2001 From: Audrey Diacre Date: Wed, 4 Jan 2012 15:43:08 +0000 Subject: [PATCH 055/113] nfc_initiator_transceive_bits_timed() function does not now use pszRxBits as parameter because this function returns it. --- examples/nfc-anticol.c | 2 +- include/nfc/nfc.h | 2 +- libnfc/chips/pn53x.c | 12 ++++++------ libnfc/chips/pn53x.h | 3 +-- libnfc/nfc-internal.h | 2 +- libnfc/nfc.c | 4 ++-- 6 files changed, 12 insertions(+), 13 deletions(-) diff --git a/examples/nfc-anticol.c b/examples/nfc-anticol.c index 1080802..3ab2c78 100644 --- a/examples/nfc-anticol.c +++ b/examples/nfc-anticol.c @@ -52,7 +52,7 @@ #define MAX_FRAME_LEN 264 static uint8_t abtRx[MAX_FRAME_LEN]; -static size_t szRxBits; +static int szRxBits; static size_t szRx = sizeof(abtRx); static uint8_t abtRawUid[12]; static uint8_t abtAtqa[2]; diff --git a/include/nfc/nfc.h b/include/nfc/nfc.h index f7c2640..b2f387a 100644 --- a/include/nfc/nfc.h +++ b/include/nfc/nfc.h @@ -80,7 +80,7 @@ extern "C" { NFC_EXPORT int nfc_initiator_transceive_bytes (nfc_device *pnd, const uint8_t *pbtTx, const size_t szTx, uint8_t *pbtRx, size_t *pszRx, int timeout); 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, size_t *pszRx, 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, size_t *pszRxBits, uint8_t *pbtRxPar, 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 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, size_t *pszRx); diff --git a/libnfc/chips/pn53x.c b/libnfc/chips/pn53x.c index aaeb8be..99247ff 100644 --- a/libnfc/chips/pn53x.c +++ b/libnfc/chips/pn53x.c @@ -1318,7 +1318,7 @@ uint32_t __pn53x_get_timer(struct nfc_device *pnd, const uint8_t last_cmd_byte) int pn53x_initiator_transceive_bits_timed (struct nfc_device *pnd, const uint8_t *pbtTx, const size_t szTxBits, - const uint8_t *pbtTxPar, uint8_t *pbtRx, size_t *pszRxBits, uint8_t *pbtRxPar, uint32_t *cycles) + const uint8_t *pbtTxPar, uint8_t *pbtRx, uint8_t *pbtRxPar, uint32_t *cycles) { // TODO Do something with these bytes... (void) pbtTxPar; @@ -1326,6 +1326,7 @@ pn53x_initiator_transceive_bits_timed (struct nfc_device *pnd, const uint8_t *pb uint16_t i; uint8_t sz; int res = 0; + size_t szRxBits = 0; // Sorry, no arbitrary parity bits support for now if (!pnd->bPar) { @@ -1373,7 +1374,6 @@ pn53x_initiator_transceive_bits_timed (struct nfc_device *pnd, const uint8_t *pb } // Recv data - *pszRxBits = 0; // we've to watch for coming data until we decide to timeout. // our PN53x timer saturates after 4.8ms so this function shouldn't be used for // responses coming very late anyway. @@ -1404,19 +1404,19 @@ pn53x_initiator_transceive_bits_timed (struct nfc_device *pnd, const uint8_t *pb return res; } for (i = 0; i < sz; i++) { - pbtRx[i+*pszRxBits] = abtRes[i+off]; + pbtRx[i+szRxBits] = abtRes[i+off]; } - *pszRxBits += (size_t) (sz & SYMBOL_FIFO_LEVEL); + szRxBits += (size_t) (sz & SYMBOL_FIFO_LEVEL); sz = abtRes[sz+off]; if (sz == 0) break; } - *pszRxBits *= 8; // in bits, not bytes + szRxBits *= 8; // in bits, not bytes // Recv corrected timer value *cycles = __pn53x_get_timer (pnd, pbtTx[szTxBits / 8]); - return *pszRxBits; + return szRxBits; } int diff --git a/libnfc/chips/pn53x.h b/libnfc/chips/pn53x.h index d193612..057fe4a 100644 --- a/libnfc/chips/pn53x.h +++ b/libnfc/chips/pn53x.h @@ -302,8 +302,7 @@ int pn53x_initiator_transceive_bits (struct nfc_device *pnd, const uint8_t *p int pn53x_initiator_transceive_bytes (struct nfc_device *pnd, const uint8_t *pbtTx, const size_t szTx, uint8_t *pbtRx, size_t *pszRx, int timeout); int pn53x_initiator_transceive_bits_timed (struct nfc_device *pnd, const uint8_t *pbtTx, const size_t szTxBits, - const uint8_t *pbtTxPar, uint8_t *pbtRx, size_t *pszRxBits, - uint8_t *pbtRxPar, uint32_t *cycles); + const uint8_t *pbtTxPar, uint8_t *pbtRx, uint8_t *pbtRxPar, uint32_t *cycles); int pn53x_initiator_transceive_bytes_timed (struct nfc_device *pnd, const uint8_t *pbtTx, const size_t szTx, uint8_t *pbtRx, size_t *pszRx, uint32_t *cycles); int pn53x_initiator_deselect_target (struct nfc_device *pnd); diff --git a/libnfc/nfc-internal.h b/libnfc/nfc-internal.h index abebdd3..1477c2e 100644 --- a/libnfc/nfc-internal.h +++ b/libnfc/nfc-internal.h @@ -140,7 +140,7 @@ struct nfc_driver_t { int (*initiator_transceive_bytes) (struct nfc_device *pnd, const uint8_t * pbtTx, const size_t szTx, uint8_t * pbtRx, size_t * pszRx, int timeout); 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, size_t * pszRx, 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, size_t * pszRxBits, uint8_t * pbtRxPar, 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 (*target_init) (struct nfc_device *pnd, nfc_target * pnt, uint8_t * pbtRx, size_t * pszRx); 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 1f620f4..8e532ac 100644 --- a/libnfc/nfc.c +++ b/libnfc/nfc.c @@ -593,9 +593,9 @@ nfc_initiator_transceive_bytes_timed (nfc_device *pnd, const uint8_t *pbtTx, con */ int nfc_initiator_transceive_bits_timed (nfc_device *pnd, const uint8_t *pbtTx, const size_t szTxBits, const uint8_t *pbtTxPar, - uint8_t *pbtRx, size_t *pszRxBits, uint8_t *pbtRxPar, uint32_t *cycles) + uint8_t *pbtRx, uint8_t *pbtRxPar, uint32_t *cycles) { - HAL (initiator_transceive_bits_timed, pnd, pbtTx, szTxBits, pbtTxPar, pbtRx, pszRxBits, pbtRxPar, cycles); + HAL (initiator_transceive_bits_timed, pnd, pbtTx, szTxBits, pbtTxPar, pbtRx, pbtRxPar, cycles); } /** From a41b2b0da8b419a0690a17a5e082dd827b6d4320 Mon Sep 17 00:00:00 2001 From: Audrey Diacre Date: Wed, 4 Jan 2012 15:53:41 +0000 Subject: [PATCH 056/113] nfc_initiator_transceive_bytes_timed() function does not now use pszRx as parameter because this function returns it. --- include/nfc/nfc.h | 2 +- libnfc/chips/pn53x.c | 11 +++++------ libnfc/chips/pn53x.h | 2 +- libnfc/nfc-internal.h | 2 +- libnfc/nfc.c | 5 ++--- 5 files changed, 10 insertions(+), 12 deletions(-) diff --git a/include/nfc/nfc.h b/include/nfc/nfc.h index b2f387a..6b8c588 100644 --- a/include/nfc/nfc.h +++ b/include/nfc/nfc.h @@ -79,7 +79,7 @@ extern "C" { NFC_EXPORT int nfc_initiator_deselect_target (nfc_device *pnd); NFC_EXPORT int nfc_initiator_transceive_bytes (nfc_device *pnd, const uint8_t *pbtTx, const size_t szTx, uint8_t *pbtRx, size_t *pszRx, int timeout); 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, size_t *pszRx, uint32_t *cycles); + 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 target: act as tag (i.e. MIFARE Classic) or NFC target device. */ diff --git a/libnfc/chips/pn53x.c b/libnfc/chips/pn53x.c index 99247ff..232b6ca 100644 --- a/libnfc/chips/pn53x.c +++ b/libnfc/chips/pn53x.c @@ -1420,8 +1420,7 @@ pn53x_initiator_transceive_bits_timed (struct nfc_device *pnd, const uint8_t *pb } int -pn53x_initiator_transceive_bytes_timed (struct nfc_device *pnd, const uint8_t *pbtTx, const size_t szTx, uint8_t *pbtRx, - size_t *pszRx, uint32_t *cycles) +pn53x_initiator_transceive_bytes_timed (struct nfc_device *pnd, const uint8_t *pbtTx, const size_t szTx, uint8_t *pbtRx, uint32_t *cycles) { uint16_t i; uint8_t sz; @@ -1469,7 +1468,7 @@ pn53x_initiator_transceive_bytes_timed (struct nfc_device *pnd, const uint8_t *p } // Recv data - *pszRx = 0; + size_t szRx = 0; // we've to watch for coming data until we decide to timeout. // our PN53x timer saturates after 4.8ms so this function shouldn't be used for // responses coming very late anyway. @@ -1500,9 +1499,9 @@ pn53x_initiator_transceive_bytes_timed (struct nfc_device *pnd, const uint8_t *p return res; } for (i = 0; i < sz; i++) { - pbtRx[i+*pszRx] = abtRes[i+off]; + pbtRx[i+szRx] = abtRes[i+off]; } - *pszRx += (size_t) (sz & SYMBOL_FIFO_LEVEL); + szRx += (size_t) (sz & SYMBOL_FIFO_LEVEL); sz = abtRes[sz+off]; if (sz == 0) break; @@ -1520,7 +1519,7 @@ pn53x_initiator_transceive_bytes_timed (struct nfc_device *pnd, const uint8_t *p } else { *cycles = __pn53x_get_timer (pnd, pbtTx[szTx -1]); } - return *pszRx; + return szRx; } int diff --git a/libnfc/chips/pn53x.h b/libnfc/chips/pn53x.h index 057fe4a..73b1f06 100644 --- a/libnfc/chips/pn53x.h +++ b/libnfc/chips/pn53x.h @@ -304,7 +304,7 @@ int pn53x_initiator_transceive_bytes (struct nfc_device *pnd, const uint8_t int pn53x_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 pn53x_initiator_transceive_bytes_timed (struct nfc_device *pnd, const uint8_t *pbtTx, const size_t szTx, - uint8_t *pbtRx, size_t *pszRx, uint32_t *cycles); + uint8_t *pbtRx, uint32_t *cycles); int pn53x_initiator_deselect_target (struct nfc_device *pnd); // NFC device as Target functions diff --git a/libnfc/nfc-internal.h b/libnfc/nfc-internal.h index 1477c2e..2092144 100644 --- a/libnfc/nfc-internal.h +++ b/libnfc/nfc-internal.h @@ -139,7 +139,7 @@ struct nfc_driver_t { int (*initiator_deselect_target) (struct nfc_device *pnd); int (*initiator_transceive_bytes) (struct nfc_device *pnd, const uint8_t * pbtTx, const size_t szTx, uint8_t * pbtRx, size_t * pszRx, int timeout); 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, size_t * pszRx, uint32_t * cycles); + 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 (*target_init) (struct nfc_device *pnd, nfc_target * pnt, uint8_t * pbtRx, size_t * pszRx); diff --git a/libnfc/nfc.c b/libnfc/nfc.c index 8e532ac..dfe7586 100644 --- a/libnfc/nfc.c +++ b/libnfc/nfc.c @@ -564,10 +564,9 @@ nfc_initiator_transceive_bits (nfc_device *pnd, const uint8_t *pbtTx, const size * @warning The configuration option \a NP_HANDLE_PARITY must be set to \c true (the default value). */ int -nfc_initiator_transceive_bytes_timed (nfc_device *pnd, const uint8_t *pbtTx, const size_t szTx, uint8_t *pbtRx, - size_t *pszRx, uint32_t *cycles) +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, pszRx, cycles) + HAL (initiator_transceive_bytes_timed, pnd, pbtTx, szTx, pbtRx, cycles) } /** From c1faa48f54a334afb3d5b4e68fc82c537e10bb7d Mon Sep 17 00:00:00 2001 From: Audrey Diacre Date: Wed, 4 Jan 2012 15:59:34 +0000 Subject: [PATCH 057/113] fix nfc_target_init(). --- libnfc/nfc.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/libnfc/nfc.c b/libnfc/nfc.c index dfe7586..8836fc9 100644 --- a/libnfc/nfc.c +++ b/libnfc/nfc.c @@ -633,27 +633,27 @@ nfc_target_init (nfc_device *pnd, nfc_target *pnt, uint8_t *pbtRx, size_t * pszR int res = 0; // Disallow invalid frame if ((res = nfc_device_set_property_bool (pnd, NP_ACCEPT_INVALID_FRAMES, false)) < 0) - return false; + return res; // Disallow multiple frames if ((res = nfc_device_set_property_bool (pnd, NP_ACCEPT_MULTIPLE_FRAMES, false)) < 0) - return false; + return res; // Make sure we reset the CRC and parity to chip handling. if ((res = nfc_device_set_property_bool (pnd, NP_HANDLE_CRC, true)) < 0) - return false; + return res; if ((res = nfc_device_set_property_bool (pnd, NP_HANDLE_PARITY, true)) < 0) - return false; + return res; // Activate auto ISO14443-4 switching by default if ((res = nfc_device_set_property_bool (pnd, NP_AUTO_ISO14443_4, true)) < 0) - return false; + return res; // Activate "easy framing" feature by default if ((res = nfc_device_set_property_bool (pnd, NP_EASY_FRAMING, true)) < 0) - return false; + return res; // Deactivate the CRYPTO1 cipher, it may could cause problems when still active if ((res = nfc_device_set_property_bool (pnd, NP_ACTIVATE_CRYPTO1, false)) < 0) - return false; + return res; // Drop explicitely the field if ((res = nfc_device_set_property_bool (pnd, NP_ACTIVATE_FIELD, false)) < 0) - return false; + return res; HAL (target_init, pnd, pnt, pbtRx, pszRx); } From d4c5476652c69b863bdbe868fe69a3fc90006c83 Mon Sep 17 00:00:00 2001 From: Audrey Diacre Date: Wed, 4 Jan 2012 16:07:57 +0000 Subject: [PATCH 058/113] pn53x_get_firmware_version() returns now 0 on success and libnfc error code on failure.. --- libnfc/chips/pn53x.c | 17 +++++++++-------- libnfc/chips/pn53x.h | 6 +++--- 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/libnfc/chips/pn53x.c b/libnfc/chips/pn53x.c index 232b6ca..e8bfd59 100644 --- a/libnfc/chips/pn53x.c +++ b/libnfc/chips/pn53x.c @@ -64,8 +64,8 @@ pn53x_init(struct nfc_device *pnd) int res = 0; // GetFirmwareVersion command is used to set PN53x chips type (PN531, PN532 or PN533) char abtFirmwareText[22]; - if (!pn53x_get_firmware_version (pnd, abtFirmwareText)) { - return NFC_ECHIP; + if ((res = pn53x_get_firmware_version (pnd, abtFirmwareText)) < 0) { + return res; } // CRC handling should be enabled by default as declared in nfc_device_new @@ -600,14 +600,15 @@ pn53x_writeback_register (struct nfc_device *pnd) return true; } -bool +int pn53x_get_firmware_version (struct nfc_device *pnd, char abtFirmwareText[22]) { const uint8_t abtCmd[] = { GetFirmwareVersion }; uint8_t abtFw[4]; size_t szFwLen = sizeof (abtFw); - if (pn53x_transceive (pnd, abtCmd, sizeof (abtCmd), abtFw, &szFwLen, -1) < 0) { - return false; + int res = 0; + if ((res = pn53x_transceive (pnd, abtCmd, sizeof (abtCmd), abtFw, &szFwLen, -1)) < 0) { + return res; } // Determine which version of chip it is: PN531 will return only 2 bytes, while others return 4 bytes and have the first to tell the version IC if (szFwLen == 2) { @@ -623,11 +624,11 @@ pn53x_get_firmware_version (struct nfc_device *pnd, char abtFirmwareText[22]) } } else { // Unknown version IC - return false; + return NFC_ENOTIMPL; } } else { // Unknown chip - return false; + return NFC_ENOTIMPL; } // Convert firmware info in text, PN531 gives 2 bytes info, but PN532 and PN533 gives 4 switch (CHIP_DATA(pnd)->type) { @@ -648,7 +649,7 @@ pn53x_get_firmware_version (struct nfc_device *pnd, char abtFirmwareText[22]) // Could not happend break; } - return true; + return NFC_SUCCESS; } uint8_t diff --git a/libnfc/chips/pn53x.h b/libnfc/chips/pn53x.h index 73b1f06..b5c5e08 100644 --- a/libnfc/chips/pn53x.h +++ b/libnfc/chips/pn53x.h @@ -275,9 +275,9 @@ bool pn53x_decode_target_data (const uint8_t *pbtRawData, size_t szRawData, nfc_target_info *pnti); int pn53x_read_register (struct nfc_device *pnd, uint16_t ui16Reg, uint8_t *ui8Value); int pn53x_write_register (struct nfc_device *pnd, uint16_t ui16Reg, uint8_t ui8SymbolMask, uint8_t ui8Value); -bool pn53x_get_firmware_version (struct nfc_device *pnd, char abtFirmwareText[22]); -int pn53x_set_property_int (struct nfc_device *pnd, const nfc_property property, const int value); -int pn53x_set_property_bool (struct nfc_device *pnd, const nfc_property property, const bool bEnable); +int pn53x_get_firmware_version (struct nfc_device *pnd, char abtFirmwareText[22]); +int pn53x_set_property_int (struct nfc_device *pnd, const nfc_property property, const int value); +int pn53x_set_property_bool (struct nfc_device *pnd, const nfc_property property, const bool bEnable); bool pn53x_check_communication (struct nfc_device *pnd); bool pn53x_idle (struct nfc_device *pnd); From 240cdcddab062d91e482fd163f849e40b6da83fb Mon Sep 17 00:00:00 2001 From: Audrey Diacre Date: Wed, 4 Jan 2012 16:11:30 +0000 Subject: [PATCH 059/113] pn53x_decode_target_data() returns now 0 on success and libnfc error code on failure. --- libnfc/chips/pn53x.c | 8 ++++---- libnfc/chips/pn53x.h | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/libnfc/chips/pn53x.c b/libnfc/chips/pn53x.c index e8bfd59..c65022b 100644 --- a/libnfc/chips/pn53x.c +++ b/libnfc/chips/pn53x.c @@ -327,7 +327,7 @@ pn53x_unwrap_frame (const uint8_t *pbtFrame, const size_t szFrameBits, uint8_t * } } -bool +int pn53x_decode_target_data (const uint8_t *pbtRawData, size_t szRawData, pn53x_type type, nfc_modulation_type nmt, nfc_target_info *pnti) { @@ -402,7 +402,7 @@ pn53x_decode_target_data (const uint8_t *pbtRawData, size_t szRawData, pn53x_typ // Skip V & T Addresses pbtRawData++; if (*pbtRawData != 0x07) { // 0x07 = REPGEN - return false; + return NFC_ECHIP; } pbtRawData++; // Store the UID @@ -465,10 +465,10 @@ pn53x_decode_target_data (const uint8_t *pbtRawData, size_t szRawData, pn53x_typ memcpy (pnti->nji.btId, pbtRawData, 4); break; default: - return false; + return NFC_ECHIP; break; } - return true; + return NFC_SUCCESS; } int diff --git a/libnfc/chips/pn53x.h b/libnfc/chips/pn53x.h index b5c5e08..d53e732 100644 --- a/libnfc/chips/pn53x.h +++ b/libnfc/chips/pn53x.h @@ -270,7 +270,7 @@ int pn53x_set_parameters (struct nfc_device *pnd, const uint8_t ui8Value, con int pn53x_set_tx_bits (struct nfc_device *pnd, const uint8_t ui8Bits); int pn53x_wrap_frame (const uint8_t *pbtTx, const size_t szTxBits, const uint8_t *pbtTxPar, uint8_t *pbtFrame); int pn53x_unwrap_frame (const uint8_t *pbtFrame, const size_t szFrameBits, uint8_t *pbtRx, uint8_t *pbtRxPar); -bool pn53x_decode_target_data (const uint8_t *pbtRawData, size_t szRawData, +int pn53x_decode_target_data (const uint8_t *pbtRawData, size_t szRawData, pn53x_type chip_type, nfc_modulation_type nmt, nfc_target_info *pnti); int pn53x_read_register (struct nfc_device *pnd, uint16_t ui16Reg, uint8_t *ui8Value); From 7e1c776bc1fe9a82bffbe343459d340795643c92 Mon Sep 17 00:00:00 2001 From: Audrey Diacre Date: Wed, 4 Jan 2012 16:19:24 +0000 Subject: [PATCH 060/113] pn53x_check_communication() returns now 0 on success and libnfc error code on failure. --- libnfc/chips/pn53x.c | 12 ++++++++---- libnfc/chips/pn53x.h | 2 +- libnfc/drivers/arygon.c | 2 +- libnfc/drivers/pn532_uart.c | 8 ++++---- 4 files changed, 14 insertions(+), 10 deletions(-) diff --git a/libnfc/chips/pn53x.c b/libnfc/chips/pn53x.c index c65022b..cc19323 100644 --- a/libnfc/chips/pn53x.c +++ b/libnfc/chips/pn53x.c @@ -870,18 +870,22 @@ pn53x_idle (struct nfc_device *pnd) return true; } -bool +int pn53x_check_communication (struct nfc_device *pnd) { const uint8_t abtCmd[] = { Diagnose, 0x00, 'l', 'i', 'b', 'n', 'f', 'c' }; const uint8_t abtExpectedRx[] = { 0x00, 'l', 'i', 'b', 'n', 'f', 'c' }; uint8_t abtRx[sizeof(abtExpectedRx)]; size_t szRx = sizeof (abtRx); + int res = 0; - if (pn53x_transceive (pnd, abtCmd, sizeof (abtCmd), abtRx, &szRx, 1000) < 0) - return false; + if ((res = pn53x_transceive (pnd, abtCmd, sizeof (abtCmd), abtRx, &szRx, 1000)) < 0) + return res; - return ((sizeof(abtExpectedRx) == szRx) && (0 == memcmp (abtRx, abtExpectedRx, sizeof(abtExpectedRx)))); + if (((sizeof(abtExpectedRx) == szRx) && (0 == memcmp (abtRx, abtExpectedRx, sizeof(abtExpectedRx)))) == 0) + return NFC_ECHIP; + + return NFC_SUCCESS; } int diff --git a/libnfc/chips/pn53x.h b/libnfc/chips/pn53x.h index d53e732..ceafa75 100644 --- a/libnfc/chips/pn53x.h +++ b/libnfc/chips/pn53x.h @@ -279,7 +279,7 @@ int pn53x_get_firmware_version (struct nfc_device *pnd, char abtFirmwareText[ int pn53x_set_property_int (struct nfc_device *pnd, const nfc_property property, const int value); int pn53x_set_property_bool (struct nfc_device *pnd, const nfc_property property, const bool bEnable); -bool pn53x_check_communication (struct nfc_device *pnd); +int pn53x_check_communication (struct nfc_device *pnd); bool pn53x_idle (struct nfc_device *pnd); // NFC device as Initiator functions diff --git a/libnfc/drivers/arygon.c b/libnfc/drivers/arygon.c index 602f144..ec20056 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 : -1; + return (pn53x_check_communication (pnd) == 0) ? 0 : -1; } int diff --git a/libnfc/drivers/pn532_uart.c b/libnfc/drivers/pn532_uart.c index 5906eba..ce3147d 100644 --- a/libnfc/drivers/pn532_uart.c +++ b/libnfc/drivers/pn532_uart.c @@ -114,14 +114,14 @@ pn532_uart_probe (nfc_connstring connstrings[], size_t connstrings_len, size_t * #endif // Check communication using "Diagnose" command, with "Communication test" (0x00) - bool res = pn53x_check_communication (pnd); - if(!res) { + 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); - if(!res) { + if(res < 0) { continue; } @@ -251,7 +251,7 @@ pn532_uart_connect (const nfc_connstring connstring) #endif // Check communication using "Diagnose" command, with "Communication test" (0x00) - if (!pn53x_check_communication (pnd)) { + if (pn53x_check_communication (pnd) < 0) { nfc_perror (pnd, "pn53x_check_communication"); pn532_uart_disconnect (pnd); return NULL; From 4b373263e4641507fc4fdd22202466958149b49a Mon Sep 17 00:00:00 2001 From: Audrey Diacre Date: Wed, 4 Jan 2012 16:26:57 +0000 Subject: [PATCH 061/113] pn53x_idle() returns now 0 on success and libnfc error code on failure. --- libnfc/chips/pn53x.c | 29 +++++++++++++++-------------- libnfc/chips/pn53x.h | 2 +- libnfc/nfc-internal.h | 2 +- 3 files changed, 17 insertions(+), 16 deletions(-) diff --git a/libnfc/chips/pn53x.c b/libnfc/chips/pn53x.c index cc19323..eaccbeb 100644 --- a/libnfc/chips/pn53x.c +++ b/libnfc/chips/pn53x.c @@ -822,43 +822,44 @@ pn53x_set_property_bool (struct nfc_device *pnd, const nfc_property property, co return NFC_EINVARG; } -bool +int pn53x_idle (struct nfc_device *pnd) { + int res = 0; switch (CHIP_DATA (pnd)->operating_mode) { case TARGET: // InRelease used in target mode stops the target emulation and no more // tag are seen from external initiator - if (pn53x_InRelease (pnd, 0) < 0) { - return false; + if ((res = pn53x_InRelease (pnd, 0)) < 0) { + return res; } if (CHIP_DATA (pnd)->type == PN532) { // Use PowerDown to go in "Low VBat" power mode - if (pn53x_PowerDown (pnd) < 0) { - return false; + if ((res = pn53x_PowerDown (pnd)) < 0) { + return res; } CHIP_DATA (pnd)->power_mode = LOWVBAT; } break; case INITIATOR: // Deselect all active communications - if (pn53x_InDeselect (pnd, 0) < 0) { - return false; + if ((res = pn53x_InDeselect (pnd, 0)) < 0) { + return res; } // Disable RF field to avoid heating - if (nfc_device_set_property_bool (pnd, NP_ACTIVATE_FIELD, false) < 0) { - return false; + if ((res = nfc_device_set_property_bool (pnd, NP_ACTIVATE_FIELD, false)) < 0) { + return res; } if (CHIP_DATA (pnd)->type == PN532) { // Use PowerDown to go in "Low VBat" power mode - if (pn53x_PowerDown (pnd) < 0) { - return false; + if ((res = pn53x_PowerDown (pnd)) < 0) { + return res; } CHIP_DATA (pnd)->power_mode = LOWVBAT; } else { // Use InRelease to go in "Standby mode" - if (pn53x_InRelease (pnd, 0) < 0) { - return false; + if ((res = pn53x_InRelease (pnd, 0)) < 0) { + return res; } } break; @@ -867,7 +868,7 @@ pn53x_idle (struct nfc_device *pnd) break; }; CHIP_DATA (pnd)->operating_mode = IDLE; - return true; + return NFC_SUCCESS; } int diff --git a/libnfc/chips/pn53x.h b/libnfc/chips/pn53x.h index ceafa75..f8bac33 100644 --- a/libnfc/chips/pn53x.h +++ b/libnfc/chips/pn53x.h @@ -280,7 +280,7 @@ int pn53x_set_property_int (struct nfc_device *pnd, const nfc_property proper int pn53x_set_property_bool (struct nfc_device *pnd, const nfc_property property, const bool bEnable); int pn53x_check_communication (struct nfc_device *pnd); -bool pn53x_idle (struct nfc_device *pnd); +int pn53x_idle (struct nfc_device *pnd); // NFC device as Initiator functions int pn53x_initiator_init (struct nfc_device *pnd); diff --git a/libnfc/nfc-internal.h b/libnfc/nfc-internal.h index 2092144..f816ea6 100644 --- a/libnfc/nfc-internal.h +++ b/libnfc/nfc-internal.h @@ -152,7 +152,7 @@ struct nfc_driver_t { int (*device_set_property_int) (struct nfc_device *pnd, const nfc_property property, const int value); bool (*abort_command) (struct nfc_device *pnd); - bool (*idle) (struct nfc_device *pnd); + int (*idle) (struct nfc_device *pnd); }; # define DEVICE_NAME_LENGTH 256 From e87241184aa41c31b50d64bc47b34113f935f41b Mon Sep 17 00:00:00 2001 From: Audrey Diacre Date: Wed, 4 Jan 2012 16:32:16 +0000 Subject: [PATCH 062/113] pn53x_check_ack_frame() and pn53x_check_error_frame() functions return now 0 on success and libnfc error code on failure. --- libnfc/chips/pn53x.c | 12 ++++++------ libnfc/chips/pn53x.h | 4 ++-- libnfc/drivers/arygon.c | 2 +- libnfc/drivers/pn532_uart.c | 2 +- libnfc/drivers/pn53x_usb.c | 2 +- 5 files changed, 11 insertions(+), 11 deletions(-) diff --git a/libnfc/chips/pn53x.c b/libnfc/chips/pn53x.c index eaccbeb..b7678d4 100644 --- a/libnfc/chips/pn53x.c +++ b/libnfc/chips/pn53x.c @@ -2432,31 +2432,31 @@ pn53x_TgInitAsTarget (struct nfc_device *pnd, pn53x_target_mode ptm, return NFC_SUCCESS; } -bool +int pn53x_check_ack_frame (struct nfc_device *pnd, const uint8_t *pbtRxFrame, const size_t szRxFrameLen) { if (szRxFrameLen >= sizeof (pn53x_ack_frame)) { if (0 == memcmp (pbtRxFrame, pn53x_ack_frame, sizeof (pn53x_ack_frame))) { log_put (LOG_CATEGORY, NFC_PRIORITY_TRACE, "PN53x ACKed"); - return true; + return NFC_SUCCESS; } } pnd->last_error = NFC_EIO; log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "Unexpected PN53x reply!"); - return false; + return pnd->last_error; } -bool +int pn53x_check_error_frame (struct nfc_device *pnd, const uint8_t *pbtRxFrame, const size_t szRxFrameLen) { if (szRxFrameLen >= sizeof (pn53x_error_frame)) { if (0 == memcmp (pbtRxFrame, pn53x_error_frame, sizeof (pn53x_error_frame))) { log_put (LOG_CATEGORY, NFC_PRIORITY_TRACE, "PN53x sent an error frame"); pnd->last_error = NFC_EIO; - return false; + return pnd->last_error; } } - return true; + return NFC_SUCCESS; } /** diff --git a/libnfc/chips/pn53x.h b/libnfc/chips/pn53x.h index f8bac33..955dfc6 100644 --- a/libnfc/chips/pn53x.h +++ b/libnfc/chips/pn53x.h @@ -351,8 +351,8 @@ int pn53x_RFConfiguration__MaxRtyCOM (struct nfc_device *pnd, const uint8_t M int pn53x_RFConfiguration__MaxRetries (struct nfc_device *pnd, const uint8_t MxRtyATR, const uint8_t MxRtyPSL, const uint8_t MxRtyPassiveActivation); // Misc -bool pn53x_check_ack_frame (struct nfc_device *pnd, const uint8_t *pbtRxFrame, const size_t szRxFrameLen); -bool pn53x_check_error_frame (struct nfc_device *pnd, const uint8_t *pbtRxFrame, const size_t szRxFrameLen); +int pn53x_check_ack_frame (struct nfc_device *pnd, const uint8_t *pbtRxFrame, const size_t szRxFrameLen); +int pn53x_check_error_frame (struct nfc_device *pnd, const uint8_t *pbtRxFrame, const size_t szRxFrameLen); bool pn53x_build_frame (uint8_t *pbtFrame, size_t *pszFrame, const uint8_t *pbtData, const size_t szData); void pn53x_data_new (struct nfc_device *pnd, const struct pn53x_io *io); diff --git a/libnfc/drivers/arygon.c b/libnfc/drivers/arygon.c index ec20056..f678e69 100644 --- a/libnfc/drivers/arygon.c +++ b/libnfc/drivers/arygon.c @@ -339,7 +339,7 @@ arygon_tama_send (nfc_device *pnd, const uint8_t *pbtData, const size_t szData, return false; } - if (pn53x_check_ack_frame (pnd, abtRxBuf, sizeof(abtRxBuf))) { + if (pn53x_check_ack_frame (pnd, abtRxBuf, sizeof(abtRxBuf)) == 0) { // The PN53x is running the sent command } else if (0 == memcmp(arygon_error_unknown_mode, abtRxBuf, sizeof(abtRxBuf))) { log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "Bad frame format." ); diff --git a/libnfc/drivers/pn532_uart.c b/libnfc/drivers/pn532_uart.c index ce3147d..6c018cf 100644 --- a/libnfc/drivers/pn532_uart.c +++ b/libnfc/drivers/pn532_uart.c @@ -340,7 +340,7 @@ pn532_uart_send (nfc_device *pnd, const uint8_t *pbtData, const size_t szData, i return false; } - if (pn53x_check_ack_frame (pnd, abtRxBuf, sizeof(abtRxBuf))) { + if (pn53x_check_ack_frame (pnd, abtRxBuf, sizeof(abtRxBuf)) == 0) { // The PN53x is running the sent command } else { return false; diff --git a/libnfc/drivers/pn53x_usb.c b/libnfc/drivers/pn53x_usb.c index 1ec7524..f789034 100644 --- a/libnfc/drivers/pn53x_usb.c +++ b/libnfc/drivers/pn53x_usb.c @@ -529,7 +529,7 @@ pn53x_usb_send (nfc_device *pnd, const uint8_t *pbtData, const size_t szData, co return false; } - if (pn53x_check_ack_frame (pnd, abtRxBuf, res)) { + if (pn53x_check_ack_frame (pnd, abtRxBuf, res) == 0) { // The PN53x is running the sent command } else { // For some reasons (eg. send another command while a previous one is From 52c72383f149ec97bc0f125e77512f78e0c5e241 Mon Sep 17 00:00:00 2001 From: Audrey Diacre Date: Wed, 4 Jan 2012 16:44:40 +0000 Subject: [PATCH 063/113] pn53x_build_frame() function returns now 0 on success and libnfc error code on failure. --- libnfc/chips/pn53x.c | 6 +++--- libnfc/chips/pn53x.h | 2 +- libnfc/drivers/arygon.c | 2 +- libnfc/drivers/pn532_uart.c | 2 +- 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/libnfc/chips/pn53x.c b/libnfc/chips/pn53x.c index b7678d4..2f504e5 100644 --- a/libnfc/chips/pn53x.c +++ b/libnfc/chips/pn53x.c @@ -2465,7 +2465,7 @@ pn53x_check_error_frame (struct nfc_device *pnd, const uint8_t *pbtRxFrame, cons * @param pbtData payload (bytes array) of the frame, will become PD0, ..., PDn in PN53x frame * @note The first byte of pbtData is the Command Code (CC) */ -bool +int pn53x_build_frame (uint8_t *pbtFrame, size_t *pszFrame, const uint8_t *pbtData, const size_t szData) { if (szData <= PN53x_NORMAL_FRAME__DATA_MAX_LEN) { @@ -2517,9 +2517,9 @@ pn53x_build_frame (uint8_t *pbtFrame, size_t *pszFrame, const uint8_t *pbtData, (*pszFrame) = szData + PN53x_EXTENDED_FRAME__OVERHEAD; } else { log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "We can't send more than %d bytes in a raw (requested: %zd)", PN53x_EXTENDED_FRAME__DATA_MAX_LEN, szData); - return false; + return NFC_ECHIP; } - return true; + return NFC_SUCCESS; } pn53x_modulation pn53x_nm_to_pm(const nfc_modulation nm) diff --git a/libnfc/chips/pn53x.h b/libnfc/chips/pn53x.h index 955dfc6..3f18c11 100644 --- a/libnfc/chips/pn53x.h +++ b/libnfc/chips/pn53x.h @@ -353,7 +353,7 @@ int pn53x_RFConfiguration__MaxRetries (struct nfc_device *pnd, const uint8_t // Misc int pn53x_check_ack_frame (struct nfc_device *pnd, const uint8_t *pbtRxFrame, const size_t szRxFrameLen); int pn53x_check_error_frame (struct nfc_device *pnd, const uint8_t *pbtRxFrame, const size_t szRxFrameLen); -bool pn53x_build_frame (uint8_t *pbtFrame, size_t *pszFrame, const uint8_t *pbtData, const size_t szData); +int pn53x_build_frame (uint8_t *pbtFrame, size_t *pszFrame, const uint8_t *pbtData, const size_t szData); void pn53x_data_new (struct nfc_device *pnd, const struct pn53x_io *io); void pn53x_data_free (struct nfc_device *pnd); diff --git a/libnfc/drivers/arygon.c b/libnfc/drivers/arygon.c index f678e69..011bb57 100644 --- a/libnfc/drivers/arygon.c +++ b/libnfc/drivers/arygon.c @@ -319,7 +319,7 @@ arygon_tama_send (nfc_device *pnd, const uint8_t *pbtData, const size_t szData, return false; } - if (!pn53x_build_frame (abtFrame + 1, &szFrame, pbtData, szData)) { + if (pn53x_build_frame (abtFrame + 1, &szFrame, pbtData, szData) < 0) { pnd->last_error = NFC_EINVARG; return false; } diff --git a/libnfc/drivers/pn532_uart.c b/libnfc/drivers/pn532_uart.c index 6c018cf..4b1049e 100644 --- a/libnfc/drivers/pn532_uart.c +++ b/libnfc/drivers/pn532_uart.c @@ -320,7 +320,7 @@ pn532_uart_send (nfc_device *pnd, const uint8_t *pbtData, const size_t szData, i uint8_t abtFrame[PN532_BUFFER_LEN] = { 0x00, 0x00, 0xff }; // Every packet must start with "00 00 ff" size_t szFrame = 0; - if (!pn53x_build_frame (abtFrame, &szFrame, pbtData, szData)) { + if (pn53x_build_frame (abtFrame, &szFrame, pbtData, szData) < 0) { pnd->last_error = NFC_EINVARG; return false; } From 254053b940403ead5bf79274e45b205fa55dfd7d Mon Sep 17 00:00:00 2001 From: Romuald Conty Date: Wed, 4 Jan 2012 20:02:51 +0000 Subject: [PATCH 064/113] 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; } } From af22d34d573a842a3cbd5bc7d371a28a4aaaa9d7 Mon Sep 17 00:00:00 2001 From: Romuald Conty Date: Wed, 4 Jan 2012 20:50:05 +0000 Subject: [PATCH 065/113] fix some return codes and error checkings --- libnfc/chips/pn53x.c | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/libnfc/chips/pn53x.c b/libnfc/chips/pn53x.c index ba748da..47851ad 100644 --- a/libnfc/chips/pn53x.c +++ b/libnfc/chips/pn53x.c @@ -132,9 +132,11 @@ pn53x_transceive (struct nfc_device *pnd, const uint8_t *pbtTx, const size_t szT pszRx = &szRx; } + int res; // Call the send/receice callback functions of the current driver - if (!CHIP_DATA (pnd)->io->send (pnd, pbtTx, szTx, timeout)) - return NFC_ECHIP; + if ((res = CHIP_DATA (pnd)->io->send (pnd, pbtTx, szTx, timeout)) < 0) { + return pnd->last_error; + } // Command is sent, we store the command CHIP_DATA (pnd)->ui8LastCommand = pbtTx[0]; @@ -144,9 +146,8 @@ pn53x_transceive (struct nfc_device *pnd, const uint8_t *pbtTx, const size_t szT CHIP_DATA (pnd)->power_mode = POWERDOWN; } - int res = CHIP_DATA(pnd)->io->receive (pnd, pbtRx, *pszRx, timeout); - if (res < 0) { - return NFC_ECHIP; + if ((res = CHIP_DATA(pnd)->io->receive (pnd, pbtRx, *pszRx, timeout)) < 0) { + return pnd->last_error; } if ((CHIP_DATA(pnd)->type == PN532) && (TgInitAsTarget == pbtTx[0])) { // PN532 automatically wakeup on external RF field @@ -979,8 +980,8 @@ pn53x_initiator_select_passive_target_ext (struct nfc_device *pnd, if (pnt) { pnt->nm = nm; // Fill the tag info struct with the values corresponding to this init modulation - if (!pn53x_decode_target_data (abtTargetsData, szTargetsData, CHIP_DATA(pnd)->type, nm.nmt, &(pnt->nti))) { - return NFC_ECHIP; + if ((res = pn53x_decode_target_data (abtTargetsData, szTargetsData, CHIP_DATA(pnd)->type, nm.nmt, &(pnt->nti))) < 0 ) { + return res; } } if (nm.nmt == NMT_ISO14443BI) { @@ -1009,8 +1010,8 @@ pn53x_initiator_select_passive_target_ext (struct nfc_device *pnd, if (pnt) { pnt->nm = nm; // Fill the tag info struct with the values corresponding to this init modulation - if (!pn53x_decode_target_data (abtTargetsData + 1, szTargetsData - 1, CHIP_DATA(pnd)->type, nm.nmt, &(pnt->nti))) { - return NFC_ECHIP; + if ((res = pn53x_decode_target_data (abtTargetsData + 1, szTargetsData - 1, CHIP_DATA(pnd)->type, nm.nmt, &(pnt->nti)) < 0)) { + return res; } } return abtTargetsData[0]; @@ -1087,6 +1088,8 @@ pn53x_initiator_poll_target (struct nfc_device *pnd, } } } while (uiPollNr==0xff); // uiPollNr==0xff means infinite polling + // We reach this point when each listing give no result, we simply have to return 0 + return 0; } return NFC_ECHIP; } @@ -2243,7 +2246,9 @@ pn53x_InAutoPoll (struct nfc_device *pnd, pntTargets[0].nm = pn53x_ptt_to_nm(ptt); // AutoPollTargetData length ln = *(pbt++); - pn53x_decode_target_data (pbt, ln, CHIP_DATA(pnd)->type, pntTargets[0].nm.nmt, &(pntTargets[0].nti)); + if ((res = pn53x_decode_target_data (pbt, ln, CHIP_DATA(pnd)->type, pntTargets[0].nm.nmt, &(pntTargets[0].nti))) < 0) { + return res; + } pbt += ln; if (abtRx[0] > 1) { From 125553c72e0fd621c5054c66733f7c1ab270bed9 Mon Sep 17 00:00:00 2001 From: Romuald Conty Date: Wed, 4 Jan 2012 21:29:43 +0000 Subject: [PATCH 066/113] verbose option back for nfc-list, and newly implemented in nfc-poll --- examples/nfc-poll.1 | 16 +++++++++++++--- examples/nfc-poll.c | 19 +++++++++++++++---- utils/nfc-list.1 | 2 +- utils/nfc-list.c | 15 +++++++++++++++ 4 files changed, 44 insertions(+), 8 deletions(-) diff --git a/examples/nfc-poll.1 b/examples/nfc-poll.1 index 86bf93a..682c99c 100644 --- a/examples/nfc-poll.1 +++ b/examples/nfc-poll.1 @@ -1,6 +1,6 @@ .TH nfc-poll 1 "June 26, 2009" "libnfc" "libnfc's examples" .SH NAME -nfc-poll \- Poll for one NFC target +nfc-poll \- poll first available NFC target .SH SYNOPSIS .B nfc-poll .SH DESCRIPTION @@ -8,8 +8,18 @@ nfc-poll \- Poll for one NFC target is a utility for polling any available target (tags but also NFCIP targets) using ISO14443-A, FeliCa, Jewel and ISO14443-B modulations. -This tool relies on a hardware polling feature of the PN532, it will display -available information retrieved from the tag. +This tool uses hardware polling feature if available (ie. PN532) or switch back +to software polling, it will display available information retrieved from the +tag. + +.SH OPTIONS +.TP +.B \-v +Tells +.I +nfc-poll +to be verbose and display detailed information about the targets shown. +This includes SAK decoding and fingerprinting is available. .SH IMPORTANT There are some well-know limits with this example: diff --git a/examples/nfc-poll.c b/examples/nfc-poll.c index 820b26e..ded0705 100644 --- a/examples/nfc-poll.c +++ b/examples/nfc-poll.c @@ -62,6 +62,13 @@ void stop_polling (int sig) exit (EXIT_FAILURE); } +void +print_usage (char* progname) +{ + printf ("usage: %s [-v]\n", progname); + printf (" -v\t verbose display\n"); +} + int main (int argc, const char *argv[]) { @@ -72,11 +79,15 @@ main (int argc, const char *argv[]) // Display libnfc version const char *acLibnfcVersion = nfc_version (); - if (argc > 1) { - errx (1, "usage: %s", argv[0]); - } - printf ("%s uses libnfc %s\n", argv[0], acLibnfcVersion); + if (argc != 1) { + if ((argc == 2) && (0 == strcmp ("-v", argv[1]))) { + verbose = true; + } else { + print_usage (argv[0]); + exit (EXIT_FAILURE); + } + } const uint8_t uiPollNr = 20; const uint8_t uiPeriod = 2; diff --git a/utils/nfc-list.1 b/utils/nfc-list.1 index 22bfa2e..cbe74ae 100644 --- a/utils/nfc-list.1 +++ b/utils/nfc-list.1 @@ -18,7 +18,7 @@ This tool displays all available information at selection time. .SH OPTIONS .TP -.B \-v, \-\-verbose +.B \-v Tells .I nfc-list diff --git a/utils/nfc-list.c b/utils/nfc-list.c index a12a4a6..fbf8f94 100644 --- a/utils/nfc-list.c +++ b/utils/nfc-list.c @@ -59,6 +59,13 @@ static nfc_device *pnd; +void +print_usage (char* progname) +{ + printf ("usage: %s [-v]\n", progname); + printf (" -v\t verbose display\n"); +} + int main (int argc, const char *argv[]) { @@ -71,6 +78,14 @@ main (int argc, const char *argv[]) // Display libnfc version acLibnfcVersion = nfc_version (); printf ("%s uses libnfc %s\n", argv[0], acLibnfcVersion); + if (argc != 1) { + if ((argc == 2) && (0 == strcmp ("-v", argv[1]))) { + verbose = true; + } else { + print_usage (argv[0]); + exit (EXIT_FAILURE); + } + } #ifdef HAVE_LIBUSB # ifdef DEBUG From 93b34fa70b1fc5955d1bfd4f1753d36223b57a1b Mon Sep 17 00:00:00 2001 From: Romuald Conty Date: Thu, 5 Jan 2012 01:39:10 +0000 Subject: [PATCH 067/113] enhance new error handling - add accessor to last error occured - add new public error NFC_ETGRELEASED (Target Released) --- include/nfc/nfc.h | 16 ++++++++------- libnfc/chips/pn53x.c | 48 +++++++++++++++++++++++++++++++++++++++++--- libnfc/nfc.c | 11 +++++++++- 3 files changed, 64 insertions(+), 11 deletions(-) diff --git a/include/nfc/nfc.h b/include/nfc/nfc.h index 6b8c588..a39d4ae 100644 --- a/include/nfc/nfc.h +++ b/include/nfc/nfc.h @@ -93,6 +93,7 @@ extern "C" { NFC_EXPORT const char *nfc_strerror (const nfc_device *pnd); NFC_EXPORT int nfc_strerror_r (const nfc_device *pnd, char *buf, size_t buflen); NFC_EXPORT void nfc_perror (const nfc_device *pnd, const char *s); + NFC_EXPORT int nfc_device_get_last_error (const nfc_device *pnd); /* Special data accessors */ NFC_EXPORT const char *nfc_device_get_name (nfc_device *pnd); @@ -111,14 +112,15 @@ extern "C" { #define NFC_SUCCESS 0 // No error #define NFC_EIO -1 // Input / output error, device will not be usable anymore #define NFC_EINVARG -2 // Invalid argument(s) -#define NFC_ENOTSUCHDEV -3 // No such device -#define NFC_ETIMEOUT -4 // Operation timed out +#define NFC_EDEVNOTSUPP -3 // Operation not supported by device +#define NFC_ENOTSUCHDEV -4 // No such device #define NFC_EOVFLOW -5 // Buffer overflow -#define NFC_EOPABORTED -6 // Operation aborted (by user) -#define NFC_ECHIP -7 // Device's internal chip error -#define NFC_ERFTRANS -8 // Error while RF transmission -#define NFC_EDEVNOTSUPP -9 // Operation not supported by device -#define NFC_ENOTIMPL -10 // Not (yet) implemented +#define NFC_ETIMEOUT -6 // Operation timed out +#define NFC_EOPABORTED -7 // Operation aborted (by user) +#define NFC_ENOTIMPL -8 // Not (yet) implemented +#define NFC_ETGRELEASED -10 // Target released +#define NFC_ERFTRANS -20 // Error while RF transmission +#define NFC_ECHIP -90 // Device's internal chip error # ifdef __cplusplus } diff --git a/libnfc/chips/pn53x.c b/libnfc/chips/pn53x.c index 47851ad..1840710 100644 --- a/libnfc/chips/pn53x.c +++ b/libnfc/chips/pn53x.c @@ -198,10 +198,52 @@ pn53x_transceive (struct nfc_device *pnd, const uint8_t *pbtTx, const size_t szT } log_put (LOG_CATEGORY, NFC_PRIORITY_TRACE, "Last command status: %s", pn53x_strerror(pnd)); - if (CHIP_DATA(pnd)->last_status_byte) - return NFC_ECHIP; + switch (CHIP_DATA(pnd)->last_status_byte) { + case 0: + res = NFC_SUCCESS; + break; + case ETIMEOUT: + case ECRC: + case EPARITY: + case EBITCOUNT: + case EFRAMING: + case EBITCOLL: + case ERFPROTO: + case ERFTIMEOUT: + case EDEPUNKCMD: + case EDEPINVSTATE: + case ENAD: + case ENFCID3: + case EINVRXFRAM: + case EBCC: + case ECID: + res = NFC_ERFTRANS; + break; + case ESMALLBUF: + case EOVCURRENT: + case EBUFOVF: + case EOVHEAT: + case EINBUFOVF: + res = NFC_ECHIP; + break; + case EINVPARAM: + case EOPNOTALL: + case ECMD: + case ENSECNOTSUPP: + res = NFC_EINVARG; + break; + case ETGREL: + case ECDISCARDED: + res = NFC_ETGRELEASED; + default: + res = NFC_ECHIP; + break; + }; +/* + { EMFAUTH, "Mifare Authentication Error" }, +*/ - return ((0 == CHIP_DATA(pnd)->last_status_byte) ? NFC_SUCCESS : NFC_ECHIP); + return res; } int diff --git a/libnfc/nfc.c b/libnfc/nfc.c index 8836fc9..ce1f8f8 100644 --- a/libnfc/nfc.c +++ b/libnfc/nfc.c @@ -433,7 +433,7 @@ nfc_initiator_poll_target (nfc_device *pnd, /** * @brief Select a target and request active or passive mode for D.E.P. (Data Exchange Protocol) - * @return Returns selected D.E.P tagets count on success, otherwise returns libnfc's error code (negative value). + * @return Returns selected D.E.P targets count on success, otherwise returns libnfc's error code (negative value). * * @param pnd \a nfc_device struct pointer that represent currently used device * @param ndm desired D.E.P. mode (\a NDM_ACTIVE or \a NDM_PASSIVE for active, respectively passive mode) @@ -791,6 +791,15 @@ nfc_perror (const nfc_device *pnd, const char *pcString) fprintf (stderr, "%s: %s\n", pcString, nfc_strerror (pnd)); } +/** + * @brief Returns last error occured on a nfc_device + */ +int +nfc_device_get_last_error (const nfc_device *pnd) +{ + return pnd->last_error; +} + /* Special data accessors */ /** From 6ad07c3efb95385e90d5b8b2260656aa8dca8319 Mon Sep 17 00:00:00 2001 From: Audrey Diacre Date: Thu, 5 Jan 2012 08:42:22 +0000 Subject: [PATCH 068/113] pn53x_writeback_register() function returns now 0 on success and libnfc error code on failure. --- libnfc/chips/pn53x.c | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/libnfc/chips/pn53x.c b/libnfc/chips/pn53x.c index 1840710..7e61258 100644 --- a/libnfc/chips/pn53x.c +++ b/libnfc/chips/pn53x.c @@ -52,7 +52,7 @@ static const uint8_t pn53x_error_frame[] = { 0x00, 0x00, 0xff, 0x01, 0xff, 0x7f, /* prototypes */ int pn53x_reset_settings (struct nfc_device *pnd); -bool pn53x_writeback_register (struct nfc_device *pnd); +int pn53x_writeback_register (struct nfc_device *pnd); nfc_modulation pn53x_ptt_to_nm (const pn53x_target_type ptt); pn53x_modulation pn53x_nm_to_pm (const nfc_modulation nm); @@ -106,9 +106,10 @@ pn53x_reset_settings(struct nfc_device *pnd) int pn53x_transceive (struct nfc_device *pnd, const uint8_t *pbtTx, const size_t szTx, uint8_t *pbtRx, size_t *pszRx, int timeout) { + int res = 0; if (CHIP_DATA (pnd)->wb_trigged) { - if (!pn53x_writeback_register (pnd)) { - return NFC_ECHIP; + if ((res = pn53x_writeback_register (pnd)) < 0) { + return res; } } @@ -132,7 +133,6 @@ pn53x_transceive (struct nfc_device *pnd, const uint8_t *pbtTx, const size_t szT pszRx = &szRx; } - int res; // Call the send/receice callback functions of the current driver if ((res = CHIP_DATA (pnd)->io->send (pnd, pbtTx, szTx, timeout)) < 0) { return pnd->last_error; @@ -581,9 +581,10 @@ pn53x_write_register (struct nfc_device *pnd, const uint16_t ui16RegisterAddress return NFC_SUCCESS; } -bool +int pn53x_writeback_register (struct nfc_device *pnd) { + int res = 0; // TODO Check at each step (ReadRegister, WriteRegister) if we didn't exceed max supported frame length BUFFER_INIT (abtReadRegisterCmd, PN53x_EXTENDED_FRAME__DATA_MAX_LEN); BUFFER_APPEND (abtReadRegisterCmd, ReadRegister); @@ -604,8 +605,8 @@ pn53x_writeback_register (struct nfc_device *pnd) uint8_t abtRes[PN53x_EXTENDED_FRAME__DATA_MAX_LEN]; size_t szRes = sizeof(abtRes); // It transceives the previously constructed ReadRegister command - if (pn53x_transceive (pnd, abtReadRegisterCmd, BUFFER_SIZE (abtReadRegisterCmd), abtRes, &szRes, -1) < 0) { - return false; + if ((res = pn53x_transceive (pnd, abtReadRegisterCmd, BUFFER_SIZE (abtReadRegisterCmd), abtRes, &szRes, -1)) < 0) { + return res; } size_t i = 0; if (CHIP_DATA(pnd)->type == PN533) { @@ -642,11 +643,11 @@ pn53x_writeback_register (struct nfc_device *pnd) if (BUFFER_SIZE (abtWriteRegisterCmd) > 1) { // We need to write some registers - if (pn53x_transceive (pnd, abtWriteRegisterCmd, BUFFER_SIZE (abtWriteRegisterCmd), NULL, NULL, -1) < 0) { - return false; + if ((res = pn53x_transceive (pnd, abtWriteRegisterCmd, BUFFER_SIZE (abtWriteRegisterCmd), NULL, NULL, -1)) < 0) { + return res; } } - return true; + return NFC_SUCCESS; } int From 331234713df6e3da0ab5d9a25732e34842ff1270 Mon Sep 17 00:00:00 2001 From: Audrey Diacre Date: Thu, 5 Jan 2012 08:57:16 +0000 Subject: [PATCH 069/113] nfc_idle() function returns now 0 on success and libnfc error code on failure. --- include/nfc/nfc.h | 2 +- libnfc/nfc.c | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/include/nfc/nfc.h b/include/nfc/nfc.h index a39d4ae..3b1bb4b 100644 --- a/include/nfc/nfc.h +++ b/include/nfc/nfc.h @@ -68,7 +68,7 @@ extern "C" { NFC_EXPORT void nfc_disconnect (nfc_device *pnd); NFC_EXPORT bool nfc_abort_command (nfc_device *pnd); NFC_EXPORT void nfc_list_devices (nfc_connstring connstrings[], size_t connstrings_len, size_t *pszDeviceFound); - NFC_EXPORT bool nfc_idle (nfc_device *pnd); + NFC_EXPORT int nfc_idle (nfc_device *pnd); /* NFC initiator: act as "reader" */ NFC_EXPORT int nfc_initiator_init (nfc_device *pnd); diff --git a/libnfc/nfc.c b/libnfc/nfc.c index ce1f8f8..8c61ea0 100644 --- a/libnfc/nfc.c +++ b/libnfc/nfc.c @@ -660,7 +660,7 @@ nfc_target_init (nfc_device *pnd, nfc_target *pnt, uint8_t *pbtRx, size_t * pszR /** * @brief Turn NFC device in idle mode - * @return Returns \c true if action was successfully performed; otherwise returns \c false. + * @return Returns 0 on success, otherwise returns libnfc's error code. * * @param pnd \a nfc_device struct pointer that represent currently used device * @@ -668,7 +668,7 @@ nfc_target_init (nfc_device *pnd, nfc_target *pnt, uint8_t *pbtRx, size_t * pszR * In initiator mode, the RF field is turned off and the device is set to low power mode (if avaible); * In target mode, the emulation is stoped (no target available from external initiator) and the device is set to low power mode (if avaible). */ -bool +int nfc_idle (nfc_device *pnd) { HAL (idle, pnd); From 5a1f0c2115a3612841fb649ccf7295786a0acba6 Mon Sep 17 00:00:00 2001 From: Audrey Diacre Date: Thu, 5 Jan 2012 10:33:50 +0000 Subject: [PATCH 070/113] check result of nfc_initiator_init() function in examples/ and utils/ --- examples/doc/quick_start_example1.c | 5 ++++- examples/nfc-anticol.c | 5 ++++- examples/nfc-poll.c | 5 ++++- examples/nfc-relay.c | 6 +++++- examples/pn53x-sam.c | 5 ++++- examples/pn53x-tamashell.c | 5 ++++- utils/nfc-list.c | 5 ++++- utils/nfc-mfclassic.c | 5 ++++- utils/nfc-mfsetuid.c | 5 ++++- utils/nfc-mfultralight.c | 5 ++++- utils/nfc-read-forum-tag3.c | 5 ++++- 11 files changed, 45 insertions(+), 11 deletions(-) diff --git a/examples/doc/quick_start_example1.c b/examples/doc/quick_start_example1.c index 448a8aa..0f88896 100644 --- a/examples/doc/quick_start_example1.c +++ b/examples/doc/quick_start_example1.c @@ -26,7 +26,10 @@ main (int argc, const char *argv[]) return EXIT_FAILURE; } // Set connected NFC device to initiator mode - nfc_initiator_init (pnd); + if (nfc_initiator_init (pnd) < 0) { + nfc_perror (pnd, "nfc_initiator_init"); + exit (EXIT_FAILURE); + } printf ("Connected to NFC reader: %s\n", nfc_device_get_name (pnd)); diff --git a/examples/nfc-anticol.c b/examples/nfc-anticol.c index 3ab2c78..30e8aa3 100644 --- a/examples/nfc-anticol.c +++ b/examples/nfc-anticol.c @@ -157,7 +157,10 @@ main (int argc, char *argv[]) } // Initialise NFC device as "initiator" - nfc_initiator_init (pnd); + if (nfc_initiator_init (pnd) < 0) { + nfc_perror (pnd, "nfc_initiator_init"); + exit (EXIT_FAILURE); + } // Configure the CRC if (nfc_device_set_property_bool (pnd, NP_HANDLE_CRC, false) < 0) { diff --git a/examples/nfc-poll.c b/examples/nfc-poll.c index ded0705..c9bd62b 100644 --- a/examples/nfc-poll.c +++ b/examples/nfc-poll.c @@ -110,7 +110,10 @@ main (int argc, const char *argv[]) exit (EXIT_FAILURE); } - nfc_initiator_init (pnd); + if (nfc_initiator_init (pnd) < 0) { + nfc_perror (pnd, "nfc_initiator_init"); + exit (EXIT_FAILURE); + } printf ("Connected to NFC reader: %s\n", nfc_device_get_name (pnd)); printf ("NFC device will poll during %ld ms (%u pollings of %lu ms for %zd modulations)\n", (unsigned long) uiPollNr * szModulations * uiPeriod * 150, uiPollNr, (unsigned long) uiPeriod * 150, szModulations); diff --git a/examples/nfc-relay.c b/examples/nfc-relay.c index 4402fa2..677e8d9 100644 --- a/examples/nfc-relay.c +++ b/examples/nfc-relay.c @@ -164,7 +164,11 @@ main (int argc, char *argv[]) printf ("Connected to the NFC reader device: %s", nfc_device_get_name (pndReader)); printf ("%s", "Configuring NFC reader settings..."); - nfc_initiator_init (pndReader); + + if (nfc_initiator_init (pndReader) < 0) { + nfc_perror (pndReader, "nfc_initiator_init"); + exit (EXIT_FAILURE); + } if ((nfc_device_set_property_bool (pndReader, NP_HANDLE_CRC, false) < 0) || (nfc_device_set_property_bool (pndReader, NP_HANDLE_PARITY, false) < 0) || (nfc_device_set_property_bool (pndReader, NP_ACCEPT_INVALID_FRAMES, true)) < 0) { diff --git a/examples/pn53x-sam.c b/examples/pn53x-sam.c index 09b67d7..713cb58 100644 --- a/examples/pn53x-sam.c +++ b/examples/pn53x-sam.c @@ -128,7 +128,10 @@ main (int argc, const char *argv[]) nfc_target nt; // Set connected NFC device to initiator mode - nfc_initiator_init (pnd); + if (nfc_initiator_init (pnd) < 0) { + nfc_perror (pnd, "nfc_initiator_init"); + exit (EXIT_FAILURE); + } // Let the reader only try once to find a tag if (nfc_device_set_property_bool (pnd, NP_INFINITE_SELECT, false) < 0) { diff --git a/examples/pn53x-tamashell.c b/examples/pn53x-tamashell.c index 87cff6f..e06e9a0 100644 --- a/examples/pn53x-tamashell.c +++ b/examples/pn53x-tamashell.c @@ -95,7 +95,10 @@ int main(int argc, const char* argv[]) } printf ("Connected to NFC reader: %s\n", nfc_device_get_name (pnd)); - nfc_initiator_init(pnd); + if (nfc_initiator_init (pnd) < 0) { + nfc_perror (pnd, "nfc_initiator_init"); + exit (EXIT_FAILURE); + } char *cmd; char *prompt = "> "; diff --git a/utils/nfc-list.c b/utils/nfc-list.c index fbf8f94..9fee2bb 100644 --- a/utils/nfc-list.c +++ b/utils/nfc-list.c @@ -130,7 +130,10 @@ main (int argc, const char *argv[]) ERR ("%s", "Unable to connect to NFC device."); return EXIT_FAILURE; } - nfc_initiator_init (pnd); + if (nfc_initiator_init (pnd) < 0) { + nfc_perror (pnd, "nfc_initiator_init"); + exit (EXIT_FAILURE); + } printf ("Connected to NFC device: %s\n", nfc_device_get_name (pnd)); diff --git a/utils/nfc-mfclassic.c b/utils/nfc-mfclassic.c index c784a7c..e89a19b 100644 --- a/utils/nfc-mfclassic.c +++ b/utils/nfc-mfclassic.c @@ -544,7 +544,10 @@ main (int argc, const char *argv[]) exit (EXIT_FAILURE); } - nfc_initiator_init (pnd); + if (nfc_initiator_init (pnd) < 0) { + nfc_perror (pnd, "nfc_initiator_init"); + exit (EXIT_FAILURE); + }; // Let the reader only try once to find a tag if (nfc_device_set_property_bool (pnd, NP_INFINITE_SELECT, false) < 0) { diff --git a/utils/nfc-mfsetuid.c b/utils/nfc-mfsetuid.c index 5ef33e4..7927536 100644 --- a/utils/nfc-mfsetuid.c +++ b/utils/nfc-mfsetuid.c @@ -186,7 +186,10 @@ main (int argc, char *argv[]) } // Initialise NFC device as "initiator" - nfc_initiator_init (pnd); + if (nfc_initiator_init (pnd) < 0) { + nfc_perror (pnd, "nfc_initiator_init"); + exit (EXIT_FAILURE); + } // Configure the CRC if (nfc_device_set_property_bool (pnd, NP_HANDLE_CRC, false) < 0) { diff --git a/utils/nfc-mfultralight.c b/utils/nfc-mfultralight.c index 3027a18..8bc32a7 100644 --- a/utils/nfc-mfultralight.c +++ b/utils/nfc-mfultralight.c @@ -211,7 +211,10 @@ main (int argc, const char *argv[]) return 1; } - nfc_initiator_init (pnd); + if (nfc_initiator_init (pnd) < 0) { + nfc_perror (pnd, "nfc_initiator_init"); + exit (EXIT_FAILURE); + } // Let the device only try once to find a tag if (nfc_device_set_property_bool (pnd, NP_INFINITE_SELECT, false) < 0) { diff --git a/utils/nfc-read-forum-tag3.c b/utils/nfc-read-forum-tag3.c index 5b3db96..69af3d5 100644 --- a/utils/nfc-read-forum-tag3.c +++ b/utils/nfc-read-forum-tag3.c @@ -213,7 +213,10 @@ main(int argc, char *argv[]) nfc_target nt; - nfc_initiator_init(pnd); + if (nfc_initiator_init (pnd) < 0) { + nfc_perror (pnd, "nfc_initiator_init"); + exit (EXIT_FAILURE); + } fprintf (message_stream, "Place your NFC Forum Tag Type 3 in the field...\n"); int error = EXIT_SUCCESS; From 81d76c8c7036685e6364d7466d43affda382510a Mon Sep 17 00:00:00 2001 From: Audrey Diacre Date: Thu, 5 Jan 2012 10:56:36 +0000 Subject: [PATCH 071/113] check result of nfc_disconnect() function in examples/nfc-dep-initiator.c --- examples/nfc-dep-initiator.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/examples/nfc-dep-initiator.c b/examples/nfc-dep-initiator.c index 2373f59..92cf7ac 100644 --- a/examples/nfc-dep-initiator.c +++ b/examples/nfc-dep-initiator.c @@ -83,7 +83,7 @@ main (int argc, const char *argv[]) if (nfc_initiator_init (pnd) < 0) { nfc_perror(pnd, "nfc_initiator_init"); - return EXIT_FAILURE; + goto error; } if(nfc_initiator_select_dep_target (pnd, NDM_PASSIVE, NBR_212, NULL, &nt, 1000) < 0) { @@ -101,7 +101,10 @@ main (int argc, const char *argv[]) abtRx[szRx] = 0; printf ("Received: %s\n", abtRx); - nfc_initiator_deselect_target (pnd); + if (nfc_initiator_deselect_target (pnd) < 0) { + nfc_perror(pnd, "nfc_initiator_deselect_target"); + goto error; + } error: nfc_disconnect (pnd); From c30e9eed3699ee31fc9296501851f64c715e189d Mon Sep 17 00:00:00 2001 From: Audrey Diacre Date: Thu, 5 Jan 2012 13:24:41 +0000 Subject: [PATCH 072/113] send/receive callbacks from internal chip io return now libnfc error code on failure. --- libnfc/buses/uart_posix.c | 4 +-- libnfc/chips/pn53x.c | 4 +-- libnfc/chips/pn53x.h | 2 +- libnfc/drivers/acr122.c | 18 +++++++------- libnfc/drivers/acr122.h | 2 +- libnfc/drivers/arygon.c | 34 ++++++++++++------------- libnfc/drivers/arygon.h | 4 +-- libnfc/drivers/pn532_uart.c | 49 +++++++++++++++++++------------------ libnfc/drivers/pn532_uart.h | 4 +-- libnfc/drivers/pn53x_usb.c | 34 ++++++++++++------------- libnfc/drivers/pn53x_usb.h | 4 +-- 11 files changed, 80 insertions(+), 79 deletions(-) diff --git a/libnfc/buses/uart_posix.c b/libnfc/buses/uart_posix.c index 5b5db3b..0fdd2d6 100644 --- a/libnfc/buses/uart_posix.c +++ b/libnfc/buses/uart_posix.c @@ -314,7 +314,7 @@ select: } while (expected_bytes_count > received_bytes_count); LOG_HEX ("RX", pbtRx, szRx); - return 0; + return NFC_SUCCESS; } /** @@ -328,7 +328,7 @@ uart_send (serial_port sp, const uint8_t *pbtTx, const size_t szTx, int timeout) (void) timeout; LOG_HEX ("TX", pbtTx, szTx); if ((int) szTx == write (UART_DATA(sp)->fd, pbtTx, szTx)) - return 0; + return NFC_SUCCESS; else return NFC_EIO; } diff --git a/libnfc/chips/pn53x.c b/libnfc/chips/pn53x.c index 7e61258..99b2e6f 100644 --- a/libnfc/chips/pn53x.c +++ b/libnfc/chips/pn53x.c @@ -135,7 +135,7 @@ pn53x_transceive (struct nfc_device *pnd, const uint8_t *pbtTx, const size_t szT // Call the send/receice callback functions of the current driver if ((res = CHIP_DATA (pnd)->io->send (pnd, pbtTx, szTx, timeout)) < 0) { - return pnd->last_error; + return res; } // Command is sent, we store the command @@ -147,7 +147,7 @@ pn53x_transceive (struct nfc_device *pnd, const uint8_t *pbtTx, const size_t szT } if ((res = CHIP_DATA(pnd)->io->receive (pnd, pbtRx, *pszRx, timeout)) < 0) { - return pnd->last_error; + return res; } if ((CHIP_DATA(pnd)->type == PN532) && (TgInitAsTarget == pbtTx[0])) { // PN532 automatically wakeup on external RF field diff --git a/libnfc/chips/pn53x.h b/libnfc/chips/pn53x.h index 3f18c11..e1b07a5 100644 --- a/libnfc/chips/pn53x.h +++ b/libnfc/chips/pn53x.h @@ -127,7 +127,7 @@ typedef enum { } pn53x_operating_mode; struct pn53x_io { - bool (*send)(struct nfc_device *pnd, const uint8_t *pbtData, const size_t szData, int timeout); + int (*send)(struct nfc_device *pnd, const uint8_t *pbtData, const size_t szData, int timeout); int (*receive)(struct nfc_device *pnd, uint8_t *pbtData, const size_t szDataLen, int timeout); }; diff --git a/libnfc/drivers/acr122.c b/libnfc/drivers/acr122.c index 615f69e..db9ecc4 100644 --- a/libnfc/drivers/acr122.c +++ b/libnfc/drivers/acr122.c @@ -309,7 +309,7 @@ acr122_disconnect (nfc_device *pnd) nfc_device_free (pnd); } -bool +int acr122_send (nfc_device *pnd, const uint8_t *pbtData, const size_t szData, int timeout) { // FIXME: timeout is not handled @@ -318,7 +318,7 @@ acr122_send (nfc_device *pnd, const uint8_t *pbtData, const size_t szData, int t // Make sure the command does not overflow the send buffer if (szData > ACR122_COMMAND_LEN) { pnd->last_error = NFC_EINVARG; - return false; + return pnd->last_error; } // Prepare and transmit the send buffer @@ -345,7 +345,7 @@ acr122_send (nfc_device *pnd, const uint8_t *pbtData, const size_t szData, int t */ if (SCardControl (DRIVER_DATA (pnd)->hCard, IOCTL_CCID_ESCAPE_SCARD_CTL_CODE, abtTxBuf, szTxBuf, DRIVER_DATA (pnd)->abtRx, ACR122_RESPONSE_LEN, &dwRxLen) != SCARD_S_SUCCESS) { pnd->last_error = NFC_EIO; - return false; + return pnd->last_error; } } else { /* @@ -354,7 +354,7 @@ acr122_send (nfc_device *pnd, const uint8_t *pbtData, const size_t szData, int t */ if (SCardTransmit (DRIVER_DATA (pnd)->hCard, &(DRIVER_DATA (pnd)->ioCard), abtTxBuf, szTxBuf, NULL, DRIVER_DATA (pnd)->abtRx, &dwRxLen) != SCARD_S_SUCCESS) { pnd->last_error = NFC_EIO; - return false; + return pnd->last_error; } } @@ -366,18 +366,18 @@ acr122_send (nfc_device *pnd, const uint8_t *pbtData, const size_t szData, int t // Make sure we received the byte-count we expected if (dwRxLen != 2) { pnd->last_error = NFC_EIO; - return false; + return pnd->last_error; } // Check if the operation was successful, so an answer is available if (DRIVER_DATA (pnd)->abtRx[0] == SCARD_OPERATION_ERROR) { pnd->last_error = NFC_EIO; - return false; + return pnd->last_error; } } else { DRIVER_DATA (pnd)->szRx = dwRxLen; } - return true; + return NFC_SUCCESS; } int @@ -397,7 +397,7 @@ acr122_receive (nfc_device *pnd, uint8_t *pbtData, const size_t szData, int time abtRxCmd[4] = DRIVER_DATA (pnd)->abtRx[1]; if (SCardTransmit (DRIVER_DATA (pnd)->hCard, &(DRIVER_DATA (pnd)->ioCard), abtRxCmd, sizeof (abtRxCmd), NULL, DRIVER_DATA (pnd)->abtRx, &dwRxLen) != SCARD_S_SUCCESS) { pnd->last_error = NFC_EIO; - return -1; + return pnd->last_error; } DRIVER_DATA (pnd)->szRx = dwRxLen; } else { @@ -410,7 +410,7 @@ acr122_receive (nfc_device *pnd, uint8_t *pbtData, const size_t szData, int time // Make sure we have an emulated answer that fits the return buffer if (DRIVER_DATA (pnd)->szRx < 4 || (DRIVER_DATA (pnd)->szRx - 4) > szData) { pnd->last_error = NFC_EIO; - return -1; + return pnd->last_error; } // Wipe out the 4 APDU emulation bytes: D5 4B .. .. .. 90 00 len = DRIVER_DATA (pnd)->szRx - 4; diff --git a/libnfc/drivers/acr122.h b/libnfc/drivers/acr122.h index b364258..9ea315e 100644 --- a/libnfc/drivers/acr122.h +++ b/libnfc/drivers/acr122.h @@ -30,7 +30,7 @@ bool acr122_probe (nfc_connstring connstrings[], size_t connstrings_len, size // Functions used by developer to handle connection to this device nfc_device *acr122_connect (const nfc_connstring connstring); -bool acr122_send (nfc_device *pnd, const uint8_t *pbtData, const size_t szData, int timeout); +int acr122_send (nfc_device *pnd, const uint8_t *pbtData, const size_t szData, int timeout); int acr122_receive (nfc_device *pnd, uint8_t *pbtData, const size_t szData, int timeout); void acr122_disconnect (nfc_device *pnd); diff --git a/libnfc/drivers/arygon.c b/libnfc/drivers/arygon.c index 13f2bff..efe418e 100644 --- a/libnfc/drivers/arygon.c +++ b/libnfc/drivers/arygon.c @@ -303,7 +303,7 @@ arygon_disconnect (nfc_device *pnd) #define ARYGON_TX_BUFFER_LEN (PN53x_NORMAL_FRAME__DATA_MAX_LEN + PN53x_NORMAL_FRAME__OVERHEAD + 1) #define ARYGON_RX_BUFFER_LEN (PN53x_EXTENDED_FRAME__DATA_MAX_LEN + PN53x_EXTENDED_FRAME__OVERHEAD) -bool +int arygon_tama_send (nfc_device *pnd, const uint8_t *pbtData, const size_t szData, int timeout) { // Before sending anything, we need to discard from any junk bytes @@ -316,19 +316,19 @@ arygon_tama_send (nfc_device *pnd, const uint8_t *pbtData, const size_t szData, // ARYGON Reader with PN532 equipped does not support extended frame (bug in ARYGON firmware?) log_put (LOG_CATEGORY, NFC_PRIORITY_TRACE, "ARYGON device does not support more than %d bytes as payload (requested: %zd)", PN53x_NORMAL_FRAME__DATA_MAX_LEN, szData); pnd->last_error = NFC_EDEVNOTSUPP; - return false; + return pnd->last_error; } if (pn53x_build_frame (abtFrame + 1, &szFrame, pbtData, szData) < 0) { pnd->last_error = NFC_EINVARG; - return false; + return pnd->last_error; } int res = uart_send (DRIVER_DATA (pnd)->port, abtFrame, szFrame + 1, timeout); if (res != 0) { log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Unable to transmit data. (TX)"); pnd->last_error = NFC_EIO; - return false; + return pnd->last_error; } uint8_t abtRxBuf[6]; @@ -336,7 +336,7 @@ arygon_tama_send (nfc_device *pnd, const uint8_t *pbtData, const size_t szData, if (res != 0) { log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Unable to read ACK"); pnd->last_error = NFC_EIO; - return false; + return pnd->last_error; } if (pn53x_check_ack_frame (pnd, abtRxBuf, sizeof(abtRxBuf)) == 0) { @@ -345,12 +345,12 @@ arygon_tama_send (nfc_device *pnd, const uint8_t *pbtData, const size_t szData, log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "Bad frame format." ); // We have already read 6 bytes and arygon_error_unknown_mode is 10 bytes long // so we have to read 4 remaining bytes to be synchronized at the next receiving pass. - uart_receive (DRIVER_DATA (pnd)->port, abtRxBuf, 4, 0, timeout); - return false; + pnd->last_error = uart_receive (DRIVER_DATA (pnd)->port, abtRxBuf, 4, 0, timeout); + return pnd->last_error; } else { - return false; + return pnd->last_error; } - return true; + return NFC_SUCCESS; } int @@ -426,40 +426,40 @@ arygon_tama_receive (nfc_device *pnd, uint8_t *pbtData, const size_t szDataLen, if (len > szDataLen) { log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "Unable to receive data: buffer too small. (szDataLen: %zu, len: %zu)", szDataLen, len); pnd->last_error = NFC_EIO; - return -1; + return pnd->last_error; } // TFI + PD0 (CC+1) pnd->last_error = uart_receive (DRIVER_DATA (pnd)->port, abtRxBuf, 2, 0, timeout); if (pnd->last_error != 0) { log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Unable to receive data. (RX)"); - return -1; + return pnd->last_error; } if (abtRxBuf[0] != 0xD5) { log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "TFI Mismatch"); pnd->last_error = NFC_EIO; - return -1; + return pnd->last_error; } if (abtRxBuf[1] != CHIP_DATA (pnd)->ui8LastCommand + 1) { log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Command Code verification failed"); pnd->last_error = NFC_EIO; - return -1; + return pnd->last_error; } if (len) { pnd->last_error = uart_receive (DRIVER_DATA (pnd)->port, pbtData, len, 0, timeout); if (pnd->last_error != 0) { log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Unable to receive data. (RX)"); - return -1; + return pnd->last_error; } } pnd->last_error = uart_receive (DRIVER_DATA (pnd)->port, abtRxBuf, 2, 0, timeout); if (pnd->last_error != 0) { log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Unable to receive data. (RX)"); - return -1; + return pnd->last_error; } uint8_t btDCS = (256 - 0xD5); @@ -471,13 +471,13 @@ arygon_tama_receive (nfc_device *pnd, uint8_t *pbtData, const size_t szDataLen, if (btDCS != abtRxBuf[0]) { log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Data checksum mismatch"); pnd->last_error = NFC_EIO; - return -1; + return pnd->last_error; } if (0x00 != abtRxBuf[1]) { log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Frame postamble mismatch"); pnd->last_error = NFC_EIO; - return -1; + return pnd->last_error; } // The PN53x command is done and we successfully received the reply return len; diff --git a/libnfc/drivers/arygon.h b/libnfc/drivers/arygon.h index b63fbc2..de1bdb3 100644 --- a/libnfc/drivers/arygon.h +++ b/libnfc/drivers/arygon.h @@ -35,8 +35,8 @@ bool arygon_probe (nfc_connstring connstrings[], size_t connstrings_len, size nfc_device *arygon_connect (const nfc_connstring connstring); void arygon_disconnect (nfc_device *pnd); -bool arygon_tama_send (nfc_device *pnd, const uint8_t *pbtData, const size_t szData, int timeout); -int arygon_tama_receive (nfc_device *pnd, uint8_t *pbtData, const size_t szDat, int timeouta); +int arygon_tama_send (nfc_device *pnd, const uint8_t *pbtData, const size_t szData, int timeout); +int arygon_tama_receive (nfc_device *pnd, uint8_t *pbtData, const size_t szDat, int timeouta); extern const struct nfc_driver_t arygon_driver; diff --git a/libnfc/drivers/pn532_uart.c b/libnfc/drivers/pn532_uart.c index ace2e6f..4f0aa43 100644 --- a/libnfc/drivers/pn532_uart.c +++ b/libnfc/drivers/pn532_uart.c @@ -285,9 +285,10 @@ pn532_uart_wakeup (nfc_device *pnd) } #define PN532_BUFFER_LEN (PN53x_EXTENDED_FRAME__DATA_MAX_LEN + PN53x_EXTENDED_FRAME__OVERHEAD) -bool +int pn532_uart_send (nfc_device *pnd, const uint8_t *pbtData, const size_t szData, int timeout) { + int res = 0; // Before sending anything, we need to discard from any junk bytes uart_flush_input (DRIVER_DATA(pnd)->port); @@ -295,17 +296,17 @@ pn532_uart_send (nfc_device *pnd, const uint8_t *pbtData, const size_t szData, i case LOWVBAT: { /** PN532C106 wakeup. */ if (-1 == pn532_uart_wakeup(pnd)) { - return false; + return NFC_ECHIP; } // 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, 500) < 0) { - return false; + if ((res = pn53x_SAMConfiguration (pnd, 0x01, 500)) < 0) { + return res; } } break; case POWERDOWN: { if (-1 == pn532_uart_wakeup(pnd)) { - return false; + return NFC_ECHIP; } } break; @@ -319,14 +320,14 @@ pn532_uart_send (nfc_device *pnd, const uint8_t *pbtData, const size_t szData, i if (pn53x_build_frame (abtFrame, &szFrame, pbtData, szData) < 0) { pnd->last_error = NFC_EINVARG; - return false; + return pnd->last_error; } - int res = uart_send (DRIVER_DATA(pnd)->port, abtFrame, szFrame, timeout); + res = uart_send (DRIVER_DATA(pnd)->port, abtFrame, szFrame, timeout); if (res != 0) { log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Unable to transmit data. (TX)"); pnd->last_error = res; - return false; + return pnd->last_error; } uint8_t abtRxBuf[6]; @@ -334,15 +335,15 @@ pn532_uart_send (nfc_device *pnd, const uint8_t *pbtData, const size_t szData, i if (res != 0) { log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Unable to read ACK"); pnd->last_error = res; - return false; + return pnd->last_error; } if (pn53x_check_ack_frame (pnd, abtRxBuf, sizeof(abtRxBuf)) == 0) { // The PN53x is running the sent command } else { - return false; + return pnd->last_error; } - return true; + return NFC_SUCCESS; } int @@ -374,7 +375,7 @@ pn532_uart_receive (nfc_device *pnd, uint8_t *pbtData, const size_t szDataLen, i if (0 != (memcmp (abtRxBuf, pn53x_preamble, 3))) { log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Frame preamble+start code mismatch"); pnd->last_error = NFC_EIO; - return -1; + return pnd->last_error; } if ((0x01 == abtRxBuf[3]) && (0xff == abtRxBuf[4])) { @@ -386,7 +387,7 @@ pn532_uart_receive (nfc_device *pnd, uint8_t *pbtData, const size_t szDataLen, i } else if ((0xff == abtRxBuf[3]) && (0xff == abtRxBuf[4])) { // Extended frame pnd->last_error = uart_receive (DRIVER_DATA(pnd)->port, abtRxBuf, 3, 0, timeout); - if (pnd->last_error) return -1; + if (pnd->last_error) return pnd->last_error; // (abtRxBuf[0] << 8) + abtRxBuf[1] (LEN) include TFI + (CC+1) len = (abtRxBuf[0] << 8) + abtRxBuf[1] - 2; @@ -394,7 +395,7 @@ pn532_uart_receive (nfc_device *pnd, uint8_t *pbtData, const size_t szDataLen, i // TODO: Retry log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Length checksum mismatch"); pnd->last_error = NFC_EIO; - return -1; + return pnd->last_error; } } else { // Normal frame @@ -402,7 +403,7 @@ pn532_uart_receive (nfc_device *pnd, uint8_t *pbtData, const size_t szDataLen, i // TODO: Retry log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Length checksum mismatch"); pnd->last_error = NFC_EIO; - return -1; + return pnd->last_error; } // abtRxBuf[3] (LEN) include TFI + (CC+1) @@ -412,40 +413,40 @@ pn532_uart_receive (nfc_device *pnd, uint8_t *pbtData, const size_t szDataLen, i if (len > szDataLen) { log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "Unable to receive data: buffer too small. (szDataLen: %zu, len: %zu)", szDataLen, len); pnd->last_error = NFC_EIO; - return -1; + return pnd->last_error; } // TFI + PD0 (CC+1) pnd->last_error = uart_receive (DRIVER_DATA (pnd)->port, abtRxBuf, 2, 0, timeout); if (pnd->last_error != 0) { log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Unable to receive data. (RX)"); - return -1; + return pnd->last_error; } if (abtRxBuf[0] != 0xD5) { log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "TFI Mismatch"); pnd->last_error = NFC_EIO; - return -1; + return pnd->last_error; } if (abtRxBuf[1] != CHIP_DATA (pnd)->ui8LastCommand + 1) { log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Command Code verification failed"); pnd->last_error = NFC_EIO; - return -1; + return pnd->last_error; } if (len) { pnd->last_error = uart_receive (DRIVER_DATA (pnd)->port, pbtData, len, 0, timeout); if (pnd->last_error != 0) { log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Unable to receive data. (RX)"); - return -1; + return pnd->last_error; } } pnd->last_error = uart_receive (DRIVER_DATA (pnd)->port, abtRxBuf, 2, 0, timeout); if (pnd->last_error != 0) { log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Unable to receive data. (RX)"); - return -1; + return pnd->last_error; } uint8_t btDCS = (256 - 0xD5); @@ -457,13 +458,13 @@ pn532_uart_receive (nfc_device *pnd, uint8_t *pbtData, const size_t szDataLen, i if (btDCS != abtRxBuf[0]) { log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Data checksum mismatch"); pnd->last_error = NFC_EIO; - return -1; + return pnd->last_error; } if (0x00 != abtRxBuf[1]) { log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Frame postamble mismatch"); pnd->last_error = NFC_EIO; - return -1; + return pnd->last_error; } // The PN53x command is done and we successfully received the reply return len; @@ -477,7 +478,7 @@ pn532_uart_ack (nfc_device *pnd) return -1; } } - return (0 == uart_send (DRIVER_DATA(pnd)->port, pn53x_ack_frame, sizeof (pn53x_ack_frame), 0)) ? 0 : -1; + return (uart_send (DRIVER_DATA(pnd)->port, pn53x_ack_frame, sizeof (pn53x_ack_frame), 0) < 0) ? 0 : -1; } bool diff --git a/libnfc/drivers/pn532_uart.h b/libnfc/drivers/pn532_uart.h index 74194f1..dbf60b1 100644 --- a/libnfc/drivers/pn532_uart.h +++ b/libnfc/drivers/pn532_uart.h @@ -33,8 +33,8 @@ bool pn532_uart_probe (nfc_connstring connstrings[], size_t connstrings_len, nfc_device *pn532_uart_connect (const nfc_connstring connstring); void pn532_uart_disconnect (nfc_device *pnd); -bool pn532_uart_send (nfc_device *pnd, const uint8_t *pbtData, const size_t szData, int timeout); -int pn532_uart_receive (nfc_device *pnd, uint8_t *pbtData, const size_t szData, int timeout); +int pn532_uart_send (nfc_device *pnd, const uint8_t *pbtData, const size_t szData, int timeout); +int pn532_uart_receive (nfc_device *pnd, uint8_t *pbtData, const size_t szData, int timeout); extern const struct nfc_driver_t pn532_uart_driver; diff --git a/libnfc/drivers/pn53x_usb.c b/libnfc/drivers/pn53x_usb.c index f789034..15f3de6 100644 --- a/libnfc/drivers/pn53x_usb.c +++ b/libnfc/drivers/pn53x_usb.c @@ -505,7 +505,7 @@ pn53x_usb_disconnect (nfc_device *pnd) #define PN53X_USB_BUFFER_LEN (PN53x_EXTENDED_FRAME__DATA_MAX_LEN + PN53x_EXTENDED_FRAME__OVERHEAD) -bool +int pn53x_usb_send (nfc_device *pnd, const uint8_t *pbtData, const size_t szData, const int timeout) { uint8_t abtFrame[PN53X_USB_BUFFER_LEN] = { 0x00, 0x00, 0xff }; // Every packet must start with "00 00 ff" @@ -517,7 +517,7 @@ pn53x_usb_send (nfc_device *pnd, const uint8_t *pbtData, const size_t szData, co if (res < 0) { pnd->last_error = NFC_EIO; - return false; + return pnd->last_error; } uint8_t abtRxBuf[PN53X_USB_BUFFER_LEN]; @@ -526,7 +526,7 @@ pn53x_usb_send (nfc_device *pnd, const uint8_t *pbtData, const size_t szData, co pnd->last_error = NFC_EIO; // try to interrupt current device state pn53x_usb_ack(pnd); - return false; + return pnd->last_error; } if (pn53x_check_ack_frame (pnd, abtRxBuf, res) == 0) { @@ -544,11 +544,11 @@ pn53x_usb_send (nfc_device *pnd, const uint8_t *pbtData, const size_t szData, co pnd->last_error = NFC_EIO; // try to interrupt current device state pn53x_usb_ack(pnd); - return false; + return pnd->last_error; } } - return true; + return NFC_SUCCESS; } #define USB_TIMEOUT_PER_PASS 200 @@ -575,7 +575,7 @@ read: remaining_time -= USB_TIMEOUT_PER_PASS; if (remaining_time <= 0) { pnd->last_error = NFC_ETIMEOUT; - return -1; + return pnd->last_error; } else { usb_timeout = MIN(remaining_time, USB_TIMEOUT_PER_PASS); } @@ -588,7 +588,7 @@ read: DRIVER_DATA (pnd)->abort_flag = false; pn53x_usb_ack (pnd); pnd->last_error = NFC_EOPABORTED; - return -1; + return pnd->last_error; } else { goto read; } @@ -598,14 +598,14 @@ read: pnd->last_error = NFC_EIO; // try to interrupt current device state pn53x_usb_ack(pnd); - return -1; + return pnd->last_error; } const uint8_t pn53x_preamble[3] = { 0x00, 0x00, 0xff }; if (0 != (memcmp (abtRxBuf, pn53x_preamble, 3))) { log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Frame preamble+start code mismatch"); pnd->last_error = NFC_EIO; - return -1; + return pnd->last_error; } offset += 3; @@ -613,7 +613,7 @@ read: // Error frame log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Application level error detected"); pnd->last_error = NFC_EIO; - return -1; + return pnd->last_error; } else if ((0xff == abtRxBuf[offset]) && (0xff == abtRxBuf[offset + 1])) { // Extended frame offset += 2; @@ -624,7 +624,7 @@ read: // TODO: Retry log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Length checksum mismatch"); pnd->last_error = NFC_EIO; - return -1; + return pnd->last_error; } offset += 3; } else { @@ -633,7 +633,7 @@ read: // TODO: Retry log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Length checksum mismatch"); pnd->last_error = NFC_EIO; - return -1; + return pnd->last_error; } // abtRxBuf[3] (LEN) include TFI + (CC+1) @@ -644,21 +644,21 @@ read: if (len > szDataLen) { log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "Unable to receive data: buffer too small. (szDataLen: %zu, len: %zu)", szDataLen, len); pnd->last_error = NFC_EIO; - return -1; + return pnd->last_error; } // TFI + PD0 (CC+1) if (abtRxBuf[offset] != 0xD5) { log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "TFI Mismatch"); pnd->last_error = NFC_EIO; - return -1; + return pnd->last_error; } offset += 1; if (abtRxBuf[offset] != CHIP_DATA (pnd)->ui8LastCommand + 1) { log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Command Code verification failed"); pnd->last_error = NFC_EIO; - return -1; + return pnd->last_error; } offset += 1; @@ -674,14 +674,14 @@ read: if (btDCS != abtRxBuf[offset]) { log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Data checksum mismatch"); pnd->last_error = NFC_EIO; - return -1; + return pnd->last_error; } offset += 1; if (0x00 != abtRxBuf[offset]) { log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Frame postamble mismatch"); pnd->last_error = NFC_EIO; - return -1; + return pnd->last_error; } // The PN53x command is done and we successfully received the reply pnd->last_error = 0; diff --git a/libnfc/drivers/pn53x_usb.h b/libnfc/drivers/pn53x_usb.h index 21dbfdb..5618038 100644 --- a/libnfc/drivers/pn53x_usb.h +++ b/libnfc/drivers/pn53x_usb.h @@ -31,8 +31,8 @@ bool pn53x_usb_probe (nfc_connstring connstrings[], size_t connstrings_len, size_t *pszDeviceFound); nfc_device *pn53x_usb_connect (const nfc_connstring connstring); -bool pn53x_usb_send (nfc_device *pnd, const uint8_t *pbtData, const size_t szData, int timeout); -int pn53x_usb_receive (nfc_device *pnd, uint8_t *pbtData, const size_t szData, int timeout); +int pn53x_usb_send (nfc_device *pnd, const uint8_t *pbtData, const size_t szData, int timeout); +int pn53x_usb_receive (nfc_device *pnd, uint8_t *pbtData, const size_t szData, int timeout); void pn53x_usb_disconnect (nfc_device *pnd); extern const struct nfc_driver_t pn53x_usb_driver; From 5d4f22c5489dd01cde514b586d0441f6c78a5f08 Mon Sep 17 00:00:00 2001 From: Audrey Diacre Date: Thu, 5 Jan 2012 13:56:12 +0000 Subject: [PATCH 073/113] libnfc/drivers: pn532_uart_ack() and arygon_reset_tama() functions return now libnfc error code on failure. --- libnfc/drivers/arygon.c | 18 ++++++++++-------- libnfc/drivers/pn532_uart.c | 18 +++++++++--------- 2 files changed, 19 insertions(+), 17 deletions(-) diff --git a/libnfc/drivers/arygon.c b/libnfc/drivers/arygon.c index efe418e..2e2a9ee 100644 --- a/libnfc/drivers/arygon.c +++ b/libnfc/drivers/arygon.c @@ -88,7 +88,7 @@ static const uint8_t arygon_error_none[] = "FF000000\x0d\x0a"; static const uint8_t arygon_error_incomplete_command[] = "FF0C0000\x0d\x0a"; static const uint8_t arygon_error_unknown_mode[] = "FF060000\x0d\x0a"; -bool arygon_reset_tama (nfc_device *pnd); +int arygon_reset_tama (nfc_device *pnd); void arygon_firmware (nfc_device *pnd, char *str); bool @@ -135,11 +135,11 @@ arygon_probe (nfc_connstring connstrings[], size_t connstrings_len, size_t *pszD DRIVER_DATA (pnd)->abort_flag = false; #endif - bool res = arygon_reset_tama (pnd); + int res = arygon_reset_tama (pnd); pn53x_data_free (pnd); nfc_device_free (pnd); uart_close (sp); - if(!res) { + if(res < 0) { continue; } @@ -269,7 +269,7 @@ arygon_connect (const nfc_connstring connstring) #endif // Check communication using "Reset TAMA" command - if (!arygon_reset_tama(pnd)) { + if (arygon_reset_tama(pnd) < 0) { arygon_disconnect (pnd); return NULL; } @@ -511,7 +511,7 @@ arygon_firmware (nfc_device *pnd, char *str) } } -bool +int arygon_reset_tama (nfc_device *pnd) { const uint8_t arygon_reset_tama_cmd[] = { DEV_ARYGON_PROTOCOL_ARYGON_ASCII, 'a', 'r' }; @@ -526,14 +526,16 @@ arygon_reset_tama (nfc_device *pnd) res = uart_receive (DRIVER_DATA (pnd)->port, abtRx, szRx, 0, 1000); if (res != 0) { log_put (LOG_CATEGORY, NFC_PRIORITY_TRACE, "No reply to 'reset TAMA' command."); - return false; + pnd->last_error = NFC_EIO; + return pnd->last_error; } if (0 != memcmp (abtRx, arygon_error_none, sizeof (arygon_error_none) - 1)) { - return false; + pnd->last_error = NFC_EIO; + return pnd->last_error; } - return true; + return NFC_SUCCESS; } bool diff --git a/libnfc/drivers/pn532_uart.c b/libnfc/drivers/pn532_uart.c index 4f0aa43..f47aeed 100644 --- a/libnfc/drivers/pn532_uart.c +++ b/libnfc/drivers/pn532_uart.c @@ -295,8 +295,8 @@ pn532_uart_send (nfc_device *pnd, const uint8_t *pbtData, const size_t szData, i switch (CHIP_DATA(pnd)->power_mode) { case LOWVBAT: { /** PN532C106 wakeup. */ - if (-1 == pn532_uart_wakeup(pnd)) { - return NFC_ECHIP; + if ((res = pn532_uart_wakeup(pnd)) < 0) { + return res; } // 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 ((res = pn53x_SAMConfiguration (pnd, 0x01, 500)) < 0) { @@ -305,8 +305,8 @@ pn532_uart_send (nfc_device *pnd, const uint8_t *pbtData, const size_t szData, i } break; case POWERDOWN: { - if (-1 == pn532_uart_wakeup(pnd)) { - return NFC_ECHIP; + if ((res = pn532_uart_wakeup(pnd)) < 0) { + return res; } } break; @@ -362,8 +362,7 @@ pn532_uart_receive (nfc_device *pnd, uint8_t *pbtData, const size_t szDataLen, i pnd->last_error = uart_receive (DRIVER_DATA (pnd)->port, abtRxBuf, 5, abort_p, timeout); if (abort_p && (NFC_EOPABORTED == pnd->last_error)) { - pn532_uart_ack (pnd); - return pnd->last_error; + return pn532_uart_ack (pnd); } if (pnd->last_error != 0) { @@ -473,12 +472,13 @@ pn532_uart_receive (nfc_device *pnd, uint8_t *pbtData, const size_t szDataLen, i int pn532_uart_ack (nfc_device *pnd) { + int res = 0; if (POWERDOWN == CHIP_DATA(pnd)->power_mode) { - if (-1 == pn532_uart_wakeup(pnd)) { - return -1; + if ((res = pn532_uart_wakeup(pnd)) < 0) { + return res; } } - return (uart_send (DRIVER_DATA(pnd)->port, pn53x_ack_frame, sizeof (pn53x_ack_frame), 0) < 0) ? 0 : -1; + return (uart_send (DRIVER_DATA(pnd)->port, pn53x_ack_frame, sizeof (pn53x_ack_frame), 0)); } bool From 8b07a5f4c752f0502598983a58d30d2b415edf89 Mon Sep 17 00:00:00 2001 From: Audrey Diacre Date: Thu, 5 Jan 2012 14:05:43 +0000 Subject: [PATCH 074/113] libnfc/drivers: pn53x_usb_init() function returns now 0 on success and libnfc error code on failure. --- libnfc/drivers/pn53x_usb.c | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/libnfc/drivers/pn53x_usb.c b/libnfc/drivers/pn53x_usb.c index 15f3de6..a0a8f6a 100644 --- a/libnfc/drivers/pn53x_usb.c +++ b/libnfc/drivers/pn53x_usb.c @@ -118,7 +118,7 @@ struct pn53x_usb_data { const struct pn53x_io pn53x_usb_io; bool pn53x_usb_get_usb_device_name (struct usb_device *dev, usb_dev_handle *udev, char *buffer, size_t len); -bool pn53x_usb_init (nfc_device *pnd); +int pn53x_usb_init (nfc_device *pnd); int pn53x_usb_bulk_read (struct pn53x_usb_data *data, uint8_t abtRx[], const size_t szRx, const int timeout) @@ -461,7 +461,7 @@ pn53x_usb_connect (const nfc_connstring connstring) // HACK2: Then send a GetFirmware command to resync USB toggle bit between host & device // in case host used set_configuration and expects the device to have reset its toggle bit, which PN53x doesn't do - if (!pn53x_usb_init (pnd)) { + if (pn53x_usb_init (pnd) < 0) { usb_close (data.pudh); goto error; } @@ -694,9 +694,10 @@ pn53x_usb_ack (nfc_device *pnd) return pn53x_usb_bulk_write (DRIVER_DATA (pnd), (uint8_t *) pn53x_ack_frame, sizeof (pn53x_ack_frame), 0); } -bool +int pn53x_usb_init (nfc_device *pnd) { + int res = 0; // Sometimes PN53x USB doesn't reply ACK one the first frame, so we need to send a dummy one... //pn53x_check_communication (pnd); // Sony RC-S360 doesn't support this command for now so let's use a get_firmware_version instead: const uint8_t abtCmd[] = { GetFirmwareVersion }; @@ -710,8 +711,8 @@ pn53x_usb_init (nfc_device *pnd) pn53x_usb_ack (pnd); } - if (pn53x_init (pnd) < 0) - return false; + if ((res = pn53x_init (pnd)) < 0) + return res; if (ASK_LOGO == DRIVER_DATA (pnd)->model) { log_put (LOG_CATEGORY, NFC_PRIORITY_TRACE, "ASK LoGO initialization."); @@ -748,7 +749,7 @@ On ASK LoGO hardware: pn53x_write_register (pnd, PN53X_SFR_P3, 0xFF, _BV (P30) | _BV (P31) | _BV (P33) | _BV (P35)); } - return true; + return NFC_SUCCESS; } int From c80ebdca25bb6d9a47d1685df0dc74c5faf24430 Mon Sep 17 00:00:00 2001 From: Audrey Diacre Date: Thu, 5 Jan 2012 14:49:02 +0000 Subject: [PATCH 075/113] nfc_abort_command() function returns now 0 on success and libnfc error code on failure and fix some warnings. --- examples/nfc-poll.c | 2 +- include/nfc/nfc.h | 2 +- libnfc/drivers/arygon.c | 4 ++-- libnfc/drivers/pn532_uart.c | 4 ++-- libnfc/drivers/pn53x_usb.c | 4 ++-- libnfc/nfc-internal.h | 2 +- libnfc/nfc.c | 4 ++-- utils/mifare.c | 2 +- utils/nfc-list.c | 2 +- utils/nfc-mfclassic.c | 2 +- 10 files changed, 14 insertions(+), 14 deletions(-) diff --git a/examples/nfc-poll.c b/examples/nfc-poll.c index c9bd62b..2392f28 100644 --- a/examples/nfc-poll.c +++ b/examples/nfc-poll.c @@ -63,7 +63,7 @@ void stop_polling (int sig) } void -print_usage (char* progname) +print_usage (const char* progname) { printf ("usage: %s [-v]\n", progname); printf (" -v\t verbose display\n"); diff --git a/include/nfc/nfc.h b/include/nfc/nfc.h index 3b1bb4b..0f05a02 100644 --- a/include/nfc/nfc.h +++ b/include/nfc/nfc.h @@ -66,7 +66,7 @@ extern "C" { NFC_EXPORT bool nfc_get_default_device (nfc_connstring *connstring); NFC_EXPORT nfc_device *nfc_connect (const nfc_connstring connstring); NFC_EXPORT void nfc_disconnect (nfc_device *pnd); - NFC_EXPORT bool nfc_abort_command (nfc_device *pnd); + NFC_EXPORT int nfc_abort_command (nfc_device *pnd); NFC_EXPORT void nfc_list_devices (nfc_connstring connstrings[], size_t connstrings_len, size_t *pszDeviceFound); NFC_EXPORT int nfc_idle (nfc_device *pnd); diff --git a/libnfc/drivers/arygon.c b/libnfc/drivers/arygon.c index 2e2a9ee..40306eb 100644 --- a/libnfc/drivers/arygon.c +++ b/libnfc/drivers/arygon.c @@ -538,7 +538,7 @@ arygon_reset_tama (nfc_device *pnd) return NFC_SUCCESS; } -bool +int arygon_abort_command (nfc_device *pnd) { if (pnd) { @@ -549,7 +549,7 @@ arygon_abort_command (nfc_device *pnd) DRIVER_DATA (pnd)->abort_flag = true; #endif } - return true; + return NFC_SUCCESS; } diff --git a/libnfc/drivers/pn532_uart.c b/libnfc/drivers/pn532_uart.c index f47aeed..9bd7e89 100644 --- a/libnfc/drivers/pn532_uart.c +++ b/libnfc/drivers/pn532_uart.c @@ -481,7 +481,7 @@ pn532_uart_ack (nfc_device *pnd) return (uart_send (DRIVER_DATA(pnd)->port, pn53x_ack_frame, sizeof (pn53x_ack_frame), 0)); } -bool +int pn532_uart_abort_command (nfc_device *pnd) { if (pnd) { @@ -492,7 +492,7 @@ pn532_uart_abort_command (nfc_device *pnd) DRIVER_DATA (pnd)->abort_flag = true; #endif } - return true; + return NFC_SUCCESS; } const struct pn53x_io pn532_uart_io = { diff --git a/libnfc/drivers/pn53x_usb.c b/libnfc/drivers/pn53x_usb.c index a0a8f6a..1ab9ee7 100644 --- a/libnfc/drivers/pn53x_usb.c +++ b/libnfc/drivers/pn53x_usb.c @@ -781,11 +781,11 @@ pn53x_usb_set_property_bool (nfc_device *pnd, const nfc_property property, const return NFC_SUCCESS; } -bool +int pn53x_usb_abort_command (nfc_device *pnd) { DRIVER_DATA (pnd)->abort_flag = true; - return true; + return NFC_SUCCESS; } const struct pn53x_io pn53x_usb_io = { diff --git a/libnfc/nfc-internal.h b/libnfc/nfc-internal.h index f816ea6..fd15dba 100644 --- a/libnfc/nfc-internal.h +++ b/libnfc/nfc-internal.h @@ -151,7 +151,7 @@ struct nfc_driver_t { int (*device_set_property_bool) (struct nfc_device *pnd, const nfc_property property, const bool bEnable); int (*device_set_property_int) (struct nfc_device *pnd, const nfc_property property, const int value); - bool (*abort_command) (struct nfc_device *pnd); + int (*abort_command) (struct nfc_device *pnd); int (*idle) (struct nfc_device *pnd); }; diff --git a/libnfc/nfc.c b/libnfc/nfc.c index 8c61ea0..a042b05 100644 --- a/libnfc/nfc.c +++ b/libnfc/nfc.c @@ -676,7 +676,7 @@ nfc_idle (nfc_device *pnd) /** * @brief Abort current running command - * @return Returns \c true if action was successfully performed; otherwise returns \c false. + * @return Returns 0 on success, otherwise returns libnfc's error code. * * @param pnd \a nfc_device struct pointer that represent currently used device * @@ -685,7 +685,7 @@ nfc_idle (nfc_device *pnd) * * @note The blocking function (ie. nfc_target_init()) will failed with DEABORT error. */ -bool +int nfc_abort_command (nfc_device *pnd) { HAL (abort_command, pnd); diff --git a/utils/mifare.c b/utils/mifare.c index 4773e45..d9ea0b0 100644 --- a/utils/mifare.c +++ b/utils/mifare.c @@ -54,7 +54,7 @@ nfc_initiator_mifare_cmd (nfc_device *pnd, const mifare_cmd mc, const uint8_t ui size_t szRx = sizeof(abtRx); size_t szParamLen; uint8_t abtCmd[265]; - bool bEasyFraming; + //bool bEasyFraming; abtCmd[0] = mc; // The MIFARE Classic command abtCmd[1] = ui8Block; // The block address (1K=0x00..0x39, 4K=0x00..0xff) diff --git a/utils/nfc-list.c b/utils/nfc-list.c index 9fee2bb..5508d9c 100644 --- a/utils/nfc-list.c +++ b/utils/nfc-list.c @@ -60,7 +60,7 @@ static nfc_device *pnd; void -print_usage (char* progname) +print_usage (const char* progname) { printf ("usage: %s [-v]\n", progname); printf (" -v\t verbose display\n"); diff --git a/utils/nfc-mfclassic.c b/utils/nfc-mfclassic.c index e89a19b..94d939b 100644 --- a/utils/nfc-mfclassic.c +++ b/utils/nfc-mfclassic.c @@ -577,7 +577,7 @@ main (int argc, const char *argv[]) uint8_t fileUid[4]; memcpy (fileUid, mtKeys.amb[0].mbm.abtUID, 4); // Compare if key dump UID is the same as the current tag UID, at least for the first 4 bytes - if (memcmp (nt.nti.nai.abtUid, fileUid, 4) != 0) { + if (memcmp (pbtUID, fileUid, 4) != 0) { printf ("Expected MIFARE Classic card with UID starting as: %02x%02x%02x%02x\n", fileUid[0], fileUid[1], fileUid[2], fileUid[3]); } From 239fd750c4755a70263500d59df9f55eeab7eb0c Mon Sep 17 00:00:00 2001 From: Audrey Diacre Date: Thu, 5 Jan 2012 15:10:11 +0000 Subject: [PATCH 076/113] add timeout on nfc_target_init() and this function returns now received bytes count on success. --- examples/nfc-dep-target.c | 2 +- examples/nfc-emulate-tag.c | 2 +- examples/nfc-emulate-uid.c | 2 +- examples/nfc-relay.c | 2 +- examples/pn53x-sam.c | 2 +- include/nfc/nfc.h | 2 +- libnfc/chips/pn53x.c | 10 +++++----- libnfc/chips/pn53x.h | 4 ++-- libnfc/nfc-emulation.c | 2 +- libnfc/nfc-internal.h | 2 +- libnfc/nfc.c | 7 ++++--- test/test_dep_active.c | 2 +- test/test_dep_passive.c | 2 +- utils/nfc-relay-picc.c | 2 +- 14 files changed, 22 insertions(+), 21 deletions(-) diff --git a/examples/nfc-dep-target.c b/examples/nfc-dep-target.c index 0597706..ad2e275 100644 --- a/examples/nfc-dep-target.c +++ b/examples/nfc-dep-target.c @@ -119,7 +119,7 @@ main (int argc, const char *argv[]) print_nfc_target (nt, false); printf ("Waiting for initiator request...\n"); - if(nfc_target_init (pnd, &nt, abtRx, &szRx) < 0) { + if(nfc_target_init (pnd, &nt, abtRx, &szRx, 0) < 0) { nfc_perror(pnd, "nfc_target_init"); goto error; } diff --git a/examples/nfc-emulate-tag.c b/examples/nfc-emulate-tag.c index 828bc86..9936a7d 100644 --- a/examples/nfc-emulate-tag.c +++ b/examples/nfc-emulate-tag.c @@ -140,7 +140,7 @@ nfc_target_emulate_tag(nfc_device *pnd, nfc_target *pnt) uint8_t abtTx[MAX_FRAME_LEN]; bool loop = true; - if (nfc_target_init (pnd, pnt, abtRx, &szRx) < 0) { + if (nfc_target_init (pnd, pnt, abtRx, &szRx, 0) < 0) { nfc_perror (pnd, "nfc_target_init"); return false; } diff --git a/examples/nfc-emulate-uid.c b/examples/nfc-emulate-uid.c index 90c01aa..ffd7bc2 100644 --- a/examples/nfc-emulate-uid.c +++ b/examples/nfc-emulate-uid.c @@ -155,7 +155,7 @@ main (int argc, char *argv[]) }, }, }; - if (nfc_target_init (pnd, &nt, abtRecv, &szRecvBits) < 0) { + if (nfc_target_init (pnd, &nt, abtRecv, &szRecvBits, 0) < 0) { nfc_perror (pnd, "nfc_target_init"); ERR ("Could not come out of auto-emulation, no command was received"); goto error; diff --git a/examples/nfc-relay.c b/examples/nfc-relay.c index 677e8d9..992f698 100644 --- a/examples/nfc-relay.c +++ b/examples/nfc-relay.c @@ -146,7 +146,7 @@ main (int argc, char *argv[]) }, }; - if (nfc_target_init (pndTag, &nt, abtReaderRx, &szReaderRxBits) < 0) { + if (nfc_target_init (pndTag, &nt, abtReaderRx, &szReaderRxBits, 0) < 0) { ERR ("%s", "Initialization of NFC emulator failed"); nfc_disconnect (pndTag); return EXIT_FAILURE; diff --git a/examples/pn53x-sam.c b/examples/pn53x-sam.c index 713cb58..023a2dd 100644 --- a/examples/pn53x-sam.c +++ b/examples/pn53x-sam.c @@ -176,7 +176,7 @@ main (int argc, const char *argv[]) }; printf ("Now both, NFC device (configured as target) and SAM are readables from an external NFC initiator.\n"); printf ("Please note that NFC device (configured as target) stay in target mode until it receive RATS, ATR_REQ or proprietary command.\n"); - if (nfc_target_init (pnd, &nt, abtRx, &szRx) < 0) { + if (nfc_target_init (pnd, &nt, abtRx, &szRx, 0) < 0) { nfc_perror(pnd, "nfc_target_init"); return EXIT_FAILURE; } diff --git a/include/nfc/nfc.h b/include/nfc/nfc.h index 0f05a02..9bcff7d 100644 --- a/include/nfc/nfc.h +++ b/include/nfc/nfc.h @@ -83,7 +83,7 @@ extern "C" { 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 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, size_t *pszRx); + NFC_EXPORT int nfc_target_init (nfc_device *pnd, nfc_target *pnt, uint8_t *pbtRx, size_t *pszRx, int timeout); NFC_EXPORT int nfc_target_send_bytes (nfc_device *pnd, const uint8_t *pbtTx, const size_t szTx, int timeout); NFC_EXPORT int nfc_target_receive_bytes (nfc_device *pnd, uint8_t *pbtRx, size_t *pszRx, int timeout); NFC_EXPORT int nfc_target_send_bits (nfc_device *pnd, const uint8_t *pbtTx, const size_t szTxBits, const uint8_t *pbtTxPar); diff --git a/libnfc/chips/pn53x.c b/libnfc/chips/pn53x.c index 99b2e6f..8d02d33 100644 --- a/libnfc/chips/pn53x.c +++ b/libnfc/chips/pn53x.c @@ -1589,7 +1589,7 @@ pn53x_initiator_deselect_target (struct nfc_device *pnd) #define SAK_ISO14443_4_COMPLIANT 0x20 #define SAK_ISO18092_COMPLIANT 0x40 int -pn53x_target_init (struct nfc_device *pnd, nfc_target *pnt, uint8_t *pbtRx, size_t *pszRx) +pn53x_target_init (struct nfc_device *pnd, nfc_target *pnt, uint8_t *pbtRx, size_t *pszRx, int timeout) { pn53x_reset_settings(pnd); @@ -1742,7 +1742,7 @@ pn53x_target_init (struct nfc_device *pnd, nfc_target *pnt, uint8_t *pbtRx, size while (!targetActivated) { uint8_t btActivatedMode; - if((res = pn53x_TgInitAsTarget(pnd, ptm, pbtMifareParams, pbtTkt, szTkt, pbtFeliCaParams, pbtNFCID3t, pbtGBt, szGBt, pbtRx, pszRx, &btActivatedMode)) < 0) { + if((res = pn53x_TgInitAsTarget(pnd, ptm, pbtMifareParams, pbtTkt, szTkt, pbtFeliCaParams, pbtNFCID3t, pbtGBt, szGBt, pbtRx, pszRx, &btActivatedMode, timeout)) < 0) { return res; } @@ -1807,7 +1807,7 @@ pn53x_target_init (struct nfc_device *pnd, nfc_target *pnt, uint8_t *pbtRx, size } } - return NFC_SUCCESS; + return *pszRx; } int @@ -2420,7 +2420,7 @@ pn53x_TgInitAsTarget (struct nfc_device *pnd, pn53x_target_mode ptm, const uint8_t *pbtTkt, size_t szTkt, const uint8_t *pbtFeliCaParams, const uint8_t *pbtNFCID3t, const uint8_t *pbtGBt, const size_t szGBt, - uint8_t *pbtRx, size_t *pszRx, uint8_t *pbtModeByte) + uint8_t *pbtRx, size_t *pszRx, uint8_t *pbtModeByte, int timeout) { uint8_t abtCmd[39 + 47 + 48] = { TgInitAsTarget }; // Worst case: 39-byte base, 47 bytes max. for General Bytes, 48 bytes max. for Historical Bytes size_t szOptionalBytes = 0; @@ -2469,7 +2469,7 @@ pn53x_TgInitAsTarget (struct nfc_device *pnd, pn53x_target_mode ptm, // Request the initialization as a target uint8_t abtRx[PN53x_EXTENDED_FRAME__DATA_MAX_LEN]; size_t szRx = sizeof (abtRx); - if ((res = pn53x_transceive (pnd, abtCmd, 36 + szOptionalBytes, abtRx, &szRx, -1)) < 0) + if ((res = pn53x_transceive (pnd, abtCmd, 36 + szOptionalBytes, abtRx, &szRx, timeout)) < 0) return res; // Note: the first byte is skip: diff --git a/libnfc/chips/pn53x.h b/libnfc/chips/pn53x.h index e1b07a5..0e6af42 100644 --- a/libnfc/chips/pn53x.h +++ b/libnfc/chips/pn53x.h @@ -308,7 +308,7 @@ int pn53x_initiator_transceive_bytes_timed (struct nfc_device *pnd, const uin int pn53x_initiator_deselect_target (struct nfc_device *pnd); // NFC device as Target functions -int pn53x_target_init (struct nfc_device *pnd, nfc_target *pnt, uint8_t *pbtRx, size_t *pszRx); +int pn53x_target_init (struct nfc_device *pnd, nfc_target *pnt, uint8_t *pbtRx, size_t *pszRx, int timeout); int pn53x_target_receive_bits (struct nfc_device *pnd, uint8_t *pbtRx, size_t *pszRxBits, uint8_t *pbtRxPar); int pn53x_target_receive_bytes (struct nfc_device *pnd, uint8_t *pbtRx, size_t *pszRx, int timeout); int pn53x_target_send_bits (struct nfc_device *pnd, const uint8_t *pbtTx, const size_t szTxBits, const uint8_t *pbtTxPar); @@ -342,7 +342,7 @@ int pn53x_TgInitAsTarget (struct nfc_device *pnd, pn53x_target_mode ptm, const uint8_t *pbtTkt, size_t szTkt, const uint8_t *pbtFeliCaParams, const uint8_t *pbtNFCID3t, const uint8_t *pbtGB, const size_t szGB, - uint8_t *pbtRx, size_t *pszRx, uint8_t *pbtModeByte); + uint8_t *pbtRx, size_t *pszRx, uint8_t *pbtModeByte, int timeout); // RFConfiguration int pn53x_RFConfiguration__RF_field (struct nfc_device *pnd, bool bEnable); diff --git a/libnfc/nfc-emulation.c b/libnfc/nfc-emulation.c index 287632c..435363c 100644 --- a/libnfc/nfc-emulation.c +++ b/libnfc/nfc-emulation.c @@ -35,7 +35,7 @@ nfc_emulate_target (nfc_device *pnd, struct nfc_emulator *emulator) uint8_t abtTx[ISO7816_SHORT_C_APDU_MAX_LEN]; int res = 0; - if (nfc_target_init (pnd, emulator->target, abtRx, &szRx) < 0) { + if (nfc_target_init (pnd, emulator->target, abtRx, &szRx, 0) < 0) { return -1; } diff --git a/libnfc/nfc-internal.h b/libnfc/nfc-internal.h index fd15dba..cab8581 100644 --- a/libnfc/nfc-internal.h +++ b/libnfc/nfc-internal.h @@ -142,7 +142,7 @@ struct nfc_driver_t { 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 (*target_init) (struct nfc_device *pnd, nfc_target * pnt, uint8_t * pbtRx, size_t * pszRx); + int (*target_init) (struct nfc_device *pnd, nfc_target * pnt, uint8_t * pbtRx, size_t * pszRx, int timeout); int (*target_send_bytes) (struct nfc_device *pnd, const uint8_t * pbtTx, const size_t szTx, int timeout); int (*target_receive_bytes) (struct nfc_device *pnd, uint8_t * pbtRx, size_t * pszRx, int timeout); int (*target_send_bits) (struct nfc_device *pnd, const uint8_t * pbtTx, const size_t szTxBits, const uint8_t * pbtTxPar); diff --git a/libnfc/nfc.c b/libnfc/nfc.c index a042b05..da683c5 100644 --- a/libnfc/nfc.c +++ b/libnfc/nfc.c @@ -599,7 +599,7 @@ nfc_initiator_transceive_bits_timed (nfc_device *pnd, const uint8_t *pbtTx, cons /** * @brief Initialize NFC device as an emulated tag - * @return Returns 0 on success, otherwise returns libnfc's error code + * @return Returns received bytes count on success, otherwise returns libnfc's error code * * @param pnd \a nfc_device struct pointer that represent currently used device * @param ntm target mode restriction that you want to emulate (eg. NTM_PASSIVE_ONLY) @@ -610,6 +610,7 @@ nfc_initiator_transceive_bits_timed (nfc_device *pnd, const uint8_t *pbtTx, cons * * @param[out] pbtRx Rx buffer pointer * @param[out] pszRx received bytes count + * @param timeout in milliseconds * * This function initializes NFC device in \e target mode in order to emulate a * tag using the specified \a nfc_target_mode_t. @@ -628,7 +629,7 @@ nfc_initiator_transceive_bits_timed (nfc_device *pnd, const uint8_t *pbtTx, cons * receive functions can be used. */ int -nfc_target_init (nfc_device *pnd, nfc_target *pnt, uint8_t *pbtRx, size_t * pszRx) +nfc_target_init (nfc_device *pnd, nfc_target *pnt, uint8_t *pbtRx, size_t * pszRx, int timeout) { int res = 0; // Disallow invalid frame @@ -655,7 +656,7 @@ nfc_target_init (nfc_device *pnd, nfc_target *pnt, uint8_t *pbtRx, size_t * pszR if ((res = nfc_device_set_property_bool (pnd, NP_ACTIVATE_FIELD, false)) < 0) return res; - HAL (target_init, pnd, pnt, pbtRx, pszRx); + HAL (target_init, pnd, pnt, pbtRx, pszRx, timeout); } /** diff --git a/test/test_dep_active.c b/test/test_dep_active.c index c2af320..85d992b 100644 --- a/test/test_dep_active.c +++ b/test/test_dep_active.c @@ -84,7 +84,7 @@ target_thread (void *arg) uint8_t abtRx[1024]; size_t szRx = sizeof (abtRx); - int res = nfc_target_init (device, &nt, abtRx, &szRx); + int res = nfc_target_init (device, &nt, abtRx, &szRx, 0); cut_assert_equal_int (0, res, cut_message ("Can't initialize NFC device as target: %s", nfc_strerror (device))); if (res < 0) { thread_res = -1; return (void*) thread_res; } diff --git a/test/test_dep_passive.c b/test/test_dep_passive.c index 442c911..9db5151 100644 --- a/test/test_dep_passive.c +++ b/test/test_dep_passive.c @@ -82,7 +82,7 @@ target_thread (void *arg) uint8_t abtRx[1024]; size_t szRx = sizeof (abtRx); - int res = nfc_target_init (device, &nt, abtRx, &szRx); + int res = nfc_target_init (device, &nt, abtRx, &szRx, 0); cut_assert_equal_int (0, res, cut_message ("Can't initialize NFC device as target: %s", nfc_strerror (device))); if (res < 0) { thread_res = -1; return (void*) thread_res; } diff --git a/utils/nfc-relay-picc.c b/utils/nfc-relay-picc.c index 234ea73..3c5caf5 100644 --- a/utils/nfc-relay-picc.c +++ b/utils/nfc-relay-picc.c @@ -354,7 +354,7 @@ main (int argc, char *argv[]) printf ("Connected to the NFC emulator device: %s\n", nfc_device_get_name (pndTarget)); - if (nfc_target_init (pndTarget, &ntEmulatedTarget, abtCapdu, &szCapduLen) < 0) { + if (nfc_target_init (pndTarget, &ntEmulatedTarget, abtCapdu, &szCapduLen, 0) < 0) { ERR ("%s", "Initialization of NFC emulator failed"); if (!target_only_mode) { nfc_disconnect (pndInitiator); From 7e7ee3299eb4490c25a65f47663ece46edf9e1d8 Mon Sep 17 00:00:00 2001 From: Audrey Diacre Date: Thu, 5 Jan 2012 15:50:07 +0000 Subject: [PATCH 077/113] nfc_target_receive_bits() function does not now use pszRxBits as parameter because this function returns it. --- examples/nfc-emulate-uid.c | 2 +- examples/nfc-relay.c | 2 +- include/nfc/nfc.h | 2 +- libnfc/chips/pn53x.c | 9 +++++---- libnfc/chips/pn53x.h | 2 +- libnfc/nfc-internal.h | 2 +- libnfc/nfc.c | 4 ++-- 7 files changed, 12 insertions(+), 11 deletions(-) diff --git a/examples/nfc-emulate-uid.c b/examples/nfc-emulate-uid.c index ffd7bc2..b8f0a39 100644 --- a/examples/nfc-emulate-uid.c +++ b/examples/nfc-emulate-uid.c @@ -172,7 +172,7 @@ main (int argc, char *argv[]) while (true) { // Test if we received a frame - if (nfc_target_receive_bits (pnd, abtRecv, &szRecvBits, NULL) > 0) { + if ((szRecvBits = nfc_target_receive_bits (pnd, abtRecv, NULL)) > 0) { // Prepare the command to send back for the anti-collision request switch (szRecvBits) { case 7: // Request or Wakeup diff --git a/examples/nfc-relay.c b/examples/nfc-relay.c index 992f698..c984765 100644 --- a/examples/nfc-relay.c +++ b/examples/nfc-relay.c @@ -179,7 +179,7 @@ main (int argc, char *argv[]) while (!quitting) { // Test if we received a frame from the reader - if (nfc_target_receive_bits (pndTag, abtReaderRx, &szReaderRxBits, abtReaderRxPar) > 0) { + if ((szReaderRxBits = nfc_target_receive_bits (pndTag, abtReaderRx, abtReaderRxPar)) > 0) { // Drop down the field before sending a REQA command and start a new session if (szReaderRxBits == 7 && abtReaderRx[0] == 0x26) { // Drop down field for a very short time (original tag will reboot) diff --git a/include/nfc/nfc.h b/include/nfc/nfc.h index 9bcff7d..51d2b7b 100644 --- a/include/nfc/nfc.h +++ b/include/nfc/nfc.h @@ -87,7 +87,7 @@ extern "C" { NFC_EXPORT int nfc_target_send_bytes (nfc_device *pnd, const uint8_t *pbtTx, const size_t szTx, int timeout); NFC_EXPORT int nfc_target_receive_bytes (nfc_device *pnd, uint8_t *pbtRx, size_t *pszRx, int timeout); NFC_EXPORT int nfc_target_send_bits (nfc_device *pnd, const uint8_t *pbtTx, const size_t szTxBits, const uint8_t *pbtTxPar); - NFC_EXPORT int nfc_target_receive_bits (nfc_device *pnd, uint8_t *pbtRx, size_t *pszRxBits, uint8_t *pbtRxPar); + NFC_EXPORT int nfc_target_receive_bits (nfc_device *pnd, uint8_t *pbtRx, uint8_t *pbtRxPar); /* Error reporting */ NFC_EXPORT const char *nfc_strerror (const nfc_device *pnd); diff --git a/libnfc/chips/pn53x.c b/libnfc/chips/pn53x.c index 8d02d33..dfbc6be 100644 --- a/libnfc/chips/pn53x.c +++ b/libnfc/chips/pn53x.c @@ -1811,8 +1811,9 @@ pn53x_target_init (struct nfc_device *pnd, nfc_target *pnt, uint8_t *pbtRx, size } int -pn53x_target_receive_bits (struct nfc_device *pnd, uint8_t *pbtRx, size_t *pszRxBits, uint8_t *pbtRxPar) +pn53x_target_receive_bits (struct nfc_device *pnd, uint8_t *pbtRx, uint8_t *pbtRxPar) { + size_t szRxBits = 0; uint8_t abtCmd[] = { TgGetInitiatorCommand }; uint8_t abtRx[PN53x_EXTENDED_FRAME__DATA_MAX_LEN]; @@ -1838,15 +1839,15 @@ pn53x_target_receive_bits (struct nfc_device *pnd, uint8_t *pbtRx, size_t *pszRx // Unwrap the response frame if ((res = pn53x_unwrap_frame (abtRx + 1, szFrameBits, pbtRx, pbtRxPar)) < 0) return res; - *pszRxBits = res; + szRxBits = res; } else { // Save the received bits - *pszRxBits = szFrameBits; + szRxBits = szFrameBits; // Copy the received bytes memcpy (pbtRx, abtRx + 1, szRx - 1); } // Everyting seems ok, return received bits count - return *pszRxBits; + return szRxBits; } int diff --git a/libnfc/chips/pn53x.h b/libnfc/chips/pn53x.h index 0e6af42..7ea7731 100644 --- a/libnfc/chips/pn53x.h +++ b/libnfc/chips/pn53x.h @@ -309,7 +309,7 @@ int pn53x_initiator_deselect_target (struct nfc_device *pnd); // NFC device as Target functions int pn53x_target_init (struct nfc_device *pnd, nfc_target *pnt, uint8_t *pbtRx, size_t *pszRx, int timeout); -int pn53x_target_receive_bits (struct nfc_device *pnd, uint8_t *pbtRx, size_t *pszRxBits, uint8_t *pbtRxPar); +int pn53x_target_receive_bits (struct nfc_device *pnd, uint8_t *pbtRx, uint8_t *pbtRxPar); int pn53x_target_receive_bytes (struct nfc_device *pnd, uint8_t *pbtRx, size_t *pszRx, int timeout); int pn53x_target_send_bits (struct nfc_device *pnd, const uint8_t *pbtTx, const size_t szTxBits, const uint8_t *pbtTxPar); int pn53x_target_send_bytes (struct nfc_device *pnd, const uint8_t *pbtTx, const size_t szTx, int timeout); diff --git a/libnfc/nfc-internal.h b/libnfc/nfc-internal.h index cab8581..91646ce 100644 --- a/libnfc/nfc-internal.h +++ b/libnfc/nfc-internal.h @@ -146,7 +146,7 @@ struct nfc_driver_t { int (*target_send_bytes) (struct nfc_device *pnd, const uint8_t * pbtTx, const size_t szTx, int timeout); int (*target_receive_bytes) (struct nfc_device *pnd, uint8_t * pbtRx, size_t * pszRx, int timeout); int (*target_send_bits) (struct nfc_device *pnd, const uint8_t * pbtTx, const size_t szTxBits, const uint8_t * pbtTxPar); - int (*target_receive_bits) (struct nfc_device *pnd, uint8_t * pbtRx, size_t * pszRxBits, uint8_t * pbtRxPar); + int (*target_receive_bits) (struct nfc_device *pnd, uint8_t * pbtRx, uint8_t * pbtRxPar); int (*device_set_property_bool) (struct nfc_device *pnd, const nfc_property property, const bool bEnable); int (*device_set_property_int) (struct nfc_device *pnd, const nfc_property property, const int value); diff --git a/libnfc/nfc.c b/libnfc/nfc.c index da683c5..d482715 100644 --- a/libnfc/nfc.c +++ b/libnfc/nfc.c @@ -758,9 +758,9 @@ nfc_target_send_bits (nfc_device *pnd, const uint8_t *pbtTx, const size_t szTxBi * frames. */ int -nfc_target_receive_bits (nfc_device *pnd, uint8_t *pbtRx, size_t *pszRxBits, uint8_t *pbtRxPar) +nfc_target_receive_bits (nfc_device *pnd, uint8_t *pbtRx, uint8_t *pbtRxPar) { - HAL (target_receive_bits, pnd, pbtRx, pszRxBits, pbtRxPar); + HAL (target_receive_bits, pnd, pbtRx, pbtRxPar); } /** From 642f9a38f776acbad27ec7a355313e5fd705a164 Mon Sep 17 00:00:00 2001 From: Audrey Diacre Date: Thu, 5 Jan 2012 16:33:55 +0000 Subject: [PATCH 078/113] nfc_target_receive_bytes() function does not now use pszRx as parameter because this function returns it. --- examples/nfc-dep-target.c | 2 +- examples/nfc-emulate-tag.c | 2 +- include/nfc/nfc.h | 2 +- libnfc/chips/pn53x.c | 8 ++++---- libnfc/chips/pn53x.h | 2 +- libnfc/nfc-emulation.c | 2 +- libnfc/nfc-internal.h | 2 +- libnfc/nfc.c | 5 ++--- test/test_dep_active.c | 6 +++--- test/test_dep_passive.c | 18 +++++++++--------- utils/nfc-relay-picc.c | 2 +- 11 files changed, 25 insertions(+), 26 deletions(-) diff --git a/examples/nfc-dep-target.c b/examples/nfc-dep-target.c index ad2e275..77a912f 100644 --- a/examples/nfc-dep-target.c +++ b/examples/nfc-dep-target.c @@ -125,7 +125,7 @@ main (int argc, const char *argv[]) } printf("Initiator request received. Waiting for data...\n"); - if (nfc_target_receive_bytes (pnd, abtRx, &szRx, 0) < 0) { + if (((int) (szRx = (size_t) nfc_target_receive_bytes (pnd, abtRx, 0))) < 0) { nfc_perror(pnd, "nfc_target_receive_bytes"); goto error; } diff --git a/examples/nfc-emulate-tag.c b/examples/nfc-emulate-tag.c index 9936a7d..3604ab7 100644 --- a/examples/nfc-emulate-tag.c +++ b/examples/nfc-emulate-tag.c @@ -158,7 +158,7 @@ nfc_target_emulate_tag(nfc_device *pnd, nfc_target *pnt) nfc_device_set_property_bool (pnd, NP_HANDLE_CRC, false); init_mfc_auth = false; } - if (nfc_target_receive_bytes(pnd, abtRx, &szRx, 0) < 0) { + if ((int) ((szRx = (size_t) nfc_target_receive_bytes(pnd, abtRx, 0))) < 0) { nfc_perror (pnd, "nfc_target_receive_bytes"); return false; } diff --git a/include/nfc/nfc.h b/include/nfc/nfc.h index 51d2b7b..6d4f475 100644 --- a/include/nfc/nfc.h +++ b/include/nfc/nfc.h @@ -85,7 +85,7 @@ extern "C" { /* 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, size_t *pszRx, int timeout); NFC_EXPORT int nfc_target_send_bytes (nfc_device *pnd, const uint8_t *pbtTx, const size_t szTx, int timeout); - NFC_EXPORT int nfc_target_receive_bytes (nfc_device *pnd, uint8_t *pbtRx, size_t *pszRx, int timeout); + NFC_EXPORT int nfc_target_receive_bytes (nfc_device *pnd, uint8_t *pbtRx, int timeout); NFC_EXPORT int nfc_target_send_bits (nfc_device *pnd, const uint8_t *pbtTx, const size_t szTxBits, const uint8_t *pbtTxPar); NFC_EXPORT int nfc_target_receive_bits (nfc_device *pnd, uint8_t *pbtRx, uint8_t *pbtRxPar); diff --git a/libnfc/chips/pn53x.c b/libnfc/chips/pn53x.c index dfbc6be..c6be8b8 100644 --- a/libnfc/chips/pn53x.c +++ b/libnfc/chips/pn53x.c @@ -1851,7 +1851,7 @@ pn53x_target_receive_bits (struct nfc_device *pnd, uint8_t *pbtRx, uint8_t *pbtR } int -pn53x_target_receive_bytes (struct nfc_device *pnd, uint8_t *pbtRx, size_t *pszRx, int timeout) +pn53x_target_receive_bytes (struct nfc_device *pnd, uint8_t *pbtRx, int timeout) { uint8_t abtCmd[1]; @@ -1890,13 +1890,13 @@ pn53x_target_receive_bytes (struct nfc_device *pnd, uint8_t *pbtRx, size_t *pszR return pnd->last_error; // Save the received bytes count - *pszRx = szRx - 1; + szRx -= 1; // Copy the received bytes - memcpy (pbtRx, abtRx + 1, *pszRx); + memcpy (pbtRx, abtRx + 1, szRx); // Everyting seems ok, return received bytes count - return *pszRx; + return szRx; } int diff --git a/libnfc/chips/pn53x.h b/libnfc/chips/pn53x.h index 7ea7731..66ccdc0 100644 --- a/libnfc/chips/pn53x.h +++ b/libnfc/chips/pn53x.h @@ -310,7 +310,7 @@ int pn53x_initiator_deselect_target (struct nfc_device *pnd); // NFC device as Target functions int pn53x_target_init (struct nfc_device *pnd, nfc_target *pnt, uint8_t *pbtRx, size_t *pszRx, int timeout); int pn53x_target_receive_bits (struct nfc_device *pnd, uint8_t *pbtRx, uint8_t *pbtRxPar); -int pn53x_target_receive_bytes (struct nfc_device *pnd, uint8_t *pbtRx, size_t *pszRx, int timeout); +int pn53x_target_receive_bytes (struct nfc_device *pnd, uint8_t *pbtRx, int timeout); int pn53x_target_send_bits (struct nfc_device *pnd, const uint8_t *pbtTx, const size_t szTxBits, const uint8_t *pbtTxPar); int pn53x_target_send_bytes (struct nfc_device *pnd, const uint8_t *pbtTx, const size_t szTx, int timeout); diff --git a/libnfc/nfc-emulation.c b/libnfc/nfc-emulation.c index 435363c..21db471 100644 --- a/libnfc/nfc-emulation.c +++ b/libnfc/nfc-emulation.c @@ -47,7 +47,7 @@ nfc_emulate_target (nfc_device *pnd, struct nfc_emulator *emulator) } } if (res >= 0) { - if (nfc_target_receive_bytes(pnd, abtRx, &szRx, 0) < 0) { + if ((int) ((szRx = (size_t) nfc_target_receive_bytes(pnd, abtRx, 0))) < 0) { return -1; } } diff --git a/libnfc/nfc-internal.h b/libnfc/nfc-internal.h index 91646ce..1de9216 100644 --- a/libnfc/nfc-internal.h +++ b/libnfc/nfc-internal.h @@ -144,7 +144,7 @@ struct nfc_driver_t { int (*target_init) (struct nfc_device *pnd, nfc_target * pnt, uint8_t * pbtRx, size_t * pszRx, int timeout); int (*target_send_bytes) (struct nfc_device *pnd, const uint8_t * pbtTx, const size_t szTx, int timeout); - int (*target_receive_bytes) (struct nfc_device *pnd, uint8_t * pbtRx, size_t * pszRx, int timeout); + int (*target_receive_bytes) (struct nfc_device *pnd, uint8_t * pbtRx, int timeout); int (*target_send_bits) (struct nfc_device *pnd, const uint8_t * pbtTx, const size_t szTxBits, const uint8_t * pbtTxPar); int (*target_receive_bits) (struct nfc_device *pnd, uint8_t * pbtRx, uint8_t * pbtRxPar); diff --git a/libnfc/nfc.c b/libnfc/nfc.c index d482715..b810803 100644 --- a/libnfc/nfc.c +++ b/libnfc/nfc.c @@ -719,7 +719,6 @@ nfc_target_send_bytes (nfc_device *pnd, const uint8_t *pbtTx, const size_t szTx, * * @param pnd \a nfc_device struct pointer that represent currently used device * @param[out] pbtRx pointer to Rx buffer - * @param[out] pszRx received byte count * @param timeout in milliseconds * * This function retrieves bytes frames (e.g. ADPU) sent by the \e initiator to the NFC device (configured as \e target). @@ -728,9 +727,9 @@ nfc_target_send_bytes (nfc_device *pnd, const uint8_t *pbtTx, const size_t szTx, * If timeout is a null pointer, the function blocks indefinitely (until an error is raised or function is completed). */ int -nfc_target_receive_bytes (nfc_device *pnd, uint8_t *pbtRx, size_t *pszRx, int timeout) +nfc_target_receive_bytes (nfc_device *pnd, uint8_t *pbtRx, int timeout) { - HAL (target_receive_bytes, pnd, pbtRx, pszRx, timeout); + HAL (target_receive_bytes, pnd, pbtRx, timeout); } /** diff --git a/test/test_dep_active.c b/test/test_dep_active.c index 85d992b..71235aa 100644 --- a/test/test_dep_active.c +++ b/test/test_dep_active.c @@ -85,11 +85,11 @@ target_thread (void *arg) uint8_t abtRx[1024]; size_t szRx = sizeof (abtRx); int res = nfc_target_init (device, &nt, abtRx, &szRx, 0); - cut_assert_equal_int (0, res, cut_message ("Can't initialize NFC device as target: %s", nfc_strerror (device))); + cut_assert_operator_int (res, >, 0, cut_message ("Can't initialize NFC device as target: %s", nfc_strerror (device))); if (res < 0) { thread_res = -1; return (void*) thread_res; } - res = nfc_target_receive_bytes (device, abtRx, &szRx, 500); - cut_assert_operator_int (res, >, 0, cut_message ("Can't receive bytes from initiator: %s", nfc_strerror (device))); + szRx = (size_t) nfc_target_receive_bytes (device, abtRx, 500); + cut_assert_operator_int (szRx, >, 0, cut_message ("Can't receive bytes from initiator: %s", nfc_strerror (device))); const uint8_t abtAttRx[] = "Hello DEP target!"; cut_assert_equal_memory (abtAttRx, sizeof (abtAttRx), abtRx, szRx, cut_message ("Invalid received data")); diff --git a/test/test_dep_passive.c b/test/test_dep_passive.c index 9db5151..a6af765 100644 --- a/test/test_dep_passive.c +++ b/test/test_dep_passive.c @@ -83,12 +83,12 @@ target_thread (void *arg) uint8_t abtRx[1024]; size_t szRx = sizeof (abtRx); int res = nfc_target_init (device, &nt, abtRx, &szRx, 0); - cut_assert_equal_int (0, res, cut_message ("Can't initialize NFC device as target: %s", nfc_strerror (device))); + cut_assert_operator_int (res, >, 0, cut_message ("Can't initialize NFC device as target: %s", nfc_strerror (device))); if (res < 0) { thread_res = -1; return (void*) thread_res; } // First pass - res = nfc_target_receive_bytes (device, abtRx, &szRx, 500); - cut_assert_operator_int (res, >, 0, cut_message ("Can't receive bytes from initiator: %s", nfc_strerror (device))); + szRx = (size_t) nfc_target_receive_bytes (device, abtRx, 500); + cut_assert_operator_int ((int) szRx, >, 0, cut_message ("Can't receive bytes from initiator: %s", nfc_strerror (device))); const uint8_t abtAttRx[] = "Hello DEP target!"; cut_assert_equal_memory (abtAttRx, sizeof (abtAttRx), abtRx, szRx, cut_message ("Invalid received data")); @@ -100,8 +100,8 @@ target_thread (void *arg) if (res <= 0) { thread_res = -1; return (void*) thread_res; } // Second pass - res = nfc_target_receive_bytes (device, abtRx, &szRx, 500); - cut_assert_operator_int (res, >, 0, cut_message ("Can't receive bytes from initiator: %s", nfc_strerror (device))); + szRx = (size_t) nfc_target_receive_bytes (device, abtRx, 500); + cut_assert_operator_int ((int) szRx, >, 0, cut_message ("Can't receive bytes from initiator: %s", nfc_strerror (device))); cut_assert_equal_memory (abtAttRx, sizeof (abtAttRx), abtRx, szRx, cut_message ("Invalid received data")); if (res <= 0) { thread_res = -1; return (void*) thread_res; } @@ -111,8 +111,8 @@ target_thread (void *arg) if (res <= 0) { thread_res = -1; return (void*) thread_res; } // Third pass - res = nfc_target_receive_bytes (device, abtRx, &szRx, 500); - cut_assert_operator_int (res, >, 0, cut_message ("Can't receive bytes from initiator: %s", nfc_strerror (device))); + szRx = (size_t) nfc_target_receive_bytes (device, abtRx, 500); + cut_assert_operator_int ((int) szRx, >, 0, cut_message ("Can't receive bytes from initiator: %s", nfc_strerror (device))); cut_assert_equal_memory (abtAttRx, sizeof (abtAttRx), abtRx, szRx, cut_message ("Invalid received data")); if (res <= 0) { thread_res = -1; return (void*) thread_res; } @@ -122,8 +122,8 @@ target_thread (void *arg) if (res <= 0) { thread_res = -1; return (void*) thread_res; } // Fourth pass - res = nfc_target_receive_bytes (device, abtRx, &szRx, 500); - cut_assert_operator_int (res, >, 0, cut_message ("Can't receive bytes from initiator: %s", nfc_strerror (device))); + szRx = (size_t) nfc_target_receive_bytes (device, abtRx, 500); + cut_assert_operator_int ((int)szRx, >, 0, cut_message ("Can't receive bytes from initiator: %s", nfc_strerror (device))); cut_assert_equal_memory (abtAttRx, sizeof (abtAttRx), abtRx, szRx, cut_message ("Invalid received data")); if (res <= 0) { thread_res = -1; return (void*) thread_res; } diff --git a/utils/nfc-relay-picc.c b/utils/nfc-relay-picc.c index 3c5caf5..d80e7ed 100644 --- a/utils/nfc-relay-picc.c +++ b/utils/nfc-relay-picc.c @@ -370,7 +370,7 @@ main (int argc, char *argv[]) bool ret; if (!initiator_only_mode) { // Receive external reader command through target - if (nfc_target_receive_bytes(pndTarget,abtCapdu,&szCapduLen, 0) < 0) { + if ((int) ((szCapduLen = (size_t) nfc_target_receive_bytes(pndTarget, abtCapdu, 0))) < 0) { nfc_perror (pndTarget, "nfc_target_receive_bytes"); if (!target_only_mode) { nfc_disconnect (pndInitiator); From 601105ef7984665fc6448f24cdcae638a8f77155 Mon Sep 17 00:00:00 2001 From: Audrey Diacre Date: Thu, 5 Jan 2012 17:03:38 +0000 Subject: [PATCH 079/113] fix bad cast done in last revision. --- examples/nfc-dep-target.c | 4 +++- examples/nfc-emulate-tag.c | 4 +++- libnfc/nfc-emulation.c | 2 +- test/test_dep_active.c | 6 +++--- test/test_dep_passive.c | 22 +++++++++++++--------- utils/nfc-relay-picc.c | 4 +++- 6 files changed, 26 insertions(+), 16 deletions(-) diff --git a/examples/nfc-dep-target.c b/examples/nfc-dep-target.c index 77a912f..82ce9f1 100644 --- a/examples/nfc-dep-target.c +++ b/examples/nfc-dep-target.c @@ -62,6 +62,7 @@ int main (int argc, const char *argv[]) { uint8_t abtRx[MAX_FRAME_LEN]; + int res = 0; size_t szRx = sizeof(abtRx); size_t szDeviceFound; uint8_t abtTx[] = "Hello Mars!"; @@ -125,10 +126,11 @@ main (int argc, const char *argv[]) } printf("Initiator request received. Waiting for data...\n"); - if (((int) (szRx = (size_t) nfc_target_receive_bytes (pnd, abtRx, 0))) < 0) { + if ((res = nfc_target_receive_bytes (pnd, abtRx, 0)) < 0) { nfc_perror(pnd, "nfc_target_receive_bytes"); goto error; } + szRx = (size_t) res; abtRx[szRx] = '\0'; printf ("Received: %s\n", abtRx); diff --git a/examples/nfc-emulate-tag.c b/examples/nfc-emulate-tag.c index 3604ab7..c83eecf 100644 --- a/examples/nfc-emulate-tag.c +++ b/examples/nfc-emulate-tag.c @@ -137,6 +137,7 @@ bool nfc_target_emulate_tag(nfc_device *pnd, nfc_target *pnt) { size_t szTx; + int res = 0; uint8_t abtTx[MAX_FRAME_LEN]; bool loop = true; @@ -158,10 +159,11 @@ nfc_target_emulate_tag(nfc_device *pnd, nfc_target *pnt) nfc_device_set_property_bool (pnd, NP_HANDLE_CRC, false); init_mfc_auth = false; } - if ((int) ((szRx = (size_t) nfc_target_receive_bytes(pnd, abtRx, 0))) < 0) { + if ((res = nfc_target_receive_bytes(pnd, abtRx, 0)) < 0) { nfc_perror (pnd, "nfc_target_receive_bytes"); return false; } + szRx = (size_t) res; } } return true; diff --git a/libnfc/nfc-emulation.c b/libnfc/nfc-emulation.c index 21db471..a5eabd7 100644 --- a/libnfc/nfc-emulation.c +++ b/libnfc/nfc-emulation.c @@ -47,7 +47,7 @@ nfc_emulate_target (nfc_device *pnd, struct nfc_emulator *emulator) } } if (res >= 0) { - if ((int) ((szRx = (size_t) nfc_target_receive_bytes(pnd, abtRx, 0))) < 0) { + if ((res = nfc_target_receive_bytes(pnd, abtRx, 0)) < 0) { return -1; } } diff --git a/test/test_dep_active.c b/test/test_dep_active.c index 71235aa..e77fce8 100644 --- a/test/test_dep_active.c +++ b/test/test_dep_active.c @@ -88,9 +88,9 @@ target_thread (void *arg) cut_assert_operator_int (res, >, 0, cut_message ("Can't initialize NFC device as target: %s", nfc_strerror (device))); if (res < 0) { thread_res = -1; return (void*) thread_res; } - szRx = (size_t) nfc_target_receive_bytes (device, abtRx, 500); - cut_assert_operator_int (szRx, >, 0, cut_message ("Can't receive bytes from initiator: %s", nfc_strerror (device))); - + res = nfc_target_receive_bytes (device, abtRx, 500); + cut_assert_operator_int (res, >, 0, cut_message ("Can't receive bytes from initiator: %s", nfc_strerror (device))); + szRx = (size_t) res; const uint8_t abtAttRx[] = "Hello DEP target!"; cut_assert_equal_memory (abtAttRx, sizeof (abtAttRx), abtRx, szRx, cut_message ("Invalid received data")); if (res <= 0) { thread_res = -1; return (void*) thread_res; } diff --git a/test/test_dep_passive.c b/test/test_dep_passive.c index a6af765..0b709da 100644 --- a/test/test_dep_passive.c +++ b/test/test_dep_passive.c @@ -87,9 +87,10 @@ target_thread (void *arg) if (res < 0) { thread_res = -1; return (void*) thread_res; } // First pass - szRx = (size_t) nfc_target_receive_bytes (device, abtRx, 500); - cut_assert_operator_int ((int) szRx, >, 0, cut_message ("Can't receive bytes from initiator: %s", nfc_strerror (device))); - + res = nfc_target_receive_bytes (device, abtRx, 500); + cut_assert_operator_int (res, >, 0, cut_message ("Can't receive bytes from initiator: %s", nfc_strerror (device))); + szRx = (size_t) res; + const uint8_t abtAttRx[] = "Hello DEP target!"; cut_assert_equal_memory (abtAttRx, sizeof (abtAttRx), abtRx, szRx, cut_message ("Invalid received data")); if (res <= 0) { thread_res = -1; return (void*) thread_res; } @@ -100,8 +101,9 @@ target_thread (void *arg) if (res <= 0) { thread_res = -1; return (void*) thread_res; } // Second pass - szRx = (size_t) nfc_target_receive_bytes (device, abtRx, 500); - cut_assert_operator_int ((int) szRx, >, 0, cut_message ("Can't receive bytes from initiator: %s", nfc_strerror (device))); + res = nfc_target_receive_bytes (device, abtRx, 500); + cut_assert_operator_int (res, >, 0, cut_message ("Can't receive bytes from initiator: %s", nfc_strerror (device))); + szRx = (size_t) res; cut_assert_equal_memory (abtAttRx, sizeof (abtAttRx), abtRx, szRx, cut_message ("Invalid received data")); if (res <= 0) { thread_res = -1; return (void*) thread_res; } @@ -111,8 +113,9 @@ target_thread (void *arg) if (res <= 0) { thread_res = -1; return (void*) thread_res; } // Third pass - szRx = (size_t) nfc_target_receive_bytes (device, abtRx, 500); - cut_assert_operator_int ((int) szRx, >, 0, cut_message ("Can't receive bytes from initiator: %s", nfc_strerror (device))); + res = nfc_target_receive_bytes (device, abtRx, 500); + cut_assert_operator_int (res, >, 0, cut_message ("Can't receive bytes from initiator: %s", nfc_strerror (device))); + szRx = (size_t) res; cut_assert_equal_memory (abtAttRx, sizeof (abtAttRx), abtRx, szRx, cut_message ("Invalid received data")); if (res <= 0) { thread_res = -1; return (void*) thread_res; } @@ -122,8 +125,9 @@ target_thread (void *arg) if (res <= 0) { thread_res = -1; return (void*) thread_res; } // Fourth pass - szRx = (size_t) nfc_target_receive_bytes (device, abtRx, 500); - cut_assert_operator_int ((int)szRx, >, 0, cut_message ("Can't receive bytes from initiator: %s", nfc_strerror (device))); + res = nfc_target_receive_bytes (device, abtRx, 500); + cut_assert_operator_int (res, >, 0, cut_message ("Can't receive bytes from initiator: %s", nfc_strerror (device))); + szRx = (size_t) res; cut_assert_equal_memory (abtAttRx, sizeof (abtAttRx), abtRx, szRx, cut_message ("Invalid received data")); if (res <= 0) { thread_res = -1; return (void*) thread_res; } diff --git a/utils/nfc-relay-picc.c b/utils/nfc-relay-picc.c index d80e7ed..002cf61 100644 --- a/utils/nfc-relay-picc.c +++ b/utils/nfc-relay-picc.c @@ -368,9 +368,10 @@ main (int argc, char *argv[]) while (!quitting) { bool ret; + int res = 0; if (!initiator_only_mode) { // Receive external reader command through target - if ((int) ((szCapduLen = (size_t) nfc_target_receive_bytes(pndTarget, abtCapdu, 0))) < 0) { + if ((res = nfc_target_receive_bytes(pndTarget, abtCapdu, 0)) < 0) { nfc_perror (pndTarget, "nfc_target_receive_bytes"); if (!target_only_mode) { nfc_disconnect (pndInitiator); @@ -378,6 +379,7 @@ main (int argc, char *argv[]) nfc_disconnect (pndTarget); exit(EXIT_FAILURE); } + szCapduLen = (size_t) res; if (target_only_mode) { if (print_hex_fd4(abtCapdu, szCapduLen, "C-APDU") != EXIT_SUCCESS) { fprintf (stderr, "Error while printing C-APDU to FD4\n"); From 22bea8d99b537b092d381e75fd634e1cf95099eb Mon Sep 17 00:00:00 2001 From: Romuald Conty Date: Thu, 5 Jan 2012 21:35:02 +0000 Subject: [PATCH 080/113] nfc_target_receive_*() need to know rx buffer size --- examples/nfc-dep-target.c | 2 +- examples/nfc-emulate-tag.c | 2 +- examples/nfc-emulate-uid.c | 2 +- examples/nfc-relay.c | 2 +- include/nfc/nfc.h | 4 ++-- libnfc/chips/pn53x.c | 11 +++++++---- libnfc/chips/pn53x.h | 4 ++-- libnfc/nfc-emulation.c | 2 +- libnfc/nfc-internal.h | 4 ++-- libnfc/nfc.c | 18 +++++++++++------- utils/nfc-relay-picc.c | 3 ++- 11 files changed, 31 insertions(+), 23 deletions(-) diff --git a/examples/nfc-dep-target.c b/examples/nfc-dep-target.c index 82ce9f1..9d7e73e 100644 --- a/examples/nfc-dep-target.c +++ b/examples/nfc-dep-target.c @@ -126,7 +126,7 @@ main (int argc, const char *argv[]) } printf("Initiator request received. Waiting for data...\n"); - if ((res = nfc_target_receive_bytes (pnd, abtRx, 0)) < 0) { + if ((res = nfc_target_receive_bytes (pnd, abtRx, sizeof (abtRx), 0)) < 0) { nfc_perror(pnd, "nfc_target_receive_bytes"); goto error; } diff --git a/examples/nfc-emulate-tag.c b/examples/nfc-emulate-tag.c index c83eecf..8345370 100644 --- a/examples/nfc-emulate-tag.c +++ b/examples/nfc-emulate-tag.c @@ -159,7 +159,7 @@ nfc_target_emulate_tag(nfc_device *pnd, nfc_target *pnt) nfc_device_set_property_bool (pnd, NP_HANDLE_CRC, false); init_mfc_auth = false; } - if ((res = nfc_target_receive_bytes(pnd, abtRx, 0)) < 0) { + if ((res = nfc_target_receive_bytes(pnd, abtRx, sizeof (abtRx), 0)) < 0) { nfc_perror (pnd, "nfc_target_receive_bytes"); return false; } diff --git a/examples/nfc-emulate-uid.c b/examples/nfc-emulate-uid.c index b8f0a39..159ad4e 100644 --- a/examples/nfc-emulate-uid.c +++ b/examples/nfc-emulate-uid.c @@ -172,7 +172,7 @@ main (int argc, char *argv[]) while (true) { // Test if we received a frame - if ((szRecvBits = nfc_target_receive_bits (pnd, abtRecv, NULL)) > 0) { + if ((szRecvBits = nfc_target_receive_bits (pnd, abtRecv, sizeof (abtRecv), 0)) > 0) { // Prepare the command to send back for the anti-collision request switch (szRecvBits) { case 7: // Request or Wakeup diff --git a/examples/nfc-relay.c b/examples/nfc-relay.c index c984765..6459259 100644 --- a/examples/nfc-relay.c +++ b/examples/nfc-relay.c @@ -179,7 +179,7 @@ main (int argc, char *argv[]) while (!quitting) { // Test if we received a frame from the reader - if ((szReaderRxBits = nfc_target_receive_bits (pndTag, abtReaderRx, abtReaderRxPar)) > 0) { + if ((szReaderRxBits = nfc_target_receive_bits (pndTag, abtReaderRx, sizeof (abtReaderRx), abtReaderRxPar)) > 0) { // Drop down the field before sending a REQA command and start a new session if (szReaderRxBits == 7 && abtReaderRx[0] == 0x26) { // Drop down field for a very short time (original tag will reboot) diff --git a/include/nfc/nfc.h b/include/nfc/nfc.h index 6d4f475..8dd9ad7 100644 --- a/include/nfc/nfc.h +++ b/include/nfc/nfc.h @@ -85,9 +85,9 @@ extern "C" { /* 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, size_t *pszRx, int timeout); NFC_EXPORT int nfc_target_send_bytes (nfc_device *pnd, const uint8_t *pbtTx, const size_t szTx, int timeout); - NFC_EXPORT int nfc_target_receive_bytes (nfc_device *pnd, uint8_t *pbtRx, int timeout); + NFC_EXPORT int nfc_target_receive_bytes (nfc_device *pnd, uint8_t *pbtRx, const size_t szRx, int timeout); NFC_EXPORT int nfc_target_send_bits (nfc_device *pnd, const uint8_t *pbtTx, const size_t szTxBits, const uint8_t *pbtTxPar); - NFC_EXPORT int nfc_target_receive_bits (nfc_device *pnd, uint8_t *pbtRx, uint8_t *pbtRxPar); + NFC_EXPORT int nfc_target_receive_bits (nfc_device *pnd, uint8_t *pbtRx, const size_t szRx, uint8_t *pbtRxPar); /* Error reporting */ NFC_EXPORT const char *nfc_strerror (const nfc_device *pnd); diff --git a/libnfc/chips/pn53x.c b/libnfc/chips/pn53x.c index c6be8b8..4475110 100644 --- a/libnfc/chips/pn53x.c +++ b/libnfc/chips/pn53x.c @@ -1811,7 +1811,7 @@ pn53x_target_init (struct nfc_device *pnd, nfc_target *pnt, uint8_t *pbtRx, size } int -pn53x_target_receive_bits (struct nfc_device *pnd, uint8_t *pbtRx, uint8_t *pbtRxPar) +pn53x_target_receive_bits (struct nfc_device *pnd, uint8_t *pbtRx, const size_t szRxLen, uint8_t *pbtRxPar) { size_t szRxBits = 0; uint8_t abtCmd[] = { TgGetInitiatorCommand }; @@ -1851,7 +1851,7 @@ pn53x_target_receive_bits (struct nfc_device *pnd, uint8_t *pbtRx, uint8_t *pbtR } int -pn53x_target_receive_bytes (struct nfc_device *pnd, uint8_t *pbtRx, int timeout) +pn53x_target_receive_bytes (struct nfc_device *pnd, uint8_t *pbtRx, const size_t szRxLen, int timeout) { uint8_t abtCmd[1]; @@ -1884,14 +1884,17 @@ pn53x_target_receive_bytes (struct nfc_device *pnd, uint8_t *pbtRx, int timeout) } // Try to gather a received frame from the reader - uint8_t abtRx[PN53x_EXTENDED_FRAME__DATA_MAX_LEN]; - size_t szRx = sizeof (abtRx); + uint8_t abtRx[PN53x_EXTENDED_FRAME__DATA_MAX_LEN]; + size_t szRx = sizeof (abtRx); if (pn53x_transceive (pnd, abtCmd, sizeof (abtCmd), abtRx, &szRx, timeout) < 0) return pnd->last_error; // Save the received bytes count szRx -= 1; + if (szRx > szRxLen) + return NFC_EOVFLOW; + // Copy the received bytes memcpy (pbtRx, abtRx + 1, szRx); diff --git a/libnfc/chips/pn53x.h b/libnfc/chips/pn53x.h index 66ccdc0..787ec38 100644 --- a/libnfc/chips/pn53x.h +++ b/libnfc/chips/pn53x.h @@ -309,8 +309,8 @@ int pn53x_initiator_deselect_target (struct nfc_device *pnd); // NFC device as Target functions int pn53x_target_init (struct nfc_device *pnd, nfc_target *pnt, uint8_t *pbtRx, size_t *pszRx, int timeout); -int pn53x_target_receive_bits (struct nfc_device *pnd, uint8_t *pbtRx, uint8_t *pbtRxPar); -int pn53x_target_receive_bytes (struct nfc_device *pnd, uint8_t *pbtRx, int timeout); +int pn53x_target_receive_bits (struct nfc_device *pnd, uint8_t *pbtRx, const size_t szRxLen, uint8_t *pbtRxPar); +int pn53x_target_receive_bytes (struct nfc_device *pnd, uint8_t *pbtRx, const size_t szRxLen, int timeout); int pn53x_target_send_bits (struct nfc_device *pnd, const uint8_t *pbtTx, const size_t szTxBits, const uint8_t *pbtTxPar); int pn53x_target_send_bytes (struct nfc_device *pnd, const uint8_t *pbtTx, const size_t szTx, int timeout); diff --git a/libnfc/nfc-emulation.c b/libnfc/nfc-emulation.c index a5eabd7..b83d01e 100644 --- a/libnfc/nfc-emulation.c +++ b/libnfc/nfc-emulation.c @@ -47,7 +47,7 @@ nfc_emulate_target (nfc_device *pnd, struct nfc_emulator *emulator) } } if (res >= 0) { - if ((res = nfc_target_receive_bytes(pnd, abtRx, 0)) < 0) { + if ((res = nfc_target_receive_bytes(pnd, abtRx, szRx, 0)) < 0) { return -1; } } diff --git a/libnfc/nfc-internal.h b/libnfc/nfc-internal.h index 1de9216..f5fbddd 100644 --- a/libnfc/nfc-internal.h +++ b/libnfc/nfc-internal.h @@ -144,9 +144,9 @@ struct nfc_driver_t { int (*target_init) (struct nfc_device *pnd, nfc_target * pnt, uint8_t * pbtRx, size_t * pszRx, int timeout); int (*target_send_bytes) (struct nfc_device *pnd, const uint8_t * pbtTx, const size_t szTx, int timeout); - int (*target_receive_bytes) (struct nfc_device *pnd, uint8_t * pbtRx, int timeout); + int (*target_receive_bytes) (struct nfc_device *pnd, uint8_t * pbtRx, const size_t szRxLen, int timeout); int (*target_send_bits) (struct nfc_device *pnd, const uint8_t * pbtTx, const size_t szTxBits, const uint8_t * pbtTxPar); - int (*target_receive_bits) (struct nfc_device *pnd, uint8_t * pbtRx, uint8_t * pbtRxPar); + int (*target_receive_bits) (struct nfc_device *pnd, uint8_t * pbtRx, const size_t szRxLen, uint8_t * pbtRxPar); int (*device_set_property_bool) (struct nfc_device *pnd, const nfc_property property, const bool bEnable); int (*device_set_property_int) (struct nfc_device *pnd, const nfc_property property, const int value); diff --git a/libnfc/nfc.c b/libnfc/nfc.c index b810803..7ed26d1 100644 --- a/libnfc/nfc.c +++ b/libnfc/nfc.c @@ -718,18 +718,19 @@ nfc_target_send_bytes (nfc_device *pnd, const uint8_t *pbtTx, const size_t szTx, * @return Returns received bytes count on success, otherwise returns libnfc's error code * * @param pnd \a nfc_device struct pointer that represent currently used device - * @param[out] pbtRx pointer to Rx buffer + * @param pbtRx pointer to Rx buffer + * @param szRx size of Rx buffer * @param timeout in milliseconds * * This function retrieves bytes frames (e.g. ADPU) sent by the \e initiator to the NFC device (configured as \e target). * - * If timeout is not a null pointer, it specifies the maximum interval to wait for the function to be executed. - * If timeout is a null pointer, the function blocks indefinitely (until an error is raised or function is completed). + * If timeout equals to 0, the function blocks indefinitely (until an error is raised or function is completed) + * If timeout equals to -1, the default timeout will be used */ int -nfc_target_receive_bytes (nfc_device *pnd, uint8_t *pbtRx, int timeout) +nfc_target_receive_bytes (nfc_device *pnd, uint8_t *pbtRx, const size_t szRx, int timeout) { - HAL (target_receive_bytes, pnd, pbtRx, timeout); + HAL (target_receive_bytes, pnd, pbtRx, szRx, timeout); } /** @@ -749,6 +750,9 @@ nfc_target_send_bits (nfc_device *pnd, const uint8_t *pbtTx, const size_t szTxBi * @brief Receive bit-frames * @return Returns received bits count on success, otherwise returns libnfc's error code * + * @param pbtRx + * @param szRx + * * This function makes it possible to receive (raw) bit-frames. It returns all * the messages that are stored in the FIFO buffer of the \e PN53x chip. It * does not require to send any frame and thereby could be used to snoop frames @@ -757,9 +761,9 @@ nfc_target_send_bits (nfc_device *pnd, const uint8_t *pbtTx, const size_t szTxBi * frames. */ int -nfc_target_receive_bits (nfc_device *pnd, uint8_t *pbtRx, uint8_t *pbtRxPar) +nfc_target_receive_bits (nfc_device *pnd, uint8_t *pbtRx, const size_t szRx, uint8_t *pbtRxPar) { - HAL (target_receive_bits, pnd, pbtRx, pbtRxPar); + HAL (target_receive_bits, pnd, pbtRx, szRx, pbtRxPar); } /** diff --git a/utils/nfc-relay-picc.c b/utils/nfc-relay-picc.c index 002cf61..4f0205a 100644 --- a/utils/nfc-relay-picc.c +++ b/utils/nfc-relay-picc.c @@ -354,6 +354,7 @@ main (int argc, char *argv[]) printf ("Connected to the NFC emulator device: %s\n", nfc_device_get_name (pndTarget)); + szCapduLen = sizeof (abtCapdu); if (nfc_target_init (pndTarget, &ntEmulatedTarget, abtCapdu, &szCapduLen, 0) < 0) { ERR ("%s", "Initialization of NFC emulator failed"); if (!target_only_mode) { @@ -371,7 +372,7 @@ main (int argc, char *argv[]) int res = 0; if (!initiator_only_mode) { // Receive external reader command through target - if ((res = nfc_target_receive_bytes(pndTarget, abtCapdu, 0)) < 0) { + if ((res = nfc_target_receive_bytes(pndTarget, abtCapdu, sizeof (abtCapdu), 0)) < 0) { nfc_perror (pndTarget, "nfc_target_receive_bytes"); if (!target_only_mode) { nfc_disconnect (pndInitiator); From 3b8598dde82a294bdd2b5b646de728e6581e5984 Mon Sep 17 00:00:00 2001 From: Audrey Diacre Date: Fri, 6 Jan 2012 08:36:24 +0000 Subject: [PATCH 081/113] libnfc/test: fix some forgotten modifications due to the last revision. --- test/test_dep_active.c | 2 +- test/test_dep_passive.c | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/test/test_dep_active.c b/test/test_dep_active.c index e77fce8..7fe577a 100644 --- a/test/test_dep_active.c +++ b/test/test_dep_active.c @@ -88,7 +88,7 @@ target_thread (void *arg) cut_assert_operator_int (res, >, 0, cut_message ("Can't initialize NFC device as target: %s", nfc_strerror (device))); if (res < 0) { thread_res = -1; return (void*) thread_res; } - res = nfc_target_receive_bytes (device, abtRx, 500); + res = nfc_target_receive_bytes (device, abtRx, sizeof (abtRx), 500); cut_assert_operator_int (res, >, 0, cut_message ("Can't receive bytes from initiator: %s", nfc_strerror (device))); szRx = (size_t) res; const uint8_t abtAttRx[] = "Hello DEP target!"; diff --git a/test/test_dep_passive.c b/test/test_dep_passive.c index 0b709da..c7d9932 100644 --- a/test/test_dep_passive.c +++ b/test/test_dep_passive.c @@ -87,7 +87,7 @@ target_thread (void *arg) if (res < 0) { thread_res = -1; return (void*) thread_res; } // First pass - res = nfc_target_receive_bytes (device, abtRx, 500); + res = nfc_target_receive_bytes (device, abtRx, sizeof (abtRx), 500); cut_assert_operator_int (res, >, 0, cut_message ("Can't receive bytes from initiator: %s", nfc_strerror (device))); szRx = (size_t) res; @@ -101,7 +101,7 @@ target_thread (void *arg) if (res <= 0) { thread_res = -1; return (void*) thread_res; } // Second pass - res = nfc_target_receive_bytes (device, abtRx, 500); + res = nfc_target_receive_bytes (device, abtRx, sizeof (abtRx), 500); cut_assert_operator_int (res, >, 0, cut_message ("Can't receive bytes from initiator: %s", nfc_strerror (device))); szRx = (size_t) res; @@ -113,7 +113,7 @@ target_thread (void *arg) if (res <= 0) { thread_res = -1; return (void*) thread_res; } // Third pass - res = nfc_target_receive_bytes (device, abtRx, 500); + res = nfc_target_receive_bytes (device, abtRx, sizeof (abtRx), 500); cut_assert_operator_int (res, >, 0, cut_message ("Can't receive bytes from initiator: %s", nfc_strerror (device))); szRx = (size_t) res; @@ -125,7 +125,7 @@ target_thread (void *arg) if (res <= 0) { thread_res = -1; return (void*) thread_res; } // Fourth pass - res = nfc_target_receive_bytes (device, abtRx, 500); + res = nfc_target_receive_bytes (device, abtRx, sizeof (abtRx), 500); cut_assert_operator_int (res, >, 0, cut_message ("Can't receive bytes from initiator: %s", nfc_strerror (device))); szRx = (size_t) res; From b41edfb0b4779a25a4ff76cb03126cc575782812 Mon Sep 17 00:00:00 2001 From: Audrey Diacre Date: Fri, 6 Jan 2012 09:20:55 +0000 Subject: [PATCH 082/113] rename ui8LastCommand by lastCommand. --- libnfc/chips/pn53x.c | 2 +- libnfc/chips/pn53x.h | 2 +- libnfc/drivers/arygon.c | 4 ++-- libnfc/drivers/pn532_uart.c | 4 ++-- libnfc/drivers/pn53x_usb.c | 4 ++-- 5 files changed, 8 insertions(+), 8 deletions(-) diff --git a/libnfc/chips/pn53x.c b/libnfc/chips/pn53x.c index 4475110..5e10fbb 100644 --- a/libnfc/chips/pn53x.c +++ b/libnfc/chips/pn53x.c @@ -139,7 +139,7 @@ pn53x_transceive (struct nfc_device *pnd, const uint8_t *pbtTx, const size_t szT } // Command is sent, we store the command - CHIP_DATA (pnd)->ui8LastCommand = pbtTx[0]; + CHIP_DATA (pnd)->lastCommand = pbtTx[0]; // Handle power mode for PN532 if ((CHIP_DATA (pnd)->type == PN532) && (TgInitAsTarget == pbtTx[0])) { // PN532 automatically goes into PowerDown mode when TgInitAsTarget command will be sent diff --git a/libnfc/chips/pn53x.h b/libnfc/chips/pn53x.h index 787ec38..95fe620 100644 --- a/libnfc/chips/pn53x.h +++ b/libnfc/chips/pn53x.h @@ -153,7 +153,7 @@ struct pn53x_data { /** Register cache for SetParameters function. */ uint8_t ui8Parameters; /** Last sent command */ - uint8_t ui8LastCommand; + uint8_t lastCommand; /** Interframe timer correction */ int16_t timer_correction; /** Timer prescaler */ diff --git a/libnfc/drivers/arygon.c b/libnfc/drivers/arygon.c index 40306eb..7220fef 100644 --- a/libnfc/drivers/arygon.c +++ b/libnfc/drivers/arygon.c @@ -442,7 +442,7 @@ arygon_tama_receive (nfc_device *pnd, uint8_t *pbtData, const size_t szDataLen, return pnd->last_error; } - if (abtRxBuf[1] != CHIP_DATA (pnd)->ui8LastCommand + 1) { + if (abtRxBuf[1] != CHIP_DATA (pnd)->lastCommand + 1) { log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Command Code verification failed"); pnd->last_error = NFC_EIO; return pnd->last_error; @@ -463,7 +463,7 @@ arygon_tama_receive (nfc_device *pnd, uint8_t *pbtData, const size_t szDataLen, } uint8_t btDCS = (256 - 0xD5); - btDCS -= CHIP_DATA (pnd)->ui8LastCommand + 1; + btDCS -= CHIP_DATA (pnd)->lastCommand + 1; for (size_t szPos = 0; szPos < len; szPos++) { btDCS -= pbtData[szPos]; } diff --git a/libnfc/drivers/pn532_uart.c b/libnfc/drivers/pn532_uart.c index 9bd7e89..75c1d31 100644 --- a/libnfc/drivers/pn532_uart.c +++ b/libnfc/drivers/pn532_uart.c @@ -428,7 +428,7 @@ pn532_uart_receive (nfc_device *pnd, uint8_t *pbtData, const size_t szDataLen, i return pnd->last_error; } - if (abtRxBuf[1] != CHIP_DATA (pnd)->ui8LastCommand + 1) { + if (abtRxBuf[1] != CHIP_DATA (pnd)->lastCommand + 1) { log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Command Code verification failed"); pnd->last_error = NFC_EIO; return pnd->last_error; @@ -449,7 +449,7 @@ pn532_uart_receive (nfc_device *pnd, uint8_t *pbtData, const size_t szDataLen, i } uint8_t btDCS = (256 - 0xD5); - btDCS -= CHIP_DATA (pnd)->ui8LastCommand + 1; + btDCS -= CHIP_DATA (pnd)->lastCommand + 1; for (size_t szPos = 0; szPos < len; szPos++) { btDCS -= pbtData[szPos]; } diff --git a/libnfc/drivers/pn53x_usb.c b/libnfc/drivers/pn53x_usb.c index 1ab9ee7..e03e31a 100644 --- a/libnfc/drivers/pn53x_usb.c +++ b/libnfc/drivers/pn53x_usb.c @@ -655,7 +655,7 @@ read: } offset += 1; - if (abtRxBuf[offset] != CHIP_DATA (pnd)->ui8LastCommand + 1) { + if (abtRxBuf[offset] != CHIP_DATA (pnd)->lastCommand + 1) { log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Command Code verification failed"); pnd->last_error = NFC_EIO; return pnd->last_error; @@ -666,7 +666,7 @@ read: offset += len; uint8_t btDCS = (256 - 0xD5); - btDCS -= CHIP_DATA (pnd)->ui8LastCommand + 1; + btDCS -= CHIP_DATA (pnd)->lastCommand + 1; for (size_t szPos = 0; szPos < len; szPos++) { btDCS -= pbtData[szPos]; } From 48e92149e41e59ff11ea3a0546125ba9b72717f3 Mon Sep 17 00:00:00 2001 From: Romuald Conty Date: Fri, 6 Jan 2012 13:05:10 +0000 Subject: [PATCH 083/113] drop log4c support --- configure.ac | 11 ------- examples/Makefile.am | 5 --- libnfc/Makefile.am | 11 ------- libnfc/buses/Makefile.am | 4 --- libnfc/chips/Makefile.am | 6 ---- libnfc/drivers/Makefile.am | 5 --- libnfc/log-log4c.c | 66 -------------------------------------- libnfc/log.h | 22 ++----------- utils/Makefile.am | 5 --- 9 files changed, 2 insertions(+), 133 deletions(-) delete mode 100644 libnfc/log-log4c.c diff --git a/configure.ac b/configure.ac index d2c5a10..fddb523 100644 --- a/configure.ac +++ b/configure.ac @@ -69,17 +69,6 @@ AC_TYPE_SIGNAL LIBNFC_CFLAGS='-I$(top_srcdir)/libnfc -I$(top_builddir)/include -I$(top_srcdir)/include' AC_SUBST(LIBNFC_CFLAGS) -# Checks for log4c -AC_PATH_PROG([LOG4C_CONFIG], [log4c-config]) -if test x"$LOG4C_CONFIG" != x""; then - log4c_CFLAGS=`$LOG4C_CONFIG --cflags` - log4c_LIBS=`$LOG4C_CONFIG --libs` - AC_SUBST([log4c_CFLAGS]) - AC_SUBST([log4c_LIBS]) - AC_DEFINE([HAS_LOG4C], [1], [Define to 1 if log4c is available.]) -fi -AM_CONDITIONAL(HAS_LOG4C, [test x"$LOG4C_CONFIG" != x""]) - # Debug support (default:no) AC_ARG_ENABLE([debug],AS_HELP_STRING([--enable-debug],[Enable debug output]),[enable_debug=$enableval],[enable_debug="no"]) diff --git a/examples/Makefile.am b/examples/Makefile.am index c0eda51..e0a93ff 100644 --- a/examples/Makefile.am +++ b/examples/Makefile.am @@ -87,9 +87,4 @@ dist_man_MANS = \ pn53x-sam.1 \ pn53x-tamashell.1 -if HAS_LOG4C - AM_CFLAGS += @log4c_CFLAGS@ - LIBADD = @log4c_LIBS@ -endif - EXTRA_DIST = CMakeLists.txt diff --git a/libnfc/Makefile.am b/libnfc/Makefile.am index a48d818..634a9e9 100644 --- a/libnfc/Makefile.am +++ b/libnfc/Makefile.am @@ -35,22 +35,11 @@ if LIBUSB_ENABLED libnfc_la_LIBADD += @libusb_LIBS@ endif -if HAS_LOG4C - libnfc_la_CFLAGS += @log4c_CFLAGS@ - libnfc_la_LIBADD += @log4c_LIBS@ - - libnfc_la_SOURCES += log-log4c.c -else if WITH_DEBUG - libnfc_la_CFLAGS += @log4c_CFLAGS@ - libnfc_la_LIBADD += @log4c_LIBS@ - libnfc_la_SOURCES += log-printf.c endif -endif EXTRA_DIST = \ CMakeLists.txt \ - log-log4c.c \ log-printf.c diff --git a/libnfc/buses/Makefile.am b/libnfc/buses/Makefile.am index f3475c4..e28ac91 100644 --- a/libnfc/buses/Makefile.am +++ b/libnfc/buses/Makefile.am @@ -9,7 +9,3 @@ libnfcbuses_la_CFLAGS = -I$(top_srcdir)/libnfc EXTRA_DIST = uart_posix.c uart_win32.c -if HAS_LOG4C - libnfcbuses_la_CFLAGS += @log4c_CFLAGS@ - libnfcbuses_la_LIBADD = @log4c_LIBS@ -endif diff --git a/libnfc/chips/Makefile.am b/libnfc/chips/Makefile.am index a8ad491..2359e78 100644 --- a/libnfc/chips/Makefile.am +++ b/libnfc/chips/Makefile.am @@ -7,9 +7,3 @@ noinst_LTLIBRARIES = libnfcchips.la libnfcchips_la_SOURCES = pn53x.c libnfcchips_la_CFLAGS = -I$(top_srcdir)/libnfc - -if HAS_LOG4C - libnfcchips_la_CFLAGS += @log4c_CFLAGS@ - libnfcchips_la_LIBADD = @log4c_LIBS@ -endif - diff --git a/libnfc/drivers/Makefile.am b/libnfc/drivers/Makefile.am index 5ea129e..0ac2250 100644 --- a/libnfc/drivers/Makefile.am +++ b/libnfc/drivers/Makefile.am @@ -34,8 +34,3 @@ if LIBUSB_ENABLED libnfcdrivers_la_LIBADD += @libusb_LIBS@ endif -if HAS_LOG4C - libnfcdrivers_la_CFLAGS += @log4c_CFLAGS@ - libnfcdrivers_la_LIBADD += @log4c_LIBS@ -endif - diff --git a/libnfc/log-log4c.c b/libnfc/log-log4c.c deleted file mode 100644 index ef0cf2e..0000000 --- a/libnfc/log-log4c.c +++ /dev/null @@ -1,66 +0,0 @@ -/*- - * Copyright (C) 2011, Romain Tartière, Romuald Conty - * - * This program is free software: you can redistribute it and/or modify it - * under the terms of the GNU Lesser General Public License as published by the - * Free Software Foundation, either version 3 of the License, or (at your - * option) any later version. - * - * This program is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for - * more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program. If not, see - */ - -#include "config.h" - -#include -#include - -#include "log.h" - -static uint8_t __log_init_counter = 0; - -int -log_init (void) -{ - int res = 0; - - if (__log_init_counter == 0) { - res = log4c_init (); - } - if (!res) { - __log_init_counter++; - } - return res; -} - -int -log_fini (void) -{ - int res = 0; - if (__log_init_counter >= 1) { - if (__log_init_counter == 1) { - res = log4c_fini (); - } - __log_init_counter--; - } else { - res = -1; - } - return res; -} - -void -log_put (char *category, int priority, char *format, ...) -{ - const log4c_category_t *cat = log4c_category_get (category); - if (log4c_category_is_priority_enabled (cat, priority)) { - va_list va; - va_start (va, format); - log4c_category_vlog (cat, priority, format, va); -// va_end (va); - } -} diff --git a/libnfc/log.h b/libnfc/log.h index 9d8b543..6a41a07 100644 --- a/libnfc/log.h +++ b/libnfc/log.h @@ -22,26 +22,8 @@ # include "config.h" #endif // HAVE_CONFIG_H -#if defined(HAS_LOG4C) && HAS_LOG4C - // log4c have been detected so we use it.. - #include - #define LOGGING 1 - - int log_init (void); - int log_fini (void); - void log_put (char *category, int priority, char *format, ...); - - #define NFC_PRIORITY_FATAL LOG4C_PRIORITY_FATAL - #define NFC_PRIORITY_ALERT LOG4C_PRIORITY_ALERT - #define NFC_PRIORITY_CRIT LOG4C_PRIORITY_CRIT - #define NFC_PRIORITY_ERROR LOG4C_PRIORITY_ERROR - #define NFC_PRIORITY_WARN LOG4C_PRIORITY_WARN - #define NFC_PRIORITY_NOTICE LOG4C_PRIORITY_NOTICE - #define NFC_PRIORITY_INFO LOG4C_PRIORITY_INFO - #define NFC_PRIORITY_DEBUG LOG4C_PRIORITY_DEBUG - #define NFC_PRIORITY_TRACE LOG4C_PRIORITY_TRACE -#elif defined DEBUG - // log4c is not detected but user want debug features +#if defined DEBUG + // User want debug features #define LOGGING 1 int log_init (void); int log_fini (void); diff --git a/utils/Makefile.am b/utils/Makefile.am index 49ec624..78c2dc2 100644 --- a/utils/Makefile.am +++ b/utils/Makefile.am @@ -52,9 +52,4 @@ dist_man_MANS = \ nfc-mfultralight.1 \ nfc-relay-picc.1 -if HAS_LOG4C - AM_CFLAGS += @log4c_CFLAGS@ - LIBADD = @log4c_LIBS@ -endif - EXTRA_DIST = CMakeLists.txt From e15f2eedbb64f321c43f5045934299075e3b9581 Mon Sep 17 00:00:00 2001 From: Audrey Diacre Date: Fri, 6 Jan 2012 13:07:37 +0000 Subject: [PATCH 084/113] rename lastCommand by last_command. --- libnfc/chips/pn53x.c | 2 +- libnfc/chips/pn53x.h | 2 +- libnfc/drivers/arygon.c | 4 ++-- libnfc/drivers/pn532_uart.c | 4 ++-- libnfc/drivers/pn53x_usb.c | 4 ++-- 5 files changed, 8 insertions(+), 8 deletions(-) diff --git a/libnfc/chips/pn53x.c b/libnfc/chips/pn53x.c index 5e10fbb..805265e 100644 --- a/libnfc/chips/pn53x.c +++ b/libnfc/chips/pn53x.c @@ -139,7 +139,7 @@ pn53x_transceive (struct nfc_device *pnd, const uint8_t *pbtTx, const size_t szT } // Command is sent, we store the command - CHIP_DATA (pnd)->lastCommand = pbtTx[0]; + CHIP_DATA (pnd)->last_command = pbtTx[0]; // Handle power mode for PN532 if ((CHIP_DATA (pnd)->type == PN532) && (TgInitAsTarget == pbtTx[0])) { // PN532 automatically goes into PowerDown mode when TgInitAsTarget command will be sent diff --git a/libnfc/chips/pn53x.h b/libnfc/chips/pn53x.h index 95fe620..fc4cea7 100644 --- a/libnfc/chips/pn53x.h +++ b/libnfc/chips/pn53x.h @@ -153,7 +153,7 @@ struct pn53x_data { /** Register cache for SetParameters function. */ uint8_t ui8Parameters; /** Last sent command */ - uint8_t lastCommand; + uint8_t last_command; /** Interframe timer correction */ int16_t timer_correction; /** Timer prescaler */ diff --git a/libnfc/drivers/arygon.c b/libnfc/drivers/arygon.c index 7220fef..45eec13 100644 --- a/libnfc/drivers/arygon.c +++ b/libnfc/drivers/arygon.c @@ -442,7 +442,7 @@ arygon_tama_receive (nfc_device *pnd, uint8_t *pbtData, const size_t szDataLen, return pnd->last_error; } - if (abtRxBuf[1] != CHIP_DATA (pnd)->lastCommand + 1) { + if (abtRxBuf[1] != CHIP_DATA (pnd)->last_command + 1) { log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Command Code verification failed"); pnd->last_error = NFC_EIO; return pnd->last_error; @@ -463,7 +463,7 @@ arygon_tama_receive (nfc_device *pnd, uint8_t *pbtData, const size_t szDataLen, } uint8_t btDCS = (256 - 0xD5); - btDCS -= CHIP_DATA (pnd)->lastCommand + 1; + btDCS -= CHIP_DATA (pnd)->last_command + 1; for (size_t szPos = 0; szPos < len; szPos++) { btDCS -= pbtData[szPos]; } diff --git a/libnfc/drivers/pn532_uart.c b/libnfc/drivers/pn532_uart.c index 75c1d31..87648c8 100644 --- a/libnfc/drivers/pn532_uart.c +++ b/libnfc/drivers/pn532_uart.c @@ -428,7 +428,7 @@ pn532_uart_receive (nfc_device *pnd, uint8_t *pbtData, const size_t szDataLen, i return pnd->last_error; } - if (abtRxBuf[1] != CHIP_DATA (pnd)->lastCommand + 1) { + if (abtRxBuf[1] != CHIP_DATA (pnd)->last_command + 1) { log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Command Code verification failed"); pnd->last_error = NFC_EIO; return pnd->last_error; @@ -449,7 +449,7 @@ pn532_uart_receive (nfc_device *pnd, uint8_t *pbtData, const size_t szDataLen, i } uint8_t btDCS = (256 - 0xD5); - btDCS -= CHIP_DATA (pnd)->lastCommand + 1; + btDCS -= CHIP_DATA (pnd)->last_command + 1; for (size_t szPos = 0; szPos < len; szPos++) { btDCS -= pbtData[szPos]; } diff --git a/libnfc/drivers/pn53x_usb.c b/libnfc/drivers/pn53x_usb.c index e03e31a..205b670 100644 --- a/libnfc/drivers/pn53x_usb.c +++ b/libnfc/drivers/pn53x_usb.c @@ -655,7 +655,7 @@ read: } offset += 1; - if (abtRxBuf[offset] != CHIP_DATA (pnd)->lastCommand + 1) { + if (abtRxBuf[offset] != CHIP_DATA (pnd)->last_command + 1) { log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Command Code verification failed"); pnd->last_error = NFC_EIO; return pnd->last_error; @@ -666,7 +666,7 @@ read: offset += len; uint8_t btDCS = (256 - 0xD5); - btDCS -= CHIP_DATA (pnd)->lastCommand + 1; + btDCS -= CHIP_DATA (pnd)->last_command + 1; for (size_t szPos = 0; szPos < len; szPos++) { btDCS -= pbtData[szPos]; } From 7df3bb5aeb41f07cee1584ce01e9743645eb5639 Mon Sep 17 00:00:00 2001 From: Romuald Conty Date: Fri, 6 Jan 2012 13:08:16 +0000 Subject: [PATCH 085/113] various minor fixes/enhancements --- examples/nfc-poll.c | 2 +- libnfc/chips/pn53x.c | 9 ++++++--- utils/nfc-utils.c | 2 +- 3 files changed, 8 insertions(+), 5 deletions(-) diff --git a/examples/nfc-poll.c b/examples/nfc-poll.c index 2392f28..88c51c7 100644 --- a/examples/nfc-poll.c +++ b/examples/nfc-poll.c @@ -118,7 +118,7 @@ main (int argc, const char *argv[]) printf ("Connected to NFC reader: %s\n", nfc_device_get_name (pnd)); printf ("NFC device will poll during %ld ms (%u pollings of %lu ms for %zd modulations)\n", (unsigned long) uiPollNr * szModulations * uiPeriod * 150, uiPollNr, (unsigned long) uiPeriod * 150, szModulations); if ((res = nfc_initiator_poll_target (pnd, nmModulations, szModulations, uiPollNr, uiPeriod, &nt)) < 0) { - nfc_perror (pnd, "nfc_initiator_poll_targets"); + nfc_perror (pnd, "nfc_initiator_poll_target"); nfc_disconnect (pnd); exit (EXIT_FAILURE); } diff --git a/libnfc/chips/pn53x.c b/libnfc/chips/pn53x.c index 805265e..e5bd058 100644 --- a/libnfc/chips/pn53x.c +++ b/libnfc/chips/pn53x.c @@ -796,7 +796,7 @@ pn53x_set_property_bool (struct nfc_device *pnd, const nfc_property property, co // to "gain" a sort-of hardware polling (ie. like PN532 does) if (pn53x_RFConfiguration__MaxRetries (pnd, (bEnable) ? 0xff : 0x00, // MxRtyATR, default: active = 0xff, passive = 0x02 - (bEnable) ? 0xff : 0x00, // MxRtyPSL, default: 0x01 + (bEnable) ? 0xff : 0x01, // MxRtyPSL, default: 0x01 (bEnable) ? 0xff : 0x02 // MxRtyPassiveActivation, default: 0xff (0x00 leads to problems with PN531) ) == 0) return NFC_SUCCESS; @@ -1138,13 +1138,13 @@ pn53x_initiator_poll_target (struct nfc_device *pnd, } int -pn53x_initiator_select_dep_target(struct nfc_device *pnd, +pn53x_initiator_select_dep_target (struct nfc_device *pnd, const nfc_dep_mode ndm, const nfc_baud_rate nbr, const nfc_dep_info *pndiInitiator, nfc_target *pnt, const int timeout) { - const uint8_t abtPassiveInitiatorData[] = { 0x00, 0xff, 0xff, 0x00, 0x00 }; // Only for 212/424 kpbs: First 4 bytes shall be set like this according to NFCIP-1, last byte is TSN (Time Slot Number) + const uint8_t abtPassiveInitiatorData[] = { 0x00, 0xff, 0xff, 0x00, 0x0f }; // Only for 212/424 kpbs: First 4 bytes shall be set like this according to NFCIP-1, last byte is TSN (Time Slot Number) const uint8_t * pbtPassiveInitiatorData = NULL; switch (nbr) { @@ -1843,6 +1843,9 @@ pn53x_target_receive_bits (struct nfc_device *pnd, uint8_t *pbtRx, const size_t } else { // Save the received bits szRxBits = szFrameBits; + + if ((szRx - 1) > szRxLen) + return NFC_EOVFLOW; // Copy the received bytes memcpy (pbtRx, abtRx + 1, szRx - 1); } diff --git a/utils/nfc-utils.c b/utils/nfc-utils.c index b0e81d5..2c9ace7 100644 --- a/utils/nfc-utils.c +++ b/utils/nfc-utils.c @@ -726,7 +726,7 @@ print_nfc_target (const nfc_target nt, bool verbose) print_nfc_iso14443b2ct_info (nt.nti.nci, verbose); break; case NMT_DEP: - printf ("D.E.P. (%s) target:\n", str_nfc_baud_rate(nt.nm.nbr)); + printf ("D.E.P. (%s, %s) target:\n", str_nfc_baud_rate(nt.nm.nbr), (nt.nti.ndi.ndm == NDM_ACTIVE)? "active mode" : "passive mode"); print_nfc_dep_info (nt.nti.ndi, verbose); break; } From c10b473361ac42e8e7d00f7dcbfcf34672abf2c5 Mon Sep 17 00:00:00 2001 From: Audrey Diacre Date: Mon, 9 Jan 2012 10:24:00 +0000 Subject: [PATCH 086/113] rx buffer size parameter of pn53x_transceive() function is now a const size_t. --- examples/pn53x-diagnose.c | 15 +++--- examples/pn53x-tamashell.c | 4 +- libnfc/chips/pn53x.c | 102 ++++++++++++++++++++----------------- libnfc/chips/pn53x.h | 2 +- libnfc/drivers/pn53x_usb.c | 4 +- test/test_dep_active.c | 2 +- test/test_dep_passive.c | 8 +-- 7 files changed, 75 insertions(+), 62 deletions(-) diff --git a/examples/pn53x-diagnose.c b/examples/pn53x-diagnose.c index d12d6c4..49e9c3e 100644 --- a/examples/pn53x-diagnose.c +++ b/examples/pn53x-diagnose.c @@ -89,8 +89,9 @@ main (int argc, const char *argv[]) printf ("NFC device [%s] connected.\n", nfc_device_get_name (pnd)); - res = pn53x_transceive (pnd, pncmd_diagnose_communication_line_test, sizeof (pncmd_diagnose_communication_line_test), abtRx, &szRx, 0); - if (res == 0) { + res = pn53x_transceive (pnd, pncmd_diagnose_communication_line_test, sizeof (pncmd_diagnose_communication_line_test), abtRx, szRx, 0); + if (res > 0) { + szRx = (size_t) res; // Result of Diagnose ping for RC-S360 doesn't contain status byte so we've to handle both cases result = (memcmp (pncmd_diagnose_communication_line_test + 1, abtRx, sizeof (pncmd_diagnose_communication_line_test) - 1) == 0) || (memcmp (pncmd_diagnose_communication_line_test + 2, abtRx, sizeof (pncmd_diagnose_communication_line_test) - 2) == 0); @@ -99,16 +100,18 @@ main (int argc, const char *argv[]) } printf (" Communication line test: %s\n", result ? "OK" : "Failed"); - res = pn53x_transceive (pnd, pncmd_diagnose_rom_test, sizeof (pncmd_diagnose_rom_test), abtRx, &szRx, 0); - if (res == 0) { + res = pn53x_transceive (pnd, pncmd_diagnose_rom_test, sizeof (pncmd_diagnose_rom_test), abtRx, szRx, 0); + if (res > 0) { + szRx = (size_t) res; result = ((szRx == 1) && (abtRx[0] == 0x00)); } else { nfc_perror (pnd, "pn53x_transceive"); } printf (" ROM test: %s\n", result ? "OK" : "Failed"); - res = pn53x_transceive (pnd, pncmd_diagnose_ram_test, sizeof (pncmd_diagnose_ram_test), abtRx, &szRx, 0); - if (res == 0) { + res = pn53x_transceive (pnd, pncmd_diagnose_ram_test, sizeof (pncmd_diagnose_ram_test), abtRx, szRx, 0); + if (res > 0) { + szRx = (size_t) res; result = ((szRx == 1) && (abtRx[0] == 0x00)); } else { nfc_perror (pnd, "pn53x_transceive"); diff --git a/examples/pn53x-tamashell.c b/examples/pn53x-tamashell.c index e06e9a0..ac0bb8f 100644 --- a/examples/pn53x-tamashell.c +++ b/examples/pn53x-tamashell.c @@ -181,11 +181,13 @@ int main(int argc, const char* argv[]) print_hex((uint8_t*)abtTx,szTx); szRx = sizeof(abtRx); - if (pn53x_transceive (pnd, abtTx, szTx, abtRx, &szRx, 0) < 0) { + int res = 0; + if ((res = pn53x_transceive (pnd, abtTx, szTx, abtRx, szRx, 0)) < 0) { free(cmd); nfc_perror (pnd, "Rx"); continue; } + szRx = (size_t) res; printf("Rx: "); print_hex(abtRx, szRx); diff --git a/libnfc/chips/pn53x.c b/libnfc/chips/pn53x.c index e5bd058..d807f57 100644 --- a/libnfc/chips/pn53x.c +++ b/libnfc/chips/pn53x.c @@ -104,7 +104,7 @@ pn53x_reset_settings(struct nfc_device *pnd) } int -pn53x_transceive (struct nfc_device *pnd, const uint8_t *pbtTx, const size_t szTx, uint8_t *pbtRx, size_t *pszRx, int timeout) +pn53x_transceive (struct nfc_device *pnd, const uint8_t *pbtTx, const size_t szTx, uint8_t *pbtRx, const size_t szRxLen, int timeout) { int res = 0; if (CHIP_DATA (pnd)->wb_trigged) { @@ -128,9 +128,10 @@ pn53x_transceive (struct nfc_device *pnd, const uint8_t *pbtTx, const size_t szT size_t szRx = sizeof(abtRx); // Check if receiving buffers are available, if not, replace them - if (!pszRx || !pbtRx) { + if (szRxLen == 0 || !pbtRx) { pbtRx = abtRx; - pszRx = &szRx; + } else { + szRx = szRxLen; } // Call the send/receice callback functions of the current driver @@ -146,7 +147,7 @@ pn53x_transceive (struct nfc_device *pnd, const uint8_t *pbtTx, const size_t szT CHIP_DATA (pnd)->power_mode = POWERDOWN; } - if ((res = CHIP_DATA(pnd)->io->receive (pnd, pbtRx, *pszRx, timeout)) < 0) { + if ((res = CHIP_DATA(pnd)->io->receive (pnd, pbtRx, szRx, timeout)) < 0) { return res; } @@ -154,7 +155,7 @@ pn53x_transceive (struct nfc_device *pnd, const uint8_t *pbtTx, const size_t szT CHIP_DATA(pnd)->power_mode = NORMAL; // When TgInitAsTarget reply that means an external RF have waken up the chip } - *pszRx = (size_t) res; + szRx = (size_t) res; switch (pbtTx[0]) { case PowerDown: case InDataExchange: @@ -200,7 +201,7 @@ pn53x_transceive (struct nfc_device *pnd, const uint8_t *pbtTx, const size_t szT switch (CHIP_DATA(pnd)->last_status_byte) { case 0: - res = NFC_SUCCESS; + res = szRx; break; case ETIMEOUT: case ECRC: @@ -529,7 +530,7 @@ pn53x_ReadRegister (struct nfc_device *pnd, uint16_t ui16RegisterAddress, uint8_ int res = 0; PNREG_TRACE (ui16RegisterAddress); - if ((res = pn53x_transceive (pnd, abtCmd, sizeof (abtCmd), abtRegValue, &szRegValue, -1)) < 0) { + if ((res = pn53x_transceive (pnd, abtCmd, sizeof (abtCmd), abtRegValue, szRegValue, -1)) < 0) { return res; } if (CHIP_DATA(pnd)->type == PN533) { @@ -551,7 +552,7 @@ pn53x_WriteRegister (struct nfc_device *pnd, const uint16_t ui16RegisterAddress, { uint8_t abtCmd[] = { WriteRegister, ui16RegisterAddress >> 8, ui16RegisterAddress & 0xff, ui8Value }; PNREG_TRACE (ui16RegisterAddress); - return pn53x_transceive (pnd, abtCmd, sizeof (abtCmd), NULL, NULL, -1); + return pn53x_transceive (pnd, abtCmd, sizeof (abtCmd), NULL, 0, -1); } int @@ -605,7 +606,7 @@ pn53x_writeback_register (struct nfc_device *pnd) uint8_t abtRes[PN53x_EXTENDED_FRAME__DATA_MAX_LEN]; size_t szRes = sizeof(abtRes); // It transceives the previously constructed ReadRegister command - if ((res = pn53x_transceive (pnd, abtReadRegisterCmd, BUFFER_SIZE (abtReadRegisterCmd), abtRes, &szRes, -1)) < 0) { + if ((res = pn53x_transceive (pnd, abtReadRegisterCmd, BUFFER_SIZE (abtReadRegisterCmd), abtRes, szRes, -1)) < 0) { return res; } size_t i = 0; @@ -643,7 +644,7 @@ pn53x_writeback_register (struct nfc_device *pnd) if (BUFFER_SIZE (abtWriteRegisterCmd) > 1) { // We need to write some registers - if ((res = pn53x_transceive (pnd, abtWriteRegisterCmd, BUFFER_SIZE (abtWriteRegisterCmd), NULL, NULL, -1)) < 0) { + if ((res = pn53x_transceive (pnd, abtWriteRegisterCmd, BUFFER_SIZE (abtWriteRegisterCmd), NULL, 0, -1)) < 0) { return res; } } @@ -657,9 +658,10 @@ pn53x_get_firmware_version (struct nfc_device *pnd, char abtFirmwareText[22]) uint8_t abtFw[4]; size_t szFwLen = sizeof (abtFw); int res = 0; - if ((res = pn53x_transceive (pnd, abtCmd, sizeof (abtCmd), abtFw, &szFwLen, -1)) < 0) { + if ((res = pn53x_transceive (pnd, abtCmd, sizeof (abtCmd), abtFw, szFwLen, -1)) < 0) { return res; } + szFwLen = (size_t) res; // Determine which version of chip it is: PN531 will return only 2 bytes, while others return 4 bytes and have the first to tell the version IC if (szFwLen == 2) { CHIP_DATA(pnd)->type = PN531; @@ -930,9 +932,9 @@ 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, 500)) < 0) + if ((res = pn53x_transceive (pnd, abtCmd, sizeof (abtCmd), abtRx, szRx, 500)) < 0) return res; - + szRx = (size_t) res; if (((sizeof(abtExpectedRx) == szRx) && (0 == memcmp (abtRx, abtExpectedRx, sizeof(abtExpectedRx)))) == 0) return NFC_SUCCESS; @@ -1206,9 +1208,9 @@ pn53x_initiator_transceive_bits (struct nfc_device *pnd, const uint8_t *pbtTx, c // We have to give the amount of bytes + (the command byte 0x42) uint8_t abtRx[PN53x_EXTENDED_FRAME__DATA_MAX_LEN]; size_t szRx = sizeof(abtRx); - if ((res = pn53x_transceive (pnd, abtCmd, szFrameBytes + 1, abtRx, &szRx, -1)) < 0) + if ((res = pn53x_transceive (pnd, abtCmd, szFrameBytes + 1, abtRx, szRx, -1)) < 0) return res; - + szRx = (size_t) res; // Get the last bit-count that is stored in the received byte if ((res = pn53x_read_register (pnd, PN53X_REG_CIU_Control, &ui8rcc)) < 0) return res; @@ -1271,7 +1273,8 @@ pn53x_initiator_transceive_bytes (struct nfc_device *pnd, const uint8_t *pbtTx, // We have to give the amount of bytes + (the two command bytes 0xD4, 0x42) uint8_t abtRx[PN53x_EXTENDED_FRAME__DATA_MAX_LEN]; size_t szRx = sizeof(abtRx); - if (pn53x_transceive (pnd, abtCmd, szTx + szExtraTxLen, abtRx, &szRx, timeout) < 0) { + int res = 0; + if ((res = pn53x_transceive (pnd, abtCmd, szTx + szExtraTxLen, abtRx, szRx, timeout)) < 0) { // FIXME pn53x_transceive should return an integer if (CHIP_DATA (pnd)->last_status_byte == EINVRXFRAM) { pnd->last_error = NFC_ERFTRANS; @@ -1281,7 +1284,7 @@ pn53x_initiator_transceive_bytes (struct nfc_device *pnd, const uint8_t *pbtTx, return pnd->last_error; } } - + szRx = (size_t) res; if (pbtRx != NULL) { // Save the received byte count *pszRx = szRx - 1; @@ -1335,7 +1338,7 @@ uint32_t __pn53x_get_timer(struct nfc_device *pnd, const uint8_t last_cmd_byte) uint8_t abtRes[PN53x_EXTENDED_FRAME__DATA_MAX_LEN]; size_t szRes = sizeof(abtRes); // Let's send the previously constructed ReadRegister command - if (pn53x_transceive (pnd, abtReadRegisterCmd, BUFFER_SIZE (abtReadRegisterCmd), abtRes, &szRes, -1) < 0) { + if (pn53x_transceive (pnd, abtReadRegisterCmd, BUFFER_SIZE (abtReadRegisterCmd), abtRes, szRes, -1) < 0) { return false; } counter_hi = abtRes[off]; @@ -1427,7 +1430,7 @@ pn53x_initiator_transceive_bits_timed (struct nfc_device *pnd, const uint8_t *pb BUFFER_APPEND (abtWriteRegisterCmd, PN53X_REG_CIU_BitFraming & 0xff); BUFFER_APPEND (abtWriteRegisterCmd, SYMBOL_START_SEND | ((szTxBits % 8) & SYMBOL_TX_LAST_BITS)); // Let's send the previously constructed WriteRegister command - if ((res = pn53x_transceive (pnd, abtWriteRegisterCmd, BUFFER_SIZE (abtWriteRegisterCmd), NULL, NULL, -1)) < 0) { + if ((res = pn53x_transceive (pnd, abtWriteRegisterCmd, BUFFER_SIZE (abtWriteRegisterCmd), NULL, 0, -1)) < 0) { return res; } @@ -1458,7 +1461,7 @@ pn53x_initiator_transceive_bits_timed (struct nfc_device *pnd, const uint8_t *pb uint8_t abtRes[PN53x_EXTENDED_FRAME__DATA_MAX_LEN]; size_t szRes = sizeof(abtRes); // Let's send the previously constructed ReadRegister command - if ((res = pn53x_transceive (pnd, abtReadRegisterCmd, BUFFER_SIZE (abtReadRegisterCmd), abtRes, &szRes, -1)) < 0) { + if ((res = pn53x_transceive (pnd, abtReadRegisterCmd, BUFFER_SIZE (abtReadRegisterCmd), abtRes, szRes, -1)) < 0) { return res; } for (i = 0; i < sz; i++) { @@ -1521,7 +1524,7 @@ pn53x_initiator_transceive_bytes_timed (struct nfc_device *pnd, const uint8_t *p BUFFER_APPEND (abtWriteRegisterCmd, PN53X_REG_CIU_BitFraming & 0xff); BUFFER_APPEND (abtWriteRegisterCmd, SYMBOL_START_SEND); // Let's send the previously constructed WriteRegister command - if ((res = pn53x_transceive (pnd, abtWriteRegisterCmd, BUFFER_SIZE (abtWriteRegisterCmd), NULL, NULL, -1)) < 0) { + if ((res = pn53x_transceive (pnd, abtWriteRegisterCmd, BUFFER_SIZE (abtWriteRegisterCmd), NULL, 0, -1)) < 0) { return res; } @@ -1553,7 +1556,7 @@ pn53x_initiator_transceive_bytes_timed (struct nfc_device *pnd, const uint8_t *p uint8_t abtRes[PN53x_EXTENDED_FRAME__DATA_MAX_LEN]; size_t szRes = sizeof(abtRes); // Let's send the previously constructed ReadRegister command - if ((res = pn53x_transceive (pnd, abtReadRegisterCmd, BUFFER_SIZE (abtReadRegisterCmd), abtRes, &szRes, -1)) < 0) { + if ((res = pn53x_transceive (pnd, abtReadRegisterCmd, BUFFER_SIZE (abtReadRegisterCmd), abtRes, szRes, -1)) < 0) { return res; } for (i = 0; i < sz; i++) { @@ -1821,9 +1824,9 @@ pn53x_target_receive_bits (struct nfc_device *pnd, uint8_t *pbtRx, const size_t int res = 0; // Try to gather a received frame from the reader - if ((res = pn53x_transceive (pnd, abtCmd, sizeof (abtCmd), abtRx, &szRx, -1)) < 0) + if ((res = pn53x_transceive (pnd, abtCmd, sizeof (abtCmd), abtRx, szRx, -1)) < 0) return res; - + szRx = (size_t) res; // Get the last bit-count that is stored in the received byte uint8_t ui8rcc; if ((res = pn53x_read_register (pnd, PN53X_REG_CIU_Control, &ui8rcc)) < 0) @@ -1889,9 +1892,10 @@ pn53x_target_receive_bytes (struct nfc_device *pnd, uint8_t *pbtRx, const size_t // Try to gather a received frame from the reader uint8_t abtRx[PN53x_EXTENDED_FRAME__DATA_MAX_LEN]; size_t szRx = sizeof (abtRx); - if (pn53x_transceive (pnd, abtCmd, sizeof (abtCmd), abtRx, &szRx, timeout) < 0) + int res = 0; + if ((res = pn53x_transceive (pnd, abtCmd, sizeof (abtCmd), abtRx, szRx, timeout)) < 0) return pnd->last_error; - + szRx = (size_t) res; // Save the received bytes count szRx -= 1; @@ -1939,7 +1943,7 @@ pn53x_target_send_bits (struct nfc_device *pnd, const uint8_t *pbtTx, const size return res; // Try to send the bits to the reader - if ((res = pn53x_transceive (pnd, abtCmd, szFrameBytes + 1, NULL, NULL, -1)) < 0) + if ((res = pn53x_transceive (pnd, abtCmd, szFrameBytes + 1, NULL, 0, -1)) < 0) return res; // Everyting seems ok, return return sent bits count @@ -1988,7 +1992,7 @@ pn53x_target_send_bytes (struct nfc_device *pnd, const uint8_t *pbtTx, const siz memcpy (abtCmd + 1, pbtTx, szTx); // Try to send the bits to the reader - if ((res = pn53x_transceive (pnd, abtCmd, szTx + 1, NULL, NULL, timeout)) < 0) + if ((res = pn53x_transceive (pnd, abtCmd, szTx + 1, NULL, 0, timeout)) < 0) return res; // Everyting seems ok, return sent byte count @@ -2053,7 +2057,7 @@ int pn53x_RFConfiguration__RF_field (struct nfc_device *pnd, bool bEnable) { uint8_t abtCmd[] = { RFConfiguration, RFCI_FIELD, (bEnable) ? 0x01 : 0x00 }; - return pn53x_transceive (pnd, abtCmd, sizeof (abtCmd), NULL, NULL, -1); + return pn53x_transceive (pnd, abtCmd, sizeof (abtCmd), NULL, 0, -1); } int @@ -2066,7 +2070,7 @@ pn53x_RFConfiguration__Various_timings (struct nfc_device *pnd, const uint8_t fA fATR_RES_Timeout, // ATR_RES timeout (default: 0x0B 102.4 ms) fRetryTimeout // TimeOut during non-DEP communications (default: 0x0A 51.2 ms) }; - return pn53x_transceive (pnd, abtCmd, sizeof (abtCmd), NULL, NULL, -1); + return pn53x_transceive (pnd, abtCmd, sizeof (abtCmd), NULL, 0, -1); } int @@ -2077,7 +2081,7 @@ pn53x_RFConfiguration__MaxRtyCOM (struct nfc_device *pnd, const uint8_t MaxRtyCO RFCI_RETRY_DATA, MaxRtyCOM // MaxRtyCOM, default: 0x00 (no retry, only one try), inifite: 0xff }; - return pn53x_transceive (pnd, abtCmd, sizeof (abtCmd), NULL, NULL, -1); + return pn53x_transceive (pnd, abtCmd, sizeof (abtCmd), NULL, 0, -1); } int @@ -2091,7 +2095,7 @@ pn53x_RFConfiguration__MaxRetries (struct nfc_device *pnd, const uint8_t MxRtyAT MxRtyPSL, // MxRtyPSL, default: 0x01 MxRtyPassiveActivation // MxRtyPassiveActivation, default: 0xff (0x00 leads to problems with PN531) }; - return pn53x_transceive (pnd, abtCmd, sizeof (abtCmd), NULL, NULL, -1); + return pn53x_transceive (pnd, abtCmd, sizeof (abtCmd), NULL, 0, -1); } int @@ -2100,7 +2104,7 @@ pn53x_SetParameters (struct nfc_device *pnd, const uint8_t ui8Value) uint8_t abtCmd[] = { SetParameters, ui8Value }; int res = 0; - if((res = pn53x_transceive (pnd, abtCmd, sizeof (abtCmd), NULL, NULL, -1)) < 0) { + if((res = pn53x_transceive (pnd, abtCmd, sizeof (abtCmd), NULL, 0, -1)) < 0) { return res; } // We save last parameters in register cache @@ -2134,14 +2138,14 @@ pn53x_SAMConfiguration (struct nfc_device *pnd, const pn532_sam_mode ui8Mode, in pnd->last_error = NFC_EINVARG; return pnd->last_error; } - return (pn53x_transceive (pnd, abtCmd, szCmd, NULL, NULL, timeout)); + return (pn53x_transceive (pnd, abtCmd, szCmd, NULL, 0, timeout)); } int pn53x_PowerDown (struct nfc_device *pnd) { uint8_t abtCmd[] = { PowerDown, 0xf0 }; - return (pn53x_transceive (pnd, abtCmd, sizeof (abtCmd), NULL, NULL, -1)); + return (pn53x_transceive (pnd, abtCmd, sizeof (abtCmd), NULL, 0, -1)); } /** @@ -2208,9 +2212,10 @@ pn53x_InListPassiveTarget (struct nfc_device *pnd, if (pbtInitiatorData) memcpy (abtCmd + 3, pbtInitiatorData, szInitiatorData); int res = 0; - if ((res = pn53x_transceive (pnd, abtCmd, 3 + szInitiatorData, pbtTargetsData, pszTargetsData, timeout)) < 0) { + if ((res = pn53x_transceive (pnd, abtCmd, 3 + szInitiatorData, pbtTargetsData, *pszTargetsData, timeout)) < 0) { return res; } + *pszTargetsData = (size_t) res; return pbtTargetsData[0]; } @@ -2223,18 +2228,19 @@ pn53x_InDeselect (struct nfc_device *pnd, const uint8_t ui8Target) size_t szStatus = sizeof(abtStatus); uint8_t abtCmdGetStatus[] = { GetGeneralStatus }; int res = 0; - if ((res = pn53x_transceive (pnd, abtCmdGetStatus, sizeof (abtCmdGetStatus), abtStatus, &szStatus, -1)) < 0) { + if ((res = pn53x_transceive (pnd, abtCmdGetStatus, sizeof (abtCmdGetStatus), abtStatus, szStatus, -1)) < 0) { return res; } + szStatus = (size_t) res; if ((szStatus < 3) || (abtStatus[2] == 0)) { return NFC_SUCCESS; } // No much choice what to deselect actually... uint8_t abtCmdRcs360[] = { InDeselect, 0x01, 0x01 }; - return (pn53x_transceive (pnd, abtCmdRcs360, sizeof (abtCmdRcs360), NULL, NULL, -1)); + return (pn53x_transceive (pnd, abtCmdRcs360, sizeof (abtCmdRcs360), NULL, 0, -1)); } uint8_t abtCmd[] = { InDeselect, ui8Target }; - return (pn53x_transceive (pnd, abtCmd, sizeof (abtCmd), NULL, NULL, -1)); + return (pn53x_transceive (pnd, abtCmd, sizeof (abtCmd), NULL, 0, -1)); } int @@ -2246,18 +2252,19 @@ pn53x_InRelease (struct nfc_device *pnd, const uint8_t ui8Target) size_t szStatus = sizeof(abtStatus); uint8_t abtCmdGetStatus[] = { GetGeneralStatus }; int res = 0; - if ((res = pn53x_transceive (pnd, abtCmdGetStatus, sizeof (abtCmdGetStatus), abtStatus, &szStatus, -1)) < 0) { + if ((res = pn53x_transceive (pnd, abtCmdGetStatus, sizeof (abtCmdGetStatus), abtStatus, szStatus, -1)) < 0) { return res; } + szStatus = (size_t) res; if ((szStatus < 3) || (abtStatus[2] == 0)) { return NFC_SUCCESS; } // No much choice what to release actually... uint8_t abtCmdRcs360[] = { InRelease, 0x01, 0x01 }; - return (pn53x_transceive (pnd, abtCmdRcs360, sizeof (abtCmdRcs360), NULL, NULL, -1)); + return (pn53x_transceive (pnd, abtCmdRcs360, sizeof (abtCmdRcs360), NULL, 0, -1)); } uint8_t abtCmd[] = { InRelease, ui8Target }; - return (pn53x_transceive (pnd, abtCmd, sizeof (abtCmd), NULL, NULL, -1)); + return (pn53x_transceive (pnd, abtCmd, sizeof (abtCmd), NULL, 0, -1)); } int @@ -2281,8 +2288,8 @@ pn53x_InAutoPoll (struct nfc_device *pnd, uint8_t abtRx[PN53x_EXTENDED_FRAME__DATA_MAX_LEN]; size_t szRx = sizeof(abtRx); - int res = pn53x_transceive (pnd, abtCmd, szTxInAutoPoll, abtRx, &szRx, timeout); - + int res = pn53x_transceive (pnd, abtCmd, szTxInAutoPoll, abtRx, szRx, timeout); +szRx = (size_t) res; if (res < 0) { return res; } else if (szRx > 0) { @@ -2394,9 +2401,9 @@ pn53x_InJumpForDEP (struct nfc_device *pnd, size_t szRx = sizeof (abtRx); int res = 0; // Try to find a target, call the transceive callback function of the current device - if ((res = pn53x_transceive (pnd, abtCmd, offset, abtRx, &szRx, timeout)) < 0) + if ((res = pn53x_transceive (pnd, abtCmd, offset, abtRx, szRx, timeout)) < 0) return res; - + szRx = (size_t) res; // Make sure one target has been found, the PN53X returns 0x00 if none was available if (abtRx[1] >= 1) { // Is a target struct available @@ -2476,8 +2483,9 @@ pn53x_TgInitAsTarget (struct nfc_device *pnd, pn53x_target_mode ptm, // Request the initialization as a target uint8_t abtRx[PN53x_EXTENDED_FRAME__DATA_MAX_LEN]; size_t szRx = sizeof (abtRx); - if ((res = pn53x_transceive (pnd, abtCmd, 36 + szOptionalBytes, abtRx, &szRx, timeout)) < 0) + if ((res = pn53x_transceive (pnd, abtCmd, 36 + szOptionalBytes, abtRx, szRx, timeout)) < 0) return res; + szRx = (size_t) res; // Note: the first byte is skip: // its the "mode" byte which contains baudrate, DEP and Framing type (Mifare, active or FeliCa) datas. diff --git a/libnfc/chips/pn53x.h b/libnfc/chips/pn53x.h index fc4cea7..fe701b9 100644 --- a/libnfc/chips/pn53x.h +++ b/libnfc/chips/pn53x.h @@ -264,7 +264,7 @@ extern const uint8_t pn53x_ack_frame[6]; extern const uint8_t pn53x_nack_frame[6]; int pn53x_init(struct nfc_device *pnd); -int pn53x_transceive (struct nfc_device *pnd, const uint8_t *pbtTx, const size_t szTx, uint8_t *pbtRx, size_t *pszRx, int timeout); +int pn53x_transceive (struct nfc_device *pnd, const uint8_t *pbtTx, const size_t szTx, uint8_t *pbtRx, const size_t szRxLen, int timeout); int pn53x_set_parameters (struct nfc_device *pnd, const uint8_t ui8Value, const bool bEnable); int pn53x_set_tx_bits (struct nfc_device *pnd, const uint8_t ui8Bits); diff --git a/libnfc/drivers/pn53x_usb.c b/libnfc/drivers/pn53x_usb.c index 205b670..3a6272c 100644 --- a/libnfc/drivers/pn53x_usb.c +++ b/libnfc/drivers/pn53x_usb.c @@ -701,13 +701,13 @@ pn53x_usb_init (nfc_device *pnd) // Sometimes PN53x USB doesn't reply ACK one the first frame, so we need to send a dummy one... //pn53x_check_communication (pnd); // Sony RC-S360 doesn't support this command for now so let's use a get_firmware_version instead: const uint8_t abtCmd[] = { GetFirmwareVersion }; - pn53x_transceive (pnd, abtCmd, sizeof (abtCmd), NULL, NULL, 0); + pn53x_transceive (pnd, abtCmd, sizeof (abtCmd), NULL, 0, 0); // ...and we don't care about error pnd->last_error = 0; if (SONY_RCS360 == DRIVER_DATA (pnd)->model) { log_put (LOG_CATEGORY, NFC_PRIORITY_TRACE, "SONY RC-S360 initialization."); const uint8_t abtCmd2[] = { 0x18, 0x01 }; - pn53x_transceive (pnd, abtCmd2, sizeof (abtCmd2), NULL, NULL, 0); + pn53x_transceive (pnd, abtCmd2, sizeof (abtCmd2), NULL, 0, 0); pn53x_usb_ack (pnd); } diff --git a/test/test_dep_active.c b/test/test_dep_active.c index 7fe577a..3715639 100644 --- a/test/test_dep_active.c +++ b/test/test_dep_active.c @@ -143,7 +143,7 @@ initiator_thread (void *arg) cut_assert_equal_memory (abtAttRx, sizeof (abtAttRx), abtRx, szRx, cut_message ("Invalid received data")); if (res < 0) { thread_res = -1; return (void*) thread_res; } res = nfc_initiator_deselect_target (device); - cut_assert_equal_int (0, res, cut_message ("Can't deselect target: %s", nfc_strerror (device))); + cut_assert_operator_int (res, >=, 0, cut_message ("Can't deselect target: %s", nfc_strerror (device))); if (res < 0) { thread_res = -1; return (void*) thread_res; } return (void *) thread_res; diff --git a/test/test_dep_passive.c b/test/test_dep_passive.c index c7d9932..6bdd804 100644 --- a/test/test_dep_passive.c +++ b/test/test_dep_passive.c @@ -180,7 +180,7 @@ initiator_thread (void *arg) if (res < 0) { thread_res = -1; return (void*) thread_res; } res = nfc_initiator_deselect_target (device); - cut_assert_equal_int (0, res, cut_message ("Can't deselect target: %s", nfc_strerror (device))); + cut_assert_operator_int (res, >=, 0, cut_message ("Can't deselect target: %s", nfc_strerror (device))); if (res < 0) { thread_res = -1; return (void*) thread_res; } // Passive mode / 212Kbps (second pass) @@ -202,7 +202,7 @@ initiator_thread (void *arg) if (res < 0) { thread_res = -1; return (void*) thread_res; } res = nfc_initiator_deselect_target (device); - cut_assert_equal_int (0, res, cut_message ("Can't deselect target: %s", nfc_strerror (device))); + cut_assert_operator_int (res, >=, 0, cut_message ("Can't deselect target: %s", nfc_strerror (device))); if (res < 0) { thread_res = -1; return (void*) thread_res; } // Passive mode / 212Kbps @@ -224,7 +224,7 @@ initiator_thread (void *arg) if (res < 0) { thread_res = -1; return (void*) thread_res; } res = nfc_initiator_deselect_target (device); - cut_assert_equal_int (0, res, cut_message ("Can't deselect target: %s", nfc_strerror (device))); + cut_assert_operator_int (res, >=, 0, cut_message ("Can't deselect target: %s", nfc_strerror (device))); if (res < 0) { thread_res = -1; return (void*) thread_res; } // Passive mode / 424Kbps @@ -246,7 +246,7 @@ initiator_thread (void *arg) if (res < 0) { thread_res = -1; return (void*) thread_res; } res = nfc_initiator_deselect_target (device); - cut_assert_equal_int (0, res, cut_message ("Can't deselect target: %s", nfc_strerror (device))); + cut_assert_operator_int (res, >=, 0, cut_message ("Can't deselect target: %s", nfc_strerror (device))); if (res < 0) { thread_res = -1; return (void*) thread_res; } return (void *) thread_res; From 5e796e0a26f70768a6956c0c40aa4b7347fee470 Mon Sep 17 00:00:00 2001 From: Audrey Diacre Date: Mon, 9 Jan 2012 10:47:35 +0000 Subject: [PATCH 087/113] rx buffer size parameter of pn53x_TgInitAsTarget() function is now a const size_t. --- libnfc/chips/pn53x.c | 15 +++++++++------ libnfc/chips/pn53x.h | 2 +- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/libnfc/chips/pn53x.c b/libnfc/chips/pn53x.c index d807f57..c3a0edc 100644 --- a/libnfc/chips/pn53x.c +++ b/libnfc/chips/pn53x.c @@ -1745,10 +1745,10 @@ pn53x_target_init (struct nfc_device *pnd, nfc_target *pnt, uint8_t *pbtRx, size while (!targetActivated) { uint8_t btActivatedMode; - if((res = pn53x_TgInitAsTarget(pnd, ptm, pbtMifareParams, pbtTkt, szTkt, pbtFeliCaParams, pbtNFCID3t, pbtGBt, szGBt, pbtRx, pszRx, &btActivatedMode, timeout)) < 0) { + if((res = pn53x_TgInitAsTarget(pnd, ptm, pbtMifareParams, pbtTkt, szTkt, pbtFeliCaParams, pbtNFCID3t, pbtGBt, szGBt, pbtRx, *pszRx, &btActivatedMode, timeout)) < 0) { return res; } - + *pszRx = (size_t) res; nfc_modulation nm = { .nmt = NMT_DEP, // Silent compilation warnings .nbr = NBR_UNDEFINED @@ -2434,7 +2434,7 @@ pn53x_TgInitAsTarget (struct nfc_device *pnd, pn53x_target_mode ptm, const uint8_t *pbtTkt, size_t szTkt, const uint8_t *pbtFeliCaParams, const uint8_t *pbtNFCID3t, const uint8_t *pbtGBt, const size_t szGBt, - uint8_t *pbtRx, size_t *pszRx, uint8_t *pbtModeByte, int timeout) + uint8_t *pbtRx, const size_t szRxLen, uint8_t *pbtModeByte, int timeout) { uint8_t abtCmd[39 + 47 + 48] = { TgInitAsTarget }; // Worst case: 39-byte base, 47 bytes max. for General Bytes, 48 bytes max. for Historical Bytes size_t szOptionalBytes = 0; @@ -2494,11 +2494,14 @@ pn53x_TgInitAsTarget (struct nfc_device *pnd, pn53x_target_mode ptm, } // Save the received byte count - *pszRx = szRx - 1; + szRx -= 1; + + if ((szRx - 1) > szRxLen) + return NFC_EOVFLOW; // Copy the received bytes - memcpy (pbtRx, abtRx + 1, *pszRx); + memcpy (pbtRx, abtRx + 1, szRx); - return NFC_SUCCESS; + return szRx; } int diff --git a/libnfc/chips/pn53x.h b/libnfc/chips/pn53x.h index fe701b9..cea741d 100644 --- a/libnfc/chips/pn53x.h +++ b/libnfc/chips/pn53x.h @@ -342,7 +342,7 @@ int pn53x_TgInitAsTarget (struct nfc_device *pnd, pn53x_target_mode ptm, const uint8_t *pbtTkt, size_t szTkt, const uint8_t *pbtFeliCaParams, const uint8_t *pbtNFCID3t, const uint8_t *pbtGB, const size_t szGB, - uint8_t *pbtRx, size_t *pszRx, uint8_t *pbtModeByte, int timeout); + uint8_t *pbtRx, const size_t szRxLen, uint8_t *pbtModeByte, int timeout); // RFConfiguration int pn53x_RFConfiguration__RF_field (struct nfc_device *pnd, bool bEnable); From 00818e048c520da2e6cfc267b801c2f18fb3f2e6 Mon Sep 17 00:00:00 2001 From: Audrey Diacre Date: Mon, 9 Jan 2012 11:26:57 +0000 Subject: [PATCH 088/113] rx buffer size parameter of nfc_target_init() function is now a const size_t. --- examples/nfc-dep-target.c | 10 ++++------ examples/nfc-emulate-tag.c | 10 ++++------ examples/nfc-emulate-uid.c | 8 ++++---- examples/nfc-relay.c | 8 ++++---- examples/pn53x-sam.c | 3 +-- include/nfc/nfc.h | 2 +- libnfc/chips/pn53x.c | 11 ++++++----- libnfc/chips/pn53x.h | 2 +- libnfc/nfc-emulation.c | 8 ++++---- libnfc/nfc-internal.h | 2 +- libnfc/nfc.c | 6 +++--- test/test_dep_active.c | 2 +- test/test_dep_passive.c | 2 +- utils/nfc-relay-picc.c | 2 +- 14 files changed, 36 insertions(+), 40 deletions(-) diff --git a/examples/nfc-dep-target.c b/examples/nfc-dep-target.c index 9d7e73e..b2e8570 100644 --- a/examples/nfc-dep-target.c +++ b/examples/nfc-dep-target.c @@ -62,8 +62,7 @@ int main (int argc, const char *argv[]) { uint8_t abtRx[MAX_FRAME_LEN]; - int res = 0; - size_t szRx = sizeof(abtRx); + int szRx; size_t szDeviceFound; uint8_t abtTx[] = "Hello Mars!"; #define MAX_DEVICE_COUNT 2 @@ -120,18 +119,17 @@ main (int argc, const char *argv[]) print_nfc_target (nt, false); printf ("Waiting for initiator request...\n"); - if(nfc_target_init (pnd, &nt, abtRx, &szRx, 0) < 0) { + if ((szRx = nfc_target_init (pnd, &nt, abtRx, sizeof(abtRx), 0)) < 0) { nfc_perror(pnd, "nfc_target_init"); goto error; } printf("Initiator request received. Waiting for data...\n"); - if ((res = nfc_target_receive_bytes (pnd, abtRx, sizeof (abtRx), 0)) < 0) { + if ((szRx = nfc_target_receive_bytes (pnd, abtRx, sizeof (abtRx), 0)) < 0) { nfc_perror(pnd, "nfc_target_receive_bytes"); goto error; } - szRx = (size_t) res; - abtRx[szRx] = '\0'; + abtRx[(size_t) szRx] = '\0'; printf ("Received: %s\n", abtRx); printf ("Sending: %s\n", abtTx); diff --git a/examples/nfc-emulate-tag.c b/examples/nfc-emulate-tag.c index 8345370..e0dc77c 100644 --- a/examples/nfc-emulate-tag.c +++ b/examples/nfc-emulate-tag.c @@ -55,7 +55,7 @@ #define SAK_ISO14443_4_COMPLIANT 0x20 static uint8_t abtRx[MAX_FRAME_LEN]; -static size_t szRx = sizeof(abtRx); +static int szRx; static nfc_device *pnd; static bool quiet_output = false; static bool init_mfc_auth = false; @@ -137,17 +137,16 @@ bool nfc_target_emulate_tag(nfc_device *pnd, nfc_target *pnt) { size_t szTx; - int res = 0; uint8_t abtTx[MAX_FRAME_LEN]; bool loop = true; - if (nfc_target_init (pnd, pnt, abtRx, &szRx, 0) < 0) { + if ((szRx = nfc_target_init (pnd, pnt, abtRx, sizeof(abtRx), 0)) < 0) { nfc_perror (pnd, "nfc_target_init"); return false; } while ( loop ) { - loop = target_io( pnt, abtRx, szRx, abtTx, &szTx ); + loop = target_io( pnt, abtRx, (size_t) szRx, abtTx, &szTx ); if (szTx) { if (nfc_target_send_bytes(pnd, abtTx, szTx, 0) < 0) { nfc_perror (pnd, "nfc_target_send_bytes"); @@ -159,11 +158,10 @@ nfc_target_emulate_tag(nfc_device *pnd, nfc_target *pnt) nfc_device_set_property_bool (pnd, NP_HANDLE_CRC, false); init_mfc_auth = false; } - if ((res = nfc_target_receive_bytes(pnd, abtRx, sizeof (abtRx), 0)) < 0) { + if ((szRx = nfc_target_receive_bytes(pnd, abtRx, sizeof (abtRx), 0)) < 0) { nfc_perror (pnd, "nfc_target_receive_bytes"); return false; } - szRx = (size_t) res; } } return true; diff --git a/examples/nfc-emulate-uid.c b/examples/nfc-emulate-uid.c index 159ad4e..9baac45 100644 --- a/examples/nfc-emulate-uid.c +++ b/examples/nfc-emulate-uid.c @@ -57,7 +57,7 @@ #define MAX_FRAME_LEN 264 static uint8_t abtRecv[MAX_FRAME_LEN]; -static size_t szRecvBits; +static int szRecvBits; static nfc_device *pnd; // ISO14443A Anti-Collision response @@ -155,13 +155,13 @@ main (int argc, char *argv[]) }, }, }; - if (nfc_target_init (pnd, &nt, abtRecv, &szRecvBits, 0) < 0) { + if ((szRecvBits = nfc_target_init (pnd, &nt, abtRecv, sizeof (abtRecv), 0)) < 0) { nfc_perror (pnd, "nfc_target_init"); ERR ("Could not come out of auto-emulation, no command was received"); goto error; } printf ("[+] Received initiator command: "); - print_hex_bits (abtRecv, szRecvBits); + print_hex_bits (abtRecv, (size_t) szRecvBits); printf ("[+] Configuring communication\n"); if ((nfc_device_set_property_bool (pnd, NP_HANDLE_CRC, false) < 0) || (nfc_device_set_property_bool (pnd, NP_HANDLE_PARITY, true) < 0)) { nfc_perror (pnd, "nfc_device_set_property_bool"); @@ -200,7 +200,7 @@ main (int argc, char *argv[]) if (!quiet_output) { printf ("R: "); - print_hex_bits (abtRecv, szRecvBits); + print_hex_bits (abtRecv, (size_t) szRecvBits); } // Test if we know how to respond if (szTxBits) { diff --git a/examples/nfc-relay.c b/examples/nfc-relay.c index 6459259..5492ed8 100644 --- a/examples/nfc-relay.c +++ b/examples/nfc-relay.c @@ -52,7 +52,7 @@ static uint8_t abtReaderRx[MAX_FRAME_LEN]; static uint8_t abtReaderRxPar[MAX_FRAME_LEN]; -static size_t szReaderRxBits; +static int szReaderRxBits; static uint8_t abtTagRx[MAX_FRAME_LEN]; static uint8_t abtTagRxPar[MAX_FRAME_LEN]; static int szTagRxBits; @@ -146,7 +146,7 @@ main (int argc, char *argv[]) }, }; - if (nfc_target_init (pndTag, &nt, abtReaderRx, &szReaderRxBits, 0) < 0) { + if ((szReaderRxBits = nfc_target_init (pndTag, &nt, abtReaderRx, sizeof (abtReaderRx), 0)) < 0) { ERR ("%s", "Initialization of NFC emulator failed"); nfc_disconnect (pndTag); return EXIT_FAILURE; @@ -197,11 +197,11 @@ main (int argc, char *argv[]) // Print the reader frame to the screen if (!quiet_output) { printf ("R: "); - print_hex_par (abtReaderRx, szReaderRxBits, abtReaderRxPar); + print_hex_par (abtReaderRx, (size_t) szReaderRxBits, abtReaderRxPar); } // Forward the frame to the original tag if ((szTagRxBits = nfc_initiator_transceive_bits - (pndReader, abtReaderRx, szReaderRxBits, abtReaderRxPar, abtTagRx, abtTagRxPar)) > 0) { + (pndReader, abtReaderRx, (size_t) szReaderRxBits, abtReaderRxPar, abtTagRx, abtTagRxPar)) > 0) { // Redirect the answer back to the reader if (nfc_target_send_bits (pndTag, abtTagRx, szTagRxBits, abtTagRxPar) < 0) { nfc_perror (pndTag, "nfc_target_send_bits"); diff --git a/examples/pn53x-sam.c b/examples/pn53x-sam.c index 023a2dd..8757624 100644 --- a/examples/pn53x-sam.c +++ b/examples/pn53x-sam.c @@ -157,7 +157,6 @@ main (int argc, const char *argv[]) case PSM_DUAL_CARD: { uint8_t abtRx[MAX_FRAME_LEN]; - size_t szRx = sizeof(abtRx); nfc_target nt = { .nm = { @@ -176,7 +175,7 @@ main (int argc, const char *argv[]) }; printf ("Now both, NFC device (configured as target) and SAM are readables from an external NFC initiator.\n"); printf ("Please note that NFC device (configured as target) stay in target mode until it receive RATS, ATR_REQ or proprietary command.\n"); - if (nfc_target_init (pnd, &nt, abtRx, &szRx, 0) < 0) { + if (nfc_target_init (pnd, &nt, abtRx, sizeof(abtRx), 0) < 0) { nfc_perror(pnd, "nfc_target_init"); return EXIT_FAILURE; } diff --git a/include/nfc/nfc.h b/include/nfc/nfc.h index 8dd9ad7..de709e5 100644 --- a/include/nfc/nfc.h +++ b/include/nfc/nfc.h @@ -83,7 +83,7 @@ extern "C" { 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 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, size_t *pszRx, int timeout); + NFC_EXPORT int nfc_target_init (nfc_device *pnd, nfc_target *pnt, uint8_t *pbtRx, const size_t szRx, int timeout); NFC_EXPORT int nfc_target_send_bytes (nfc_device *pnd, const uint8_t *pbtTx, const size_t szTx, int timeout); NFC_EXPORT int nfc_target_receive_bytes (nfc_device *pnd, uint8_t *pbtRx, const size_t szRx, int timeout); NFC_EXPORT int nfc_target_send_bits (nfc_device *pnd, const uint8_t *pbtTx, const size_t szTxBits, const uint8_t *pbtTxPar); diff --git a/libnfc/chips/pn53x.c b/libnfc/chips/pn53x.c index c3a0edc..d30a708 100644 --- a/libnfc/chips/pn53x.c +++ b/libnfc/chips/pn53x.c @@ -1592,7 +1592,7 @@ pn53x_initiator_deselect_target (struct nfc_device *pnd) #define SAK_ISO14443_4_COMPLIANT 0x20 #define SAK_ISO18092_COMPLIANT 0x40 int -pn53x_target_init (struct nfc_device *pnd, nfc_target *pnt, uint8_t *pbtRx, size_t *pszRx, int timeout) +pn53x_target_init (struct nfc_device *pnd, nfc_target *pnt, uint8_t *pbtRx, const size_t szRxLen, int timeout) { pn53x_reset_settings(pnd); @@ -1742,13 +1742,14 @@ pn53x_target_init (struct nfc_device *pnd, nfc_target *pnt, uint8_t *pbtRx, size } bool targetActivated = false; + size_t szRx; while (!targetActivated) { uint8_t btActivatedMode; - if((res = pn53x_TgInitAsTarget(pnd, ptm, pbtMifareParams, pbtTkt, szTkt, pbtFeliCaParams, pbtNFCID3t, pbtGBt, szGBt, pbtRx, *pszRx, &btActivatedMode, timeout)) < 0) { + if((res = pn53x_TgInitAsTarget(pnd, ptm, pbtMifareParams, pbtTkt, szTkt, pbtFeliCaParams, pbtNFCID3t, pbtGBt, szGBt, pbtRx, szRxLen, &btActivatedMode, timeout)) < 0) { return res; } - *pszRx = (size_t) res; + szRx = (size_t) res; nfc_modulation nm = { .nmt = NMT_DEP, // Silent compilation warnings .nbr = NBR_UNDEFINED @@ -1805,12 +1806,12 @@ pn53x_target_init (struct nfc_device *pnd, nfc_target *pnt, uint8_t *pbtRx, size if (ptm & PTM_ISO14443_4_PICC_ONLY) { // When PN532 is in PICC target mode, it automatically reply to RATS so // we don't need to forward this command - *pszRx = 0; + szRx = 0; } } } - return *pszRx; + return szRx; } int diff --git a/libnfc/chips/pn53x.h b/libnfc/chips/pn53x.h index cea741d..2115b3f 100644 --- a/libnfc/chips/pn53x.h +++ b/libnfc/chips/pn53x.h @@ -308,7 +308,7 @@ int pn53x_initiator_transceive_bytes_timed (struct nfc_device *pnd, const uin int pn53x_initiator_deselect_target (struct nfc_device *pnd); // NFC device as Target functions -int pn53x_target_init (struct nfc_device *pnd, nfc_target *pnt, uint8_t *pbtRx, size_t *pszRx, int timeout); +int pn53x_target_init (struct nfc_device *pnd, nfc_target *pnt, uint8_t *pbtRx, const size_t szRxLen, int timeout); int pn53x_target_receive_bits (struct nfc_device *pnd, uint8_t *pbtRx, const size_t szRxLen, uint8_t *pbtRxPar); int pn53x_target_receive_bytes (struct nfc_device *pnd, uint8_t *pbtRx, const size_t szRxLen, int timeout); int pn53x_target_send_bits (struct nfc_device *pnd, const uint8_t *pbtTx, const size_t szTxBits, const uint8_t *pbtTxPar); diff --git a/libnfc/nfc-emulation.c b/libnfc/nfc-emulation.c index b83d01e..d952fc5 100644 --- a/libnfc/nfc-emulation.c +++ b/libnfc/nfc-emulation.c @@ -31,23 +31,23 @@ int nfc_emulate_target (nfc_device *pnd, struct nfc_emulator *emulator) { uint8_t abtRx[ISO7816_SHORT_R_APDU_MAX_LEN]; - size_t szRx = sizeof(abtRx); + int szRx; uint8_t abtTx[ISO7816_SHORT_C_APDU_MAX_LEN]; int res = 0; - if (nfc_target_init (pnd, emulator->target, abtRx, &szRx, 0) < 0) { + if ((szRx = nfc_target_init (pnd, emulator->target, abtRx, sizeof(abtRx), 0)) < 0) { return -1; } while (res >= 0) { - res = emulator->state_machine->io (emulator, abtRx, szRx, abtTx, sizeof (abtTx)); + res = emulator->state_machine->io (emulator, abtRx, (size_t) szRx, abtTx, sizeof (abtTx)); if (res > 0) { if (nfc_target_send_bytes(pnd, abtTx, res, 0) < 0) { return -1; } } if (res >= 0) { - if ((res = nfc_target_receive_bytes(pnd, abtRx, szRx, 0)) < 0) { + if ((res = nfc_target_receive_bytes(pnd, abtRx, (size_t) szRx, 0)) < 0) { return -1; } } diff --git a/libnfc/nfc-internal.h b/libnfc/nfc-internal.h index f5fbddd..9f0693d 100644 --- a/libnfc/nfc-internal.h +++ b/libnfc/nfc-internal.h @@ -142,7 +142,7 @@ struct nfc_driver_t { 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 (*target_init) (struct nfc_device *pnd, nfc_target * pnt, uint8_t * pbtRx, size_t * pszRx, int timeout); + 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); int (*target_receive_bytes) (struct nfc_device *pnd, uint8_t * pbtRx, const size_t szRxLen, int timeout); int (*target_send_bits) (struct nfc_device *pnd, const uint8_t * pbtTx, const size_t szTxBits, const uint8_t * pbtTxPar); diff --git a/libnfc/nfc.c b/libnfc/nfc.c index 7ed26d1..a7dd02f 100644 --- a/libnfc/nfc.c +++ b/libnfc/nfc.c @@ -609,7 +609,7 @@ nfc_initiator_transceive_bits_timed (nfc_device *pnd, const uint8_t *pbtTx, cons * and/or NDM_UNDEFINED (ie. for DEP mode), these fields will be updated. * * @param[out] pbtRx Rx buffer pointer - * @param[out] pszRx received bytes count + * @param[out] szRx received bytes count * @param timeout in milliseconds * * This function initializes NFC device in \e target mode in order to emulate a @@ -629,7 +629,7 @@ nfc_initiator_transceive_bits_timed (nfc_device *pnd, const uint8_t *pbtTx, cons * receive functions can be used. */ int -nfc_target_init (nfc_device *pnd, nfc_target *pnt, uint8_t *pbtRx, size_t * pszRx, int timeout) +nfc_target_init (nfc_device *pnd, nfc_target *pnt, uint8_t *pbtRx, const size_t szRx, int timeout) { int res = 0; // Disallow invalid frame @@ -656,7 +656,7 @@ nfc_target_init (nfc_device *pnd, nfc_target *pnt, uint8_t *pbtRx, size_t * pszR if ((res = nfc_device_set_property_bool (pnd, NP_ACTIVATE_FIELD, false)) < 0) return res; - HAL (target_init, pnd, pnt, pbtRx, pszRx, timeout); + HAL (target_init, pnd, pnt, pbtRx, szRx, timeout); } /** diff --git a/test/test_dep_active.c b/test/test_dep_active.c index 3715639..b0e1b76 100644 --- a/test/test_dep_active.c +++ b/test/test_dep_active.c @@ -84,7 +84,7 @@ target_thread (void *arg) uint8_t abtRx[1024]; size_t szRx = sizeof (abtRx); - int res = nfc_target_init (device, &nt, abtRx, &szRx, 0); + int res = nfc_target_init (device, &nt, abtRx, sizeof (abtRx), 0); cut_assert_operator_int (res, >, 0, cut_message ("Can't initialize NFC device as target: %s", nfc_strerror (device))); if (res < 0) { thread_res = -1; return (void*) thread_res; } diff --git a/test/test_dep_passive.c b/test/test_dep_passive.c index 6bdd804..42d2b7f 100644 --- a/test/test_dep_passive.c +++ b/test/test_dep_passive.c @@ -82,7 +82,7 @@ target_thread (void *arg) uint8_t abtRx[1024]; size_t szRx = sizeof (abtRx); - int res = nfc_target_init (device, &nt, abtRx, &szRx, 0); + int res = nfc_target_init (device, &nt, abtRx, szRx, 0); cut_assert_operator_int (res, >, 0, cut_message ("Can't initialize NFC device as target: %s", nfc_strerror (device))); if (res < 0) { thread_res = -1; return (void*) thread_res; } diff --git a/utils/nfc-relay-picc.c b/utils/nfc-relay-picc.c index 4f0205a..ba89e41 100644 --- a/utils/nfc-relay-picc.c +++ b/utils/nfc-relay-picc.c @@ -355,7 +355,7 @@ main (int argc, char *argv[]) printf ("Connected to the NFC emulator device: %s\n", nfc_device_get_name (pndTarget)); szCapduLen = sizeof (abtCapdu); - if (nfc_target_init (pndTarget, &ntEmulatedTarget, abtCapdu, &szCapduLen, 0) < 0) { + if (nfc_target_init (pndTarget, &ntEmulatedTarget, abtCapdu, szCapduLen, 0) < 0) { ERR ("%s", "Initialization of NFC emulator failed"); if (!target_only_mode) { nfc_disconnect (pndInitiator); From 1ec504e163e8eada32176b93d44c0bf26335e641 Mon Sep 17 00:00:00 2001 From: Audrey Diacre Date: Tue, 10 Jan 2012 10:35:36 +0000 Subject: [PATCH 089/113] nfc_list_devices() function returns now the number of devices found. --- examples/nfc-dep-target.c | 3 +-- examples/nfc-relay.c | 3 +-- examples/pn53x-diagnose.c | 3 +-- include/nfc/nfc.h | 2 +- libnfc/nfc.c | 21 +++++++++++---------- test/test_access_storm.c | 5 ++--- test/test_dep_active.c | 4 +--- test/test_dep_passive.c | 4 +--- test/test_register_access.c | 3 +-- test/test_register_endianness.c | 3 +-- utils/nfc-list.c | 3 +-- utils/nfc-relay-picc.c | 3 +-- 12 files changed, 23 insertions(+), 34 deletions(-) diff --git a/examples/nfc-dep-target.c b/examples/nfc-dep-target.c index b2e8570..aa19ef2 100644 --- a/examples/nfc-dep-target.c +++ b/examples/nfc-dep-target.c @@ -63,11 +63,10 @@ main (int argc, const char *argv[]) { uint8_t abtRx[MAX_FRAME_LEN]; int szRx; - size_t szDeviceFound; uint8_t abtTx[] = "Hello Mars!"; #define MAX_DEVICE_COUNT 2 nfc_connstring connstrings[MAX_DEVICE_COUNT]; - nfc_list_devices (connstrings, MAX_DEVICE_COUNT, &szDeviceFound); + size_t szDeviceFound = nfc_list_devices (connstrings, MAX_DEVICE_COUNT); // Little hack to allow using nfc-dep-initiator & nfc-dep-target from // the same machine: if there is more than one readers connected // nfc-dep-target will connect to the second reader diff --git a/examples/nfc-relay.c b/examples/nfc-relay.c index 5492ed8..9cb7a54 100644 --- a/examples/nfc-relay.c +++ b/examples/nfc-relay.c @@ -82,7 +82,6 @@ main (int argc, char *argv[]) { int arg; bool quiet_output = false; - size_t szFound; const char *acLibnfcVersion = nfc_version (); // Get commandline options @@ -110,7 +109,7 @@ main (int argc, char *argv[]) nfc_connstring connstrings[MAX_DEVICE_COUNT]; // List available devices - nfc_list_devices (connstrings, MAX_DEVICE_COUNT, &szFound); + size_t szFound = nfc_list_devices (connstrings, MAX_DEVICE_COUNT); if (szFound < 2) { ERR ("%zd device found but two connected devices are needed to relay NFC.", szFound); diff --git a/examples/pn53x-diagnose.c b/examples/pn53x-diagnose.c index 49e9c3e..649f6e9 100644 --- a/examples/pn53x-diagnose.c +++ b/examples/pn53x-diagnose.c @@ -52,7 +52,6 @@ int main (int argc, const char *argv[]) { - size_t szFound; size_t i; nfc_device *pnd; const char *acLibnfcVersion; @@ -73,7 +72,7 @@ main (int argc, const char *argv[]) printf ("%s uses libnfc %s\n", argv[0], acLibnfcVersion); nfc_connstring connstrings[MAX_DEVICE_COUNT]; - nfc_list_devices (connstrings, MAX_DEVICE_COUNT, &szFound); + size_t szFound = nfc_list_devices (connstrings, MAX_DEVICE_COUNT); if (szFound == 0) { printf ("No NFC device found.\n"); diff --git a/include/nfc/nfc.h b/include/nfc/nfc.h index de709e5..9993e2d 100644 --- a/include/nfc/nfc.h +++ b/include/nfc/nfc.h @@ -67,7 +67,7 @@ extern "C" { NFC_EXPORT nfc_device *nfc_connect (const nfc_connstring connstring); NFC_EXPORT void nfc_disconnect (nfc_device *pnd); NFC_EXPORT int nfc_abort_command (nfc_device *pnd); - NFC_EXPORT void nfc_list_devices (nfc_connstring connstrings[], size_t connstrings_len, size_t *pszDeviceFound); + NFC_EXPORT size_t nfc_list_devices (nfc_connstring connstrings[], size_t connstrings_len); NFC_EXPORT int nfc_idle (nfc_device *pnd); /* NFC initiator: act as "reader" */ diff --git a/libnfc/nfc.c b/libnfc/nfc.c index a7dd02f..7c96e2b 100644 --- a/libnfc/nfc.c +++ b/libnfc/nfc.c @@ -81,9 +81,8 @@ nfc_get_default_device (nfc_connstring *connstring) char *env_default_connstring = getenv ("LIBNFC_DEFAULT_DEVICE"); if (NULL == env_default_connstring) { // LIBNFC_DEFAULT_DEVICE is not set, we fallback on probing for the first available device - size_t szDeviceFound; nfc_connstring listed_cs[1]; - nfc_list_devices (listed_cs, 1, &szDeviceFound); + size_t szDeviceFound = nfc_list_devices (listed_cs, 1); if (szDeviceFound) { strncpy (*connstring, listed_cs[0], sizeof(nfc_connstring)); } else { @@ -177,30 +176,32 @@ nfc_disconnect (nfc_device *pnd) /** * @brief Probe for discoverable supported devices (ie. only available for some drivers) + * @return Returns the number of devices found. * @param[out] pnddDevices array of \a nfc_device_desc_t previously allocated by the caller. * @param szDevices size of the \a pnddDevices array. - * @param[out] pszDeviceFound number of devices found. + * */ -void -nfc_list_devices (nfc_connstring connstrings[] , size_t szDevices, size_t *pszDeviceFound) +size_t +nfc_list_devices (nfc_connstring connstrings[] , size_t szDevices) { - size_t szN; - *pszDeviceFound = 0; + size_t szN; + size_t szDeviceFound = 0; const struct nfc_driver_t *ndr; const struct nfc_driver_t **pndr = nfc_drivers; log_init (); while ((ndr = *pndr)) { szN = 0; - if (ndr->probe (connstrings + (*pszDeviceFound), szDevices - (*pszDeviceFound), &szN)) { - *pszDeviceFound += szN; + if (ndr->probe (connstrings + (szDeviceFound), szDevices - (szDeviceFound), &szN)) { + szDeviceFound += szN; log_put (LOG_CATEGORY, NFC_PRIORITY_TRACE, "%ld device(s) found using %s driver", (unsigned long) szN, ndr->name); - if (*pszDeviceFound == szDevices) + if (szDeviceFound == szDevices) break; } pndr++; } log_fini (); + return szDeviceFound; } /** diff --git a/test/test_access_storm.c b/test/test_access_storm.c index 2646e21..94a671a 100644 --- a/test/test_access_storm.c +++ b/test/test_access_storm.c @@ -15,17 +15,16 @@ test_access_storm (void) { int n = NTESTS; nfc_connstring connstrings[MAX_DEVICE_COUNT]; - size_t device_count, ref_device_count; int res = 0; - nfc_list_devices (connstrings, MAX_DEVICE_COUNT, &ref_device_count); + size_t ref_device_count = nfc_list_devices (connstrings, MAX_DEVICE_COUNT); if (!ref_device_count) cut_omit ("No NFC device found"); while (n) { size_t i; - nfc_list_devices (connstrings, MAX_DEVICE_COUNT, &device_count); + size_t device_count = nfc_list_devices (connstrings, MAX_DEVICE_COUNT); cut_assert_equal_int (ref_device_count, device_count, cut_message ("device count")); for (i = 0; i < device_count; i++) { diff --git a/test/test_dep_active.c b/test/test_dep_active.c index b0e1b76..4a984fd 100644 --- a/test/test_dep_active.c +++ b/test/test_dep_active.c @@ -27,9 +27,7 @@ abort_test_by_keypress (int sig) void cut_setup (void) { - size_t n; - - nfc_list_devices (connstrings, 2, &n); + size_t n = nfc_list_devices (connstrings, 2); if (n < 2) { cut_omit ("At least two NFC devices must be plugged-in to run this test"); } diff --git a/test/test_dep_passive.c b/test/test_dep_passive.c index 42d2b7f..b300594 100644 --- a/test/test_dep_passive.c +++ b/test/test_dep_passive.c @@ -26,9 +26,7 @@ abort_test_by_keypress (int sig) void cut_setup (void) { - size_t n; - - nfc_list_devices (connstrings, 2, &n); + size_t n = nfc_list_devices (connstrings, 2); if (n < 2) { cut_omit ("At least two NFC devices must be plugged-in to run this test"); } diff --git a/test/test_register_access.c b/test/test_register_access.c index 9b57222..bf72375 100644 --- a/test/test_register_access.c +++ b/test/test_register_access.c @@ -10,10 +10,9 @@ void test_register_endianness (void) { nfc_connstring connstrings[MAX_DEVICE_COUNT]; - size_t device_count; int res = 0; - nfc_list_devices (connstrings, MAX_DEVICE_COUNT, &device_count); + size_t device_count = nfc_list_devices (connstrings, MAX_DEVICE_COUNT); if (!device_count) cut_omit ("No NFC device found"); diff --git a/test/test_register_endianness.c b/test/test_register_endianness.c index 150c840..b82b9fb 100644 --- a/test/test_register_endianness.c +++ b/test/test_register_endianness.c @@ -11,10 +11,9 @@ void test_register_endianness (void) { nfc_connstring connstrings[MAX_DEVICE_COUNT]; - size_t device_count; int res = 0; - nfc_list_devices (connstrings, MAX_DEVICE_COUNT, &device_count); + size_t device_count = nfc_list_devices (connstrings, MAX_DEVICE_COUNT); if (!device_count) cut_omit ("No NFC device found"); diff --git a/utils/nfc-list.c b/utils/nfc-list.c index 5508d9c..d8acd07 100644 --- a/utils/nfc-list.c +++ b/utils/nfc-list.c @@ -114,9 +114,8 @@ main (int argc, const char *argv[]) strcpy(ndd.acDevice, "SCM Micro / SCL3711-NFC&RW"); pnd = nfc_connect (&ndd); #endif - size_t szDeviceFound; nfc_connstring connstrings[MAX_DEVICE_COUNT]; - nfc_list_devices (connstrings, MAX_DEVICE_COUNT, &szDeviceFound); + size_t szDeviceFound = nfc_list_devices (connstrings, MAX_DEVICE_COUNT); if (szDeviceFound == 0) { printf ("No NFC device found.\n"); diff --git a/utils/nfc-relay-picc.c b/utils/nfc-relay-picc.c index ba89e41..932ad84 100644 --- a/utils/nfc-relay-picc.c +++ b/utils/nfc-relay-picc.c @@ -149,7 +149,6 @@ int main (int argc, char *argv[]) { int arg; - size_t szFound; const char *acLibnfcVersion = nfc_version (); nfc_target ntRealTarget; @@ -193,7 +192,7 @@ main (int argc, char *argv[]) nfc_connstring connstrings[MAX_DEVICE_COUNT]; // List available devices - nfc_list_devices (connstrings, MAX_DEVICE_COUNT, &szFound); + size_t szFound = nfc_list_devices (connstrings, MAX_DEVICE_COUNT); if (initiator_only_mode || target_only_mode) { if (szFound < 1) { From efa86f0e3507f7c9f664f8ab8139c813554714df Mon Sep 17 00:00:00 2001 From: Romuald Conty Date: Tue, 10 Jan 2012 14:10:44 +0000 Subject: [PATCH 090/113] fix various minor mistakes, ajust timeouts --- libnfc/buses/uart_posix.c | 4 ++-- libnfc/chips/pn53x.c | 2 +- libnfc/drivers/pn532_uart.c | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/libnfc/buses/uart_posix.c b/libnfc/buses/uart_posix.c index 0fdd2d6..4e41d8e 100644 --- a/libnfc/buses/uart_posix.c +++ b/libnfc/buses/uart_posix.c @@ -273,7 +273,7 @@ select: timeout_tv.tv_usec = ((timeout % 1000) * 1000); } - res = select (MAX(UART_DATA(sp)->fd, iAbortFd) + 1, &rfds, NULL, NULL, &timeout_tv); + res = select (MAX(UART_DATA(sp)->fd, iAbortFd) + 1, &rfds, NULL, NULL, timeout ? &timeout_tv : NULL); if ((res < 0) && (EINTR == errno)) { // The system call was interupted by a signal and a signal handler was @@ -283,7 +283,7 @@ select: // Read error if (res < 0) { - log_put (LOG_CATEGORY, NFC_PRIORITY_TRACE, "%s", "RX error."); + log_put (LOG_CATEGORY, NFC_PRIORITY_TRACE, "Error: %s", strerror(errno)); return NFC_EIO; } // Read time-out diff --git a/libnfc/chips/pn53x.c b/libnfc/chips/pn53x.c index d30a708..34891b9 100644 --- a/libnfc/chips/pn53x.c +++ b/libnfc/chips/pn53x.c @@ -935,7 +935,7 @@ pn53x_check_communication (struct nfc_device *pnd) if ((res = pn53x_transceive (pnd, abtCmd, sizeof (abtCmd), abtRx, szRx, 500)) < 0) return res; szRx = (size_t) res; - if (((sizeof(abtExpectedRx) == szRx) && (0 == memcmp (abtRx, abtExpectedRx, sizeof(abtExpectedRx)))) == 0) + if ((sizeof(abtExpectedRx) == szRx) && (0 == memcmp (abtRx, abtExpectedRx, sizeof(abtExpectedRx)))) return NFC_SUCCESS; return NFC_EIO; diff --git a/libnfc/drivers/pn532_uart.c b/libnfc/drivers/pn532_uart.c index 87648c8..a99d31f 100644 --- a/libnfc/drivers/pn532_uart.c +++ b/libnfc/drivers/pn532_uart.c @@ -299,7 +299,7 @@ pn532_uart_send (nfc_device *pnd, const uint8_t *pbtData, const size_t szData, i return res; } // 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 ((res = pn53x_SAMConfiguration (pnd, 0x01, 500)) < 0) { + if ((res = pn53x_SAMConfiguration (pnd, 0x01, 1000)) < 0) { return res; } } From e86d08218bb594580b695b0a5d6690dcc746639c Mon Sep 17 00:00:00 2001 From: Romuald Conty Date: Tue, 10 Jan 2012 14:17:18 +0000 Subject: [PATCH 091/113] add nfc_initiator_poll_dep_target() --- include/nfc/nfc.h | 1 + libnfc/nfc.c | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/include/nfc/nfc.h b/include/nfc/nfc.h index 9993e2d..b5fec58 100644 --- a/include/nfc/nfc.h +++ b/include/nfc/nfc.h @@ -76,6 +76,7 @@ extern "C" { NFC_EXPORT int nfc_initiator_list_passive_targets (nfc_device *pnd, const nfc_modulation nm, nfc_target ant[], const size_t szTargets); NFC_EXPORT int nfc_initiator_poll_target (nfc_device *pnd, const nfc_modulation *pnmTargetTypes, const size_t szTargetTypes, const uint8_t uiPollNr, const uint8_t uiPeriod, nfc_target *pnt); NFC_EXPORT int nfc_initiator_select_dep_target (nfc_device *pnd, const nfc_dep_mode ndm, const nfc_baud_rate nbr, const nfc_dep_info *pndiInitiator, nfc_target *pnt, const int timeout); + NFC_EXPORT int nfc_initiator_poll_dep_target (nfc_device *pnd, const nfc_dep_mode ndm, const nfc_baud_rate nbr, const nfc_dep_info *pndiInitiator, nfc_target *pnt, const int timeout); NFC_EXPORT int nfc_initiator_deselect_target (nfc_device *pnd); NFC_EXPORT int nfc_initiator_transceive_bytes (nfc_device *pnd, const uint8_t *pbtTx, const size_t szTx, uint8_t *pbtRx, size_t *pszRx, int timeout); 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); diff --git a/libnfc/nfc.c b/libnfc/nfc.c index 7c96e2b..b008425 100644 --- a/libnfc/nfc.c +++ b/libnfc/nfc.c @@ -455,6 +455,45 @@ nfc_initiator_select_dep_target (nfc_device *pnd, HAL (initiator_select_dep_target, pnd, ndm, nbr, pndiInitiator, pnt, timeout); } +/** + * @brief Poll a target and request active or passive mode for D.E.P. (Data Exchange Protocol) + * @return Returns selected D.E.P targets count on success, otherwise returns libnfc's error code (negative value). + * + * @param pnd \a nfc_device struct pointer that represent currently used device + * @param ndm desired D.E.P. mode (\a NDM_ACTIVE or \a NDM_PASSIVE for active, respectively passive mode) + * @param ndiInitiator pointer \a nfc_dep_info struct that contains \e NFCID3 and \e General \e Bytes to set to the initiator device (optionnal, can be \e NULL) + * @param[out] pnt is a \a nfc_target struct pointer where target information will be put. + * + * The NFC device will try to find an available D.E.P. target. The standards + * (ISO18092 and ECMA-340) describe the modulation that can be used for reader + * to passive communications. + * + * @note \a nfc_dep_info will be returned when the target was acquired successfully. + */ +int +nfc_initiator_poll_dep_target (struct nfc_device *pnd, + const nfc_dep_mode ndm, const nfc_baud_rate nbr, + const nfc_dep_info *pndiInitiator, + nfc_target *pnt, + const int timeout) +{ + const int period = 300; + int remaining_time = timeout; + int res; + if ((res = nfc_device_set_property_bool (pnd, NP_INFINITE_SELECT, true)) < 0) + return res; + while (remaining_time > 0) { + if ((res = nfc_initiator_select_dep_target (pnd, ndm, nbr, pndiInitiator, pnt, period)) < 0) { + if (res != NFC_ETIMEOUT) + return res; + } + if (res == 1) + return res; + remaining_time -= period; + } + return 0; +} + /** * @brief Deselect a selected passive or emulated tag * @return Returns 0 on success, otherwise returns libnfc's error code (negative value). From f9a464e25659394f885eca3ed693dc3bf133b6c5 Mon Sep 17 00:00:00 2001 From: Audrey Diacre Date: Wed, 11 Jan 2012 08:47:14 +0000 Subject: [PATCH 092/113] nfc_perror() function displays now LIBNFC_ERROR. --- libnfc/nfc.c | 38 +++++++++++++++++++++++++++++++++----- 1 file changed, 33 insertions(+), 5 deletions(-) diff --git a/libnfc/nfc.c b/libnfc/nfc.c index b008425..c05ac39 100644 --- a/libnfc/nfc.c +++ b/libnfc/nfc.c @@ -806,18 +806,46 @@ nfc_target_receive_bits (nfc_device *pnd, uint8_t *pbtRx, const size_t szRx, uin HAL (target_receive_bits, pnd, pbtRx, szRx, pbtRxPar); } +static struct sErrorMessage { + int iErrorCode; + const char *pcErrorMsg; +} sErrorMessages[] = { + /* Chip-level errors (internal errors, RF errors, etc.) */ + { NFC_SUCCESS, "Success" }, + { NFC_EIO, "Input / Output Error" }, + { NFC_EINVARG, "Invalid argument(s)" }, + { NFC_EDEVNOTSUPP, "Not Supported by Device" }, + { NFC_ENOTSUCHDEV, "No Such Device" }, + { NFC_EOVFLOW, "Buffer Overflow" }, + { NFC_ETIMEOUT, "Timeout" }, + { NFC_EOPABORTED, "Operation Aborted" }, + { NFC_ENOTIMPL, "Not (yet) Implemented" }, + { NFC_ETGRELEASED, "Target Released" }, + { NFC_ERFTRANS, "RF Transmission Error" }, + { NFC_ECHIP, "Device's Internal Chip Error" }, +}; + /** - * @brief Return the PCD error string + * @brief Return the last error string * @return Returns a string */ const char * nfc_strerror (const nfc_device *pnd) -{ - return pnd->driver->strerror (pnd); +{ + const char *pcRes = "Unknown error"; + size_t i; + for (i = 0; i < (sizeof (sErrorMessages) / sizeof (struct sErrorMessage)); i++) { + if (sErrorMessages[i].iErrorCode == pnd->last_error) { + pcRes = sErrorMessages[i].pcErrorMsg; + break; + } + } + + return pcRes; } /** - * @brief Renders the PCD error in pcStrErrBuf for a maximum size of szBufLen chars + * @brief Renders the last error in pcStrErrBuf for a maximum size of szBufLen chars * @return Returns 0 upon success */ int @@ -827,7 +855,7 @@ nfc_strerror_r (const nfc_device *pnd, char *pcStrErrBuf, size_t szBufLen) } /** - * @brief Display the PCD error a-la perror + * @brief Display the last error occured on a nfc_device */ void nfc_perror (const nfc_device *pnd, const char *pcString) From cc8d4f68abd691e54a41d8aa6c948aabad2e388b Mon Sep 17 00:00:00 2001 From: Audrey Diacre Date: Thu, 12 Jan 2012 08:49:19 +0000 Subject: [PATCH 093/113] tests: add test_dep which tests states of 2 devices. --- test/Makefile.am | 4 + test/test_dep.c | 190 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 194 insertions(+) create mode 100644 test/test_dep.c diff --git a/test/Makefile.am b/test/Makefile.am index 5075c36..6c61715 100644 --- a/test/Makefile.am +++ b/test/Makefile.am @@ -9,6 +9,7 @@ TESTS_ENVIRONMENT = NO_MAKE=yes CUTTER="$(CUTTER)" cutter_unit_test_libs = \ test_access_storm.la \ + test_dep.la \ test_dep_active.la \ test_dep_passive.la \ test_register_access.la \ @@ -25,6 +26,9 @@ AM_LDFLAGS = -module -rpath $(libdir) -avoid-version -no-undefined test_access_storm_la_SOURCES = test_access_storm.c test_access_storm_la_LIBADD = $(top_builddir)/libnfc/libnfc.la +test_dep_la_SOURCES = test_dep.c +test_dep_la_LIBADD = $(top_builddir)/libnfc/libnfc.la + test_dep_active_la_SOURCES = test_dep_active.c test_dep_active_la_LIBADD = $(top_builddir)/libnfc/libnfc.la \ $(top_builddir)/utils/libnfcutils.la diff --git a/test/test_dep.c b/test/test_dep.c new file mode 100644 index 0000000..0c7170c --- /dev/null +++ b/test/test_dep.c @@ -0,0 +1,190 @@ +#include +#include +#include +#include + +#include "nfc/nfc.h" +#include "utils/nfc-utils.h" + +pthread_t threads[2]; +nfc_connstring connstrings[2]; +nfc_device *first_device, *second_device; +intptr_t result[2]; + +void +abort_test_by_keypress (int sig) +{ + (void) sig; + printf ("\033[0;1;31mSIGINT\033[0m"); + + nfc_abort_command (first_device); + nfc_abort_command (second_device); +} + +void +cut_setup (void) +{ + size_t n = nfc_list_devices (connstrings, 2); + if (n < 2) { + cut_omit ("At least two NFC devices must be plugged-in to run this test"); + } + + second_device = nfc_connect (connstrings[0]); + first_device = nfc_connect (connstrings[1]); + + signal (SIGINT, abort_test_by_keypress); +} + +void +cut_teardown (void) +{ + nfc_disconnect (second_device); + nfc_disconnect (first_device); +} + +struct thread_data { + nfc_device *device; + void *cut_test_context; +}; + +void * +target_thread (void *arg) +{ + intptr_t thread_res = 0; + nfc_device *device = ((struct thread_data *) arg)->device; + cut_set_current_test_context (((struct thread_data *) arg)->cut_test_context); + + printf ("=========== TARGET %s =========\n", nfc_device_get_name (device)); + nfc_target nt = { + .nm = { + .nmt = NMT_DEP, + .nbr = NBR_UNDEFINED + }, + .nti = { + .ndi = { + .abtNFCID3 = { 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xAA }, + .szGB = 4, + .abtGB = { 0x12, 0x34, 0x56, 0x78 }, + .ndm = NDM_PASSIVE, + /* These bytes are not used by nfc_target_init: the chip will provide them automatically to the initiator */ + .btDID = 0x00, + .btBS = 0x00, + .btBR = 0x00, + .btTO = 0x00, + .btPP = 0x01, + }, + }, + }; + + uint8_t abtRx[1024]; + size_t szRx = sizeof (abtRx); + int res = nfc_target_init (device, &nt, abtRx, szRx, 0); + cut_assert_operator_int (res, >, 0, cut_message ("Can't initialize NFC device as target: %s", nfc_strerror (device))); + if (res < 0) { thread_res = -1; return (void*) thread_res; } + + // First pass + res = nfc_target_receive_bytes (device, abtRx, sizeof (abtRx), 500); + cut_assert_operator_int (res, >, 0, cut_message ("Can't receive bytes from initiator: %s", nfc_strerror (device))); + szRx = (size_t) res; + + const uint8_t abtAttRx[] = "Hello DEP target!"; + cut_assert_equal_memory (abtAttRx, sizeof (abtAttRx), abtRx, szRx, cut_message ("Invalid received data")); + if (res <= 0) { thread_res = -1; return (void*) thread_res; } + + const uint8_t abtTx[] = "Hello DEP initiator!"; + res = nfc_target_send_bytes (device, abtTx, sizeof(abtTx), 500); + cut_assert_operator_int (res, >, 0, cut_message ("Can't send bytes to initiator: %s", nfc_strerror (device))); + if (res <= 0) { thread_res = -1; return (void*) thread_res; } + + sleep (1); + nfc_idle (device); + + return (void *) thread_res; +} + +void * +initiator_thread (void *arg) +{ + intptr_t thread_res = 0; + nfc_device *device = ((struct thread_data *) arg)->device; + cut_set_current_test_context (((struct thread_data *) arg)->cut_test_context); + + /* + * Wait some time for the other thread to initialise NFC device as target + */ + sleep (1); + printf ("=========== INITIATOR %s =========\n", nfc_device_get_name (device)); + + int res = nfc_initiator_init (device); + cut_assert_equal_int (0, res, cut_message ("Can't initialize NFC device as initiator: %s", nfc_strerror (device))); + if (res < 0) { thread_res = -1; return (void*) thread_res; } + + nfc_target nt; + + // Passive mode / 106Kbps + printf ("=========== INITIATOR %s (Passive mode / 106Kbps) =========\n", nfc_device_get_name (device)); + res = nfc_initiator_poll_dep_target (device, NDM_PASSIVE, NBR_106, NULL, &nt, 5000); + cut_assert_operator_int (res, >, 0, cut_message ("Can't select any DEP target: %s", nfc_strerror (device))); + cut_assert_equal_int (NMT_DEP, nt.nm.nmt, cut_message ("Invalid target modulation")); + cut_assert_equal_int (NBR_106, nt.nm.nbr, cut_message ("Invalid target baud rate")); + cut_assert_equal_memory ("\x11\x22\x33\x44\x55\x66\x77\x88\x99\xAA", 10, nt.nti.ndi.abtNFCID3, 10, cut_message ("Invalid target NFCID3")); + cut_assert_equal_int (NDM_PASSIVE, nt.nti.ndi.ndm, cut_message ("Invalid target DEP mode")); + cut_assert_equal_memory ("\x12\x34\x56\x78", 4, nt.nti.ndi.abtGB, nt.nti.ndi.szGB, cut_message ("Invalid target general bytes")); + if (res <= 0) { thread_res = -1; return (void*) thread_res; } + + const uint8_t abtTx[] = "Hello DEP target!"; + uint8_t abtRx[1024]; + size_t szRx = sizeof (abtRx); + res = nfc_initiator_transceive_bytes (device, abtTx, sizeof (abtTx), abtRx, &szRx, 500); + cut_assert_operator_int (res, >=, 0, cut_message ("Can't transceive bytes to target: %s", nfc_strerror (device))); + + const uint8_t abtAttRx[] = "Hello DEP initiator!"; + cut_assert_equal_memory (abtAttRx, sizeof (abtAttRx), abtRx, szRx, cut_message ("Invalid received data")); + if (res < 0) { thread_res = -1; return (void*) thread_res; } + + res = nfc_initiator_deselect_target (device); + cut_assert_operator_int (res, >=, 0, cut_message ("Can't deselect target: %s", nfc_strerror (device))); + if (res < 0) { thread_res = -1; return (void*) thread_res; } +nfc_target nt1; + res = nfc_initiator_poll_dep_target (device, NDM_PASSIVE, NBR_106, NULL, &nt1, 1000); + cut_assert_equal_int (-1, res, cut_message ("Problem with nfc_idle")); + if (res != -1) { thread_res = -1; return (void*) thread_res; } + + return (void *) thread_res; +} + +void +test_dep_states (void) +{ + int res; + + CutTestContext *test_context = cut_get_current_test_context (); + struct thread_data target_data = { + .device = first_device, + .cut_test_context = test_context, + }; + + struct thread_data initiator_data = { + .device = second_device, + .cut_test_context = test_context, + }; + + for (int i = 0; i < 2; i++) { + if ((res = pthread_create (&(threads[1]), NULL, target_thread, &target_data))) + cut_fail ("pthread_create() returned %d", res); + + if ((res = pthread_create (&(threads[0]), NULL, initiator_thread, &initiator_data))) + cut_fail ("pthread_create() returned %d", res); + + if ((res = pthread_join (threads[0], (void *) &result[0]))) + cut_fail ("pthread_join() returned %d", res); + if ((res = pthread_join (threads[1], (void *) &result[1]))) + cut_fail ("pthread_join() returned %d", res); + + cut_assert_equal_int (0, result[0], cut_message ("Unexpected initiator return code")); + cut_assert_equal_int (0, result[1], cut_message ("Unexpected target return code")); + + target_data.device = second_device; + initiator_data.device = first_device; + } +} From 0e2c60d0fa0d7e2518ca9694328d55dfa705ae68 Mon Sep 17 00:00:00 2001 From: Audrey Diacre Date: Thu, 12 Jan 2012 13:52:48 +0000 Subject: [PATCH 094/113] fix some last_error with result of previous function. --- libnfc/chips/pn53x.c | 15 +++++---------- libnfc/chips/pn53x.h | 2 +- libnfc/drivers/arygon.c | 17 ++++++++--------- libnfc/drivers/pn532_uart.c | 4 ++-- libnfc/drivers/pn53x_usb.c | 19 ++++++++----------- 5 files changed, 24 insertions(+), 33 deletions(-) diff --git a/libnfc/chips/pn53x.c b/libnfc/chips/pn53x.c index 34891b9..cd1e580 100644 --- a/libnfc/chips/pn53x.c +++ b/libnfc/chips/pn53x.c @@ -1244,6 +1244,7 @@ pn53x_initiator_transceive_bytes (struct nfc_device *pnd, const uint8_t *pbtTx, { size_t szExtraTxLen; uint8_t abtCmd[PN53x_EXTENDED_FRAME__DATA_MAX_LEN]; + int res = 0; // We can not just send bytes without parity if while the PN53X expects we handled them if (!pnd->bPar) { @@ -1264,8 +1265,8 @@ pn53x_initiator_transceive_bytes (struct nfc_device *pnd, const uint8_t *pbtTx, } // To transfer command frames bytes we can not have any leading bits, reset this to zero - if (pn53x_set_tx_bits (pnd, 0) < 0) { - pnd->last_error = NFC_EIO; // FIXME pn53x_set_tx_bits should return an integer + if ((res = pn53x_set_tx_bits (pnd, 0)) < 0) { + pnd->last_error = res; return pnd->last_error; } @@ -1273,16 +1274,10 @@ pn53x_initiator_transceive_bytes (struct nfc_device *pnd, const uint8_t *pbtTx, // We have to give the amount of bytes + (the two command bytes 0xD4, 0x42) uint8_t abtRx[PN53x_EXTENDED_FRAME__DATA_MAX_LEN]; size_t szRx = sizeof(abtRx); - int res = 0; + if ((res = pn53x_transceive (pnd, abtCmd, szTx + szExtraTxLen, abtRx, szRx, timeout)) < 0) { - // FIXME pn53x_transceive should return an integer - if (CHIP_DATA (pnd)->last_status_byte == EINVRXFRAM) { - pnd->last_error = NFC_ERFTRANS; + pnd->last_error = res; return pnd->last_error; - } else { - pnd->last_error = NFC_EIO; - return pnd->last_error; - } } szRx = (size_t) res; if (pbtRx != NULL) { diff --git a/libnfc/chips/pn53x.h b/libnfc/chips/pn53x.h index 2115b3f..998506a 100644 --- a/libnfc/chips/pn53x.h +++ b/libnfc/chips/pn53x.h @@ -299,7 +299,7 @@ int pn53x_initiator_select_dep_target (struct nfc_device *pnd, const int timeout); int pn53x_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 pn53x_initiator_transceive_bytes (struct nfc_device *pnd, const uint8_t *pbtTx, const size_t szTx, +int pn53x_initiator_transceive_bytes (struct nfc_device *pnd, const uint8_t *pbtTx, const size_t szTx, uint8_t *pbtRx, size_t *pszRx, int timeout); int pn53x_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); diff --git a/libnfc/drivers/arygon.c b/libnfc/drivers/arygon.c index 45eec13..9ea6180 100644 --- a/libnfc/drivers/arygon.c +++ b/libnfc/drivers/arygon.c @@ -306,6 +306,7 @@ arygon_disconnect (nfc_device *pnd) int arygon_tama_send (nfc_device *pnd, const uint8_t *pbtData, const size_t szData, int timeout) { + int res = 0; // Before sending anything, we need to discard from any junk bytes uart_flush_input (DRIVER_DATA(pnd)->port); @@ -319,23 +320,21 @@ arygon_tama_send (nfc_device *pnd, const uint8_t *pbtData, const size_t szData, return pnd->last_error; } - if (pn53x_build_frame (abtFrame + 1, &szFrame, pbtData, szData) < 0) { - pnd->last_error = NFC_EINVARG; + if ((res = pn53x_build_frame (abtFrame + 1, &szFrame, pbtData, szData)) < 0) { + pnd->last_error = res; return pnd->last_error; } - int res = uart_send (DRIVER_DATA (pnd)->port, abtFrame, szFrame + 1, timeout); - if (res != 0) { + if ((res = uart_send (DRIVER_DATA (pnd)->port, abtFrame, szFrame + 1, timeout)) != 0) { log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Unable to transmit data. (TX)"); - pnd->last_error = NFC_EIO; + pnd->last_error = res; return pnd->last_error; } uint8_t abtRxBuf[6]; - res = uart_receive (DRIVER_DATA (pnd)->port, abtRxBuf, sizeof (abtRxBuf), 0, timeout); - if (res != 0) { + if ((res = uart_receive (DRIVER_DATA (pnd)->port, abtRxBuf, sizeof (abtRxBuf), 0, timeout)) != 0) { log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Unable to read ACK"); - pnd->last_error = NFC_EIO; + pnd->last_error = res; return pnd->last_error; } @@ -526,7 +525,7 @@ arygon_reset_tama (nfc_device *pnd) res = uart_receive (DRIVER_DATA (pnd)->port, abtRx, szRx, 0, 1000); if (res != 0) { log_put (LOG_CATEGORY, NFC_PRIORITY_TRACE, "No reply to 'reset TAMA' command."); - pnd->last_error = NFC_EIO; + pnd->last_error = res; return pnd->last_error; } diff --git a/libnfc/drivers/pn532_uart.c b/libnfc/drivers/pn532_uart.c index a99d31f..f36ff31 100644 --- a/libnfc/drivers/pn532_uart.c +++ b/libnfc/drivers/pn532_uart.c @@ -318,8 +318,8 @@ pn532_uart_send (nfc_device *pnd, const uint8_t *pbtData, const size_t szData, i uint8_t abtFrame[PN532_BUFFER_LEN] = { 0x00, 0x00, 0xff }; // Every packet must start with "00 00 ff" size_t szFrame = 0; - if (pn53x_build_frame (abtFrame, &szFrame, pbtData, szData) < 0) { - pnd->last_error = NFC_EINVARG; + if ((res = pn53x_build_frame (abtFrame, &szFrame, pbtData, szData)) < 0) { + pnd->last_error = res; return pnd->last_error; } diff --git a/libnfc/drivers/pn53x_usb.c b/libnfc/drivers/pn53x_usb.c index 3a6272c..8611486 100644 --- a/libnfc/drivers/pn53x_usb.c +++ b/libnfc/drivers/pn53x_usb.c @@ -510,20 +510,18 @@ pn53x_usb_send (nfc_device *pnd, const uint8_t *pbtData, const size_t szData, co { uint8_t abtFrame[PN53X_USB_BUFFER_LEN] = { 0x00, 0x00, 0xff }; // Every packet must start with "00 00 ff" size_t szFrame = 0; + int res = 0; pn53x_build_frame (abtFrame, &szFrame, pbtData, szData); - int res = pn53x_usb_bulk_write (DRIVER_DATA (pnd), abtFrame, szFrame, timeout); - - if (res < 0) { - pnd->last_error = NFC_EIO; + if ((res = pn53x_usb_bulk_write (DRIVER_DATA (pnd), abtFrame, szFrame, timeout)) < 0) { + pnd->last_error = res; return pnd->last_error; } uint8_t abtRxBuf[PN53X_USB_BUFFER_LEN]; - res = pn53x_usb_bulk_read (DRIVER_DATA (pnd), abtRxBuf, sizeof (abtRxBuf), timeout); - if (res < 0) { - pnd->last_error = NFC_EIO; + if ((res = pn53x_usb_bulk_read (DRIVER_DATA (pnd), abtRxBuf, sizeof (abtRxBuf), timeout)) < 0) { + pnd->last_error = res; // try to interrupt current device state pn53x_usb_ack(pnd); return pnd->last_error; @@ -539,9 +537,8 @@ pn53x_usb_send (nfc_device *pnd, const uint8_t *pbtData, const size_t szData, co // pn53x_usb_receive()) will be able to retreive the correct response // packet. // FIXME Sony reader is also affected by this bug but NACK is not supported - int res = pn53x_usb_bulk_write (DRIVER_DATA (pnd), (uint8_t *)pn53x_nack_frame, sizeof(pn53x_nack_frame), timeout); - if (res < 0) { - pnd->last_error = NFC_EIO; + if ((res = pn53x_usb_bulk_write (DRIVER_DATA (pnd), (uint8_t *)pn53x_nack_frame, sizeof(pn53x_nack_frame), timeout)) < 0) { + pnd->last_error = res; // try to interrupt current device state pn53x_usb_ack(pnd); return pnd->last_error; @@ -595,7 +592,7 @@ read: } if (res < 0) { - pnd->last_error = NFC_EIO; + pnd->last_error = res; // try to interrupt current device state pn53x_usb_ack(pnd); return pnd->last_error; From bd0de9bd61fb356c7a8c01080a05b0bcd89a4701 Mon Sep 17 00:00:00 2001 From: Audrey Diacre Date: Fri, 13 Jan 2012 09:58:47 +0000 Subject: [PATCH 095/113] test: rename test_dep to test_device_modes_as_dep and improve it. --- libnfc/chips/pn53x.c | 3 ++ test/Makefile.am | 8 ++-- ...{test_dep.c => test_device_modes_as_dep.c} | 47 +++++++++++++------ 3 files changed, 40 insertions(+), 18 deletions(-) rename test/{test_dep.c => test_device_modes_as_dep.c} (77%) diff --git a/libnfc/chips/pn53x.c b/libnfc/chips/pn53x.c index cd1e580..dad67e0 100644 --- a/libnfc/chips/pn53x.c +++ b/libnfc/chips/pn53x.c @@ -1742,6 +1742,9 @@ pn53x_target_init (struct nfc_device *pnd, nfc_target *pnt, uint8_t *pbtRx, cons uint8_t btActivatedMode; if((res = pn53x_TgInitAsTarget(pnd, ptm, pbtMifareParams, pbtTkt, szTkt, pbtFeliCaParams, pbtNFCID3t, pbtGBt, szGBt, pbtRx, szRxLen, &btActivatedMode, timeout)) < 0) { + if (res == NFC_ETIMEOUT) { + return pn53x_idle(pnd); + } return res; } szRx = (size_t) res; diff --git a/test/Makefile.am b/test/Makefile.am index 6c61715..1e34224 100644 --- a/test/Makefile.am +++ b/test/Makefile.am @@ -9,8 +9,8 @@ TESTS_ENVIRONMENT = NO_MAKE=yes CUTTER="$(CUTTER)" cutter_unit_test_libs = \ test_access_storm.la \ - test_dep.la \ test_dep_active.la \ + test_device_modes_as_dep.la \ test_dep_passive.la \ test_register_access.la \ test_register_endianness.la @@ -26,13 +26,13 @@ AM_LDFLAGS = -module -rpath $(libdir) -avoid-version -no-undefined test_access_storm_la_SOURCES = test_access_storm.c test_access_storm_la_LIBADD = $(top_builddir)/libnfc/libnfc.la -test_dep_la_SOURCES = test_dep.c -test_dep_la_LIBADD = $(top_builddir)/libnfc/libnfc.la - test_dep_active_la_SOURCES = test_dep_active.c test_dep_active_la_LIBADD = $(top_builddir)/libnfc/libnfc.la \ $(top_builddir)/utils/libnfcutils.la +test_device_modes_as_dep_la_SOURCES = test_device_modes_as_dep.c +test_device_modes_as_dep_la_LIBADD = $(top_builddir)/libnfc/libnfc.la + test_dep_passive_la_SOURCES = test_dep_passive.c test_dep_passive_la_LIBADD = $(top_builddir)/libnfc/libnfc.la diff --git a/test/test_dep.c b/test/test_device_modes_as_dep.c similarity index 77% rename from test/test_dep.c rename to test/test_device_modes_as_dep.c index 0c7170c..2307ec8 100644 --- a/test/test_dep.c +++ b/test/test_device_modes_as_dep.c @@ -78,11 +78,18 @@ target_thread (void *arg) uint8_t abtRx[1024]; size_t szRx = sizeof (abtRx); - int res = nfc_target_init (device, &nt, abtRx, szRx, 0); + + // 1) nfc_target_init should take target in idle mode + int res = nfc_target_init (device, &nt, abtRx, szRx, 500); + cut_assert_operator_int (res, >=, 0, cut_message ("Can't initialize NFC device as target: %s", nfc_strerror (device))); + if (res < 0) { thread_res = -1; return (void*) thread_res; } + + // 2) act as target + sleep(1); + res = nfc_target_init (device, &nt, abtRx, szRx, 0); cut_assert_operator_int (res, >, 0, cut_message ("Can't initialize NFC device as target: %s", nfc_strerror (device))); if (res < 0) { thread_res = -1; return (void*) thread_res; } - // First pass res = nfc_target_receive_bytes (device, abtRx, sizeof (abtRx), 500); cut_assert_operator_int (res, >, 0, cut_message ("Can't receive bytes from initiator: %s", nfc_strerror (device))); szRx = (size_t) res; @@ -95,7 +102,8 @@ target_thread (void *arg) res = nfc_target_send_bytes (device, abtTx, sizeof(abtTx), 500); cut_assert_operator_int (res, >, 0, cut_message ("Can't send bytes to initiator: %s", nfc_strerror (device))); if (res <= 0) { thread_res = -1; return (void*) thread_res; } - + + // 3) idle mode sleep (1); nfc_idle (device); @@ -112,24 +120,32 @@ initiator_thread (void *arg) /* * Wait some time for the other thread to initialise NFC device as target */ - sleep (1); + sleep (5); printf ("=========== INITIATOR %s =========\n", nfc_device_get_name (device)); int res = nfc_initiator_init (device); cut_assert_equal_int (0, res, cut_message ("Can't initialize NFC device as initiator: %s", nfc_strerror (device))); if (res < 0) { thread_res = -1; return (void*) thread_res; } + // 1) As other device should be in idle mode, nfc_initiator_poll_dep_target should return 0 nfc_target nt; + res = nfc_initiator_poll_dep_target (device, NDM_PASSIVE, NBR_106, NULL, &nt, 1000); + cut_assert_equal_int (0, res, cut_message ("Problem with nfc_idle")); + if (res != 0) { thread_res = -1; return (void*) thread_res; } + + + // 2 As other device should be in target mode, nfc_initiator_poll_dep_target should be positive. + nfc_target nt1; // Passive mode / 106Kbps printf ("=========== INITIATOR %s (Passive mode / 106Kbps) =========\n", nfc_device_get_name (device)); - res = nfc_initiator_poll_dep_target (device, NDM_PASSIVE, NBR_106, NULL, &nt, 5000); + res = nfc_initiator_poll_dep_target (device, NDM_PASSIVE, NBR_106, NULL, &nt1, 5000); cut_assert_operator_int (res, >, 0, cut_message ("Can't select any DEP target: %s", nfc_strerror (device))); - cut_assert_equal_int (NMT_DEP, nt.nm.nmt, cut_message ("Invalid target modulation")); - cut_assert_equal_int (NBR_106, nt.nm.nbr, cut_message ("Invalid target baud rate")); - cut_assert_equal_memory ("\x11\x22\x33\x44\x55\x66\x77\x88\x99\xAA", 10, nt.nti.ndi.abtNFCID3, 10, cut_message ("Invalid target NFCID3")); - cut_assert_equal_int (NDM_PASSIVE, nt.nti.ndi.ndm, cut_message ("Invalid target DEP mode")); - cut_assert_equal_memory ("\x12\x34\x56\x78", 4, nt.nti.ndi.abtGB, nt.nti.ndi.szGB, cut_message ("Invalid target general bytes")); + cut_assert_equal_int (NMT_DEP, nt1.nm.nmt, cut_message ("Invalid target modulation")); + cut_assert_equal_int (NBR_106, nt1.nm.nbr, cut_message ("Invalid target baud rate")); + cut_assert_equal_memory ("\x11\x22\x33\x44\x55\x66\x77\x88\x99\xAA", 10, nt1.nti.ndi.abtNFCID3, 10, cut_message ("Invalid target NFCID3")); + cut_assert_equal_int (NDM_PASSIVE, nt1.nti.ndi.ndm, cut_message ("Invalid target DEP mode")); + cut_assert_equal_memory ("\x12\x34\x56\x78", 4, nt1.nti.ndi.abtGB, nt1.nti.ndi.szGB, cut_message ("Invalid target general bytes")); if (res <= 0) { thread_res = -1; return (void*) thread_res; } const uint8_t abtTx[] = "Hello DEP target!"; @@ -145,10 +161,12 @@ initiator_thread (void *arg) res = nfc_initiator_deselect_target (device); cut_assert_operator_int (res, >=, 0, cut_message ("Can't deselect target: %s", nfc_strerror (device))); if (res < 0) { thread_res = -1; return (void*) thread_res; } -nfc_target nt1; - res = nfc_initiator_poll_dep_target (device, NDM_PASSIVE, NBR_106, NULL, &nt1, 1000); - cut_assert_equal_int (-1, res, cut_message ("Problem with nfc_idle")); - if (res != -1) { thread_res = -1; return (void*) thread_res; } + + // 3) As other device should be in idle mode, nfc_initiator_poll_dep_target should return 0 + nfc_target nt2; + res = nfc_initiator_poll_dep_target (device, NDM_PASSIVE, NBR_106, NULL, &nt2, 1000); + cut_assert_equal_int (0, res, cut_message ("Problem with nfc_idle")); + if (res != 0) { thread_res = -1; return (void*) thread_res; } return (void *) thread_res; } @@ -184,6 +202,7 @@ test_dep_states (void) cut_assert_equal_int (0, result[0], cut_message ("Unexpected initiator return code")); cut_assert_equal_int (0, result[1], cut_message ("Unexpected target return code")); + // initiator --> target, target --> initiator target_data.device = second_device; initiator_data.device = first_device; } From 84a864f8d3c84ad2ba2b976bdee98a829102be37 Mon Sep 17 00:00:00 2001 From: Audrey Diacre Date: Fri, 13 Jan 2012 14:41:27 +0000 Subject: [PATCH 096/113] test/test_device_modes_as_dep: increase duration of sleep() between nfc_idle() and nfc_target_init() to wait for the end of nfc_initiator_poll_dep_target(). --- test/test_device_modes_as_dep.c | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/test/test_device_modes_as_dep.c b/test/test_device_modes_as_dep.c index 2307ec8..5ac9ce5 100644 --- a/test/test_device_modes_as_dep.c +++ b/test/test_device_modes_as_dep.c @@ -55,7 +55,18 @@ target_thread (void *arg) cut_set_current_test_context (((struct thread_data *) arg)->cut_test_context); printf ("=========== TARGET %s =========\n", nfc_device_get_name (device)); - nfc_target nt = { + nfc_target nt; + + uint8_t abtRx[1024]; + size_t szRx = sizeof (abtRx); + + // 1) nfc_target_init should take target in idle mode + int res = nfc_target_init (device, &nt, abtRx, szRx, 500); + cut_assert_operator_int (res, >=, 0, cut_message ("Can't initialize NFC device as target: %s", nfc_strerror (device))); + if (res < 0) { thread_res = -1; return (void*) thread_res; } + + // 2) act as target + nfc_target nt1 = { .nm = { .nmt = NMT_DEP, .nbr = NBR_UNDEFINED @@ -75,18 +86,8 @@ target_thread (void *arg) }, }, }; - - uint8_t abtRx[1024]; - size_t szRx = sizeof (abtRx); - - // 1) nfc_target_init should take target in idle mode - int res = nfc_target_init (device, &nt, abtRx, szRx, 500); - cut_assert_operator_int (res, >=, 0, cut_message ("Can't initialize NFC device as target: %s", nfc_strerror (device))); - if (res < 0) { thread_res = -1; return (void*) thread_res; } - - // 2) act as target - sleep(1); - res = nfc_target_init (device, &nt, abtRx, szRx, 0); + sleep(6); + res = nfc_target_init (device, &nt1, abtRx, szRx, 0); cut_assert_operator_int (res, >, 0, cut_message ("Can't initialize NFC device as target: %s", nfc_strerror (device))); if (res < 0) { thread_res = -1; return (void*) thread_res; } From ba58138aa93cb6bcf32f926cbca71e8654769268 Mon Sep 17 00:00:00 2001 From: Romain Tartiere Date: Tue, 17 Jan 2012 13:51:58 +0000 Subject: [PATCH 097/113] PRIu32 is defined in inttypes.h. --- libnfc/drivers/acr122.c | 1 + 1 file changed, 1 insertion(+) diff --git a/libnfc/drivers/acr122.c b/libnfc/drivers/acr122.c index db9ecc4..69fdf42 100644 --- a/libnfc/drivers/acr122.c +++ b/libnfc/drivers/acr122.c @@ -26,6 +26,7 @@ # include "config.h" #endif // HAVE_CONFIG_H +#include #include #include #include From 9d3ca39a44d631df820c8afc2b053e6c53f1e943 Mon Sep 17 00:00:00 2001 From: Audrey Diacre Date: Tue, 17 Jan 2012 14:17:01 +0000 Subject: [PATCH 098/113] remove _t suffix from nfc_driver_t type. --- libnfc/drivers.h | 2 +- libnfc/drivers/acr122.c | 2 +- libnfc/drivers/acr122.h | 2 +- libnfc/drivers/arygon.c | 2 +- libnfc/drivers/arygon.h | 2 +- libnfc/drivers/pn532_uart.c | 2 +- libnfc/drivers/pn532_uart.h | 2 +- libnfc/drivers/pn53x_usb.c | 2 +- libnfc/drivers/pn53x_usb.h | 2 +- libnfc/nfc-internal.h | 4 ++-- libnfc/nfc.c | 10 +++++----- 11 files changed, 16 insertions(+), 16 deletions(-) diff --git a/libnfc/drivers.h b/libnfc/drivers.h index df010dc..568e119 100644 --- a/libnfc/drivers.h +++ b/libnfc/drivers.h @@ -46,6 +46,6 @@ # define DRIVERS_MAX_DEVICES 16 -extern const struct nfc_driver_t *nfc_drivers[]; +extern const struct nfc_driver *nfc_drivers[]; #endif // __NFC_DRIVERS_H__ diff --git a/libnfc/drivers/acr122.c b/libnfc/drivers/acr122.c index 69fdf42..cc70465 100644 --- a/libnfc/drivers/acr122.c +++ b/libnfc/drivers/acr122.c @@ -463,7 +463,7 @@ const struct pn53x_io acr122_io = { .receive = acr122_receive, }; -const struct nfc_driver_t acr122_driver = { +const struct nfc_driver acr122_driver = { .name = ACR122_DRIVER_NAME, .probe = acr122_probe, .connect = acr122_connect, diff --git a/libnfc/drivers/acr122.h b/libnfc/drivers/acr122.h index 9ea315e..fb8e2b0 100644 --- a/libnfc/drivers/acr122.h +++ b/libnfc/drivers/acr122.h @@ -34,6 +34,6 @@ int acr122_send (nfc_device *pnd, const uint8_t *pbtData, const size_t szData int acr122_receive (nfc_device *pnd, uint8_t *pbtData, const size_t szData, int timeout); void acr122_disconnect (nfc_device *pnd); -extern const struct nfc_driver_t acr122_driver; +extern const struct nfc_driver acr122_driver; #endif // ! __NFC_DRIVER_ACR122_H__ diff --git a/libnfc/drivers/arygon.c b/libnfc/drivers/arygon.c index 9ea6180..ad563c9 100644 --- a/libnfc/drivers/arygon.c +++ b/libnfc/drivers/arygon.c @@ -557,7 +557,7 @@ const struct pn53x_io arygon_tama_io = { .receive = arygon_tama_receive, }; -const struct nfc_driver_t arygon_driver = { +const struct nfc_driver arygon_driver = { .name = ARYGON_DRIVER_NAME, .probe = arygon_probe, .connect = arygon_connect, diff --git a/libnfc/drivers/arygon.h b/libnfc/drivers/arygon.h index de1bdb3..9873366 100644 --- a/libnfc/drivers/arygon.h +++ b/libnfc/drivers/arygon.h @@ -38,6 +38,6 @@ void arygon_disconnect (nfc_device *pnd); int arygon_tama_send (nfc_device *pnd, const uint8_t *pbtData, const size_t szData, int timeout); int arygon_tama_receive (nfc_device *pnd, uint8_t *pbtData, const size_t szDat, int timeouta); -extern const struct nfc_driver_t arygon_driver; +extern const struct nfc_driver arygon_driver; #endif // ! __NFC_DRIVER_ARYGON_H__ diff --git a/libnfc/drivers/pn532_uart.c b/libnfc/drivers/pn532_uart.c index f36ff31..ec7e95a 100644 --- a/libnfc/drivers/pn532_uart.c +++ b/libnfc/drivers/pn532_uart.c @@ -500,7 +500,7 @@ const struct pn53x_io pn532_uart_io = { .receive = pn532_uart_receive, }; -const struct nfc_driver_t pn532_uart_driver = { +const struct nfc_driver pn532_uart_driver = { .name = PN532_UART_DRIVER_NAME, .probe = pn532_uart_probe, .connect = pn532_uart_connect, diff --git a/libnfc/drivers/pn532_uart.h b/libnfc/drivers/pn532_uart.h index dbf60b1..9f9f7c7 100644 --- a/libnfc/drivers/pn532_uart.h +++ b/libnfc/drivers/pn532_uart.h @@ -36,6 +36,6 @@ void pn532_uart_disconnect (nfc_device *pnd); int pn532_uart_send (nfc_device *pnd, const uint8_t *pbtData, const size_t szData, int timeout); int pn532_uart_receive (nfc_device *pnd, uint8_t *pbtData, const size_t szData, int timeout); -extern const struct nfc_driver_t pn532_uart_driver; +extern const struct nfc_driver pn532_uart_driver; #endif // ! __NFC_DRIVER_PN532_UART_H__ diff --git a/libnfc/drivers/pn53x_usb.c b/libnfc/drivers/pn53x_usb.c index 8611486..1873819 100644 --- a/libnfc/drivers/pn53x_usb.c +++ b/libnfc/drivers/pn53x_usb.c @@ -790,7 +790,7 @@ const struct pn53x_io pn53x_usb_io = { .receive = pn53x_usb_receive, }; -const struct nfc_driver_t pn53x_usb_driver = { +const struct nfc_driver pn53x_usb_driver = { .name = PN53X_USB_DRIVER_NAME, .probe = pn53x_usb_probe, .connect = pn53x_usb_connect, diff --git a/libnfc/drivers/pn53x_usb.h b/libnfc/drivers/pn53x_usb.h index 5618038..3f617a7 100644 --- a/libnfc/drivers/pn53x_usb.h +++ b/libnfc/drivers/pn53x_usb.h @@ -35,6 +35,6 @@ int pn53x_usb_send (nfc_device *pnd, const uint8_t *pbtData, const size_t szD int pn53x_usb_receive (nfc_device *pnd, uint8_t *pbtData, const size_t szData, int timeout); void pn53x_usb_disconnect (nfc_device *pnd); -extern const struct nfc_driver_t pn53x_usb_driver; +extern const struct nfc_driver pn53x_usb_driver; #endif // ! __NFC_DRIVER_PN53X_USB_H__ diff --git a/libnfc/nfc-internal.h b/libnfc/nfc-internal.h index 9f0693d..04add92 100644 --- a/libnfc/nfc-internal.h +++ b/libnfc/nfc-internal.h @@ -125,7 +125,7 @@ #endif -struct nfc_driver_t { +struct nfc_driver { const char *name; bool (*probe)(nfc_connstring connstrings[], size_t connstrings_len, size_t * pszDeviceFound); struct nfc_device *(*connect) (const nfc_connstring connstring); @@ -163,7 +163,7 @@ struct nfc_driver_t { * @brief NFC device information */ struct nfc_device { - const struct nfc_driver_t *driver; + const struct nfc_driver *driver; void *driver_data; void *chip_data; diff --git a/libnfc/nfc.c b/libnfc/nfc.c index c05ac39..2809577 100644 --- a/libnfc/nfc.c +++ b/libnfc/nfc.c @@ -45,7 +45,7 @@ #define LOG_CATEGORY "libnfc.general" -const struct nfc_driver_t *nfc_drivers[] = { +const struct nfc_driver *nfc_drivers[] = { # if defined (DRIVER_PN53X_USB_ENABLED) &pn53x_usb_driver, # endif /* DRIVER_PN53X_USB_ENABLED */ @@ -127,8 +127,8 @@ nfc_connect (const nfc_connstring connstring) } // Search through the device list for an available device - const struct nfc_driver_t *ndr; - const struct nfc_driver_t **pndr = nfc_drivers; + const struct nfc_driver *ndr; + const struct nfc_driver **pndr = nfc_drivers; while ((ndr = *pndr)) { // Specific device is requested: using device description if (0 != strncmp (ndr->name, ncs, strlen(ndr->name))) { @@ -186,8 +186,8 @@ nfc_list_devices (nfc_connstring connstrings[] , size_t szDevices) { size_t szN; size_t szDeviceFound = 0; - const struct nfc_driver_t *ndr; - const struct nfc_driver_t **pndr = nfc_drivers; + const struct nfc_driver *ndr; + const struct nfc_driver **pndr = nfc_drivers; log_init (); while ((ndr = *pndr)) { From 9eb37b3eeeabadb5731ddf20edf385ff5f9541df Mon Sep 17 00:00:00 2001 From: Audrey Diacre Date: Tue, 17 Jan 2012 14:52:39 +0000 Subject: [PATCH 099/113] rename nfc_disconnect() function to nfc_close(). --- examples/doc/quick_start_example1.c | 2 +- examples/nfc-anticol.c | 4 +-- examples/nfc-dep-initiator.c | 2 +- examples/nfc-dep-target.c | 2 +- examples/nfc-emulate-forum-tag2.c | 4 +-- examples/nfc-emulate-tag.c | 4 +-- examples/nfc-emulate-uid.c | 4 +-- examples/nfc-poll.c | 4 +-- examples/nfc-relay.c | 6 ++-- examples/pn53x-sam.c | 2 +- examples/pn53x-tamashell.c | 2 +- include/nfc/nfc.h | 2 +- libnfc/drivers/acr122.c | 4 +-- libnfc/drivers/acr122.h | 2 +- libnfc/drivers/arygon.c | 6 ++-- libnfc/drivers/arygon.h | 2 +- libnfc/drivers/pn532_uart.c | 6 ++-- libnfc/drivers/pn532_uart.h | 2 +- libnfc/drivers/pn53x_usb.c | 4 +-- libnfc/drivers/pn53x_usb.h | 2 +- libnfc/nfc-internal.h | 2 +- libnfc/nfc.c | 8 ++--- test/test_access_storm.c | 2 +- test/test_dep_active.c | 4 +-- test/test_dep_passive.c | 4 +-- test/test_device_modes_as_dep.c | 4 +-- test/test_register_access.c | 2 +- test/test_register_endianness.c | 2 +- utils/nfc-emulate-forum-tag4.c | 2 +- utils/nfc-list.c | 2 +- utils/nfc-mfclassic.c | 4 +-- utils/nfc-mfsetuid.c | 4 +-- utils/nfc-mfultralight.c | 6 ++-- utils/nfc-read-forum-tag3.c | 2 +- utils/nfc-relay-picc.c | 46 ++++++++++++++--------------- 35 files changed, 80 insertions(+), 80 deletions(-) diff --git a/examples/doc/quick_start_example1.c b/examples/doc/quick_start_example1.c index 0f88896..2273618 100644 --- a/examples/doc/quick_start_example1.c +++ b/examples/doc/quick_start_example1.c @@ -52,6 +52,6 @@ main (int argc, const char *argv[]) } } // Disconnect from NFC device - nfc_disconnect (pnd); + nfc_close (pnd); return EXIT_SUCCESS; } diff --git a/examples/nfc-anticol.c b/examples/nfc-anticol.c index 30e8aa3..f211931 100644 --- a/examples/nfc-anticol.c +++ b/examples/nfc-anticol.c @@ -183,7 +183,7 @@ main (int argc, char *argv[]) // Send the 7 bits request command specified in ISO 14443A (0x26) if (!transmit_bits (abtReqa, 7)) { printf ("Error: No tag available\n"); - nfc_disconnect (pnd); + nfc_close (pnd); return 1; } memcpy (abtAtqa, abtRx, 2); @@ -312,6 +312,6 @@ main (int argc, char *argv[]) print_hex (abtAts, szAts); } - nfc_disconnect (pnd); + nfc_close (pnd); return 0; } diff --git a/examples/nfc-dep-initiator.c b/examples/nfc-dep-initiator.c index 92cf7ac..7e4c4da 100644 --- a/examples/nfc-dep-initiator.c +++ b/examples/nfc-dep-initiator.c @@ -107,6 +107,6 @@ main (int argc, const char *argv[]) } error: - nfc_disconnect (pnd); + nfc_close (pnd); return EXIT_SUCCESS; } diff --git a/examples/nfc-dep-target.c b/examples/nfc-dep-target.c index aa19ef2..ceff103 100644 --- a/examples/nfc-dep-target.c +++ b/examples/nfc-dep-target.c @@ -139,6 +139,6 @@ main (int argc, const char *argv[]) printf("Data sent.\n"); error: - nfc_disconnect (pnd); + nfc_close (pnd); return EXIT_SUCCESS; } diff --git a/examples/nfc-emulate-forum-tag2.c b/examples/nfc-emulate-forum-tag2.c index c682eb7..feacba5 100644 --- a/examples/nfc-emulate-forum-tag2.c +++ b/examples/nfc-emulate-forum-tag2.c @@ -196,13 +196,13 @@ main(int argc, char *argv[]) goto error; } - nfc_disconnect(pnd); + nfc_close(pnd); exit (EXIT_SUCCESS); error: if (pnd) { nfc_perror (pnd, argv[0]); - nfc_disconnect (pnd); + nfc_close (pnd); } } diff --git a/examples/nfc-emulate-tag.c b/examples/nfc-emulate-tag.c index e0dc77c..25bbb67 100644 --- a/examples/nfc-emulate-tag.c +++ b/examples/nfc-emulate-tag.c @@ -65,7 +65,7 @@ intr_hdlr (void) { printf ("\nQuitting...\n"); if (pnd != NULL) { - nfc_disconnect(pnd); + nfc_close(pnd); } exit (EXIT_FAILURE); } @@ -265,7 +265,7 @@ main (int argc, char *argv[]) exit (EXIT_FAILURE); } - nfc_disconnect(pnd); + nfc_close(pnd); exit (EXIT_SUCCESS); } diff --git a/examples/nfc-emulate-uid.c b/examples/nfc-emulate-uid.c index 9baac45..5609fd0 100644 --- a/examples/nfc-emulate-uid.c +++ b/examples/nfc-emulate-uid.c @@ -216,10 +216,10 @@ main (int argc, char *argv[]) } } } - nfc_disconnect (pnd); + nfc_close (pnd); exit (EXIT_SUCCESS); error: - nfc_disconnect (pnd); + nfc_close (pnd); exit (EXIT_FAILURE); } diff --git a/examples/nfc-poll.c b/examples/nfc-poll.c index 88c51c7..8ebe9c4 100644 --- a/examples/nfc-poll.c +++ b/examples/nfc-poll.c @@ -119,7 +119,7 @@ main (int argc, const char *argv[]) printf ("NFC device will poll during %ld ms (%u pollings of %lu ms for %zd modulations)\n", (unsigned long) uiPollNr * szModulations * uiPeriod * 150, uiPollNr, (unsigned long) uiPeriod * 150, szModulations); if ((res = nfc_initiator_poll_target (pnd, nmModulations, szModulations, uiPollNr, uiPeriod, &nt)) < 0) { nfc_perror (pnd, "nfc_initiator_poll_target"); - nfc_disconnect (pnd); + nfc_close (pnd); exit (EXIT_FAILURE); } @@ -128,6 +128,6 @@ main (int argc, const char *argv[]) } else { printf ("No target found.\n"); } - nfc_disconnect (pnd); + nfc_close (pnd); exit (EXIT_SUCCESS); } diff --git a/examples/nfc-relay.c b/examples/nfc-relay.c index 9cb7a54..8be7b29 100644 --- a/examples/nfc-relay.c +++ b/examples/nfc-relay.c @@ -147,7 +147,7 @@ main (int argc, char *argv[]) if ((szReaderRxBits = nfc_target_init (pndTag, &nt, abtReaderRx, sizeof (abtReaderRx), 0)) < 0) { ERR ("%s", "Initialization of NFC emulator failed"); - nfc_disconnect (pndTag); + nfc_close (pndTag); return EXIT_FAILURE; } printf ("%s", "Configuring emulator settings..."); @@ -215,7 +215,7 @@ main (int argc, char *argv[]) } } - nfc_disconnect (pndTag); - nfc_disconnect (pndReader); + nfc_close (pndTag); + nfc_close (pndReader); exit (EXIT_SUCCESS); } diff --git a/examples/pn53x-sam.c b/examples/pn53x-sam.c index 8757624..085b9ed 100644 --- a/examples/pn53x-sam.c +++ b/examples/pn53x-sam.c @@ -190,7 +190,7 @@ main (int argc, const char *argv[]) pn53x_SAMConfiguration (pnd, PSM_NORMAL, 0); // Disconnect from NFC device - nfc_disconnect (pnd); + nfc_close (pnd); exit (EXIT_SUCCESS); } diff --git a/examples/pn53x-tamashell.c b/examples/pn53x-tamashell.c index ac0bb8f..7f5937d 100644 --- a/examples/pn53x-tamashell.c +++ b/examples/pn53x-tamashell.c @@ -197,6 +197,6 @@ int main(int argc, const char* argv[]) if (input != NULL) { fclose(input); } - nfc_disconnect(pnd); + nfc_close(pnd); return 1; } diff --git a/include/nfc/nfc.h b/include/nfc/nfc.h index b5fec58..762316b 100644 --- a/include/nfc/nfc.h +++ b/include/nfc/nfc.h @@ -65,7 +65,7 @@ extern "C" { /* NFC Device/Hardware manipulation */ NFC_EXPORT bool nfc_get_default_device (nfc_connstring *connstring); NFC_EXPORT nfc_device *nfc_connect (const nfc_connstring connstring); - NFC_EXPORT void nfc_disconnect (nfc_device *pnd); + NFC_EXPORT void nfc_close (nfc_device *pnd); NFC_EXPORT int nfc_abort_command (nfc_device *pnd); NFC_EXPORT size_t nfc_list_devices (nfc_connstring connstrings[], size_t connstrings_len); NFC_EXPORT int nfc_idle (nfc_device *pnd); diff --git a/libnfc/drivers/acr122.c b/libnfc/drivers/acr122.c index cc70465..9dc688b 100644 --- a/libnfc/drivers/acr122.c +++ b/libnfc/drivers/acr122.c @@ -301,7 +301,7 @@ error: } void -acr122_disconnect (nfc_device *pnd) +acr122_close (nfc_device *pnd) { SCardDisconnect (DRIVER_DATA (pnd)->hCard, SCARD_LEAVE_CARD); acr122_free_scardcontext (); @@ -467,7 +467,7 @@ const struct nfc_driver acr122_driver = { .name = ACR122_DRIVER_NAME, .probe = acr122_probe, .connect = acr122_connect, - .disconnect = acr122_disconnect, + .close = acr122_close, .strerror = pn53x_strerror, .initiator_init = pn53x_initiator_init, diff --git a/libnfc/drivers/acr122.h b/libnfc/drivers/acr122.h index fb8e2b0..98c2c1c 100644 --- a/libnfc/drivers/acr122.h +++ b/libnfc/drivers/acr122.h @@ -32,7 +32,7 @@ bool acr122_probe (nfc_connstring connstrings[], size_t connstrings_len, size nfc_device *acr122_connect (const nfc_connstring connstring); int acr122_send (nfc_device *pnd, const uint8_t *pbtData, const size_t szData, int timeout); int acr122_receive (nfc_device *pnd, uint8_t *pbtData, const size_t szData, int timeout); -void acr122_disconnect (nfc_device *pnd); +void acr122_close (nfc_device *pnd); extern const struct nfc_driver acr122_driver; diff --git a/libnfc/drivers/arygon.c b/libnfc/drivers/arygon.c index ad563c9..c6694a6 100644 --- a/libnfc/drivers/arygon.c +++ b/libnfc/drivers/arygon.c @@ -270,7 +270,7 @@ arygon_connect (const nfc_connstring connstring) // Check communication using "Reset TAMA" command if (arygon_reset_tama(pnd) < 0) { - arygon_disconnect (pnd); + arygon_close (pnd); return NULL; } @@ -286,7 +286,7 @@ arygon_connect (const nfc_connstring connstring) } void -arygon_disconnect (nfc_device *pnd) +arygon_close (nfc_device *pnd) { // Release UART port uart_close (DRIVER_DATA (pnd)->port); @@ -561,7 +561,7 @@ const struct nfc_driver arygon_driver = { .name = ARYGON_DRIVER_NAME, .probe = arygon_probe, .connect = arygon_connect, - .disconnect = arygon_disconnect, + .close = arygon_close, .strerror = pn53x_strerror, .initiator_init = pn53x_initiator_init, diff --git a/libnfc/drivers/arygon.h b/libnfc/drivers/arygon.h index 9873366..c324e93 100644 --- a/libnfc/drivers/arygon.h +++ b/libnfc/drivers/arygon.h @@ -33,7 +33,7 @@ bool arygon_probe (nfc_connstring connstrings[], size_t connstrings_len, size_t *pszDeviceFound); nfc_device *arygon_connect (const nfc_connstring connstring); -void arygon_disconnect (nfc_device *pnd); +void arygon_close (nfc_device *pnd); int arygon_tama_send (nfc_device *pnd, const uint8_t *pbtData, const size_t szData, int timeout); int arygon_tama_receive (nfc_device *pnd, uint8_t *pbtData, const size_t szDat, int timeouta); diff --git a/libnfc/drivers/pn532_uart.c b/libnfc/drivers/pn532_uart.c index ec7e95a..7c4cad9 100644 --- a/libnfc/drivers/pn532_uart.c +++ b/libnfc/drivers/pn532_uart.c @@ -250,7 +250,7 @@ pn532_uart_connect (const nfc_connstring connstring) // Check communication using "Diagnose" command, with "Communication test" (0x00) if (pn53x_check_communication (pnd) < 0) { nfc_perror (pnd, "pn53x_check_communication"); - pn532_uart_disconnect (pnd); + pn532_uart_close (pnd); return NULL; } @@ -259,7 +259,7 @@ pn532_uart_connect (const nfc_connstring connstring) } void -pn532_uart_disconnect (nfc_device *pnd) +pn532_uart_close (nfc_device *pnd) { // Release UART port uart_close (DRIVER_DATA(pnd)->port); @@ -504,7 +504,7 @@ const struct nfc_driver pn532_uart_driver = { .name = PN532_UART_DRIVER_NAME, .probe = pn532_uart_probe, .connect = pn532_uart_connect, - .disconnect = pn532_uart_disconnect, + .close = pn532_uart_close, .strerror = pn53x_strerror, .initiator_init = pn53x_initiator_init, diff --git a/libnfc/drivers/pn532_uart.h b/libnfc/drivers/pn532_uart.h index 9f9f7c7..9b55347 100644 --- a/libnfc/drivers/pn532_uart.h +++ b/libnfc/drivers/pn532_uart.h @@ -32,7 +32,7 @@ bool pn532_uart_probe (nfc_connstring connstrings[], size_t connstrings_len, size_t *pszDeviceFound); nfc_device *pn532_uart_connect (const nfc_connstring connstring); -void pn532_uart_disconnect (nfc_device *pnd); +void pn532_uart_close (nfc_device *pnd); int pn532_uart_send (nfc_device *pnd, const uint8_t *pbtData, const size_t szData, int timeout); int pn532_uart_receive (nfc_device *pnd, uint8_t *pbtData, const size_t szData, int timeout); diff --git a/libnfc/drivers/pn53x_usb.c b/libnfc/drivers/pn53x_usb.c index 1873819..d42849e 100644 --- a/libnfc/drivers/pn53x_usb.c +++ b/libnfc/drivers/pn53x_usb.c @@ -479,7 +479,7 @@ error: } void -pn53x_usb_disconnect (nfc_device *pnd) +pn53x_usb_close (nfc_device *pnd) { pn53x_usb_ack (pnd); @@ -794,7 +794,7 @@ const struct nfc_driver pn53x_usb_driver = { .name = PN53X_USB_DRIVER_NAME, .probe = pn53x_usb_probe, .connect = pn53x_usb_connect, - .disconnect = pn53x_usb_disconnect, + .close = pn53x_usb_close, .strerror = pn53x_strerror, .initiator_init = pn53x_initiator_init, diff --git a/libnfc/drivers/pn53x_usb.h b/libnfc/drivers/pn53x_usb.h index 3f617a7..0827016 100644 --- a/libnfc/drivers/pn53x_usb.h +++ b/libnfc/drivers/pn53x_usb.h @@ -33,7 +33,7 @@ bool pn53x_usb_probe (nfc_connstring connstrings[], size_t connstrings_len, s nfc_device *pn53x_usb_connect (const nfc_connstring connstring); int pn53x_usb_send (nfc_device *pnd, const uint8_t *pbtData, const size_t szData, int timeout); int pn53x_usb_receive (nfc_device *pnd, uint8_t *pbtData, const size_t szData, int timeout); -void pn53x_usb_disconnect (nfc_device *pnd); +void pn53x_usb_close (nfc_device *pnd); extern const struct nfc_driver pn53x_usb_driver; diff --git a/libnfc/nfc-internal.h b/libnfc/nfc-internal.h index 04add92..76f2731 100644 --- a/libnfc/nfc-internal.h +++ b/libnfc/nfc-internal.h @@ -129,7 +129,7 @@ struct nfc_driver { const char *name; bool (*probe)(nfc_connstring connstrings[], size_t connstrings_len, size_t * pszDeviceFound); struct nfc_device *(*connect) (const nfc_connstring connstring); - void (*disconnect) (struct nfc_device *pnd); + void (*close) (struct nfc_device *pnd); const char *(*strerror) (const struct nfc_device *pnd); int (*initiator_init) (struct nfc_device *pnd); diff --git a/libnfc/nfc.c b/libnfc/nfc.c index 2809577..d97a9e3 100644 --- a/libnfc/nfc.c +++ b/libnfc/nfc.c @@ -156,19 +156,19 @@ nfc_connect (const nfc_connstring connstring) } /** - * @brief Disconnect from a NFC device + * @brief Close from a NFC device * @param pnd \a nfc_device struct pointer that represent currently used device * - * Initiator's selected tag is disconnected and the device, including allocated \a nfc_device struct, is released. + * Initiator's selected tag is closed and the device, including allocated \a nfc_device struct, is released. */ void -nfc_disconnect (nfc_device *pnd) +nfc_close (nfc_device *pnd) { if (pnd) { // Go in idle mode nfc_idle (pnd); // Disconnect, clean up and release the device - pnd->driver->disconnect (pnd); + pnd->driver->close (pnd); log_fini (); } diff --git a/test/test_access_storm.c b/test/test_access_storm.c index 94a671a..b80b6d0 100644 --- a/test/test_access_storm.c +++ b/test/test_access_storm.c @@ -44,7 +44,7 @@ test_access_storm (void) res = nfc_initiator_list_passive_targets(device, nm, ant, MAX_TARGET_COUNT); cut_assert_operator_int (res, >=, 0, cut_message ("nfc_initiator_list_passive_targets")); - nfc_disconnect (device); + nfc_close (device); } n--; diff --git a/test/test_dep_active.c b/test/test_dep_active.c index 4a984fd..a419044 100644 --- a/test/test_dep_active.c +++ b/test/test_dep_active.c @@ -41,8 +41,8 @@ cut_setup (void) void cut_teardown (void) { - nfc_disconnect (devices[TARGET]); - nfc_disconnect (devices[INITIATOR]); + nfc_close (devices[TARGET]); + nfc_close (devices[INITIATOR]); } struct thread_data { diff --git a/test/test_dep_passive.c b/test/test_dep_passive.c index b300594..6e55200 100644 --- a/test/test_dep_passive.c +++ b/test/test_dep_passive.c @@ -40,8 +40,8 @@ cut_setup (void) void cut_teardown (void) { - nfc_disconnect (devices[TARGET]); - nfc_disconnect (devices[INITIATOR]); + nfc_close (devices[TARGET]); + nfc_close (devices[INITIATOR]); } struct thread_data { diff --git a/test/test_device_modes_as_dep.c b/test/test_device_modes_as_dep.c index 5ac9ce5..3f5a297 100644 --- a/test/test_device_modes_as_dep.c +++ b/test/test_device_modes_as_dep.c @@ -38,8 +38,8 @@ cut_setup (void) void cut_teardown (void) { - nfc_disconnect (second_device); - nfc_disconnect (first_device); + nfc_close (second_device); + nfc_close (first_device); } struct thread_data { diff --git a/test/test_register_access.c b/test/test_register_access.c index bf72375..aa37994 100644 --- a/test/test_register_access.c +++ b/test/test_register_access.c @@ -41,5 +41,5 @@ test_register_endianness (void) cut_assert_equal_int (0, res, cut_message ("read register value")); cut_assert_equal_uint (0x55, value, cut_message ("check register value")); - nfc_disconnect (device); + nfc_close (device); } diff --git a/test/test_register_endianness.c b/test/test_register_endianness.c index b82b9fb..6c7ce7e 100644 --- a/test/test_register_endianness.c +++ b/test/test_register_endianness.c @@ -32,5 +32,5 @@ test_register_endianness (void) res = pn53x_read_register (device, 0xFFF0, &value); cut_assert_equal_int (0, res, cut_message ("read register 0xFFF0")); - nfc_disconnect (device); + nfc_close (device); } diff --git a/utils/nfc-emulate-forum-tag4.c b/utils/nfc-emulate-forum-tag4.c index 8f633f7..321b601 100644 --- a/utils/nfc-emulate-forum-tag4.c +++ b/utils/nfc-emulate-forum-tag4.c @@ -363,7 +363,7 @@ main (int argc, char *argv[]) nfc_perror (pnd, "nfc_emulate_target"); } - nfc_disconnect(pnd); + nfc_close(pnd); if (argc == 3) { if (!(ndef_message_save (argv[2], &nfcforum_tag4_data))) { diff --git a/utils/nfc-list.c b/utils/nfc-list.c index d8acd07..48c2466 100644 --- a/utils/nfc-list.c +++ b/utils/nfc-list.c @@ -247,7 +247,7 @@ main (int argc, const char *argv[]) printf("\n"); } } - nfc_disconnect (pnd); + nfc_close (pnd); } return 0; diff --git a/utils/nfc-mfclassic.c b/utils/nfc-mfclassic.c index 94d939b..91efcd4 100644 --- a/utils/nfc-mfclassic.c +++ b/utils/nfc-mfclassic.c @@ -562,7 +562,7 @@ main (int argc, const char *argv[]) // Try to find a MIFARE Classic tag if (nfc_initiator_select_passive_target (pnd, nmMifare, NULL, 0, &nt) < 0) { printf ("Error: no tag was found\n"); - nfc_disconnect (pnd); + nfc_close (pnd); exit (EXIT_FAILURE); } // Test if we are dealing with a MIFARE compatible tag @@ -618,7 +618,7 @@ main (int argc, const char *argv[]) write_card (unlock); } - nfc_disconnect (pnd); + nfc_close (pnd); break; case ACTION_EXTRACT:{ diff --git a/utils/nfc-mfsetuid.c b/utils/nfc-mfsetuid.c index 7927536..7304b27 100644 --- a/utils/nfc-mfsetuid.c +++ b/utils/nfc-mfsetuid.c @@ -212,7 +212,7 @@ main (int argc, char *argv[]) // Send the 7 bits request command specified in ISO 14443A (0x26) if (!transmit_bits (abtReqa, 7)) { printf ("Error: No tag available\n"); - nfc_disconnect (pnd); + nfc_close (pnd); return 1; } memcpy (abtAtqa, abtRx, 2); @@ -350,6 +350,6 @@ main (int argc, char *argv[]) } - nfc_disconnect (pnd); + nfc_close (pnd); return 0; } diff --git a/utils/nfc-mfultralight.c b/utils/nfc-mfultralight.c index 8bc32a7..e37646a 100644 --- a/utils/nfc-mfultralight.c +++ b/utils/nfc-mfultralight.c @@ -227,14 +227,14 @@ main (int argc, const char *argv[]) // Try to find a MIFARE Ultralight tag if (nfc_initiator_select_passive_target (pnd, nmMifare, NULL, 0, &nt) < 0) { ERR ("no tag was found\n"); - nfc_disconnect (pnd); + nfc_close (pnd); return 1; } // Test if we are dealing with a MIFARE compatible tag if (nt.nti.nai.abtAtqa[1] != 0x44) { ERR ("tag is not a MIFARE Ultralight card\n"); - nfc_disconnect (pnd); + nfc_close (pnd); return EXIT_FAILURE; } // Get the info from the current tag @@ -265,7 +265,7 @@ main (int argc, const char *argv[]) write_card (); } - nfc_disconnect (pnd); + nfc_close (pnd); return EXIT_SUCCESS; } diff --git a/utils/nfc-read-forum-tag3.c b/utils/nfc-read-forum-tag3.c index 69af3d5..6eda7da 100644 --- a/utils/nfc-read-forum-tag3.c +++ b/utils/nfc-read-forum-tag3.c @@ -313,7 +313,7 @@ main(int argc, char *argv[]) error: fclose (ndef_stream); if (pnd) { - nfc_disconnect (pnd); + nfc_close (pnd); } exit (error); } diff --git a/utils/nfc-relay-picc.c b/utils/nfc-relay-picc.c index 932ad84..4dcf884 100644 --- a/utils/nfc-relay-picc.c +++ b/utils/nfc-relay-picc.c @@ -230,7 +230,7 @@ main (int argc, char *argv[]) if (nfc_initiator_init (pndInitiator) < 0) { printf ("Error: fail initializing initiator\n"); - nfc_disconnect (pndInitiator); + nfc_close (pndInitiator); exit (EXIT_FAILURE); } @@ -241,7 +241,7 @@ main (int argc, char *argv[]) }; if (nfc_initiator_select_passive_target (pndInitiator, nm, NULL, 0, &ntRealTarget) < 0) { printf ("Error: no tag was found\n"); - nfc_disconnect (pndInitiator); + nfc_close (pndInitiator); exit (EXIT_FAILURE); } @@ -250,22 +250,22 @@ main (int argc, char *argv[]) if (initiator_only_mode) { if (print_hex_fd4(ntRealTarget.nti.nai.abtUid, ntRealTarget.nti.nai.szUidLen, "UID") != EXIT_SUCCESS) { fprintf (stderr, "Error while printing UID to FD4\n"); - nfc_disconnect (pndInitiator); + nfc_close (pndInitiator); exit(EXIT_FAILURE); } if (print_hex_fd4(ntRealTarget.nti.nai.abtAtqa, 2, "ATQA") != EXIT_SUCCESS) { fprintf (stderr, "Error while printing ATQA to FD4\n"); - nfc_disconnect (pndInitiator); + nfc_close (pndInitiator); exit(EXIT_FAILURE); } if (print_hex_fd4(&(ntRealTarget.nti.nai.btSak), 1, "SAK") != EXIT_SUCCESS) { fprintf (stderr, "Error while printing SAK to FD4\n"); - nfc_disconnect (pndInitiator); + nfc_close (pndInitiator); exit(EXIT_FAILURE); } if (print_hex_fd4(ntRealTarget.nti.nai.abtAts, ntRealTarget.nti.nai.szAtsLen, "ATS") != EXIT_SUCCESS) { fprintf (stderr, "Error while printing ATS to FD4\n"); - nfc_disconnect (pndInitiator); + nfc_close (pndInitiator); exit(EXIT_FAILURE); } } @@ -288,22 +288,22 @@ main (int argc, char *argv[]) size_t foo; if (scan_hex_fd3(ntEmulatedTarget.nti.nai.abtUid, &(ntEmulatedTarget.nti.nai.szUidLen), "UID") != EXIT_SUCCESS) { fprintf (stderr, "Error while scanning UID from FD3\n"); - nfc_disconnect (pndInitiator); + nfc_close (pndInitiator); exit(EXIT_FAILURE); } if (scan_hex_fd3(ntEmulatedTarget.nti.nai.abtAtqa, &foo, "ATQA") != EXIT_SUCCESS) { fprintf (stderr, "Error while scanning ATQA from FD3\n"); - nfc_disconnect (pndInitiator); + nfc_close (pndInitiator); exit(EXIT_FAILURE); } if (scan_hex_fd3(&(ntEmulatedTarget.nti.nai.btSak), &foo, "SAK") != EXIT_SUCCESS) { fprintf (stderr, "Error while scanning SAK from FD3\n"); - nfc_disconnect (pndInitiator); + nfc_close (pndInitiator); exit(EXIT_FAILURE); } if (scan_hex_fd3(ntEmulatedTarget.nti.nai.abtAts, &(ntEmulatedTarget.nti.nai.szAtsLen), "ATS") != EXIT_SUCCESS) { fprintf (stderr, "Error while scanning ATS from FD3\n"); - nfc_disconnect (pndInitiator); + nfc_close (pndInitiator); exit(EXIT_FAILURE); } } else { @@ -346,7 +346,7 @@ main (int argc, char *argv[]) if (pndTarget == NULL) { printf ("Error connecting NFC emulator device\n"); if (!target_only_mode) { - nfc_disconnect (pndInitiator); + nfc_close (pndInitiator); } return EXIT_FAILURE; } @@ -357,9 +357,9 @@ main (int argc, char *argv[]) if (nfc_target_init (pndTarget, &ntEmulatedTarget, abtCapdu, szCapduLen, 0) < 0) { ERR ("%s", "Initialization of NFC emulator failed"); if (!target_only_mode) { - nfc_disconnect (pndInitiator); + nfc_close (pndInitiator); } - nfc_disconnect (pndTarget); + nfc_close (pndTarget); exit(EXIT_FAILURE); } printf ("%s\n", "Done, relaying frames now!"); @@ -374,23 +374,23 @@ main (int argc, char *argv[]) if ((res = nfc_target_receive_bytes(pndTarget, abtCapdu, sizeof (abtCapdu), 0)) < 0) { nfc_perror (pndTarget, "nfc_target_receive_bytes"); if (!target_only_mode) { - nfc_disconnect (pndInitiator); + nfc_close (pndInitiator); } - nfc_disconnect (pndTarget); + nfc_close (pndTarget); exit(EXIT_FAILURE); } szCapduLen = (size_t) res; if (target_only_mode) { if (print_hex_fd4(abtCapdu, szCapduLen, "C-APDU") != EXIT_SUCCESS) { fprintf (stderr, "Error while printing C-APDU to FD4\n"); - nfc_disconnect (pndTarget); + nfc_close (pndTarget); exit(EXIT_FAILURE); } } } else { if (scan_hex_fd3(abtCapdu, &szCapduLen, "C-APDU") != EXIT_SUCCESS) { fprintf (stderr, "Error while scanning C-APDU from FD3\n"); - nfc_disconnect (pndInitiator); + nfc_close (pndInitiator); exit(EXIT_FAILURE); } } @@ -407,7 +407,7 @@ main (int argc, char *argv[]) } else { if (scan_hex_fd3(abtRapdu, &szRapduLen, "R-APDU") != EXIT_SUCCESS) { fprintf (stderr, "Error while scanning R-APDU from FD3\n"); - nfc_disconnect (pndTarget); + nfc_close (pndTarget); exit(EXIT_FAILURE); } ret = true; @@ -430,17 +430,17 @@ main (int argc, char *argv[]) if (nfc_target_send_bytes(pndTarget, abtRapdu, szRapduLen, 0) < 0) { nfc_perror (pndTarget, "nfc_target_send_bytes"); if (!target_only_mode) { - nfc_disconnect (pndInitiator); + nfc_close (pndInitiator); } if (!initiator_only_mode) { - nfc_disconnect (pndTarget); + nfc_close (pndTarget); } exit(EXIT_FAILURE); } } else { if (print_hex_fd4(abtRapdu, szRapduLen, "R-APDU") != EXIT_SUCCESS) { fprintf (stderr, "Error while printing R-APDU to FD4\n"); - nfc_disconnect (pndInitiator); + nfc_close (pndInitiator); exit(EXIT_FAILURE); } } @@ -448,10 +448,10 @@ main (int argc, char *argv[]) } if (!target_only_mode) { - nfc_disconnect (pndInitiator); + nfc_close (pndInitiator); } if (!initiator_only_mode) { - nfc_disconnect (pndTarget); + nfc_close (pndTarget); } exit (EXIT_SUCCESS); } From 324af418dbe4a0ce172bfd42349b129cefdc2d48 Mon Sep 17 00:00:00 2001 From: Audrey Diacre Date: Tue, 17 Jan 2012 15:21:56 +0000 Subject: [PATCH 100/113] rename nfc_connect() function to nfc_open(). --- examples/doc/quick_start_example1.c | 12 ++++++------ examples/nfc-anticol.c | 6 +++--- examples/nfc-dep-initiator.c | 6 +++--- examples/nfc-dep-target.c | 12 ++++++------ examples/nfc-emulate-forum-tag2.c | 6 +++--- examples/nfc-emulate-tag.c | 6 +++--- examples/nfc-emulate-uid.c | 6 +++--- examples/nfc-poll.c | 6 +++--- examples/nfc-relay.c | 12 ++++++------ examples/pn53x-diagnose.c | 6 +++--- examples/pn53x-sam.c | 12 ++++++------ examples/pn53x-tamashell.c | 6 +++--- include/nfc/nfc.h | 2 +- libnfc/drivers/acr122.c | 10 +++++----- libnfc/drivers/acr122.h | 2 +- libnfc/drivers/arygon.c | 8 ++++---- libnfc/drivers/arygon.h | 2 +- libnfc/drivers/pn532_uart.c | 8 ++++---- libnfc/drivers/pn532_uart.h | 2 +- libnfc/drivers/pn53x_usb.c | 4 ++-- libnfc/drivers/pn53x_usb.h | 2 +- libnfc/nfc-internal.h | 2 +- libnfc/nfc.c | 12 ++++++------ test/test_access_storm.c | 4 ++-- test/test_dep_active.c | 4 ++-- test/test_dep_passive.c | 4 ++-- test/test_device_modes_as_dep.c | 4 ++-- test/test_register_access.c | 4 ++-- test/test_register_endianness.c | 4 ++-- utils/nfc-emulate-forum-tag4.c | 6 +++--- utils/nfc-list.c | 12 ++++++------ utils/nfc-mfclassic.c | 6 +++--- utils/nfc-mfsetuid.c | 6 +++--- utils/nfc-mfultralight.c | 6 +++--- utils/nfc-read-forum-tag3.c | 6 +++--- utils/nfc-relay-picc.c | 18 +++++++++--------- 36 files changed, 117 insertions(+), 117 deletions(-) diff --git a/examples/doc/quick_start_example1.c b/examples/doc/quick_start_example1.c index 2273618..18b35e6 100644 --- a/examples/doc/quick_start_example1.c +++ b/examples/doc/quick_start_example1.c @@ -18,20 +18,20 @@ main (int argc, const char *argv[]) const char *acLibnfcVersion = nfc_version (); printf ("%s uses libnfc %s\n", argv[0], acLibnfcVersion); - // Connect using the first available NFC device - pnd = nfc_connect (NULL); + // Ope, using the first available NFC device + pnd = nfc_open (NULL); if (pnd == NULL) { - ERR ("%s", "Unable to connect to NFC device."); + ERR ("%s", "Unable to open NFC device."); return EXIT_FAILURE; } - // Set connected NFC device to initiator mode + // Set opened NFC device to initiator mode if (nfc_initiator_init (pnd) < 0) { nfc_perror (pnd, "nfc_initiator_init"); exit (EXIT_FAILURE); } - printf ("Connected to NFC reader: %s\n", nfc_device_get_name (pnd)); + printf ("NFC reader: %s opened\n", nfc_device_get_name (pnd)); // Poll for a ISO14443A (MIFARE) tag const nfc_modulation nmMifare = { @@ -51,7 +51,7 @@ main (int argc, const char *argv[]) print_hex (nt.nti.nai.abtAts, nt.nti.nai.szAtsLen); } } - // Disconnect from NFC device + // Close NFC device nfc_close (pnd); return EXIT_SUCCESS; } diff --git a/examples/nfc-anticol.c b/examples/nfc-anticol.c index f211931..c48517c 100644 --- a/examples/nfc-anticol.c +++ b/examples/nfc-anticol.c @@ -149,10 +149,10 @@ main (int argc, char *argv[]) } // Try to open the NFC reader - pnd = nfc_connect (NULL); + pnd = nfc_open (NULL); if (!pnd) { - printf ("Error connecting NFC reader\n"); + printf ("Error opening NFC reader\n"); exit(EXIT_FAILURE); } @@ -178,7 +178,7 @@ main (int argc, char *argv[]) exit (EXIT_FAILURE); } - printf ("Connected to NFC reader: %s\n\n", nfc_device_get_name (pnd)); + printf ("NFC reader: %s opened\n\n", nfc_device_get_name (pnd)); // Send the 7 bits request command specified in ISO 14443A (0x26) if (!transmit_bits (abtReqa, 7)) { diff --git a/examples/nfc-dep-initiator.c b/examples/nfc-dep-initiator.c index 7e4c4da..94adf8a 100644 --- a/examples/nfc-dep-initiator.c +++ b/examples/nfc-dep-initiator.c @@ -72,12 +72,12 @@ main (int argc, const char *argv[]) return EXIT_FAILURE; } - pnd = nfc_connect (NULL); + pnd = nfc_open (NULL); if (!pnd) { - printf("Unable to connect to NFC device.\n"); + printf("Unable to open NFC device.\n"); return EXIT_FAILURE; } - printf ("Connected to NFC device: %s\n", nfc_device_get_name (pnd)); + printf ("NFC device: %s\n opened", nfc_device_get_name (pnd)); signal (SIGINT, stop_dep_communication); diff --git a/examples/nfc-dep-target.c b/examples/nfc-dep-target.c index ceff103..fa1203c 100644 --- a/examples/nfc-dep-target.c +++ b/examples/nfc-dep-target.c @@ -68,13 +68,13 @@ main (int argc, const char *argv[]) nfc_connstring connstrings[MAX_DEVICE_COUNT]; size_t szDeviceFound = nfc_list_devices (connstrings, MAX_DEVICE_COUNT); // Little hack to allow using nfc-dep-initiator & nfc-dep-target from - // the same machine: if there is more than one readers connected - // nfc-dep-target will connect to the second reader + // the same machine: if there is more than one readers opened + // nfc-dep-target will open the second reader // (we hope they're always detected in the same order) if (szDeviceFound == 1) { - pnd = nfc_connect (connstrings[0]); + pnd = nfc_open (connstrings[0]); } else if (szDeviceFound > 1) { - pnd = nfc_connect (connstrings[1]); + pnd = nfc_open (connstrings[1]); } else { printf("No device found.\n"); return EXIT_FAILURE; @@ -107,10 +107,10 @@ main (int argc, const char *argv[]) }; if (!pnd) { - printf("Unable to connect to NFC device.\n"); + printf("Unable to open NFC device.\n"); return EXIT_FAILURE; } - printf ("Connected to NFC device: %s\n", nfc_device_get_name (pnd)); + printf ("NFC device: %s opened\n", nfc_device_get_name (pnd)); signal (SIGINT, stop_dep_communication); diff --git a/examples/nfc-emulate-forum-tag2.c b/examples/nfc-emulate-forum-tag2.c index feacba5..19fe7c7 100644 --- a/examples/nfc-emulate-forum-tag2.c +++ b/examples/nfc-emulate-forum-tag2.c @@ -182,14 +182,14 @@ main(int argc, char *argv[]) }; signal (SIGINT, stop_emulation); - pnd = nfc_connect (NULL); + pnd = nfc_open (NULL); if (pnd == NULL) { - ERR("Unable to connect to NFC device"); + ERR("Unable to open NFC device"); exit (EXIT_FAILURE); } - printf ("Connected to NFC device: %s\n", nfc_device_get_name (pnd)); + printf ("NFC device: %s opened\n", nfc_device_get_name (pnd)); printf ("Emulating NDEF tag now, please touch it with a second NFC device\n"); if (nfc_emulate_target (pnd, &emulator) < 0) { diff --git a/examples/nfc-emulate-tag.c b/examples/nfc-emulate-tag.c index 25bbb67..93dd0b6 100644 --- a/examples/nfc-emulate-tag.c +++ b/examples/nfc-emulate-tag.c @@ -180,18 +180,18 @@ main (int argc, char *argv[]) #endif // Try to open the NFC reader - pnd = nfc_connect (NULL); + pnd = nfc_open (NULL); // Display libnfc version acLibnfcVersion = nfc_version (); printf ("%s uses libnfc %s\n", argv[0], acLibnfcVersion); if (pnd == NULL) { - ERR("Unable to connect to NFC device"); + ERR("Unable to open NFC device"); exit (EXIT_FAILURE); } - printf ("Connected to NFC device: %s\n", nfc_device_get_name (pnd)); + printf ("NFC device: %s opened\n", nfc_device_get_name (pnd)); // Notes for ISO14443-A emulated tags: // * Only short UIDs are supported diff --git a/examples/nfc-emulate-uid.c b/examples/nfc-emulate-uid.c index 5609fd0..d2854cb 100644 --- a/examples/nfc-emulate-uid.c +++ b/examples/nfc-emulate-uid.c @@ -126,15 +126,15 @@ main (int argc, char *argv[]) #endif // Try to open the NFC device - pnd = nfc_connect (NULL); + pnd = nfc_open (NULL); if (pnd == NULL) { - printf ("Unable to connect to NFC device\n"); + printf ("Unable to open NFC device\n"); exit(EXIT_FAILURE); } printf ("\n"); - printf ("Connected to NFC device: %s\n", nfc_device_get_name (pnd)); + printf ("NFC device: %s opened\n", nfc_device_get_name (pnd)); printf ("[+] Try to break out the auto-emulation, this requires a second NFC device!\n"); printf ("[+] To do this, please send any command after the anti-collision\n"); printf ("[+] For example, send a RATS command or use the \"nfc-anticol\" or \"nfc-list\" tool.\n"); diff --git a/examples/nfc-poll.c b/examples/nfc-poll.c index 8ebe9c4..d800a14 100644 --- a/examples/nfc-poll.c +++ b/examples/nfc-poll.c @@ -103,10 +103,10 @@ main (int argc, const char *argv[]) nfc_target nt; int res = 0; - pnd = nfc_connect (NULL); + pnd = nfc_open (NULL); if (pnd == NULL) { - ERR ("%s", "Unable to connect to NFC device."); + ERR ("%s", "Unable to open NFC device."); exit (EXIT_FAILURE); } @@ -115,7 +115,7 @@ main (int argc, const char *argv[]) exit (EXIT_FAILURE); } - printf ("Connected to NFC reader: %s\n", nfc_device_get_name (pnd)); + printf ("NFC reader: %s opened\n", nfc_device_get_name (pnd)); printf ("NFC device will poll during %ld ms (%u pollings of %lu ms for %zd modulations)\n", (unsigned long) uiPollNr * szModulations * uiPeriod * 150, uiPollNr, (unsigned long) uiPeriod * 150, szModulations); if ((res = nfc_initiator_poll_target (pnd, nmModulations, szModulations, uiPollNr, uiPeriod, &nt)) < 0) { nfc_perror (pnd, "nfc_initiator_poll_target"); diff --git a/examples/nfc-relay.c b/examples/nfc-relay.c index 8be7b29..4fad476 100644 --- a/examples/nfc-relay.c +++ b/examples/nfc-relay.c @@ -112,19 +112,19 @@ main (int argc, char *argv[]) size_t szFound = nfc_list_devices (connstrings, MAX_DEVICE_COUNT); if (szFound < 2) { - ERR ("%zd device found but two connected devices are needed to relay NFC.", szFound); + ERR ("%zd device found but two opened devices are needed to relay NFC.", szFound); return EXIT_FAILURE; } // Try to open the NFC emulator device - pndTag = nfc_connect (connstrings[0]); + pndTag = nfc_open (connstrings[0]); if (pndTag == NULL) { - printf ("Error connecting NFC emulator device\n"); + printf ("Error opening NFC emulator device\n"); return EXIT_FAILURE; } printf ("Hint: tag <---> initiator (relay) <---> target (relay) <---> original reader\n\n"); - printf ("Connected to the NFC emulator device: %s\n", nfc_device_get_name (pndTag)); + printf ("NFC emulator device: %s opened\n", nfc_device_get_name (pndTag)); printf ("[+] Try to break out the auto-emulation, this requires a second reader!\n"); printf ("[+] To do this, please send any command after the anti-collision\n"); printf ("[+] For example, send a RATS command or use the \"nfc-anticol\" tool\n"); @@ -159,9 +159,9 @@ main (int argc, char *argv[]) printf ("%s", "Done, emulated tag is initialized"); // Try to open the NFC reader - pndReader = nfc_connect (connstrings[1]); + pndReader = nfc_open (connstrings[1]); - printf ("Connected to the NFC reader device: %s", nfc_device_get_name (pndReader)); + printf ("NFC reader device: %s opened", nfc_device_get_name (pndReader)); printf ("%s", "Configuring NFC reader settings..."); if (nfc_initiator_init (pndReader) < 0) { diff --git a/examples/pn53x-diagnose.c b/examples/pn53x-diagnose.c index 649f6e9..46f0532 100644 --- a/examples/pn53x-diagnose.c +++ b/examples/pn53x-diagnose.c @@ -79,14 +79,14 @@ main (int argc, const char *argv[]) } for (i = 0; i < szFound; i++) { - pnd = nfc_connect (connstrings[i]); + pnd = nfc_open (connstrings[i]); if (pnd == NULL) { - ERR ("%s", "Unable to connect to NFC device."); + ERR ("%s", "Unable to open NFC device."); return EXIT_FAILURE; } - printf ("NFC device [%s] connected.\n", nfc_device_get_name (pnd)); + printf ("NFC device [%s] opened.\n", nfc_device_get_name (pnd)); res = pn53x_transceive (pnd, pncmd_diagnose_communication_line_test, sizeof (pncmd_diagnose_communication_line_test), abtRx, szRx, 0); if (res > 0) { diff --git a/examples/pn53x-sam.c b/examples/pn53x-sam.c index 085b9ed..9745149 100644 --- a/examples/pn53x-sam.c +++ b/examples/pn53x-sam.c @@ -81,15 +81,15 @@ main (int argc, const char *argv[]) const char *acLibnfcVersion = nfc_version (); printf ("%s uses libnfc %s\n", argv[0], acLibnfcVersion); - // Connect using the first available NFC device - pnd = nfc_connect (NULL); + // Open using the first available NFC device + pnd = nfc_open (NULL); if (pnd == NULL) { - ERR ("%s", "Unable to connect to NFC device."); + ERR ("%s", "Unable to open NFC device."); return EXIT_FAILURE; } - printf ("Connected to NFC device: %s\n", nfc_device_get_name (pnd)); + printf ("NFC device: %s opened\n", nfc_device_get_name (pnd)); // Print the example's menu printf ("\nSelect the communication mode:\n"); @@ -127,7 +127,7 @@ main (int argc, const char *argv[]) { nfc_target nt; - // Set connected NFC device to initiator mode + // Set opened NFC device to initiator mode if (nfc_initiator_init (pnd) < 0) { nfc_perror (pnd, "nfc_initiator_init"); exit (EXIT_FAILURE); @@ -189,7 +189,7 @@ main (int argc, const char *argv[]) // Disconnect from the SAM pn53x_SAMConfiguration (pnd, PSM_NORMAL, 0); - // Disconnect from NFC device + // Close NFC device nfc_close (pnd); exit (EXIT_SUCCESS); diff --git a/examples/pn53x-tamashell.c b/examples/pn53x-tamashell.c index 7f5937d..7d23100 100644 --- a/examples/pn53x-tamashell.c +++ b/examples/pn53x-tamashell.c @@ -87,14 +87,14 @@ int main(int argc, const char* argv[]) } // Try to open the NFC reader - pnd = nfc_connect(NULL); + pnd = nfc_open(NULL); if (pnd == NULL) { - ERR ("%s", "Unable to connect to NFC device."); + ERR ("%s", "Unable to open NFC device."); return EXIT_FAILURE; } - printf ("Connected to NFC reader: %s\n", nfc_device_get_name (pnd)); + printf ("NFC reader: %s opened\n", nfc_device_get_name (pnd)); if (nfc_initiator_init (pnd) < 0) { nfc_perror (pnd, "nfc_initiator_init"); exit (EXIT_FAILURE); diff --git a/include/nfc/nfc.h b/include/nfc/nfc.h index 762316b..cdbf404 100644 --- a/include/nfc/nfc.h +++ b/include/nfc/nfc.h @@ -64,7 +64,7 @@ extern "C" { /* NFC Device/Hardware manipulation */ NFC_EXPORT bool nfc_get_default_device (nfc_connstring *connstring); - NFC_EXPORT nfc_device *nfc_connect (const nfc_connstring connstring); + NFC_EXPORT nfc_device *nfc_open (const nfc_connstring connstring); NFC_EXPORT void nfc_close (nfc_device *pnd); NFC_EXPORT int nfc_abort_command (nfc_device *pnd); NFC_EXPORT size_t nfc_list_devices (nfc_connstring connstrings[], size_t connstrings_len); diff --git a/libnfc/drivers/acr122.c b/libnfc/drivers/acr122.c index 9dc688b..7be531a 100644 --- a/libnfc/drivers/acr122.c +++ b/libnfc/drivers/acr122.c @@ -124,7 +124,7 @@ acr122_free_scardcontext (void) #define PCSC_MAX_DEVICES 16 /** - * @brief List connected devices + * @brief List opened devices * * Probe PCSC to find NFC capable hardware. * @@ -241,7 +241,7 @@ acr122_connstring_decode (const nfc_connstring connstring, struct acr122_descrip } nfc_device * -acr122_connect (const nfc_connstring connstring) +acr122_open (const nfc_connstring connstring) { struct acr122_descriptor ndd; int connstring_decode_level = acr122_connstring_decode (connstring, &ndd); @@ -249,7 +249,7 @@ acr122_connect (const nfc_connstring connstring) if (connstring_decode_level < 2) { return NULL; } - // FIXME: acr122_connect() does not take care about bus index + // FIXME: acr122_open() does not take care about bus index char *pcFirmware; nfc_device *pnd = nfc_device_new (); @@ -260,7 +260,7 @@ acr122_connect (const nfc_connstring connstring) SCARDCONTEXT *pscc; - log_put (LOG_CATEGORY, NFC_PRIORITY_TRACE, "Attempt to connect to %s", ndd.pcsc_device_name); + log_put (LOG_CATEGORY, NFC_PRIORITY_TRACE, "Attempt to open %s", ndd.pcsc_device_name); // Test if context succeeded if (!(pscc = acr122_get_scardcontext ())) goto error; @@ -466,7 +466,7 @@ const struct pn53x_io acr122_io = { const struct nfc_driver acr122_driver = { .name = ACR122_DRIVER_NAME, .probe = acr122_probe, - .connect = acr122_connect, + .open = acr122_open, .close = acr122_close, .strerror = pn53x_strerror, diff --git a/libnfc/drivers/acr122.h b/libnfc/drivers/acr122.h index 98c2c1c..5c2ba98 100644 --- a/libnfc/drivers/acr122.h +++ b/libnfc/drivers/acr122.h @@ -29,7 +29,7 @@ bool acr122_probe (nfc_connstring connstrings[], size_t connstrings_len, size_t *pszDeviceFound); // Functions used by developer to handle connection to this device -nfc_device *acr122_connect (const nfc_connstring connstring); +nfc_device *acr122_open (const nfc_connstring connstring); int acr122_send (nfc_device *pnd, const uint8_t *pbtData, const size_t szData, int timeout); int acr122_receive (nfc_device *pnd, uint8_t *pbtData, const size_t szData, int timeout); void acr122_close (nfc_device *pnd); diff --git a/libnfc/drivers/arygon.c b/libnfc/drivers/arygon.c index c6694a6..0b64d2f 100644 --- a/libnfc/drivers/arygon.c +++ b/libnfc/drivers/arygon.c @@ -216,7 +216,7 @@ arygon_connstring_decode (const nfc_connstring connstring, struct arygon_descrip } nfc_device * -arygon_connect (const nfc_connstring connstring) +arygon_open (const nfc_connstring connstring) { struct arygon_descriptor ndd; int connstring_decode_level = arygon_connstring_decode (connstring, &ndd); @@ -230,7 +230,7 @@ arygon_connect (const nfc_connstring connstring) serial_port sp; nfc_device *pnd = NULL; - log_put (LOG_CATEGORY, NFC_PRIORITY_TRACE, "Attempt to connect to: %s at %d bauds.", ndd.port, ndd.speed); + log_put (LOG_CATEGORY, NFC_PRIORITY_TRACE, "Attempt to open: %s at %d bauds.", ndd.port, ndd.speed); sp = uart_open (ndd.port); if (sp == INVALID_SERIAL_PORT) @@ -254,7 +254,7 @@ arygon_connect (const nfc_connstring connstring) // Alloc and init chip's data pn53x_data_new (pnd, &arygon_tama_io); - // The PN53x chip connected to ARYGON MCU doesn't seems to be in LowVBat mode + // The PN53x chip opened to ARYGON MCU doesn't seems to be in LowVBat mode CHIP_DATA (pnd)->power_mode = NORMAL; // empirical tuning @@ -560,7 +560,7 @@ const struct pn53x_io arygon_tama_io = { const struct nfc_driver arygon_driver = { .name = ARYGON_DRIVER_NAME, .probe = arygon_probe, - .connect = arygon_connect, + .open = arygon_open, .close = arygon_close, .strerror = pn53x_strerror, diff --git a/libnfc/drivers/arygon.h b/libnfc/drivers/arygon.h index c324e93..711a9c1 100644 --- a/libnfc/drivers/arygon.h +++ b/libnfc/drivers/arygon.h @@ -32,7 +32,7 @@ bool arygon_probe (nfc_connstring connstrings[], size_t connstrings_len, size_t *pszDeviceFound); -nfc_device *arygon_connect (const nfc_connstring connstring); +nfc_device *arygon_open (const nfc_connstring connstring); void arygon_close (nfc_device *pnd); int arygon_tama_send (nfc_device *pnd, const uint8_t *pbtData, const size_t szData, int timeout); diff --git a/libnfc/drivers/pn532_uart.c b/libnfc/drivers/pn532_uart.c index 7c4cad9..251da1a 100644 --- a/libnfc/drivers/pn532_uart.c +++ b/libnfc/drivers/pn532_uart.c @@ -91,7 +91,7 @@ pn532_uart_probe (nfc_connstring connstrings[], size_t connstrings_len, size_t * if ((sp != INVALID_SERIAL_PORT) && (sp != CLAIMED_SERIAL_PORT)) { // We need to flush input to be sure first reply does not comes from older byte transceive uart_flush_input (sp); - // Serial port claimed but we need to check if a PN532_UART is connected. + // Serial port claimed but we need to check if a PN532_UART is opened. uart_set_speed (sp, PN532_UART_DEFAULT_SPEED); nfc_device *pnd = nfc_device_new (); @@ -194,7 +194,7 @@ pn532_connstring_decode (const nfc_connstring connstring, struct pn532_uart_desc } nfc_device * -pn532_uart_connect (const nfc_connstring connstring) +pn532_uart_open (const nfc_connstring connstring) { struct pn532_uart_descriptor ndd; int connstring_decode_level = pn532_connstring_decode (connstring, &ndd); @@ -208,7 +208,7 @@ pn532_uart_connect (const nfc_connstring connstring) serial_port sp; nfc_device *pnd = NULL; - log_put (LOG_CATEGORY, NFC_PRIORITY_TRACE, "Attempt to connect to: %s at %d bauds.", ndd.port, ndd.speed); + log_put (LOG_CATEGORY, NFC_PRIORITY_TRACE, "Attempt to open: %s at %d bauds.", ndd.port, ndd.speed); sp = uart_open (ndd.port); if (sp == INVALID_SERIAL_PORT) @@ -503,7 +503,7 @@ const struct pn53x_io pn532_uart_io = { const struct nfc_driver pn532_uart_driver = { .name = PN532_UART_DRIVER_NAME, .probe = pn532_uart_probe, - .connect = pn532_uart_connect, + .open = pn532_uart_open, .close = pn532_uart_close, .strerror = pn53x_strerror, diff --git a/libnfc/drivers/pn532_uart.h b/libnfc/drivers/pn532_uart.h index 9b55347..c066e75 100644 --- a/libnfc/drivers/pn532_uart.h +++ b/libnfc/drivers/pn532_uart.h @@ -31,7 +31,7 @@ bool pn532_uart_probe (nfc_connstring connstrings[], size_t connstrings_len, size_t *pszDeviceFound); -nfc_device *pn532_uart_connect (const nfc_connstring connstring); +nfc_device *pn532_uart_open (const nfc_connstring connstring); void pn532_uart_close (nfc_device *pnd); int pn532_uart_send (nfc_device *pnd, const uint8_t *pbtData, const size_t szData, int timeout); int pn532_uart_receive (nfc_device *pnd, uint8_t *pbtData, const size_t szData, int timeout); diff --git a/libnfc/drivers/pn53x_usb.c b/libnfc/drivers/pn53x_usb.c index d42849e..8bf97cd 100644 --- a/libnfc/drivers/pn53x_usb.c +++ b/libnfc/drivers/pn53x_usb.c @@ -366,7 +366,7 @@ pn53x_usb_get_usb_device_name (struct usb_device *dev, usb_dev_handle *udev, cha } nfc_device * -pn53x_usb_connect (const nfc_connstring connstring) +pn53x_usb_open (const nfc_connstring connstring) { struct pn53x_usb_descriptor desc; int connstring_decode_level = pn53x_usb_connstring_decode (connstring, &desc); @@ -793,7 +793,7 @@ const struct pn53x_io pn53x_usb_io = { const struct nfc_driver pn53x_usb_driver = { .name = PN53X_USB_DRIVER_NAME, .probe = pn53x_usb_probe, - .connect = pn53x_usb_connect, + .open = pn53x_usb_open, .close = pn53x_usb_close, .strerror = pn53x_strerror, diff --git a/libnfc/drivers/pn53x_usb.h b/libnfc/drivers/pn53x_usb.h index 0827016..ac27b9b 100644 --- a/libnfc/drivers/pn53x_usb.h +++ b/libnfc/drivers/pn53x_usb.h @@ -30,7 +30,7 @@ # include bool pn53x_usb_probe (nfc_connstring connstrings[], size_t connstrings_len, size_t *pszDeviceFound); -nfc_device *pn53x_usb_connect (const nfc_connstring connstring); +nfc_device *pn53x_usb_open (const nfc_connstring connstring); int pn53x_usb_send (nfc_device *pnd, const uint8_t *pbtData, const size_t szData, int timeout); int pn53x_usb_receive (nfc_device *pnd, uint8_t *pbtData, const size_t szData, int timeout); void pn53x_usb_close (nfc_device *pnd); diff --git a/libnfc/nfc-internal.h b/libnfc/nfc-internal.h index 76f2731..c9bdf1d 100644 --- a/libnfc/nfc-internal.h +++ b/libnfc/nfc-internal.h @@ -128,7 +128,7 @@ struct nfc_driver { const char *name; bool (*probe)(nfc_connstring connstrings[], size_t connstrings_len, size_t * pszDeviceFound); - struct nfc_device *(*connect) (const nfc_connstring connstring); + struct nfc_device *(*open) (const nfc_connstring connstring); void (*close) (struct nfc_device *pnd); const char *(*strerror) (const struct nfc_device *pnd); diff --git a/libnfc/nfc.c b/libnfc/nfc.c index d97a9e3..6abc614 100644 --- a/libnfc/nfc.c +++ b/libnfc/nfc.c @@ -95,7 +95,7 @@ nfc_get_default_device (nfc_connstring *connstring) } /** - * @brief Connect to a NFC device + * @brief Open a NFC device * @param connstring The device connection string if specific device is wanted, \c NULL otherwise * @return Returns pointer to a \a nfc_device struct if successfull; otherwise returns \c NULL value. * @@ -111,7 +111,7 @@ nfc_get_default_device (nfc_connstring *connstring) * optionally followed by manual tuning of the parameters if the default parameters are not suiting your goals. */ nfc_device * -nfc_connect (const nfc_connstring connstring) +nfc_open (const nfc_connstring connstring) { log_init (); nfc_device *pnd = NULL; @@ -136,10 +136,10 @@ nfc_connect (const nfc_connstring connstring) continue; } - pnd = ndr->connect (ncs); - // Test if the connection was successful + pnd = ndr->open (ncs); + // Test if the opening was successful if (pnd == NULL) { - log_put (LOG_CATEGORY, NFC_PRIORITY_TRACE, "Unable to connect to \"%s\".", ncs); + log_put (LOG_CATEGORY, NFC_PRIORITY_TRACE, "Unable to open \"%s\".", ncs); log_fini (); return pnd; } @@ -167,7 +167,7 @@ nfc_close (nfc_device *pnd) if (pnd) { // Go in idle mode nfc_idle (pnd); - // Disconnect, clean up and release the device + // Close, clean up and release the device pnd->driver->close (pnd); log_fini (); diff --git a/test/test_access_storm.c b/test/test_access_storm.c index b80b6d0..e934cee 100644 --- a/test/test_access_storm.c +++ b/test/test_access_storm.c @@ -31,8 +31,8 @@ test_access_storm (void) nfc_device *device; nfc_target ant[MAX_TARGET_COUNT]; - device = nfc_connect (connstrings[i]); - cut_assert_not_null (device, cut_message ("nfc_connect")); + device = nfc_open (connstrings[i]); + cut_assert_not_null (device, cut_message ("nfc_open")); res = nfc_initiator_init(device); cut_assert_equal_int (0, res, cut_message ("nfc_initiator_init")); diff --git a/test/test_dep_active.c b/test/test_dep_active.c index a419044..58dda2c 100644 --- a/test/test_dep_active.c +++ b/test/test_dep_active.c @@ -32,8 +32,8 @@ cut_setup (void) cut_omit ("At least two NFC devices must be plugged-in to run this test"); } - devices[TARGET] = nfc_connect (connstrings[TARGET]); - devices[INITIATOR] = nfc_connect (connstrings[INITIATOR]); + devices[TARGET] = nfc_open (connstrings[TARGET]); + devices[INITIATOR] = nfc_open (connstrings[INITIATOR]); signal (SIGINT, abort_test_by_keypress); } diff --git a/test/test_dep_passive.c b/test/test_dep_passive.c index 6e55200..fb8cae6 100644 --- a/test/test_dep_passive.c +++ b/test/test_dep_passive.c @@ -31,8 +31,8 @@ cut_setup (void) cut_omit ("At least two NFC devices must be plugged-in to run this test"); } - devices[TARGET] = nfc_connect (connstrings[TARGET]); - devices[INITIATOR] = nfc_connect (connstrings[INITIATOR]); + devices[TARGET] = nfc_open (connstrings[TARGET]); + devices[INITIATOR] = nfc_open (connstrings[INITIATOR]); signal (SIGINT, abort_test_by_keypress); } diff --git a/test/test_device_modes_as_dep.c b/test/test_device_modes_as_dep.c index 3f5a297..04e1c48 100644 --- a/test/test_device_modes_as_dep.c +++ b/test/test_device_modes_as_dep.c @@ -29,8 +29,8 @@ cut_setup (void) cut_omit ("At least two NFC devices must be plugged-in to run this test"); } - second_device = nfc_connect (connstrings[0]); - first_device = nfc_connect (connstrings[1]); + second_device = nfc_open (connstrings[0]); + first_device = nfc_open (connstrings[1]); signal (SIGINT, abort_test_by_keypress); } diff --git a/test/test_register_access.c b/test/test_register_access.c index aa37994..d073cf9 100644 --- a/test/test_register_access.c +++ b/test/test_register_access.c @@ -18,8 +18,8 @@ test_register_endianness (void) nfc_device *device; - device = nfc_connect (connstrings[0]); - cut_assert_not_null (device, cut_message ("nfc_connect")); + device = nfc_open (connstrings[0]); + cut_assert_not_null (device, cut_message ("nfc_open")); uint8_t value; diff --git a/test/test_register_endianness.c b/test/test_register_endianness.c index 6c7ce7e..ab3dd70 100644 --- a/test/test_register_endianness.c +++ b/test/test_register_endianness.c @@ -19,8 +19,8 @@ test_register_endianness (void) nfc_device *device; - device = nfc_connect (connstrings[0]); - cut_assert_not_null (device, cut_message ("nfc_connect")); + device = nfc_open (connstrings[0]); + cut_assert_not_null (device, cut_message ("nfc_open")); uint8_t value; diff --git a/utils/nfc-emulate-forum-tag4.c b/utils/nfc-emulate-forum-tag4.c index 321b601..403bbd1 100644 --- a/utils/nfc-emulate-forum-tag4.c +++ b/utils/nfc-emulate-forum-tag4.c @@ -347,16 +347,16 @@ main (int argc, char *argv[]) } // Try to open the NFC reader - pnd = nfc_connect (NULL); + pnd = nfc_open (NULL); if (pnd == NULL) { - ERR("Unable to connect to NFC device"); + ERR("Unable to open NFC device"); exit (EXIT_FAILURE); } signal (SIGINT, stop_emulation); - printf ("Connected to NFC device: %s\n", nfc_device_get_name(pnd)); + printf ("NFC device: %s opened\n", nfc_device_get_name(pnd)); printf ("Emulating NDEF tag now, please touch it with a second NFC device\n"); if (0 != nfc_emulate_target (pnd, &emulator)) { // contains already nfc_target_init() call diff --git a/utils/nfc-list.c b/utils/nfc-list.c index 48c2466..876c569 100644 --- a/utils/nfc-list.c +++ b/utils/nfc-list.c @@ -95,7 +95,7 @@ main (int argc, const char *argv[]) /* Lazy way to open an NFC device */ #if 0 - pnd = nfc_connect (NULL); + pnd = nfc_open (NULL); #endif /* If specific device is wanted, i.e. an ARYGON device on /dev/ttyUSB0 */ @@ -104,7 +104,7 @@ main (int argc, const char *argv[]) ndd.pcDriver = "ARYGON"; ndd.pcPort = "/dev/ttyUSB0"; ndd.uiSpeed = 115200; - pnd = nfc_connect (&ndd); + pnd = nfc_open (&ndd); #endif /* If specific device is wanted, i.e. a SCL3711 on USB */ @@ -112,7 +112,7 @@ main (int argc, const char *argv[]) nfc_device_desc_t ndd; ndd.pcDriver = "PN533_USB"; strcpy(ndd.acDevice, "SCM Micro / SCL3711-NFC&RW"); - pnd = nfc_connect (&ndd); + pnd = nfc_open (&ndd); #endif nfc_connstring connstrings[MAX_DEVICE_COUNT]; size_t szDeviceFound = nfc_list_devices (connstrings, MAX_DEVICE_COUNT); @@ -123,10 +123,10 @@ main (int argc, const char *argv[]) for (i = 0; i < szDeviceFound; i++) { nfc_target ant[MAX_TARGET_COUNT]; - pnd = nfc_connect (connstrings[i]); + pnd = nfc_open (connstrings[i]); if (pnd == NULL) { - ERR ("%s", "Unable to connect to NFC device."); + ERR ("%s", "Unable to open NFC device."); return EXIT_FAILURE; } if (nfc_initiator_init (pnd) < 0) { @@ -134,7 +134,7 @@ main (int argc, const char *argv[]) exit (EXIT_FAILURE); } - printf ("Connected to NFC device: %s\n", nfc_device_get_name (pnd)); + printf ("NFC device: %s opened\n", nfc_device_get_name (pnd)); nfc_modulation nm; diff --git a/utils/nfc-mfclassic.c b/utils/nfc-mfclassic.c index 91efcd4..686cd30 100644 --- a/utils/nfc-mfclassic.c +++ b/utils/nfc-mfclassic.c @@ -538,9 +538,9 @@ main (int argc, const char *argv[]) // printf("Successfully opened required files\n"); // Try to open the NFC reader - pnd = nfc_connect (NULL); + pnd = nfc_open (NULL); if (pnd == NULL) { - printf ("Error connecting NFC reader\n"); + printf ("Error opening NFC reader\n"); exit (EXIT_FAILURE); } @@ -557,7 +557,7 @@ main (int argc, const char *argv[]) // Disable ISO14443-4 switching in order to read devices that emulate Mifare Classic with ISO14443-4 compliance. nfc_device_set_property_bool (pnd, NP_AUTO_ISO14443_4, false); - printf ("Connected to NFC reader: %s\n", nfc_device_get_name (pnd)); + printf ("NFC reader: %s opened\n", nfc_device_get_name (pnd)); // Try to find a MIFARE Classic tag if (nfc_initiator_select_passive_target (pnd, nmMifare, NULL, 0, &nt) < 0) { diff --git a/utils/nfc-mfsetuid.c b/utils/nfc-mfsetuid.c index 7304b27..470b821 100644 --- a/utils/nfc-mfsetuid.c +++ b/utils/nfc-mfsetuid.c @@ -178,10 +178,10 @@ main (int argc, char *argv[]) } // Try to open the NFC reader - pnd = nfc_connect (NULL); + pnd = nfc_open (NULL); if (!pnd) { - printf ("Error connecting NFC reader\n"); + printf ("Error opening NFC reader\n"); exit(EXIT_FAILURE); } @@ -207,7 +207,7 @@ main (int argc, char *argv[]) exit (EXIT_FAILURE); } - printf ("Connected to NFC reader: %s\n", nfc_device_get_name (pnd)); + printf ("NFC reader: %s opened\n", nfc_device_get_name (pnd)); // Send the 7 bits request command specified in ISO 14443A (0x26) if (!transmit_bits (abtReqa, 7)) { diff --git a/utils/nfc-mfultralight.c b/utils/nfc-mfultralight.c index e37646a..7959f31 100644 --- a/utils/nfc-mfultralight.c +++ b/utils/nfc-mfultralight.c @@ -205,9 +205,9 @@ main (int argc, const char *argv[]) DBG ("Successfully opened the dump file\n"); // Try to open the NFC device - pnd = nfc_connect (NULL); + pnd = nfc_open (NULL); if (pnd == NULL) { - ERR ("Error connecting NFC device\n"); + ERR ("Error opening NFC device\n"); return 1; } @@ -222,7 +222,7 @@ main (int argc, const char *argv[]) exit (EXIT_FAILURE); } - printf ("Connected to NFC device: %s\n", nfc_device_get_name (pnd)); + printf ("NFC device: %s opened\n", nfc_device_get_name (pnd)); // Try to find a MIFARE Ultralight tag if (nfc_initiator_select_passive_target (pnd, nmMifare, NULL, 0, &nt) < 0) { diff --git a/utils/nfc-read-forum-tag3.c b/utils/nfc-read-forum-tag3.c index 6eda7da..8d42efe 100644 --- a/utils/nfc-read-forum-tag3.c +++ b/utils/nfc-read-forum-tag3.c @@ -195,14 +195,14 @@ main(int argc, char *argv[]) } } - pnd = nfc_connect (NULL); + pnd = nfc_open (NULL); if (pnd == NULL) { - ERR("Unable to connect to NFC device"); + ERR("Unable to open NFC device"); exit (EXIT_FAILURE); } - fprintf (message_stream, "Connected to NFC device: %s\n", nfc_device_get_name (pnd)); + fprintf (message_stream, "NFC device: %s opened\n", nfc_device_get_name (pnd)); nfc_modulation nm = { .nmt = NMT_FELICA, diff --git a/utils/nfc-relay-picc.c b/utils/nfc-relay-picc.c index 4dcf884..44b32e6 100644 --- a/utils/nfc-relay-picc.c +++ b/utils/nfc-relay-picc.c @@ -204,7 +204,7 @@ main (int argc, char *argv[]) } else { if (szFound < 2) { - ERR ("%zd device found but two connected devices are needed to relay NFC.", szFound); + ERR ("%zd device found but two opened devices are needed to relay NFC.", szFound); return EXIT_FAILURE; } } @@ -213,20 +213,20 @@ main (int argc, char *argv[]) // Try to open the NFC reader used as initiator // Little hack to allow using initiator no matter if // there is already a target used locally or not on the same machine: - // if there is more than one readers connected we connect to the second reader + // if there is more than one readers opened we open the second reader // (we hope they're always detected in the same order) if (szFound == 1) { - pndInitiator = nfc_connect (connstrings[0]); + pndInitiator = nfc_open (connstrings[0]); } else { - pndInitiator = nfc_connect (connstrings[1]); + pndInitiator = nfc_open (connstrings[1]); } if (!pndInitiator) { - printf ("Error connecting NFC reader\n"); + printf ("Error opening NFC reader\n"); exit(EXIT_FAILURE); } - printf ("Connected to the NFC reader device: %s\n", nfc_device_get_name (pndInitiator)); + printf ("NFC reader device: %s opened\n", nfc_device_get_name (pndInitiator)); if (nfc_initiator_init (pndInitiator) < 0) { printf ("Error: fail initializing initiator\n"); @@ -342,16 +342,16 @@ main (int argc, char *argv[]) print_nfc_iso14443a_info (ntEmulatedTarget.nti.nai, false); // Try to open the NFC emulator device - pndTarget = nfc_connect (connstrings[0]); + pndTarget = nfc_open (connstrings[0]); if (pndTarget == NULL) { - printf ("Error connecting NFC emulator device\n"); + printf ("Error opening NFC emulator device\n"); if (!target_only_mode) { nfc_close (pndInitiator); } return EXIT_FAILURE; } - printf ("Connected to the NFC emulator device: %s\n", nfc_device_get_name (pndTarget)); + printf ("NFC emulator device: %s opened\n", nfc_device_get_name (pndTarget)); szCapduLen = sizeof (abtCapdu); if (nfc_target_init (pndTarget, &ntEmulatedTarget, abtCapdu, szCapduLen, 0) < 0) { From 38bdfe3281ded7f8ca28c40d94573a96673471e2 Mon Sep 17 00:00:00 2001 From: Romuald Conty Date: Wed, 18 Jan 2012 09:39:33 +0000 Subject: [PATCH 101/113] pn53x_usb: minor code clean up. --- libnfc/drivers/pn53x_usb.c | 31 ------------------------------- libnfc/drivers/pn53x_usb.h | 8 +++++--- 2 files changed, 5 insertions(+), 34 deletions(-) diff --git a/libnfc/drivers/pn53x_usb.c b/libnfc/drivers/pn53x_usb.c index 8bf97cd..3176ec3 100644 --- a/libnfc/drivers/pn53x_usb.c +++ b/libnfc/drivers/pn53x_usb.c @@ -66,37 +66,6 @@ Thanks to d18c7db and Okko for example code #define DRIVER_DATA(pnd) ((struct pn53x_usb_data*)(pnd->driver_data)) -/* This modified from some GNU example _not_ to overwrite y */ -int timeval_subtract(struct timeval *result, - const struct timeval *x, - const struct timeval *y) -{ - struct timeval tmp; - - tmp.tv_sec = y->tv_sec; - tmp.tv_usec = y->tv_usec; - - /* Perform the carry for the later subtraction */ - if (x->tv_usec < y->tv_usec) { - int nsec = (y->tv_usec - x->tv_usec) / 1000000 + 1; - tmp.tv_usec -= 1000000 * nsec; - tmp.tv_sec += nsec; - } - if (x->tv_usec - y->tv_usec > 1000000) { - int nsec = (x->tv_usec - y->tv_usec) / 1000000; - tmp.tv_usec += 1000000 * nsec; - tmp.tv_sec -= nsec; - } - - /* Compute the time remaining to wait. - tv_usec is certainly positive. */ - result->tv_sec = x->tv_sec - tmp.tv_sec; - result->tv_usec = x->tv_usec - tmp.tv_usec; - - /* Return 1 if result is negative. */ - return x->tv_sec < tmp.tv_sec; -} - typedef enum { UNKNOWN, NXP_PN531, diff --git a/libnfc/drivers/pn53x_usb.h b/libnfc/drivers/pn53x_usb.h index ac27b9b..566f0bb 100644 --- a/libnfc/drivers/pn53x_usb.h +++ b/libnfc/drivers/pn53x_usb.h @@ -23,11 +23,13 @@ */ #ifndef __NFC_DRIVER_PN53X_USB_H__ -# define __NFC_DRIVER_PN53X_USB_H__ +#define __NFC_DRIVER_PN53X_USB_H__ -# include +#include -# include +#include + +#include "nfc-internal.h" bool pn53x_usb_probe (nfc_connstring connstrings[], size_t connstrings_len, size_t *pszDeviceFound); nfc_device *pn53x_usb_open (const nfc_connstring connstring); From d1b0e93e8e6c57b6b4d7af6146d95ef840b55413 Mon Sep 17 00:00:00 2001 From: Audrey Diacre Date: Wed, 18 Jan 2012 09:53:45 +0000 Subject: [PATCH 102/113] add initialization and deinitialization. --- include/nfc/nfc.h | 4 ++++ libnfc/nfc.c | 20 ++++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/include/nfc/nfc.h b/include/nfc/nfc.h index cdbf404..63367ef 100644 --- a/include/nfc/nfc.h +++ b/include/nfc/nfc.h @@ -62,6 +62,10 @@ extern "C" { # endif // __cplusplus +/* Library initialization/deinitialization */ + NFC_EXPORT void nfc_init(); + NFC_EXPORT void nfc_exit(); + /* NFC Device/Hardware manipulation */ NFC_EXPORT bool nfc_get_default_device (nfc_connstring *connstring); NFC_EXPORT nfc_device *nfc_open (const nfc_connstring connstring); diff --git a/libnfc/nfc.c b/libnfc/nfc.c index 6abc614..5baf7dd 100644 --- a/libnfc/nfc.c +++ b/libnfc/nfc.c @@ -61,6 +61,26 @@ const struct nfc_driver *nfc_drivers[] = { NULL }; +/** + * @brief Initialize libnfc. + * + */ +void +nfc_init() +{ + log_init (); +} + +/** + * @brief Deinitialize libnfc. + * + */ +void +nfc_exit() +{ + log_fini (); +} + /** * @brief Get the defaut NFC device * @param connstring \a nfc_connstring pointer where the default connection string will be stored From 1d55b6f8c602dd130d40622f7e1faae91ee4d01d Mon Sep 17 00:00:00 2001 From: Audrey Diacre Date: Wed, 18 Jan 2012 11:09:01 +0000 Subject: [PATCH 103/113] examples, test and utils use now nfc_init() function. --- examples/doc/quick_start_example1.c | 4 +++- examples/nfc-anticol.c | 2 ++ examples/nfc-dep-initiator.c | 2 ++ examples/nfc-dep-target.c | 1 + examples/nfc-emulate-forum-tag2.c | 1 + examples/nfc-emulate-tag.c | 2 ++ examples/nfc-emulate-uid.c | 2 ++ examples/nfc-poll.c | 2 ++ examples/nfc-relay.c | 3 +++ examples/pn53x-diagnose.c | 3 +++ examples/pn53x-sam.c | 2 ++ examples/pn53x-tamashell.c | 2 ++ libnfc/nfc.c | 6 ++---- test/test_access_storm.c | 2 ++ test/test_dep_active.c | 2 +- test/test_dep_passive.c | 3 ++- test/test_device_modes_as_dep.c | 1 + test/test_register_access.c | 2 ++ test/test_register_endianness.c | 2 ++ utils/nfc-emulate-forum-tag4.c | 2 ++ utils/nfc-list.c | 2 ++ utils/nfc-mfclassic.c | 2 ++ utils/nfc-mfsetuid.c | 2 ++ utils/nfc-mfultralight.c | 2 ++ utils/nfc-read-forum-tag3.c | 2 ++ utils/nfc-relay-picc.c | 2 ++ 26 files changed, 51 insertions(+), 7 deletions(-) diff --git a/examples/doc/quick_start_example1.c b/examples/doc/quick_start_example1.c index 18b35e6..413e055 100644 --- a/examples/doc/quick_start_example1.c +++ b/examples/doc/quick_start_example1.c @@ -13,12 +13,14 @@ main (int argc, const char *argv[]) { nfc_device *pnd; nfc_target nt; + + nfc_init(); // Display libnfc version const char *acLibnfcVersion = nfc_version (); printf ("%s uses libnfc %s\n", argv[0], acLibnfcVersion); - // Ope, using the first available NFC device + // Open, using the first available NFC device pnd = nfc_open (NULL); if (pnd == NULL) { diff --git a/examples/nfc-anticol.c b/examples/nfc-anticol.c index c48517c..fc9d62d 100644 --- a/examples/nfc-anticol.c +++ b/examples/nfc-anticol.c @@ -148,6 +148,8 @@ main (int argc, char *argv[]) } } + nfc_init(); + // Try to open the NFC reader pnd = nfc_open (NULL); diff --git a/examples/nfc-dep-initiator.c b/examples/nfc-dep-initiator.c index 94adf8a..3c54877 100644 --- a/examples/nfc-dep-initiator.c +++ b/examples/nfc-dep-initiator.c @@ -71,6 +71,8 @@ main (int argc, const char *argv[]) printf ("Usage: %s\n", argv[0]); return EXIT_FAILURE; } + + nfc_init(); pnd = nfc_open (NULL); if (!pnd) { diff --git a/examples/nfc-dep-target.c b/examples/nfc-dep-target.c index fa1203c..4ca6636 100644 --- a/examples/nfc-dep-target.c +++ b/examples/nfc-dep-target.c @@ -71,6 +71,7 @@ main (int argc, const char *argv[]) // the same machine: if there is more than one readers opened // nfc-dep-target will open the second reader // (we hope they're always detected in the same order) + nfc_init (); if (szDeviceFound == 1) { pnd = nfc_open (connstrings[0]); } else if (szDeviceFound > 1) { diff --git a/examples/nfc-emulate-forum-tag2.c b/examples/nfc-emulate-forum-tag2.c index 19fe7c7..ea0c9d0 100644 --- a/examples/nfc-emulate-forum-tag2.c +++ b/examples/nfc-emulate-forum-tag2.c @@ -182,6 +182,7 @@ main(int argc, char *argv[]) }; signal (SIGINT, stop_emulation); + nfc_init (); pnd = nfc_open (NULL); if (pnd == NULL) { diff --git a/examples/nfc-emulate-tag.c b/examples/nfc-emulate-tag.c index 93dd0b6..509dff0 100644 --- a/examples/nfc-emulate-tag.c +++ b/examples/nfc-emulate-tag.c @@ -179,6 +179,8 @@ main (int argc, char *argv[]) signal (SIGINT, (void (*)()) intr_hdlr); #endif + nfc_init(); + // Try to open the NFC reader pnd = nfc_open (NULL); diff --git a/examples/nfc-emulate-uid.c b/examples/nfc-emulate-uid.c index d2854cb..5e28ca7 100644 --- a/examples/nfc-emulate-uid.c +++ b/examples/nfc-emulate-uid.c @@ -125,6 +125,8 @@ main (int argc, char *argv[]) signal (SIGINT, (void (*)()) intr_hdlr); #endif + nfc_init (); + // Try to open the NFC device pnd = nfc_open (NULL); diff --git a/examples/nfc-poll.c b/examples/nfc-poll.c index d800a14..12894c3 100644 --- a/examples/nfc-poll.c +++ b/examples/nfc-poll.c @@ -102,6 +102,8 @@ main (int argc, const char *argv[]) nfc_target nt; int res = 0; + + nfc_init (); pnd = nfc_open (NULL); diff --git a/examples/nfc-relay.c b/examples/nfc-relay.c index 4fad476..a255977 100644 --- a/examples/nfc-relay.c +++ b/examples/nfc-relay.c @@ -115,6 +115,9 @@ main (int argc, char *argv[]) ERR ("%zd device found but two opened devices are needed to relay NFC.", szFound); return EXIT_FAILURE; } + + nfc_init (); + // Try to open the NFC emulator device pndTag = nfc_open (connstrings[0]); if (pndTag == NULL) { diff --git a/examples/pn53x-diagnose.c b/examples/pn53x-diagnose.c index 46f0532..625bab9 100644 --- a/examples/pn53x-diagnose.c +++ b/examples/pn53x-diagnose.c @@ -67,6 +67,9 @@ main (int argc, const char *argv[]) if (argc > 1) { errx (1, "usage: %s", argv[0]); } + + nfc_init(); + // Display libnfc version acLibnfcVersion = nfc_version (); printf ("%s uses libnfc %s\n", argv[0], acLibnfcVersion); diff --git a/examples/pn53x-sam.c b/examples/pn53x-sam.c index 9745149..b584161 100644 --- a/examples/pn53x-sam.c +++ b/examples/pn53x-sam.c @@ -77,6 +77,8 @@ main (int argc, const char *argv[]) (void) argc; (void) argv; + nfc_init (); + // Display libnfc version const char *acLibnfcVersion = nfc_version (); printf ("%s uses libnfc %s\n", argv[0], acLibnfcVersion); diff --git a/examples/pn53x-tamashell.c b/examples/pn53x-tamashell.c index 7d23100..b2ed7ca 100644 --- a/examples/pn53x-tamashell.c +++ b/examples/pn53x-tamashell.c @@ -86,6 +86,8 @@ int main(int argc, const char* argv[]) } } + nfc_init (); + // Try to open the NFC reader pnd = nfc_open(NULL); diff --git a/libnfc/nfc.c b/libnfc/nfc.c index 5baf7dd..47a411a 100644 --- a/libnfc/nfc.c +++ b/libnfc/nfc.c @@ -63,7 +63,7 @@ const struct nfc_driver *nfc_drivers[] = { /** * @brief Initialize libnfc. - * + * This function must be called before calling any other libnfc function */ void nfc_init() @@ -75,7 +75,7 @@ nfc_init() * @brief Deinitialize libnfc. * */ -void +void nfc_exit() { log_fini (); @@ -133,7 +133,6 @@ nfc_get_default_device (nfc_connstring *connstring) nfc_device * nfc_open (const nfc_connstring connstring) { - log_init (); nfc_device *pnd = NULL; nfc_connstring ncs; @@ -209,7 +208,6 @@ nfc_list_devices (nfc_connstring connstrings[] , size_t szDevices) const struct nfc_driver *ndr; const struct nfc_driver **pndr = nfc_drivers; - log_init (); while ((ndr = *pndr)) { szN = 0; if (ndr->probe (connstrings + (szDeviceFound), szDevices - (szDeviceFound), &szN)) { diff --git a/test/test_access_storm.c b/test/test_access_storm.c index e934cee..d373401 100644 --- a/test/test_access_storm.c +++ b/test/test_access_storm.c @@ -17,6 +17,8 @@ test_access_storm (void) nfc_connstring connstrings[MAX_DEVICE_COUNT]; int res = 0; + nfc_init (); + size_t ref_device_count = nfc_list_devices (connstrings, MAX_DEVICE_COUNT); if (!ref_device_count) cut_omit ("No NFC device found"); diff --git a/test/test_dep_active.c b/test/test_dep_active.c index 58dda2c..69f3fb3 100644 --- a/test/test_dep_active.c +++ b/test/test_dep_active.c @@ -31,7 +31,7 @@ cut_setup (void) if (n < 2) { cut_omit ("At least two NFC devices must be plugged-in to run this test"); } - + nfc_init (); devices[TARGET] = nfc_open (connstrings[TARGET]); devices[INITIATOR] = nfc_open (connstrings[INITIATOR]); diff --git a/test/test_dep_passive.c b/test/test_dep_passive.c index fb8cae6..93a1491 100644 --- a/test/test_dep_passive.c +++ b/test/test_dep_passive.c @@ -30,7 +30,8 @@ cut_setup (void) if (n < 2) { cut_omit ("At least two NFC devices must be plugged-in to run this test"); } - + + nfc_init (); devices[TARGET] = nfc_open (connstrings[TARGET]); devices[INITIATOR] = nfc_open (connstrings[INITIATOR]); diff --git a/test/test_device_modes_as_dep.c b/test/test_device_modes_as_dep.c index 04e1c48..0b64bfb 100644 --- a/test/test_device_modes_as_dep.c +++ b/test/test_device_modes_as_dep.c @@ -29,6 +29,7 @@ cut_setup (void) cut_omit ("At least two NFC devices must be plugged-in to run this test"); } + nfc_init (); second_device = nfc_open (connstrings[0]); first_device = nfc_open (connstrings[1]); diff --git a/test/test_register_access.c b/test/test_register_access.c index d073cf9..a126815 100644 --- a/test/test_register_access.c +++ b/test/test_register_access.c @@ -11,6 +11,8 @@ test_register_endianness (void) { nfc_connstring connstrings[MAX_DEVICE_COUNT]; int res = 0; + + nfc_init (); size_t device_count = nfc_list_devices (connstrings, MAX_DEVICE_COUNT); if (!device_count) diff --git a/test/test_register_endianness.c b/test/test_register_endianness.c index ab3dd70..f0239af 100644 --- a/test/test_register_endianness.c +++ b/test/test_register_endianness.c @@ -12,6 +12,8 @@ test_register_endianness (void) { nfc_connstring connstrings[MAX_DEVICE_COUNT]; int res = 0; + + nfc_init (); size_t device_count = nfc_list_devices (connstrings, MAX_DEVICE_COUNT); if (!device_count) diff --git a/utils/nfc-emulate-forum-tag4.c b/utils/nfc-emulate-forum-tag4.c index 403bbd1..d2794d6 100644 --- a/utils/nfc-emulate-forum-tag4.c +++ b/utils/nfc-emulate-forum-tag4.c @@ -345,6 +345,8 @@ main (int argc, char *argv[]) err (EXIT_FAILURE, "Can't load NDEF file '%s'", argv[1]); } } + + nfc_init (); // Try to open the NFC reader pnd = nfc_open (NULL); diff --git a/utils/nfc-list.c b/utils/nfc-list.c index 876c569..0515682 100644 --- a/utils/nfc-list.c +++ b/utils/nfc-list.c @@ -75,6 +75,8 @@ main (int argc, const char *argv[]) bool verbose = false; int res = 0; + nfc_init (); + // Display libnfc version acLibnfcVersion = nfc_version (); printf ("%s uses libnfc %s\n", argv[0], acLibnfcVersion); diff --git a/utils/nfc-mfclassic.c b/utils/nfc-mfclassic.c index 686cd30..481b0de 100644 --- a/utils/nfc-mfclassic.c +++ b/utils/nfc-mfclassic.c @@ -537,6 +537,8 @@ main (int argc, const char *argv[]) } // printf("Successfully opened required files\n"); + nfc_init (); + // Try to open the NFC reader pnd = nfc_open (NULL); if (pnd == NULL) { diff --git a/utils/nfc-mfsetuid.c b/utils/nfc-mfsetuid.c index 470b821..8c8ed7f 100644 --- a/utils/nfc-mfsetuid.c +++ b/utils/nfc-mfsetuid.c @@ -177,6 +177,8 @@ main (int argc, char *argv[]) } } + nfc_init (); + // Try to open the NFC reader pnd = nfc_open (NULL); diff --git a/utils/nfc-mfultralight.c b/utils/nfc-mfultralight.c index 7959f31..dfc0000 100644 --- a/utils/nfc-mfultralight.c +++ b/utils/nfc-mfultralight.c @@ -204,6 +204,8 @@ main (int argc, const char *argv[]) } DBG ("Successfully opened the dump file\n"); + nfc_init (); + // Try to open the NFC device pnd = nfc_open (NULL); if (pnd == NULL) { diff --git a/utils/nfc-read-forum-tag3.c b/utils/nfc-read-forum-tag3.c index 8d42efe..8a80b28 100644 --- a/utils/nfc-read-forum-tag3.c +++ b/utils/nfc-read-forum-tag3.c @@ -194,6 +194,8 @@ main(int argc, char *argv[]) exit (EXIT_FAILURE); } } + + nfc_init (); pnd = nfc_open (NULL); diff --git a/utils/nfc-relay-picc.c b/utils/nfc-relay-picc.c index 44b32e6..0ef048d 100644 --- a/utils/nfc-relay-picc.c +++ b/utils/nfc-relay-picc.c @@ -190,6 +190,8 @@ main (int argc, char *argv[]) signal (SIGINT, (void (*)()) intr_hdlr); #endif + nfc_init (); + nfc_connstring connstrings[MAX_DEVICE_COUNT]; // List available devices size_t szFound = nfc_list_devices (connstrings, MAX_DEVICE_COUNT); From 207199dc34c6bba921177580555461f8814d9c98 Mon Sep 17 00:00:00 2001 From: Audrey Diacre Date: Wed, 18 Jan 2012 11:36:18 +0000 Subject: [PATCH 104/113] examples, test and utils use now nfc_exit() function. --- examples/doc/quick_start_example1.c | 1 + examples/nfc-anticol.c | 2 ++ examples/nfc-dep-initiator.c | 1 + examples/nfc-dep-target.c | 1 + examples/nfc-emulate-forum-tag2.c | 2 ++ examples/nfc-emulate-tag.c | 4 +++- examples/nfc-emulate-uid.c | 2 ++ examples/nfc-poll.c | 2 ++ examples/nfc-relay.c | 4 +++- examples/pn53x-sam.c | 1 + examples/pn53x-tamashell.c | 1 + libnfc/nfc.c | 5 ++--- test/test_access_storm.c | 1 + test/test_dep_active.c | 1 + test/test_dep_passive.c | 1 + test/test_device_modes_as_dep.c | 1 + test/test_register_access.c | 1 + test/test_register_endianness.c | 1 + utils/nfc-emulate-forum-tag4.c | 3 ++- utils/nfc-list.c | 3 ++- utils/nfc-mfclassic.c | 4 +++- utils/nfc-mfsetuid.c | 2 ++ utils/nfc-mfultralight.c | 4 +++- utils/nfc-read-forum-tag3.c | 1 + utils/nfc-relay-picc.c | 19 +++++++++++++++++++ 25 files changed, 59 insertions(+), 9 deletions(-) diff --git a/examples/doc/quick_start_example1.c b/examples/doc/quick_start_example1.c index 413e055..3215caf 100644 --- a/examples/doc/quick_start_example1.c +++ b/examples/doc/quick_start_example1.c @@ -55,5 +55,6 @@ main (int argc, const char *argv[]) } // Close NFC device nfc_close (pnd); + nfc_exit (); return EXIT_SUCCESS; } diff --git a/examples/nfc-anticol.c b/examples/nfc-anticol.c index fc9d62d..ec5dd17 100644 --- a/examples/nfc-anticol.c +++ b/examples/nfc-anticol.c @@ -186,6 +186,7 @@ main (int argc, char *argv[]) if (!transmit_bits (abtReqa, 7)) { printf ("Error: No tag available\n"); nfc_close (pnd); + nfc_exit (); return 1; } memcpy (abtAtqa, abtRx, 2); @@ -315,5 +316,6 @@ main (int argc, char *argv[]) } nfc_close (pnd); + nfc_exit (); return 0; } diff --git a/examples/nfc-dep-initiator.c b/examples/nfc-dep-initiator.c index 3c54877..dc2d82f 100644 --- a/examples/nfc-dep-initiator.c +++ b/examples/nfc-dep-initiator.c @@ -110,5 +110,6 @@ main (int argc, const char *argv[]) error: nfc_close (pnd); + nfc_exit (); return EXIT_SUCCESS; } diff --git a/examples/nfc-dep-target.c b/examples/nfc-dep-target.c index 4ca6636..592b9e1 100644 --- a/examples/nfc-dep-target.c +++ b/examples/nfc-dep-target.c @@ -141,5 +141,6 @@ main (int argc, const char *argv[]) error: nfc_close (pnd); + nfc_exit (); return EXIT_SUCCESS; } diff --git a/examples/nfc-emulate-forum-tag2.c b/examples/nfc-emulate-forum-tag2.c index ea0c9d0..8af29a6 100644 --- a/examples/nfc-emulate-forum-tag2.c +++ b/examples/nfc-emulate-forum-tag2.c @@ -198,6 +198,7 @@ main(int argc, char *argv[]) } nfc_close(pnd); + nfc_exit (); exit (EXIT_SUCCESS); @@ -205,5 +206,6 @@ error: if (pnd) { nfc_perror (pnd, argv[0]); nfc_close (pnd); + nfc_exit (); } } diff --git a/examples/nfc-emulate-tag.c b/examples/nfc-emulate-tag.c index 509dff0..0e7a4c3 100644 --- a/examples/nfc-emulate-tag.c +++ b/examples/nfc-emulate-tag.c @@ -65,8 +65,9 @@ intr_hdlr (void) { printf ("\nQuitting...\n"); if (pnd != NULL) { - nfc_close(pnd); + nfc_close(pnd); } + nfc_exit (); exit (EXIT_FAILURE); } @@ -268,6 +269,7 @@ main (int argc, char *argv[]) } nfc_close(pnd); + nfc_exit (); exit (EXIT_SUCCESS); } diff --git a/examples/nfc-emulate-uid.c b/examples/nfc-emulate-uid.c index 5e28ca7..df14eb2 100644 --- a/examples/nfc-emulate-uid.c +++ b/examples/nfc-emulate-uid.c @@ -219,9 +219,11 @@ main (int argc, char *argv[]) } } nfc_close (pnd); + nfc_exit (); exit (EXIT_SUCCESS); error: nfc_close (pnd); + nfc_exit (); exit (EXIT_FAILURE); } diff --git a/examples/nfc-poll.c b/examples/nfc-poll.c index 12894c3..2cfc115 100644 --- a/examples/nfc-poll.c +++ b/examples/nfc-poll.c @@ -122,6 +122,7 @@ main (int argc, const char *argv[]) if ((res = nfc_initiator_poll_target (pnd, nmModulations, szModulations, uiPollNr, uiPeriod, &nt)) < 0) { nfc_perror (pnd, "nfc_initiator_poll_target"); nfc_close (pnd); + nfc_exit (); exit (EXIT_FAILURE); } @@ -131,5 +132,6 @@ main (int argc, const char *argv[]) printf ("No target found.\n"); } nfc_close (pnd); + nfc_exit (); exit (EXIT_SUCCESS); } diff --git a/examples/nfc-relay.c b/examples/nfc-relay.c index a255977..580b8d0 100644 --- a/examples/nfc-relay.c +++ b/examples/nfc-relay.c @@ -151,6 +151,7 @@ main (int argc, char *argv[]) if ((szReaderRxBits = nfc_target_init (pndTag, &nt, abtReaderRx, sizeof (abtReaderRx), 0)) < 0) { ERR ("%s", "Initialization of NFC emulator failed"); nfc_close (pndTag); + nfc_exit (); return EXIT_FAILURE; } printf ("%s", "Configuring emulator settings..."); @@ -218,7 +219,8 @@ main (int argc, char *argv[]) } } - nfc_close (pndTag); + nfc_close (pndTag); nfc_close (pndReader); + nfc_exit (); exit (EXIT_SUCCESS); } diff --git a/examples/pn53x-sam.c b/examples/pn53x-sam.c index b584161..8de030e 100644 --- a/examples/pn53x-sam.c +++ b/examples/pn53x-sam.c @@ -193,6 +193,7 @@ main (int argc, const char *argv[]) // Close NFC device nfc_close (pnd); + nfc_exit (); exit (EXIT_SUCCESS); } diff --git a/examples/pn53x-tamashell.c b/examples/pn53x-tamashell.c index b2ed7ca..bcd6341 100644 --- a/examples/pn53x-tamashell.c +++ b/examples/pn53x-tamashell.c @@ -200,5 +200,6 @@ int main(int argc, const char* argv[]) fclose(input); } nfc_close(pnd); + nfc_exit (); return 1; } diff --git a/libnfc/nfc.c b/libnfc/nfc.c index 47a411a..c03d91c 100644 --- a/libnfc/nfc.c +++ b/libnfc/nfc.c @@ -73,7 +73,7 @@ nfc_init() /** * @brief Deinitialize libnfc. - * + * Should be called after closing all open devices and before your application terminates. */ void nfc_exit() @@ -188,8 +188,7 @@ nfc_close (nfc_device *pnd) nfc_idle (pnd); // Close, clean up and release the device pnd->driver->close (pnd); - - log_fini (); + } } diff --git a/test/test_access_storm.c b/test/test_access_storm.c index d373401..6609f3f 100644 --- a/test/test_access_storm.c +++ b/test/test_access_storm.c @@ -51,4 +51,5 @@ test_access_storm (void) n--; } + nfc_exit (); } diff --git a/test/test_dep_active.c b/test/test_dep_active.c index 69f3fb3..c8eb2a5 100644 --- a/test/test_dep_active.c +++ b/test/test_dep_active.c @@ -43,6 +43,7 @@ cut_teardown (void) { nfc_close (devices[TARGET]); nfc_close (devices[INITIATOR]); + nfc_exit (); } struct thread_data { diff --git a/test/test_dep_passive.c b/test/test_dep_passive.c index 93a1491..ff0ffb0 100644 --- a/test/test_dep_passive.c +++ b/test/test_dep_passive.c @@ -43,6 +43,7 @@ cut_teardown (void) { nfc_close (devices[TARGET]); nfc_close (devices[INITIATOR]); + nfc_exit (); } struct thread_data { diff --git a/test/test_device_modes_as_dep.c b/test/test_device_modes_as_dep.c index 0b64bfb..cd45661 100644 --- a/test/test_device_modes_as_dep.c +++ b/test/test_device_modes_as_dep.c @@ -41,6 +41,7 @@ cut_teardown (void) { nfc_close (second_device); nfc_close (first_device); + nfc_exit (); } struct thread_data { diff --git a/test/test_register_access.c b/test/test_register_access.c index a126815..4839437 100644 --- a/test/test_register_access.c +++ b/test/test_register_access.c @@ -44,4 +44,5 @@ test_register_endianness (void) cut_assert_equal_uint (0x55, value, cut_message ("check register value")); nfc_close (device); + nfc_exit (); } diff --git a/test/test_register_endianness.c b/test/test_register_endianness.c index f0239af..a6b15c4 100644 --- a/test/test_register_endianness.c +++ b/test/test_register_endianness.c @@ -35,4 +35,5 @@ test_register_endianness (void) cut_assert_equal_int (0, res, cut_message ("read register 0xFFF0")); nfc_close (device); + nfc_exit (); } diff --git a/utils/nfc-emulate-forum-tag4.c b/utils/nfc-emulate-forum-tag4.c index d2794d6..65ff665 100644 --- a/utils/nfc-emulate-forum-tag4.c +++ b/utils/nfc-emulate-forum-tag4.c @@ -372,6 +372,7 @@ main (int argc, char *argv[]) err (EXIT_FAILURE, "Can't save NDEF file '%s'", argv[2]); } } - + + nfc_exit (); exit (EXIT_SUCCESS); } diff --git a/utils/nfc-list.c b/utils/nfc-list.c index 0515682..6afd126 100644 --- a/utils/nfc-list.c +++ b/utils/nfc-list.c @@ -251,6 +251,7 @@ main (int argc, const char *argv[]) } nfc_close (pnd); } - + + nfc_exit (); return 0; } diff --git a/utils/nfc-mfclassic.c b/utils/nfc-mfclassic.c index 481b0de..450ae6a 100644 --- a/utils/nfc-mfclassic.c +++ b/utils/nfc-mfclassic.c @@ -565,6 +565,7 @@ main (int argc, const char *argv[]) if (nfc_initiator_select_passive_target (pnd, nmMifare, NULL, 0, &nt) < 0) { printf ("Error: no tag was found\n"); nfc_close (pnd); + nfc_exit (); exit (EXIT_FAILURE); } // Test if we are dealing with a MIFARE compatible tag @@ -663,6 +664,7 @@ main (int argc, const char *argv[]) printf ("Done, all bytes have been extracted!\n"); } }; - + + nfc_exit (); exit (EXIT_SUCCESS); } diff --git a/utils/nfc-mfsetuid.c b/utils/nfc-mfsetuid.c index 8c8ed7f..b2cbf3f 100644 --- a/utils/nfc-mfsetuid.c +++ b/utils/nfc-mfsetuid.c @@ -215,6 +215,7 @@ main (int argc, char *argv[]) if (!transmit_bits (abtReqa, 7)) { printf ("Error: No tag available\n"); nfc_close (pnd); + nfc_exit (); return 1; } memcpy (abtAtqa, abtRx, 2); @@ -353,5 +354,6 @@ main (int argc, char *argv[]) nfc_close (pnd); + nfc_exit (); return 0; } diff --git a/utils/nfc-mfultralight.c b/utils/nfc-mfultralight.c index dfc0000..515001e 100644 --- a/utils/nfc-mfultralight.c +++ b/utils/nfc-mfultralight.c @@ -230,6 +230,7 @@ main (int argc, const char *argv[]) if (nfc_initiator_select_passive_target (pnd, nmMifare, NULL, 0, &nt) < 0) { ERR ("no tag was found\n"); nfc_close (pnd); + nfc_exit (); return 1; } // Test if we are dealing with a MIFARE compatible tag @@ -237,6 +238,7 @@ main (int argc, const char *argv[]) if (nt.nti.nai.abtAtqa[1] != 0x44) { ERR ("tag is not a MIFARE Ultralight card\n"); nfc_close (pnd); + nfc_exit (); return EXIT_FAILURE; } // Get the info from the current tag @@ -268,6 +270,6 @@ main (int argc, const char *argv[]) } nfc_close (pnd); - + nfc_exit (); return EXIT_SUCCESS; } diff --git a/utils/nfc-read-forum-tag3.c b/utils/nfc-read-forum-tag3.c index 8a80b28..c91b7a7 100644 --- a/utils/nfc-read-forum-tag3.c +++ b/utils/nfc-read-forum-tag3.c @@ -317,5 +317,6 @@ error: if (pnd) { nfc_close (pnd); } + nfc_exit (); exit (error); } diff --git a/utils/nfc-relay-picc.c b/utils/nfc-relay-picc.c index 0ef048d..ace9f74 100644 --- a/utils/nfc-relay-picc.c +++ b/utils/nfc-relay-picc.c @@ -233,6 +233,7 @@ main (int argc, char *argv[]) if (nfc_initiator_init (pndInitiator) < 0) { printf ("Error: fail initializing initiator\n"); nfc_close (pndInitiator); + nfc_exit (); exit (EXIT_FAILURE); } @@ -244,6 +245,7 @@ main (int argc, char *argv[]) if (nfc_initiator_select_passive_target (pndInitiator, nm, NULL, 0, &ntRealTarget) < 0) { printf ("Error: no tag was found\n"); nfc_close (pndInitiator); + nfc_exit (); exit (EXIT_FAILURE); } @@ -253,21 +255,25 @@ main (int argc, char *argv[]) if (print_hex_fd4(ntRealTarget.nti.nai.abtUid, ntRealTarget.nti.nai.szUidLen, "UID") != EXIT_SUCCESS) { fprintf (stderr, "Error while printing UID to FD4\n"); nfc_close (pndInitiator); + nfc_exit (); exit(EXIT_FAILURE); } if (print_hex_fd4(ntRealTarget.nti.nai.abtAtqa, 2, "ATQA") != EXIT_SUCCESS) { fprintf (stderr, "Error while printing ATQA to FD4\n"); nfc_close (pndInitiator); + nfc_exit (); exit(EXIT_FAILURE); } if (print_hex_fd4(&(ntRealTarget.nti.nai.btSak), 1, "SAK") != EXIT_SUCCESS) { fprintf (stderr, "Error while printing SAK to FD4\n"); nfc_close (pndInitiator); + nfc_exit (); exit(EXIT_FAILURE); } if (print_hex_fd4(ntRealTarget.nti.nai.abtAts, ntRealTarget.nti.nai.szAtsLen, "ATS") != EXIT_SUCCESS) { fprintf (stderr, "Error while printing ATS to FD4\n"); nfc_close (pndInitiator); + nfc_exit (); exit(EXIT_FAILURE); } } @@ -291,6 +297,7 @@ main (int argc, char *argv[]) if (scan_hex_fd3(ntEmulatedTarget.nti.nai.abtUid, &(ntEmulatedTarget.nti.nai.szUidLen), "UID") != EXIT_SUCCESS) { fprintf (stderr, "Error while scanning UID from FD3\n"); nfc_close (pndInitiator); + nfc_exit (); exit(EXIT_FAILURE); } if (scan_hex_fd3(ntEmulatedTarget.nti.nai.abtAtqa, &foo, "ATQA") != EXIT_SUCCESS) { @@ -301,11 +308,13 @@ main (int argc, char *argv[]) if (scan_hex_fd3(&(ntEmulatedTarget.nti.nai.btSak), &foo, "SAK") != EXIT_SUCCESS) { fprintf (stderr, "Error while scanning SAK from FD3\n"); nfc_close (pndInitiator); + nfc_exit (); exit(EXIT_FAILURE); } if (scan_hex_fd3(ntEmulatedTarget.nti.nai.abtAts, &(ntEmulatedTarget.nti.nai.szAtsLen), "ATS") != EXIT_SUCCESS) { fprintf (stderr, "Error while scanning ATS from FD3\n"); nfc_close (pndInitiator); + nfc_exit (); exit(EXIT_FAILURE); } } else { @@ -350,6 +359,7 @@ main (int argc, char *argv[]) if (!target_only_mode) { nfc_close (pndInitiator); } + nfc_exit (); return EXIT_FAILURE; } @@ -362,6 +372,7 @@ main (int argc, char *argv[]) nfc_close (pndInitiator); } nfc_close (pndTarget); + nfc_exit (); exit(EXIT_FAILURE); } printf ("%s\n", "Done, relaying frames now!"); @@ -379,6 +390,7 @@ main (int argc, char *argv[]) nfc_close (pndInitiator); } nfc_close (pndTarget); + nfc_exit (); exit(EXIT_FAILURE); } szCapduLen = (size_t) res; @@ -386,6 +398,7 @@ main (int argc, char *argv[]) if (print_hex_fd4(abtCapdu, szCapduLen, "C-APDU") != EXIT_SUCCESS) { fprintf (stderr, "Error while printing C-APDU to FD4\n"); nfc_close (pndTarget); + nfc_exit (); exit(EXIT_FAILURE); } } @@ -393,6 +406,7 @@ main (int argc, char *argv[]) if (scan_hex_fd3(abtCapdu, &szCapduLen, "C-APDU") != EXIT_SUCCESS) { fprintf (stderr, "Error while scanning C-APDU from FD3\n"); nfc_close (pndInitiator); + nfc_exit (); exit(EXIT_FAILURE); } } @@ -410,6 +424,7 @@ main (int argc, char *argv[]) if (scan_hex_fd3(abtRapdu, &szRapduLen, "R-APDU") != EXIT_SUCCESS) { fprintf (stderr, "Error while scanning R-APDU from FD3\n"); nfc_close (pndTarget); + nfc_exit (); exit(EXIT_FAILURE); } ret = true; @@ -436,13 +451,16 @@ main (int argc, char *argv[]) } if (!initiator_only_mode) { nfc_close (pndTarget); + nfc_exit (); } + nfc_exit (); exit(EXIT_FAILURE); } } else { if (print_hex_fd4(abtRapdu, szRapduLen, "R-APDU") != EXIT_SUCCESS) { fprintf (stderr, "Error while printing R-APDU to FD4\n"); nfc_close (pndInitiator); + nfc_exit (); exit(EXIT_FAILURE); } } @@ -455,6 +473,7 @@ main (int argc, char *argv[]) if (!initiator_only_mode) { nfc_close (pndTarget); } + nfc_exit (); exit (EXIT_SUCCESS); } From 39216f9d7cf0d7bd8a7085f21bf6a9fe064cb68a Mon Sep 17 00:00:00 2001 From: Romain Tartiere Date: Wed, 18 Jan 2012 13:13:36 +0000 Subject: [PATCH 105/113] Do not rely on int to locate USB bus and devices: FreeBSD's libusb use path of devices in /dev for bus (/dev/usb) and devices (e.g. /dev/ugen0.3) so directly Compare strings. --- libnfc/drivers/pn53x_usb.c | 109 +++++++++++++++++-------------------- 1 file changed, 49 insertions(+), 60 deletions(-) diff --git a/libnfc/drivers/pn53x_usb.c b/libnfc/drivers/pn53x_usb.c index 3176ec3..4fae76c 100644 --- a/libnfc/drivers/pn53x_usb.c +++ b/libnfc/drivers/pn53x_usb.c @@ -250,61 +250,35 @@ pn53x_usb_probe (nfc_connstring connstrings[], size_t connstrings_len, size_t *p } struct pn53x_usb_descriptor { - uint16_t bus; - uint16_t dev; + char *dirname; + char *filename; }; int pn53x_usb_connstring_decode (const nfc_connstring connstring, struct pn53x_usb_descriptor *desc) { - char *cs = malloc (strlen (connstring) + 1); - if (!cs) { - perror ("malloc"); - return -1; - } - strcpy (cs, connstring); - const char *driver_name = strtok (cs, ":"); - if (!driver_name) { - // Parse error - free (cs); - return -1; - } + int n = strlen (connstring) + 1; + char *driver_name = malloc (n); + char *dirname = malloc (n); + char *filename = malloc (n); - if (0 != strcmp (driver_name, PN53X_USB_DRIVER_NAME)) { + driver_name[0] = '\0'; + + int res = sscanf (connstring, "%[^:]:%[^:]:%[^:]", driver_name, dirname, filename); + + if (!res || (0 != strcmp (driver_name, PN53X_USB_DRIVER_NAME))) { // Driver name does not match. - free (cs); - return 0; + res = 0; + } else { + desc->dirname = strdup (dirname); + desc->filename = strdup (filename); } - const char *bus_s = strtok (NULL, ":"); - if (!bus_s) { - // bus not specified (or parsing error) - free (cs); - return 1; - } - unsigned int bus; - if (sscanf (bus_s, "%u", &bus) != 1) { - // bus_s is not a number - free (cs); - return 1; - } - desc->bus = bus; + free (driver_name); + free (dirname); + free (filename); - const char *dev_s = strtok (NULL, ":"); - if (!dev_s) { - // dev not specified (or parsing error) - free (cs); - return 2; - } - unsigned int dev; - if (sscanf (dev_s, "%u", &dev) != 1) { - // dev_s is not a number - free (cs); - return 2; - } - desc->dev = dev; - free (cs); - return 3; + return res; } bool @@ -337,14 +311,14 @@ pn53x_usb_get_usb_device_name (struct usb_device *dev, usb_dev_handle *udev, cha nfc_device * pn53x_usb_open (const nfc_connstring connstring) { - struct pn53x_usb_descriptor desc; + nfc_device *pnd = NULL; + struct pn53x_usb_descriptor desc = { NULL, NULL } ; int connstring_decode_level = pn53x_usb_connstring_decode (connstring, &desc); - log_put (LOG_CATEGORY, NFC_PRIORITY_TRACE, "%d element(s) have been decoded from \"%s\"", connstring_decode_level, connstring); + log_put (LOG_CATEGORY, NFC_PRIORITY_TRACE, "%d element(s) have been decoded from \"%s\"", connstring_decode_level, connstring); if (connstring_decode_level < 1) { - return NULL; + goto free_mem; } - nfc_device *pnd = NULL; struct pn53x_usb_data data = { .pudh = NULL, .uiEndPointIn = 0, @@ -355,20 +329,32 @@ pn53x_usb_open (const nfc_connstring connstring) usb_init (); + int res; + // usb_find_busses will find all of the busses on the system. Returns the + // number of changes since previous call to this function (total of new + // busses and busses removed). + if ((res = usb_find_busses () < 0)) { + log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "Unable to find USB busses (%s)", _usb_strerror (res)); + goto free_mem; + } + // usb_find_devices will find all of the devices on each bus. This should be + // called after usb_find_busses. Returns the number of changes since the + // previous call to this function (total of new device and devices removed). + if ((res = usb_find_devices () < 0)) { + log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "Unable to find USB devices (%s)", _usb_strerror (res)); + goto free_mem; + } + for (bus = usb_get_busses (); bus; bus = bus->next) { if (connstring_decode_level > 1) { // A specific bus have been specified - unsigned int bus_current; - sscanf (bus->dirname, "%u", &bus_current); - if (bus_current != desc.bus) + if (0 != strcmp (bus->dirname, desc.dirname)) continue; } for (dev = bus->devices; dev; dev = dev->next) { if (connstring_decode_level > 2) { // A specific dev have been specified - unsigned int dev_current; - sscanf (dev->filename, "%u", &dev_current); - if (dev_current != desc.dev) + if (0 != strcmp (dev->filename, desc.filename)) continue; } // Open the USB device @@ -384,7 +370,7 @@ pn53x_usb_open (const nfc_connstring connstring) } usb_close (data.pudh); // we failed to use the specified device - return NULL; + goto free_mem; } res = usb_claim_interface (data.pudh, 0); @@ -392,7 +378,7 @@ pn53x_usb_open (const nfc_connstring connstring) log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "Unable to claim USB interface (%s)", _usb_strerror (res)); usb_close (data.pudh); // we failed to use the specified device - return NULL; + goto free_mem; } data.model = pn53x_usb_get_device_model (dev->descriptor.idVendor, dev->descriptor.idProduct); // Allocate memory for the device info and specification, fill it and return the info @@ -435,16 +421,19 @@ pn53x_usb_open (const nfc_connstring connstring) goto error; } DRIVER_DATA (pnd)->abort_flag = false; - return pnd; + goto free_mem; } } // We ran out of devices before the index required - return NULL; + goto free_mem; error: // Free allocated structure on error. nfc_device_free (pnd); - return NULL; +free_mem: + free (desc.dirname); + free (desc.filename); + return pnd; } void From b366b8c02730013cea4b78b014793b3ce9ef6f5b Mon Sep 17 00:00:00 2001 From: Romuald Conty Date: Wed, 18 Jan 2012 13:17:01 +0000 Subject: [PATCH 106/113] add nfc_device_get_connstring() function and nfc-probe example to show devices connection strings --- include/nfc/nfc.h | 1 + libnfc/chips/pn53x.c | 4 +- libnfc/drivers/arygon.c | 15 +++-- libnfc/drivers/pn532_uart.c | 10 ++-- libnfc/drivers/pn53x_usb.c | 4 +- libnfc/nfc-device.c | 4 +- libnfc/nfc-internal.h | 6 +- libnfc/nfc.c | 14 ++++- utils/Makefile.am | 5 ++ utils/nfc-probe.c | 115 ++++++++++++++++++++++++++++++++++++ 10 files changed, 159 insertions(+), 19 deletions(-) create mode 100644 utils/nfc-probe.c diff --git a/include/nfc/nfc.h b/include/nfc/nfc.h index 63367ef..cbb17a8 100644 --- a/include/nfc/nfc.h +++ b/include/nfc/nfc.h @@ -102,6 +102,7 @@ extern "C" { /* Special data accessors */ NFC_EXPORT const char *nfc_device_get_name (nfc_device *pnd); + NFC_EXPORT const char *nfc_device_get_connstring (nfc_device *pnd); /* Properties accessors */ NFC_EXPORT int nfc_device_set_property_int (nfc_device *pnd, const nfc_property property, const int value); diff --git a/libnfc/chips/pn53x.c b/libnfc/chips/pn53x.c index dad67e0..bf8f389 100644 --- a/libnfc/chips/pn53x.c +++ b/libnfc/chips/pn53x.c @@ -85,8 +85,8 @@ pn53x_init(struct nfc_device *pnd) // Add the firmware revision to the device name char *pcName; - pcName = strdup (pnd->acName); - snprintf (pnd->acName, DEVICE_NAME_LENGTH - 1, "%s - %s", pcName, abtFirmwareText); + pcName = strdup (pnd->name); + snprintf (pnd->name, DEVICE_NAME_LENGTH - 1, "%s - %s", pcName, abtFirmwareText); free (pcName); return NFC_SUCCESS; } diff --git a/libnfc/drivers/arygon.c b/libnfc/drivers/arygon.c index 0b64d2f..56fd340 100644 --- a/libnfc/drivers/arygon.c +++ b/libnfc/drivers/arygon.c @@ -120,7 +120,10 @@ arygon_probe (nfc_connstring connstrings[], size_t connstrings_len, size_t *pszD uart_flush_input (sp); uart_set_speed (sp, ARYGON_DEFAULT_SPEED); - nfc_device *pnd = nfc_device_new (); + nfc_connstring connstring; + snprintf (connstring, sizeof(nfc_connstring), "%s:%s:%"PRIu32, ARYGON_DRIVER_NAME, acPort, ARYGON_DEFAULT_SPEED); + nfc_device *pnd = nfc_device_new (connstring); + pnd->driver = &arygon_driver; pnd->driver_data = malloc(sizeof(struct arygon_data)); DRIVER_DATA (pnd)->port = sp; @@ -144,7 +147,7 @@ arygon_probe (nfc_connstring connstrings[], size_t connstrings_len, size_t *pszD } // ARYGON reader is found - snprintf (connstrings[*pszDeviceFound], sizeof(nfc_connstring), "%s:%s:%"PRIu32, ARYGON_DRIVER_NAME, acPort, ARYGON_DEFAULT_SPEED); + memcpy (connstrings[*pszDeviceFound], connstring, sizeof(nfc_connstring)); (*pszDeviceFound)++; // Test if we reach the maximum "wanted" devices @@ -245,8 +248,8 @@ arygon_open (const nfc_connstring connstring) uart_set_speed (sp, ndd.speed); // We have a connection - pnd = nfc_device_new (); - snprintf (pnd->acName, sizeof (pnd->acName), "%s:%s", ARYGON_DRIVER_NAME, ndd.port); + pnd = nfc_device_new (connstring); + snprintf (pnd->name, sizeof (pnd->name), "%s:%s", ARYGON_DRIVER_NAME, ndd.port); pnd->driver_data = malloc(sizeof(struct arygon_data)); DRIVER_DATA (pnd)->port = sp; @@ -277,8 +280,8 @@ arygon_open (const nfc_connstring connstring) char arygon_firmware_version[10]; arygon_firmware (pnd, arygon_firmware_version); char *pcName; - pcName = strdup (pnd->acName); - snprintf (pnd->acName, sizeof (pnd->acName), "%s %s", pcName, arygon_firmware_version); + pcName = strdup (pnd->name); + snprintf (pnd->name, sizeof (pnd->name), "%s %s", pcName, arygon_firmware_version); free (pcName); pn53x_init(pnd); diff --git a/libnfc/drivers/pn532_uart.c b/libnfc/drivers/pn532_uart.c index 251da1a..19d3efc 100644 --- a/libnfc/drivers/pn532_uart.c +++ b/libnfc/drivers/pn532_uart.c @@ -94,7 +94,9 @@ pn532_uart_probe (nfc_connstring connstrings[], size_t connstrings_len, size_t * // Serial port claimed but we need to check if a PN532_UART is opened. uart_set_speed (sp, PN532_UART_DEFAULT_SPEED); - nfc_device *pnd = nfc_device_new (); + nfc_connstring connstring; + snprintf (connstring, sizeof(nfc_connstring), "%s:%s:%"PRIu32, PN532_UART_DRIVER_NAME, acPort, PN532_UART_DEFAULT_SPEED); + nfc_device *pnd = nfc_device_new (connstring); pnd->driver = &pn532_uart_driver; pnd->driver_data = malloc(sizeof(struct pn532_uart_data)); DRIVER_DATA (pnd)->port = sp; @@ -122,7 +124,7 @@ pn532_uart_probe (nfc_connstring connstrings[], size_t connstrings_len, size_t * continue; } - snprintf (connstrings[*pszDeviceFound], sizeof(nfc_connstring), "%s:%s:%"PRIu32, PN532_UART_DRIVER_NAME, acPort, PN532_UART_DEFAULT_SPEED); + memcpy (connstrings[*pszDeviceFound], connstring, sizeof (nfc_connstring)); (*pszDeviceFound)++; // Test if we reach the maximum "wanted" devices @@ -223,8 +225,8 @@ pn532_uart_open (const nfc_connstring connstring) uart_set_speed (sp, ndd.speed); // We have a connection - pnd = nfc_device_new (); - snprintf (pnd->acName, sizeof (pnd->acName), "%s:%s", PN532_UART_DRIVER_NAME, ndd.port); + pnd = nfc_device_new (connstring); + snprintf (pnd->name, sizeof (pnd->name), "%s:%s", PN532_UART_DRIVER_NAME, ndd.port); pnd->driver_data = malloc(sizeof(struct pn532_uart_data)); DRIVER_DATA (pnd)->port = sp; diff --git a/libnfc/drivers/pn53x_usb.c b/libnfc/drivers/pn53x_usb.c index 4fae76c..75f90fc 100644 --- a/libnfc/drivers/pn53x_usb.c +++ b/libnfc/drivers/pn53x_usb.c @@ -382,8 +382,8 @@ pn53x_usb_open (const nfc_connstring connstring) } data.model = pn53x_usb_get_device_model (dev->descriptor.idVendor, dev->descriptor.idProduct); // Allocate memory for the device info and specification, fill it and return the info - pnd = nfc_device_new (); - pn53x_usb_get_usb_device_name (dev, data.pudh, pnd->acName, sizeof (pnd->acName)); + pnd = nfc_device_new (connstring); + pn53x_usb_get_usb_device_name (dev, data.pudh, pnd->name, sizeof (pnd->name)); pnd->driver_data = malloc(sizeof(struct pn53x_usb_data)); *DRIVER_DATA (pnd) = data; diff --git a/libnfc/nfc-device.c b/libnfc/nfc-device.c index aad31d8..b19dec6 100644 --- a/libnfc/nfc-device.c +++ b/libnfc/nfc-device.c @@ -25,6 +25,7 @@ /* vim:set et sw=2 ts=2: */ #include +#include #ifdef HAVE_CONFIG_H # include "config.h" @@ -33,7 +34,7 @@ #include "nfc-internal.h" nfc_device * -nfc_device_new (void) +nfc_device_new (const nfc_connstring connstring) { nfc_device *res = malloc (sizeof (*res)); @@ -50,6 +51,7 @@ nfc_device_new (void) res->bEasyFraming = false; res->bAutoIso14443_4 = false; res->last_error = 0; + memcpy (res->connstring, connstring, sizeof (res->connstring)); res->driver_data = NULL; res->chip_data = NULL; diff --git a/libnfc/nfc-internal.h b/libnfc/nfc-internal.h index c9bdf1d..11d971b 100644 --- a/libnfc/nfc-internal.h +++ b/libnfc/nfc-internal.h @@ -168,7 +168,9 @@ struct nfc_device { void *chip_data; /** Device name string, including device wrapper firmware */ - char acName[DEVICE_NAME_LENGTH]; + char name[DEVICE_NAME_LENGTH]; +/** Device connection string */ + nfc_connstring connstring; /** Is the CRC automaticly added, checked and removed from the frames */ bool bCrc; /** Does the chip handle parity bits, all parities are handled as data */ @@ -184,7 +186,7 @@ struct nfc_device { int last_error; }; -nfc_device *nfc_device_new (void); +nfc_device *nfc_device_new (const nfc_connstring connstring); void nfc_device_free (nfc_device *dev); void iso14443_cascade_uid (const uint8_t abtUID[], const size_t szUID, uint8_t * pbtCascadedUID, size_t * pszCascadedUID); diff --git a/libnfc/nfc.c b/libnfc/nfc.c index c03d91c..bd4ec9e 100644 --- a/libnfc/nfc.c +++ b/libnfc/nfc.c @@ -163,7 +163,7 @@ nfc_open (const nfc_connstring connstring) return pnd; } - log_put (LOG_CATEGORY, NFC_PRIORITY_TRACE, "[%s] has been claimed.", pnd->acName); + log_put (LOG_CATEGORY, NFC_PRIORITY_TRACE, "\"%s\" (%s) has been claimed.", pnd->name, pnd->connstring); log_fini (); return pnd; } @@ -898,7 +898,17 @@ nfc_device_get_last_error (const nfc_device *pnd) const char * nfc_device_get_name (nfc_device *pnd) { - return pnd->acName; + return pnd->name; +} + +/** + * @brief Returns the device connection string + * @return Returns a string with the device connstring + */ +const char * +nfc_device_get_connstring (nfc_device *pnd) +{ + return pnd->connstring; } /* Misc. functions */ diff --git a/utils/Makefile.am b/utils/Makefile.am index 78c2dc2..5e8b322 100644 --- a/utils/Makefile.am +++ b/utils/Makefile.am @@ -4,6 +4,7 @@ bin_PROGRAMS = \ nfc-mfclassic \ nfc-mfsetuid \ nfc-mfultralight \ + nfc-probe \ nfc-read-forum-tag3 \ nfc-relay-picc @@ -36,6 +37,10 @@ nfc_mfsetuid_LDADD = $(top_builddir)/libnfc/libnfc.la \ nfc_mfultralight_SOURCES = nfc-mfultralight.c mifare.c mifare.h nfc_mfultralight_LDADD = $(top_builddir)/libnfc/libnfc.la +nfc_probe_SOURCES = nfc-probe.c +nfc_probe_LDADD = $(top_builddir)/libnfc/libnfc.la \ + libnfcutils.la + nfc_read_forum_tag3_SOURCES = nfc-read-forum-tag3.c nfc_read_forum_tag3_LDADD = $(top_builddir)/libnfc/libnfc.la \ libnfcutils.la diff --git a/utils/nfc-probe.c b/utils/nfc-probe.c new file mode 100644 index 0000000..1f751b4 --- /dev/null +++ b/utils/nfc-probe.c @@ -0,0 +1,115 @@ +/*- + * Public platform independent Near Field Communication (NFC) library examples + * + * Copyright (C) 2009, Roel Verdult + * Copyright (C) 2010, Romuald Conty, Romain Tartière + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * 1) Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2 )Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + * Note that this license only applies on the examples, NFC library itself is under LGPL + * + */ + +/** + * @file nfc-probe.c + * @brief Lists each available NFC device + */ + +#ifdef HAVE_CONFIG_H +# include "config.h" +#endif // HAVE_CONFIG_H + +#ifdef HAVE_LIBUSB +# ifdef DEBUG +# include +# include +# endif +#endif + +#include +#include +#include +#include +#include + +#include + +#include "nfc-utils.h" + +#define MAX_DEVICE_COUNT 16 +#define MAX_TARGET_COUNT 16 + +static nfc_device *pnd; + +void +print_usage (const char* progname) +{ + printf ("usage: %s [-v]\n", progname); + printf (" -v\t verbose display\n"); +} + +int +main (int argc, const char *argv[]) +{ + (void) argc; + const char *acLibnfcVersion; + size_t i; + bool verbose = false; + + nfc_init (); + + // Display libnfc version + acLibnfcVersion = nfc_version (); + printf ("%s uses libnfc %s\n", argv[0], acLibnfcVersion); + if (argc != 1) { + if ((argc == 2) && (0 == strcmp ("-v", argv[1]))) { + verbose = true; + } else { + print_usage (argv[0]); + exit (EXIT_FAILURE); + } + } + +#ifdef HAVE_LIBUSB +# ifdef DEBUG + usb_set_debug (4); +# endif +#endif + + nfc_connstring connstrings[MAX_DEVICE_COUNT]; + size_t szDeviceFound = nfc_list_devices (connstrings, MAX_DEVICE_COUNT); + + if (szDeviceFound == 0) { + printf ("No NFC device found.\n"); + } + + printf ("%d NFC device(s) found:\n", szDeviceFound); + for (i = 0; i < szDeviceFound; i++) { + pnd = nfc_open (connstrings[i]); + if (pnd != NULL) { + printf ("- %s:\n %s\n", nfc_device_get_name (pnd), nfc_device_get_connstring (pnd)); + } + nfc_close (pnd); + } + + nfc_exit (); + return 0; +} From 3aa31abe1810c72d802f460c64387f61b5726622 Mon Sep 17 00:00:00 2001 From: Romain Tartiere Date: Wed, 18 Jan 2012 13:21:06 +0000 Subject: [PATCH 107/113] Add missing header. --- libnfc/log-printf.c | 1 + 1 file changed, 1 insertion(+) diff --git a/libnfc/log-printf.c b/libnfc/log-printf.c index 1ef4203..f440f79 100644 --- a/libnfc/log-printf.c +++ b/libnfc/log-printf.c @@ -17,6 +17,7 @@ #include "config.h" +#include #include #include #include From 86c8ce536bca900df5cd51d47ff7602f375ad7e8 Mon Sep 17 00:00:00 2001 From: Audrey Diacre Date: Wed, 18 Jan 2012 14:44:57 +0000 Subject: [PATCH 108/113] acr122.c: apply changes done in r1296. --- libnfc/drivers/acr122.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/libnfc/drivers/acr122.c b/libnfc/drivers/acr122.c index 7be531a..2acfaf8 100644 --- a/libnfc/drivers/acr122.c +++ b/libnfc/drivers/acr122.c @@ -252,7 +252,7 @@ acr122_open (const nfc_connstring connstring) // FIXME: acr122_open() does not take care about bus index char *pcFirmware; - nfc_device *pnd = nfc_device_new (); + nfc_device *pnd = nfc_device_new (connstring); pnd->driver_data = malloc (sizeof (struct acr122_data)); // Alloc and init chip's data @@ -281,7 +281,7 @@ acr122_open (const nfc_connstring connstring) if (strstr (pcFirmware, FIRMWARE_TEXT) != NULL) { // Done, we found the reader we are looking for - snprintf (pnd->acName, sizeof (pnd->acName), "%s / %s", ndd.pcsc_device_name, pcFirmware); + snprintf (pnd->name, sizeof (pnd->name), "%s / %s", ndd.pcsc_device_name, pcFirmware); // 50: empirical tuning on Touchatag // 46: empirical tuning on ACR122U From 4c011279ffa73b7316c26e306eef1e33387d8ee2 Mon Sep 17 00:00:00 2001 From: Audrey Diacre Date: Wed, 18 Jan 2012 16:22:06 +0000 Subject: [PATCH 109/113] add context to nfc_init(), nfc_exit(), nfc_open() and nfc_list_devices() functions. --- examples/doc/quick_start_example1.c | 6 ++-- examples/nfc-anticol.c | 8 ++--- examples/nfc-dep-initiator.c | 6 ++-- examples/nfc-dep-target.c | 10 +++--- examples/nfc-emulate-forum-tag2.c | 8 ++--- examples/nfc-emulate-tag.c | 8 ++--- examples/nfc-emulate-uid.c | 8 ++--- examples/nfc-poll.c | 8 ++--- examples/nfc-relay.c | 12 ++++---- examples/pn53x-diagnose.c | 6 ++-- examples/pn53x-sam.c | 6 ++-- examples/pn53x-tamashell.c | 6 ++-- include/nfc/nfc-types.h | 2 ++ include/nfc/nfc.h | 8 ++--- libnfc/nfc.c | 14 +++++---- test/test_access_storm.c | 10 +++--- test/test_dep_active.c | 10 +++--- test/test_dep_passive.c | 10 +++--- test/test_device_modes_as_dep.c | 10 +++--- test/test_register_access.c | 8 ++--- test/test_register_endianness.c | 8 ++--- utils/nfc-emulate-forum-tag4.c | 6 ++-- utils/nfc-list.c | 14 ++++----- utils/nfc-mfclassic.c | 8 ++--- utils/nfc-mfsetuid.c | 8 ++--- utils/nfc-mfultralight.c | 10 +++--- utils/nfc-probe.c | 8 ++--- utils/nfc-read-forum-tag3.c | 6 ++-- utils/nfc-relay-picc.c | 48 ++++++++++++++--------------- 29 files changed, 142 insertions(+), 138 deletions(-) diff --git a/examples/doc/quick_start_example1.c b/examples/doc/quick_start_example1.c index 3215caf..9c405aa 100644 --- a/examples/doc/quick_start_example1.c +++ b/examples/doc/quick_start_example1.c @@ -14,14 +14,14 @@ main (int argc, const char *argv[]) nfc_device *pnd; nfc_target nt; - nfc_init(); + nfc_init (NULL); // Display libnfc version const char *acLibnfcVersion = nfc_version (); printf ("%s uses libnfc %s\n", argv[0], acLibnfcVersion); // Open, using the first available NFC device - pnd = nfc_open (NULL); + pnd = nfc_open (NULL, NULL); if (pnd == NULL) { ERR ("%s", "Unable to open NFC device."); @@ -55,6 +55,6 @@ main (int argc, const char *argv[]) } // Close NFC device nfc_close (pnd); - nfc_exit (); + nfc_exit (NULL); return EXIT_SUCCESS; } diff --git a/examples/nfc-anticol.c b/examples/nfc-anticol.c index ec5dd17..87aa1c4 100644 --- a/examples/nfc-anticol.c +++ b/examples/nfc-anticol.c @@ -148,10 +148,10 @@ main (int argc, char *argv[]) } } - nfc_init(); + nfc_init (NULL); // Try to open the NFC reader - pnd = nfc_open (NULL); + pnd = nfc_open (NULL, NULL); if (!pnd) { printf ("Error opening NFC reader\n"); @@ -186,7 +186,7 @@ main (int argc, char *argv[]) if (!transmit_bits (abtReqa, 7)) { printf ("Error: No tag available\n"); nfc_close (pnd); - nfc_exit (); + nfc_exit (NULL); return 1; } memcpy (abtAtqa, abtRx, 2); @@ -316,6 +316,6 @@ main (int argc, char *argv[]) } nfc_close (pnd); - nfc_exit (); + nfc_exit (NULL); return 0; } diff --git a/examples/nfc-dep-initiator.c b/examples/nfc-dep-initiator.c index dc2d82f..272cdf0 100644 --- a/examples/nfc-dep-initiator.c +++ b/examples/nfc-dep-initiator.c @@ -72,9 +72,9 @@ main (int argc, const char *argv[]) return EXIT_FAILURE; } - nfc_init(); + nfc_init (NULL); - pnd = nfc_open (NULL); + pnd = nfc_open (NULL, NULL); if (!pnd) { printf("Unable to open NFC device.\n"); return EXIT_FAILURE; @@ -110,6 +110,6 @@ main (int argc, const char *argv[]) error: nfc_close (pnd); - nfc_exit (); + nfc_exit (NULL); return EXIT_SUCCESS; } diff --git a/examples/nfc-dep-target.c b/examples/nfc-dep-target.c index 592b9e1..9786ead 100644 --- a/examples/nfc-dep-target.c +++ b/examples/nfc-dep-target.c @@ -66,16 +66,16 @@ main (int argc, const char *argv[]) uint8_t abtTx[] = "Hello Mars!"; #define MAX_DEVICE_COUNT 2 nfc_connstring connstrings[MAX_DEVICE_COUNT]; - size_t szDeviceFound = nfc_list_devices (connstrings, MAX_DEVICE_COUNT); + size_t szDeviceFound = nfc_list_devices (NULL, connstrings, MAX_DEVICE_COUNT); // Little hack to allow using nfc-dep-initiator & nfc-dep-target from // the same machine: if there is more than one readers opened // nfc-dep-target will open the second reader // (we hope they're always detected in the same order) - nfc_init (); + nfc_init (NULL); if (szDeviceFound == 1) { - pnd = nfc_open (connstrings[0]); + pnd = nfc_open (NULL, connstrings[0]); } else if (szDeviceFound > 1) { - pnd = nfc_open (connstrings[1]); + pnd = nfc_open (NULL, connstrings[1]); } else { printf("No device found.\n"); return EXIT_FAILURE; @@ -141,6 +141,6 @@ main (int argc, const char *argv[]) error: nfc_close (pnd); - nfc_exit (); + nfc_exit (NULL); return EXIT_SUCCESS; } diff --git a/examples/nfc-emulate-forum-tag2.c b/examples/nfc-emulate-forum-tag2.c index 8af29a6..2e8ef6e 100644 --- a/examples/nfc-emulate-forum-tag2.c +++ b/examples/nfc-emulate-forum-tag2.c @@ -182,8 +182,8 @@ main(int argc, char *argv[]) }; signal (SIGINT, stop_emulation); - nfc_init (); - pnd = nfc_open (NULL); + nfc_init (NULL); + pnd = nfc_open (NULL, NULL); if (pnd == NULL) { ERR("Unable to open NFC device"); @@ -198,7 +198,7 @@ main(int argc, char *argv[]) } nfc_close(pnd); - nfc_exit (); + nfc_exit (NULL); exit (EXIT_SUCCESS); @@ -206,6 +206,6 @@ error: if (pnd) { nfc_perror (pnd, argv[0]); nfc_close (pnd); - nfc_exit (); + nfc_exit (NULL); } } diff --git a/examples/nfc-emulate-tag.c b/examples/nfc-emulate-tag.c index 0e7a4c3..7e236fe 100644 --- a/examples/nfc-emulate-tag.c +++ b/examples/nfc-emulate-tag.c @@ -67,7 +67,7 @@ intr_hdlr (void) if (pnd != NULL) { nfc_close(pnd); } - nfc_exit (); + nfc_exit (NULL); exit (EXIT_FAILURE); } @@ -180,10 +180,10 @@ main (int argc, char *argv[]) signal (SIGINT, (void (*)()) intr_hdlr); #endif - nfc_init(); + nfc_init (NULL); // Try to open the NFC reader - pnd = nfc_open (NULL); + pnd = nfc_open (NULL, NULL); // Display libnfc version acLibnfcVersion = nfc_version (); @@ -269,7 +269,7 @@ main (int argc, char *argv[]) } nfc_close(pnd); - nfc_exit (); + nfc_exit (NULL); exit (EXIT_SUCCESS); } diff --git a/examples/nfc-emulate-uid.c b/examples/nfc-emulate-uid.c index df14eb2..cfa2b4f 100644 --- a/examples/nfc-emulate-uid.c +++ b/examples/nfc-emulate-uid.c @@ -125,10 +125,10 @@ main (int argc, char *argv[]) signal (SIGINT, (void (*)()) intr_hdlr); #endif - nfc_init (); + nfc_init (NULL); // Try to open the NFC device - pnd = nfc_open (NULL); + pnd = nfc_open (NULL, NULL); if (pnd == NULL) { printf ("Unable to open NFC device\n"); @@ -219,11 +219,11 @@ main (int argc, char *argv[]) } } nfc_close (pnd); - nfc_exit (); + nfc_exit (NULL); exit (EXIT_SUCCESS); error: nfc_close (pnd); - nfc_exit (); + nfc_exit (NULL); exit (EXIT_FAILURE); } diff --git a/examples/nfc-poll.c b/examples/nfc-poll.c index 2cfc115..e1873a4 100644 --- a/examples/nfc-poll.c +++ b/examples/nfc-poll.c @@ -103,9 +103,9 @@ main (int argc, const char *argv[]) nfc_target nt; int res = 0; - nfc_init (); + nfc_init (NULL); - pnd = nfc_open (NULL); + pnd = nfc_open (NULL, NULL); if (pnd == NULL) { ERR ("%s", "Unable to open NFC device."); @@ -122,7 +122,7 @@ main (int argc, const char *argv[]) if ((res = nfc_initiator_poll_target (pnd, nmModulations, szModulations, uiPollNr, uiPeriod, &nt)) < 0) { nfc_perror (pnd, "nfc_initiator_poll_target"); nfc_close (pnd); - nfc_exit (); + nfc_exit (NULL); exit (EXIT_FAILURE); } @@ -132,6 +132,6 @@ main (int argc, const char *argv[]) printf ("No target found.\n"); } nfc_close (pnd); - nfc_exit (); + nfc_exit (NULL); exit (EXIT_SUCCESS); } diff --git a/examples/nfc-relay.c b/examples/nfc-relay.c index 580b8d0..6a95c3f 100644 --- a/examples/nfc-relay.c +++ b/examples/nfc-relay.c @@ -109,17 +109,17 @@ main (int argc, char *argv[]) nfc_connstring connstrings[MAX_DEVICE_COUNT]; // List available devices - size_t szFound = nfc_list_devices (connstrings, MAX_DEVICE_COUNT); + size_t szFound = nfc_list_devices (NULL, connstrings, MAX_DEVICE_COUNT); if (szFound < 2) { ERR ("%zd device found but two opened devices are needed to relay NFC.", szFound); return EXIT_FAILURE; } - nfc_init (); + nfc_init (NULL); // Try to open the NFC emulator device - pndTag = nfc_open (connstrings[0]); + pndTag = nfc_open (NULL, connstrings[0]); if (pndTag == NULL) { printf ("Error opening NFC emulator device\n"); return EXIT_FAILURE; @@ -151,7 +151,7 @@ main (int argc, char *argv[]) if ((szReaderRxBits = nfc_target_init (pndTag, &nt, abtReaderRx, sizeof (abtReaderRx), 0)) < 0) { ERR ("%s", "Initialization of NFC emulator failed"); nfc_close (pndTag); - nfc_exit (); + nfc_exit (NULL); return EXIT_FAILURE; } printf ("%s", "Configuring emulator settings..."); @@ -163,7 +163,7 @@ main (int argc, char *argv[]) printf ("%s", "Done, emulated tag is initialized"); // Try to open the NFC reader - pndReader = nfc_open (connstrings[1]); + pndReader = nfc_open (NULL, connstrings[1]); printf ("NFC reader device: %s opened", nfc_device_get_name (pndReader)); printf ("%s", "Configuring NFC reader settings..."); @@ -221,6 +221,6 @@ main (int argc, char *argv[]) nfc_close (pndTag); nfc_close (pndReader); - nfc_exit (); + nfc_exit (NULL); exit (EXIT_SUCCESS); } diff --git a/examples/pn53x-diagnose.c b/examples/pn53x-diagnose.c index 625bab9..6743eaa 100644 --- a/examples/pn53x-diagnose.c +++ b/examples/pn53x-diagnose.c @@ -68,21 +68,21 @@ main (int argc, const char *argv[]) errx (1, "usage: %s", argv[0]); } - nfc_init(); + nfc_init (NULL); // Display libnfc version acLibnfcVersion = nfc_version (); printf ("%s uses libnfc %s\n", argv[0], acLibnfcVersion); nfc_connstring connstrings[MAX_DEVICE_COUNT]; - size_t szFound = nfc_list_devices (connstrings, MAX_DEVICE_COUNT); + size_t szFound = nfc_list_devices (NULL, connstrings, MAX_DEVICE_COUNT); if (szFound == 0) { printf ("No NFC device found.\n"); } for (i = 0; i < szFound; i++) { - pnd = nfc_open (connstrings[i]); + pnd = nfc_open (NULL, connstrings[i]); if (pnd == NULL) { ERR ("%s", "Unable to open NFC device."); diff --git a/examples/pn53x-sam.c b/examples/pn53x-sam.c index 8de030e..10ef075 100644 --- a/examples/pn53x-sam.c +++ b/examples/pn53x-sam.c @@ -77,14 +77,14 @@ main (int argc, const char *argv[]) (void) argc; (void) argv; - nfc_init (); + nfc_init (NULL); // Display libnfc version const char *acLibnfcVersion = nfc_version (); printf ("%s uses libnfc %s\n", argv[0], acLibnfcVersion); // Open using the first available NFC device - pnd = nfc_open (NULL); + pnd = nfc_open (NULL, NULL); if (pnd == NULL) { ERR ("%s", "Unable to open NFC device."); @@ -193,7 +193,7 @@ main (int argc, const char *argv[]) // Close NFC device nfc_close (pnd); - nfc_exit (); + nfc_exit (NULL); exit (EXIT_SUCCESS); } diff --git a/examples/pn53x-tamashell.c b/examples/pn53x-tamashell.c index bcd6341..4ceee23 100644 --- a/examples/pn53x-tamashell.c +++ b/examples/pn53x-tamashell.c @@ -86,10 +86,10 @@ int main(int argc, const char* argv[]) } } - nfc_init (); + nfc_init (NULL); // Try to open the NFC reader - pnd = nfc_open(NULL); + pnd = nfc_open(NULL, NULL); if (pnd == NULL) { ERR ("%s", "Unable to open NFC device."); @@ -200,6 +200,6 @@ int main(int argc, const char* argv[]) fclose(input); } nfc_close(pnd); - nfc_exit (); + nfc_exit (NULL); return 1; } diff --git a/include/nfc/nfc-types.h b/include/nfc/nfc-types.h index 81a1270..b0c001c 100644 --- a/include/nfc/nfc-types.h +++ b/include/nfc/nfc-types.h @@ -32,6 +32,8 @@ # include # include +typedef void *nfc_context; + /** * NFC device */ diff --git a/include/nfc/nfc.h b/include/nfc/nfc.h index cbb17a8..8c2b1b7 100644 --- a/include/nfc/nfc.h +++ b/include/nfc/nfc.h @@ -63,15 +63,15 @@ extern "C" { # endif // __cplusplus /* Library initialization/deinitialization */ - NFC_EXPORT void nfc_init(); - NFC_EXPORT void nfc_exit(); + NFC_EXPORT void nfc_init(nfc_context *context); + NFC_EXPORT void nfc_exit(nfc_context *context); /* NFC Device/Hardware manipulation */ NFC_EXPORT bool nfc_get_default_device (nfc_connstring *connstring); - NFC_EXPORT nfc_device *nfc_open (const nfc_connstring connstring); + NFC_EXPORT nfc_device *nfc_open (nfc_context *context, const nfc_connstring connstring); NFC_EXPORT void nfc_close (nfc_device *pnd); NFC_EXPORT int nfc_abort_command (nfc_device *pnd); - NFC_EXPORT size_t nfc_list_devices (nfc_connstring connstrings[], size_t connstrings_len); + NFC_EXPORT size_t nfc_list_devices (nfc_context *context, nfc_connstring connstrings[], size_t connstrings_len); NFC_EXPORT int nfc_idle (nfc_device *pnd); /* NFC initiator: act as "reader" */ diff --git a/libnfc/nfc.c b/libnfc/nfc.c index bd4ec9e..d39c59e 100644 --- a/libnfc/nfc.c +++ b/libnfc/nfc.c @@ -64,19 +64,21 @@ const struct nfc_driver *nfc_drivers[] = { /** * @brief Initialize libnfc. * This function must be called before calling any other libnfc function + * @param context Optional output location for context pointer */ void -nfc_init() +nfc_init(nfc_context *context) { log_init (); } /** * @brief Deinitialize libnfc. - * Should be called after closing all open devices and before your application terminates. + * Should be called after closing all open devices and before your application terminates. + *@param context The context to deinitialize */ void -nfc_exit() +nfc_exit(nfc_context *context) { log_fini (); } @@ -102,7 +104,7 @@ nfc_get_default_device (nfc_connstring *connstring) if (NULL == env_default_connstring) { // LIBNFC_DEFAULT_DEVICE is not set, we fallback on probing for the first available device nfc_connstring listed_cs[1]; - size_t szDeviceFound = nfc_list_devices (listed_cs, 1); + size_t szDeviceFound = nfc_list_devices (NULL, listed_cs, 1); if (szDeviceFound) { strncpy (*connstring, listed_cs[0], sizeof(nfc_connstring)); } else { @@ -131,7 +133,7 @@ nfc_get_default_device (nfc_connstring *connstring) * optionally followed by manual tuning of the parameters if the default parameters are not suiting your goals. */ nfc_device * -nfc_open (const nfc_connstring connstring) +nfc_open (nfc_context *context, const nfc_connstring connstring) { nfc_device *pnd = NULL; @@ -200,7 +202,7 @@ nfc_close (nfc_device *pnd) * */ size_t -nfc_list_devices (nfc_connstring connstrings[] , size_t szDevices) +nfc_list_devices (nfc_context *context, nfc_connstring connstrings[] , size_t szDevices) { size_t szN; size_t szDeviceFound = 0; diff --git a/test/test_access_storm.c b/test/test_access_storm.c index 6609f3f..558f34c 100644 --- a/test/test_access_storm.c +++ b/test/test_access_storm.c @@ -17,23 +17,23 @@ test_access_storm (void) nfc_connstring connstrings[MAX_DEVICE_COUNT]; int res = 0; - nfc_init (); + nfc_init (NULL); - size_t ref_device_count = nfc_list_devices (connstrings, MAX_DEVICE_COUNT); + size_t ref_device_count = nfc_list_devices (NULL, connstrings, MAX_DEVICE_COUNT); if (!ref_device_count) cut_omit ("No NFC device found"); while (n) { size_t i; - size_t device_count = nfc_list_devices (connstrings, MAX_DEVICE_COUNT); + size_t device_count = nfc_list_devices (NULL, connstrings, MAX_DEVICE_COUNT); cut_assert_equal_int (ref_device_count, device_count, cut_message ("device count")); for (i = 0; i < device_count; i++) { nfc_device *device; nfc_target ant[MAX_TARGET_COUNT]; - device = nfc_open (connstrings[i]); + device = nfc_open (NULL, connstrings[i]); cut_assert_not_null (device, cut_message ("nfc_open")); res = nfc_initiator_init(device); @@ -51,5 +51,5 @@ test_access_storm (void) n--; } - nfc_exit (); + nfc_exit (NULL); } diff --git a/test/test_dep_active.c b/test/test_dep_active.c index c8eb2a5..de0692d 100644 --- a/test/test_dep_active.c +++ b/test/test_dep_active.c @@ -27,13 +27,13 @@ abort_test_by_keypress (int sig) void cut_setup (void) { - size_t n = nfc_list_devices (connstrings, 2); + size_t n = nfc_list_devices (NULL, connstrings, 2); if (n < 2) { cut_omit ("At least two NFC devices must be plugged-in to run this test"); } - nfc_init (); - devices[TARGET] = nfc_open (connstrings[TARGET]); - devices[INITIATOR] = nfc_open (connstrings[INITIATOR]); + nfc_init (NULL); + devices[TARGET] = nfc_open (NULL, connstrings[TARGET]); + devices[INITIATOR] = nfc_open (NULL, connstrings[INITIATOR]); signal (SIGINT, abort_test_by_keypress); } @@ -43,7 +43,7 @@ cut_teardown (void) { nfc_close (devices[TARGET]); nfc_close (devices[INITIATOR]); - nfc_exit (); + nfc_exit (NULL); } struct thread_data { diff --git a/test/test_dep_passive.c b/test/test_dep_passive.c index ff0ffb0..e792f15 100644 --- a/test/test_dep_passive.c +++ b/test/test_dep_passive.c @@ -26,14 +26,14 @@ abort_test_by_keypress (int sig) void cut_setup (void) { - size_t n = nfc_list_devices (connstrings, 2); + size_t n = nfc_list_devices (NULL, connstrings, 2); if (n < 2) { cut_omit ("At least two NFC devices must be plugged-in to run this test"); } - nfc_init (); - devices[TARGET] = nfc_open (connstrings[TARGET]); - devices[INITIATOR] = nfc_open (connstrings[INITIATOR]); + nfc_init (NULL); + devices[TARGET] = nfc_open (NULL, connstrings[TARGET]); + devices[INITIATOR] = nfc_open (NULL, connstrings[INITIATOR]); signal (SIGINT, abort_test_by_keypress); } @@ -43,7 +43,7 @@ cut_teardown (void) { nfc_close (devices[TARGET]); nfc_close (devices[INITIATOR]); - nfc_exit (); + nfc_exit (NULL); } struct thread_data { diff --git a/test/test_device_modes_as_dep.c b/test/test_device_modes_as_dep.c index cd45661..138b405 100644 --- a/test/test_device_modes_as_dep.c +++ b/test/test_device_modes_as_dep.c @@ -24,14 +24,14 @@ abort_test_by_keypress (int sig) void cut_setup (void) { - size_t n = nfc_list_devices (connstrings, 2); + size_t n = nfc_list_devices (NULL, connstrings, 2); if (n < 2) { cut_omit ("At least two NFC devices must be plugged-in to run this test"); } - nfc_init (); - second_device = nfc_open (connstrings[0]); - first_device = nfc_open (connstrings[1]); + nfc_init (NULL); + second_device = nfc_open (NULL, connstrings[0]); + first_device = nfc_open (NULL, connstrings[1]); signal (SIGINT, abort_test_by_keypress); } @@ -41,7 +41,7 @@ cut_teardown (void) { nfc_close (second_device); nfc_close (first_device); - nfc_exit (); + nfc_exit (NULL); } struct thread_data { diff --git a/test/test_register_access.c b/test/test_register_access.c index 4839437..f6abfef 100644 --- a/test/test_register_access.c +++ b/test/test_register_access.c @@ -12,15 +12,15 @@ test_register_endianness (void) nfc_connstring connstrings[MAX_DEVICE_COUNT]; int res = 0; - nfc_init (); + nfc_init (NULL); - size_t device_count = nfc_list_devices (connstrings, MAX_DEVICE_COUNT); + size_t device_count = nfc_list_devices (NULL, connstrings, MAX_DEVICE_COUNT); if (!device_count) cut_omit ("No NFC device found"); nfc_device *device; - device = nfc_open (connstrings[0]); + device = nfc_open (NULL, connstrings[0]); cut_assert_not_null (device, cut_message ("nfc_open")); uint8_t value; @@ -44,5 +44,5 @@ test_register_endianness (void) cut_assert_equal_uint (0x55, value, cut_message ("check register value")); nfc_close (device); - nfc_exit (); + nfc_exit (NULL); } diff --git a/test/test_register_endianness.c b/test/test_register_endianness.c index a6b15c4..09d622c 100644 --- a/test/test_register_endianness.c +++ b/test/test_register_endianness.c @@ -13,15 +13,15 @@ test_register_endianness (void) nfc_connstring connstrings[MAX_DEVICE_COUNT]; int res = 0; - nfc_init (); + nfc_init (NULL); - size_t device_count = nfc_list_devices (connstrings, MAX_DEVICE_COUNT); + size_t device_count = nfc_list_devices (NULL, connstrings, MAX_DEVICE_COUNT); if (!device_count) cut_omit ("No NFC device found"); nfc_device *device; - device = nfc_open (connstrings[0]); + device = nfc_open (NULL, connstrings[0]); cut_assert_not_null (device, cut_message ("nfc_open")); uint8_t value; @@ -35,5 +35,5 @@ test_register_endianness (void) cut_assert_equal_int (0, res, cut_message ("read register 0xFFF0")); nfc_close (device); - nfc_exit (); + nfc_exit (NULL); } diff --git a/utils/nfc-emulate-forum-tag4.c b/utils/nfc-emulate-forum-tag4.c index 65ff665..68edf4a 100644 --- a/utils/nfc-emulate-forum-tag4.c +++ b/utils/nfc-emulate-forum-tag4.c @@ -346,10 +346,10 @@ main (int argc, char *argv[]) } } - nfc_init (); + nfc_init (NULL); // Try to open the NFC reader - pnd = nfc_open (NULL); + pnd = nfc_open (NULL, NULL); if (pnd == NULL) { ERR("Unable to open NFC device"); @@ -373,6 +373,6 @@ main (int argc, char *argv[]) } } - nfc_exit (); + nfc_exit (NULL); exit (EXIT_SUCCESS); } diff --git a/utils/nfc-list.c b/utils/nfc-list.c index 6afd126..d9cf393 100644 --- a/utils/nfc-list.c +++ b/utils/nfc-list.c @@ -75,7 +75,7 @@ main (int argc, const char *argv[]) bool verbose = false; int res = 0; - nfc_init (); + nfc_init (NULL); // Display libnfc version acLibnfcVersion = nfc_version (); @@ -97,7 +97,7 @@ main (int argc, const char *argv[]) /* Lazy way to open an NFC device */ #if 0 - pnd = nfc_open (NULL); + pnd = nfc_open (NULL, NULL); #endif /* If specific device is wanted, i.e. an ARYGON device on /dev/ttyUSB0 */ @@ -106,7 +106,7 @@ main (int argc, const char *argv[]) ndd.pcDriver = "ARYGON"; ndd.pcPort = "/dev/ttyUSB0"; ndd.uiSpeed = 115200; - pnd = nfc_open (&ndd); + pnd = nfc_open (NULL, &ndd); #endif /* If specific device is wanted, i.e. a SCL3711 on USB */ @@ -114,10 +114,10 @@ main (int argc, const char *argv[]) nfc_device_desc_t ndd; ndd.pcDriver = "PN533_USB"; strcpy(ndd.acDevice, "SCM Micro / SCL3711-NFC&RW"); - pnd = nfc_open (&ndd); + pnd = nfc_open (NULL, &ndd); #endif nfc_connstring connstrings[MAX_DEVICE_COUNT]; - size_t szDeviceFound = nfc_list_devices (connstrings, MAX_DEVICE_COUNT); + size_t szDeviceFound = nfc_list_devices (NULL, connstrings, MAX_DEVICE_COUNT); if (szDeviceFound == 0) { printf ("No NFC device found.\n"); @@ -125,7 +125,7 @@ main (int argc, const char *argv[]) for (i = 0; i < szDeviceFound; i++) { nfc_target ant[MAX_TARGET_COUNT]; - pnd = nfc_open (connstrings[i]); + pnd = nfc_open (NULL, connstrings[i]); if (pnd == NULL) { ERR ("%s", "Unable to open NFC device."); @@ -252,6 +252,6 @@ main (int argc, const char *argv[]) nfc_close (pnd); } - nfc_exit (); + nfc_exit (NULL); return 0; } diff --git a/utils/nfc-mfclassic.c b/utils/nfc-mfclassic.c index 450ae6a..8ba1b1d 100644 --- a/utils/nfc-mfclassic.c +++ b/utils/nfc-mfclassic.c @@ -537,10 +537,10 @@ main (int argc, const char *argv[]) } // printf("Successfully opened required files\n"); - nfc_init (); + nfc_init (NULL); // Try to open the NFC reader - pnd = nfc_open (NULL); + pnd = nfc_open (NULL, NULL); if (pnd == NULL) { printf ("Error opening NFC reader\n"); exit (EXIT_FAILURE); @@ -565,7 +565,7 @@ main (int argc, const char *argv[]) if (nfc_initiator_select_passive_target (pnd, nmMifare, NULL, 0, &nt) < 0) { printf ("Error: no tag was found\n"); nfc_close (pnd); - nfc_exit (); + nfc_exit (NULL); exit (EXIT_FAILURE); } // Test if we are dealing with a MIFARE compatible tag @@ -665,6 +665,6 @@ main (int argc, const char *argv[]) } }; - nfc_exit (); + nfc_exit (NULL); exit (EXIT_SUCCESS); } diff --git a/utils/nfc-mfsetuid.c b/utils/nfc-mfsetuid.c index b2cbf3f..64ea4d2 100644 --- a/utils/nfc-mfsetuid.c +++ b/utils/nfc-mfsetuid.c @@ -177,10 +177,10 @@ main (int argc, char *argv[]) } } - nfc_init (); + nfc_init (NULL); // Try to open the NFC reader - pnd = nfc_open (NULL); + pnd = nfc_open (NULL, NULL); if (!pnd) { printf ("Error opening NFC reader\n"); @@ -215,7 +215,7 @@ main (int argc, char *argv[]) if (!transmit_bits (abtReqa, 7)) { printf ("Error: No tag available\n"); nfc_close (pnd); - nfc_exit (); + nfc_exit (NULL); return 1; } memcpy (abtAtqa, abtRx, 2); @@ -354,6 +354,6 @@ main (int argc, char *argv[]) nfc_close (pnd); - nfc_exit (); + nfc_exit (NULL); return 0; } diff --git a/utils/nfc-mfultralight.c b/utils/nfc-mfultralight.c index 515001e..ba7394c 100644 --- a/utils/nfc-mfultralight.c +++ b/utils/nfc-mfultralight.c @@ -204,10 +204,10 @@ main (int argc, const char *argv[]) } DBG ("Successfully opened the dump file\n"); - nfc_init (); + nfc_init (NULL); // Try to open the NFC device - pnd = nfc_open (NULL); + pnd = nfc_open (NULL, NULL); if (pnd == NULL) { ERR ("Error opening NFC device\n"); return 1; @@ -230,7 +230,7 @@ main (int argc, const char *argv[]) if (nfc_initiator_select_passive_target (pnd, nmMifare, NULL, 0, &nt) < 0) { ERR ("no tag was found\n"); nfc_close (pnd); - nfc_exit (); + nfc_exit (NULL); return 1; } // Test if we are dealing with a MIFARE compatible tag @@ -238,7 +238,7 @@ main (int argc, const char *argv[]) if (nt.nti.nai.abtAtqa[1] != 0x44) { ERR ("tag is not a MIFARE Ultralight card\n"); nfc_close (pnd); - nfc_exit (); + nfc_exit (NULL); return EXIT_FAILURE; } // Get the info from the current tag @@ -270,6 +270,6 @@ main (int argc, const char *argv[]) } nfc_close (pnd); - nfc_exit (); + nfc_exit (NULL); return EXIT_SUCCESS; } diff --git a/utils/nfc-probe.c b/utils/nfc-probe.c index 1f751b4..9536398 100644 --- a/utils/nfc-probe.c +++ b/utils/nfc-probe.c @@ -74,7 +74,7 @@ main (int argc, const char *argv[]) size_t i; bool verbose = false; - nfc_init (); + nfc_init (NULL); // Display libnfc version acLibnfcVersion = nfc_version (); @@ -95,7 +95,7 @@ main (int argc, const char *argv[]) #endif nfc_connstring connstrings[MAX_DEVICE_COUNT]; - size_t szDeviceFound = nfc_list_devices (connstrings, MAX_DEVICE_COUNT); + size_t szDeviceFound = nfc_list_devices (NULL, connstrings, MAX_DEVICE_COUNT); if (szDeviceFound == 0) { printf ("No NFC device found.\n"); @@ -103,13 +103,13 @@ main (int argc, const char *argv[]) printf ("%d NFC device(s) found:\n", szDeviceFound); for (i = 0; i < szDeviceFound; i++) { - pnd = nfc_open (connstrings[i]); + pnd = nfc_open (NULL, connstrings[i]); if (pnd != NULL) { printf ("- %s:\n %s\n", nfc_device_get_name (pnd), nfc_device_get_connstring (pnd)); } nfc_close (pnd); } - nfc_exit (); + nfc_exit (NULL); return 0; } diff --git a/utils/nfc-read-forum-tag3.c b/utils/nfc-read-forum-tag3.c index c91b7a7..cc25a14 100644 --- a/utils/nfc-read-forum-tag3.c +++ b/utils/nfc-read-forum-tag3.c @@ -195,9 +195,9 @@ main(int argc, char *argv[]) } } - nfc_init (); + nfc_init (NULL); - pnd = nfc_open (NULL); + pnd = nfc_open (NULL, NULL); if (pnd == NULL) { ERR("Unable to open NFC device"); @@ -317,6 +317,6 @@ error: if (pnd) { nfc_close (pnd); } - nfc_exit (); + nfc_exit (NULL); exit (error); } diff --git a/utils/nfc-relay-picc.c b/utils/nfc-relay-picc.c index ace9f74..b74d371 100644 --- a/utils/nfc-relay-picc.c +++ b/utils/nfc-relay-picc.c @@ -190,11 +190,11 @@ main (int argc, char *argv[]) signal (SIGINT, (void (*)()) intr_hdlr); #endif - nfc_init (); + nfc_init (NULL); nfc_connstring connstrings[MAX_DEVICE_COUNT]; // List available devices - size_t szFound = nfc_list_devices (connstrings, MAX_DEVICE_COUNT); + size_t szFound = nfc_list_devices (NULL, connstrings, MAX_DEVICE_COUNT); if (initiator_only_mode || target_only_mode) { if (szFound < 1) { @@ -218,9 +218,9 @@ main (int argc, char *argv[]) // if there is more than one readers opened we open the second reader // (we hope they're always detected in the same order) if (szFound == 1) { - pndInitiator = nfc_open (connstrings[0]); + pndInitiator = nfc_open (NULL, connstrings[0]); } else { - pndInitiator = nfc_open (connstrings[1]); + pndInitiator = nfc_open (NULL, connstrings[1]); } if (!pndInitiator) { @@ -233,7 +233,7 @@ main (int argc, char *argv[]) if (nfc_initiator_init (pndInitiator) < 0) { printf ("Error: fail initializing initiator\n"); nfc_close (pndInitiator); - nfc_exit (); + nfc_exit (NULL); exit (EXIT_FAILURE); } @@ -245,7 +245,7 @@ main (int argc, char *argv[]) if (nfc_initiator_select_passive_target (pndInitiator, nm, NULL, 0, &ntRealTarget) < 0) { printf ("Error: no tag was found\n"); nfc_close (pndInitiator); - nfc_exit (); + nfc_exit (NULL); exit (EXIT_FAILURE); } @@ -255,25 +255,25 @@ main (int argc, char *argv[]) if (print_hex_fd4(ntRealTarget.nti.nai.abtUid, ntRealTarget.nti.nai.szUidLen, "UID") != EXIT_SUCCESS) { fprintf (stderr, "Error while printing UID to FD4\n"); nfc_close (pndInitiator); - nfc_exit (); + nfc_exit (NULL); exit(EXIT_FAILURE); } if (print_hex_fd4(ntRealTarget.nti.nai.abtAtqa, 2, "ATQA") != EXIT_SUCCESS) { fprintf (stderr, "Error while printing ATQA to FD4\n"); nfc_close (pndInitiator); - nfc_exit (); + nfc_exit (NULL); exit(EXIT_FAILURE); } if (print_hex_fd4(&(ntRealTarget.nti.nai.btSak), 1, "SAK") != EXIT_SUCCESS) { fprintf (stderr, "Error while printing SAK to FD4\n"); nfc_close (pndInitiator); - nfc_exit (); + nfc_exit (NULL); exit(EXIT_FAILURE); } if (print_hex_fd4(ntRealTarget.nti.nai.abtAts, ntRealTarget.nti.nai.szAtsLen, "ATS") != EXIT_SUCCESS) { fprintf (stderr, "Error while printing ATS to FD4\n"); nfc_close (pndInitiator); - nfc_exit (); + nfc_exit (NULL); exit(EXIT_FAILURE); } } @@ -297,7 +297,7 @@ main (int argc, char *argv[]) if (scan_hex_fd3(ntEmulatedTarget.nti.nai.abtUid, &(ntEmulatedTarget.nti.nai.szUidLen), "UID") != EXIT_SUCCESS) { fprintf (stderr, "Error while scanning UID from FD3\n"); nfc_close (pndInitiator); - nfc_exit (); + nfc_exit (NULL); exit(EXIT_FAILURE); } if (scan_hex_fd3(ntEmulatedTarget.nti.nai.abtAtqa, &foo, "ATQA") != EXIT_SUCCESS) { @@ -308,13 +308,13 @@ main (int argc, char *argv[]) if (scan_hex_fd3(&(ntEmulatedTarget.nti.nai.btSak), &foo, "SAK") != EXIT_SUCCESS) { fprintf (stderr, "Error while scanning SAK from FD3\n"); nfc_close (pndInitiator); - nfc_exit (); + nfc_exit (NULL); exit(EXIT_FAILURE); } if (scan_hex_fd3(ntEmulatedTarget.nti.nai.abtAts, &(ntEmulatedTarget.nti.nai.szAtsLen), "ATS") != EXIT_SUCCESS) { fprintf (stderr, "Error while scanning ATS from FD3\n"); nfc_close (pndInitiator); - nfc_exit (); + nfc_exit (NULL); exit(EXIT_FAILURE); } } else { @@ -353,13 +353,13 @@ main (int argc, char *argv[]) print_nfc_iso14443a_info (ntEmulatedTarget.nti.nai, false); // Try to open the NFC emulator device - pndTarget = nfc_open (connstrings[0]); + pndTarget = nfc_open (NULL, connstrings[0]); if (pndTarget == NULL) { printf ("Error opening NFC emulator device\n"); if (!target_only_mode) { nfc_close (pndInitiator); } - nfc_exit (); + nfc_exit (NULL); return EXIT_FAILURE; } @@ -372,7 +372,7 @@ main (int argc, char *argv[]) nfc_close (pndInitiator); } nfc_close (pndTarget); - nfc_exit (); + nfc_exit (NULL); exit(EXIT_FAILURE); } printf ("%s\n", "Done, relaying frames now!"); @@ -390,7 +390,7 @@ main (int argc, char *argv[]) nfc_close (pndInitiator); } nfc_close (pndTarget); - nfc_exit (); + nfc_exit (NULL); exit(EXIT_FAILURE); } szCapduLen = (size_t) res; @@ -398,7 +398,7 @@ main (int argc, char *argv[]) if (print_hex_fd4(abtCapdu, szCapduLen, "C-APDU") != EXIT_SUCCESS) { fprintf (stderr, "Error while printing C-APDU to FD4\n"); nfc_close (pndTarget); - nfc_exit (); + nfc_exit (NULL); exit(EXIT_FAILURE); } } @@ -406,7 +406,7 @@ main (int argc, char *argv[]) if (scan_hex_fd3(abtCapdu, &szCapduLen, "C-APDU") != EXIT_SUCCESS) { fprintf (stderr, "Error while scanning C-APDU from FD3\n"); nfc_close (pndInitiator); - nfc_exit (); + nfc_exit (NULL); exit(EXIT_FAILURE); } } @@ -424,7 +424,7 @@ main (int argc, char *argv[]) if (scan_hex_fd3(abtRapdu, &szRapduLen, "R-APDU") != EXIT_SUCCESS) { fprintf (stderr, "Error while scanning R-APDU from FD3\n"); nfc_close (pndTarget); - nfc_exit (); + nfc_exit (NULL); exit(EXIT_FAILURE); } ret = true; @@ -451,16 +451,16 @@ main (int argc, char *argv[]) } if (!initiator_only_mode) { nfc_close (pndTarget); - nfc_exit (); + nfc_exit (NULL); } - nfc_exit (); + nfc_exit (NULL); exit(EXIT_FAILURE); } } else { if (print_hex_fd4(abtRapdu, szRapduLen, "R-APDU") != EXIT_SUCCESS) { fprintf (stderr, "Error while printing R-APDU to FD4\n"); nfc_close (pndInitiator); - nfc_exit (); + nfc_exit (NULL); exit(EXIT_FAILURE); } } @@ -473,7 +473,7 @@ main (int argc, char *argv[]) if (!initiator_only_mode) { nfc_close (pndTarget); } - nfc_exit (); + nfc_exit (NULL); exit (EXIT_SUCCESS); } From a3a0002f8deae30350cb14a086fba9a18ed09a01 Mon Sep 17 00:00:00 2001 From: Romuald Conty Date: Wed, 18 Jan 2012 18:08:39 +0000 Subject: [PATCH 110/113] fix miss-returned error code (ETGREL -> NFC_ETGRELEASED). --- libnfc/chips/pn53x.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/libnfc/chips/pn53x.c b/libnfc/chips/pn53x.c index bf8f389..e1d5d10 100644 --- a/libnfc/chips/pn53x.c +++ b/libnfc/chips/pn53x.c @@ -197,11 +197,10 @@ pn53x_transceive (struct nfc_device *pnd, const uint8_t *pbtTx, const size_t szT default: CHIP_DATA(pnd)->last_status_byte = 0; } - log_put (LOG_CATEGORY, NFC_PRIORITY_TRACE, "Last command status: %s", pn53x_strerror(pnd)); switch (CHIP_DATA(pnd)->last_status_byte) { case 0: - res = szRx; + res = (int)szRx; break; case ETIMEOUT: case ECRC: @@ -236,6 +235,7 @@ pn53x_transceive (struct nfc_device *pnd, const uint8_t *pbtTx, const size_t szT case ETGREL: case ECDISCARDED: res = NFC_ETGRELEASED; + break; default: res = NFC_ECHIP; break; @@ -244,6 +244,12 @@ pn53x_transceive (struct nfc_device *pnd, const uint8_t *pbtTx, const size_t szT { EMFAUTH, "Mifare Authentication Error" }, */ + if (res < 0) { + pnd->last_error = res; + log_put (LOG_CATEGORY, NFC_PRIORITY_TRACE, "Chip error: \"%s\" (%02x), returned error: \"%s\" (%d))", pn53x_strerror (pnd), CHIP_DATA(pnd)->last_status_byte, nfc_strerror (pnd), res); + } else { + pnd->last_error = 0; + } return res; } From 838faa8d7ec8ecf9299406f1b3af003ac50142c4 Mon Sep 17 00:00:00 2001 From: Audrey Diacre Date: Thu, 19 Jan 2012 11:50:15 +0000 Subject: [PATCH 111/113] define symbols to expose (Fixes Issue 183). --- libnfc/Makefile.am | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libnfc/Makefile.am b/libnfc/Makefile.am index 634a9e9..575f2b2 100644 --- a/libnfc/Makefile.am +++ b/libnfc/Makefile.am @@ -18,7 +18,7 @@ libnfc_la_SOURCES = \ nfc-emulation.c \ nfc-internal.c -libnfc_la_LDFLAGS = -no-undefined -version-info 2:0:0 +libnfc_la_LDFLAGS = -no-undefined -version-info 2:0:0 -export-symbols-regex '^nfc_|iso14443a_crc_append|iso14443a_locate_historical_bytes|pn53x_transceive|pn53x_SAMConfiguration' libnfc_la_CFLAGS = @DRIVERS_CFLAGS@ libnfc_la_LIBADD = \ $(top_builddir)/libnfc/chips/libnfcchips.la \ From 5a475cf074844d5105b71578a1d5a8c8d9e277ba Mon Sep 17 00:00:00 2001 From: Audrey Diacre Date: Fri, 20 Jan 2012 09:17:38 +0000 Subject: [PATCH 112/113] add some whitespaces --- examples/nfc-emulate-forum-tag2.c | 4 ++-- examples/pn53x-tamashell.c | 22 +++++++++++----------- libnfc/chips/pn53x.c | 18 +++++++++--------- test/test_dep_active.c | 2 +- utils/nfc-mfsetuid.c | 2 +- utils/nfc-read-forum-tag3.c | 8 ++++---- utils/nfc-relay-picc.c | 12 ++++++------ 7 files changed, 34 insertions(+), 34 deletions(-) diff --git a/examples/nfc-emulate-forum-tag2.c b/examples/nfc-emulate-forum-tag2.c index 2e8ef6e..c636c7d 100644 --- a/examples/nfc-emulate-forum-tag2.c +++ b/examples/nfc-emulate-forum-tag2.c @@ -172,11 +172,11 @@ main(int argc, char *argv[]) }; struct nfc_emulation_state_machine state_machine = { - .io = nfcforum_tag2_io + .io = nfcforum_tag2_io }; struct nfc_emulator emulator = { - .target= &nt, + .target = &nt, .state_machine = &state_machine, .user_data = __nfcforum_tag2_memory_area, }; diff --git a/examples/pn53x-tamashell.c b/examples/pn53x-tamashell.c index 4ceee23..aad5006 100644 --- a/examples/pn53x-tamashell.c +++ b/examples/pn53x-tamashell.c @@ -80,7 +80,7 @@ int main(int argc, const char* argv[]) FILE* input = NULL; if (argc >= 2) { - if((input=fopen(argv[1], "r"))==NULL) { + if((input = fopen(argv[1], "r"))==NULL) { ERR ("%s", "Cannot open file."); return EXIT_FAILURE; } @@ -105,12 +105,12 @@ int main(int argc, const char* argv[]) char *cmd; char *prompt = "> "; while(1) { - int offset=0; + int offset = 0; #if defined(HAVE_READLINE) - if (input==NULL) { // means we use stdin - cmd=readline(prompt); + if (input == NULL) { // means we use stdin + cmd = readline(prompt); // NULL if ctrl-d - if (cmd==NULL) { + if (cmd == NULL) { printf("Bye!\n"); break; } @@ -137,18 +137,18 @@ int main(int argc, const char* argv[]) #if defined(HAVE_READLINE) } #endif //HAVE_READLINE - if (cmd[0]=='q') { + if (cmd[0] == 'q') { printf("Bye!\n"); free(cmd); break; } - if (cmd[0]=='p') { - int s=0; + if (cmd[0] == 'p') { + int s = 0; offset++; while (isspace(cmd[offset])) { offset++; } - sscanf(cmd+offset, "%d", &s); + sscanf(cmd + offset, "%d", &s); printf("Pause for %i msecs\n", s); if (s>0) { sleep(s * SUSP_TIME); @@ -157,14 +157,14 @@ int main(int argc, const char* argv[]) continue; } szTx = 0; - for(int i = 0; itype == PN532) { size_t szTargetTypes = 0; pn53x_target_type apttTargetTypes[32]; - for (size_t n=0; nlast_error = NFC_EINVARG; @@ -1122,8 +1122,8 @@ pn53x_initiator_poll_target (struct nfc_device *pnd, pn53x_set_property_bool (pnd, NP_INFINITE_SELECT, true); // FIXME It does not support DEP targets do { - for (size_t p=0; p> 8); BUFFER_APPEND (abtWriteRegisterCmd, PN53X_REG_CIU_FIFOLevel & 0xff); BUFFER_APPEND (abtWriteRegisterCmd, SYMBOL_FLUSH_BUFFER); - for (i=0; i< ((szTxBits / 8) + 1); i++) { + for (i = 0; i < ((szTxBits / 8) + 1); i++) { BUFFER_APPEND (abtWriteRegisterCmd, PN53X_REG_CIU_FIFOData >> 8); BUFFER_APPEND (abtWriteRegisterCmd, PN53X_REG_CIU_FIFOData & 0xff); BUFFER_APPEND (abtWriteRegisterCmd, pbtTx[i]); @@ -1440,7 +1440,7 @@ pn53x_initiator_transceive_bits_timed (struct nfc_device *pnd, const uint8_t *pb // our PN53x timer saturates after 4.8ms so this function shouldn't be used for // responses coming very late anyway. // Ideally we should implement a real timer here too but looping a few times is good enough. - for (i=0; i<(3 *(CHIP_DATA (pnd)->timer_prescaler * 2 + 1)); i++) { + for (i = 0; i < (3 *(CHIP_DATA (pnd)->timer_prescaler * 2 + 1)); i++) { pn53x_read_register (pnd, PN53X_REG_CIU_FIFOLevel, &sz); if (sz > 0) break; @@ -1453,7 +1453,7 @@ pn53x_initiator_transceive_bits_timed (struct nfc_device *pnd, const uint8_t *pb while (1) { BUFFER_INIT (abtReadRegisterCmd, PN53x_EXTENDED_FRAME__DATA_MAX_LEN); BUFFER_APPEND (abtReadRegisterCmd, ReadRegister); - for (i=0; i> 8); BUFFER_APPEND (abtReadRegisterCmd, PN53X_REG_CIU_FIFOData & 0xff); } @@ -1515,7 +1515,7 @@ pn53x_initiator_transceive_bytes_timed (struct nfc_device *pnd, const uint8_t *p BUFFER_APPEND (abtWriteRegisterCmd, PN53X_REG_CIU_FIFOLevel >> 8); BUFFER_APPEND (abtWriteRegisterCmd, PN53X_REG_CIU_FIFOLevel & 0xff); BUFFER_APPEND (abtWriteRegisterCmd, SYMBOL_FLUSH_BUFFER); - for (i=0; i< szTx; i++) { + for (i = 0; i < szTx; i++) { BUFFER_APPEND (abtWriteRegisterCmd, PN53X_REG_CIU_FIFOData >> 8); BUFFER_APPEND (abtWriteRegisterCmd, PN53X_REG_CIU_FIFOData & 0xff); BUFFER_APPEND (abtWriteRegisterCmd, pbtTx[i]); @@ -1535,7 +1535,7 @@ pn53x_initiator_transceive_bytes_timed (struct nfc_device *pnd, const uint8_t *p // our PN53x timer saturates after 4.8ms so this function shouldn't be used for // responses coming very late anyway. // Ideally we should implement a real timer here too but looping a few times is good enough. - for (i=0; i<(3 *(CHIP_DATA (pnd)->timer_prescaler * 2 + 1)); i++) { + for (i = 0; i < (3 *(CHIP_DATA (pnd)->timer_prescaler * 2 + 1)); i++) { pn53x_read_register (pnd, PN53X_REG_CIU_FIFOLevel, &sz); if (sz > 0) break; @@ -1548,7 +1548,7 @@ pn53x_initiator_transceive_bytes_timed (struct nfc_device *pnd, const uint8_t *p while (1) { BUFFER_INIT (abtReadRegisterCmd, PN53x_EXTENDED_FRAME__DATA_MAX_LEN); BUFFER_APPEND (abtReadRegisterCmd, ReadRegister); - for (i=0; i> 8); BUFFER_APPEND (abtReadRegisterCmd, PN53X_REG_CIU_FIFOData & 0xff); } diff --git a/test/test_dep_active.c b/test/test_dep_active.c index de0692d..42b180d 100644 --- a/test/test_dep_active.c +++ b/test/test_dep_active.c @@ -165,7 +165,7 @@ test_dep (void) .cut_test_context = test_context, }; - for (int i=0; i<3; i++) { + for (int i = 0; i < 3; i++) { initiator_data.nbr = nbrs[i]; if ((res = pthread_create (&(threads[TARGET]), NULL, target_thread, &target_data))) diff --git a/utils/nfc-mfsetuid.c b/utils/nfc-mfsetuid.c index 64ea4d2..4bf01b6 100644 --- a/utils/nfc-mfsetuid.c +++ b/utils/nfc-mfsetuid.c @@ -344,7 +344,7 @@ main (int argc, char *argv[]) transmit_bytes (abtWrite,4); transmit_bytes (abtData,18); if(format) { - for(i= 3 ; i < 64 ; i += 4) { + for(i = 3 ; i < 64 ; i += 4) { abtWrite[1]= (char) i; iso14443a_crc_append (abtWrite, 2); transmit_bytes (abtWrite,4); diff --git a/utils/nfc-read-forum-tag3.c b/utils/nfc-read-forum-tag3.c index cc25a14..6b2b7b3 100644 --- a/utils/nfc-read-forum-tag3.c +++ b/utils/nfc-read-forum-tag3.c @@ -98,7 +98,7 @@ nfc_forum_tag_type3_check (nfc_device *pnd, const nfc_target nt, const uint16_t }; size_t payload_len = 1 + 2 + 1; - for (uint8_t b=0; b MAX_FRAME_LEN) { return EXIT_FAILURE; } - if (fprintf (fd4, "#%s %04zx: ", pchPrefix, szBytes)<0) { + if (fprintf (fd4, "#%s %04zx: ", pchPrefix, szBytes) < 0) { return EXIT_FAILURE; } for (szPos = 0; szPos < szBytes; szPos++) { - if (fprintf (fd4, "%02x ", pbtData[szPos])<0) { + if (fprintf (fd4, "%02x ", pbtData[szPos]) < 0) { return EXIT_FAILURE; } } - if (fprintf (fd4, "\n")<0) { + if (fprintf (fd4, "\n") < 0) { return EXIT_FAILURE; } fflush(fd4); @@ -129,7 +129,7 @@ bool scan_hex_fd3 (uint8_t *pbtData, size_t *pszBytes, const char *pchPrefix) } strncpy(pchScan, pchPrefix, 250); strcat(pchScan, " %04x:"); - if (fscanf (fd3, pchScan, &uiBytes)<1) { + if (fscanf (fd3, pchScan, &uiBytes) < 1) { return EXIT_FAILURE; } *pszBytes=uiBytes; @@ -137,7 +137,7 @@ bool scan_hex_fd3 (uint8_t *pbtData, size_t *pszBytes, const char *pchPrefix) return EXIT_FAILURE; } for (szPos = 0; szPos < *pszBytes; szPos++) { - if (fscanf (fd3, "%02x", &uiData)<1) { + if (fscanf (fd3, "%02x", &uiData) < 1) { return EXIT_FAILURE; } pbtData[szPos]=uiData; @@ -168,7 +168,7 @@ main (int argc, char *argv[]) initiator_only_mode = true; target_only_mode = false; } else if (0 == strcmp (argv[arg], "-n")) { - if (++arg==argc || (sscanf(argv[arg], "%i", &waiting_time)<1)) { + if (++arg == argc || (sscanf(argv[arg], "%i", &waiting_time) < 1)) { ERR ("Missing or wrong waiting time value: %s.", argv[arg]); print_usage (argv); return EXIT_FAILURE; From 1f3b995c2ae5c5a1b2ef32bb9ad760f0c451d100 Mon Sep 17 00:00:00 2001 From: Audrey Diacre Date: Tue, 24 Jan 2012 08:42:47 +0000 Subject: [PATCH 113/113] add iso14443a_crc as symbol to expose for libfreefare. --- libnfc/Makefile.am | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libnfc/Makefile.am b/libnfc/Makefile.am index 575f2b2..7a62c23 100644 --- a/libnfc/Makefile.am +++ b/libnfc/Makefile.am @@ -18,7 +18,7 @@ libnfc_la_SOURCES = \ nfc-emulation.c \ nfc-internal.c -libnfc_la_LDFLAGS = -no-undefined -version-info 2:0:0 -export-symbols-regex '^nfc_|iso14443a_crc_append|iso14443a_locate_historical_bytes|pn53x_transceive|pn53x_SAMConfiguration' +libnfc_la_LDFLAGS = -no-undefined -version-info 2:0:0 -export-symbols-regex '^nfc_|^iso14443a_|pn53x_transceive|pn53x_SAMConfiguration' libnfc_la_CFLAGS = @DRIVERS_CFLAGS@ libnfc_la_LIBADD = \ $(top_builddir)/libnfc/chips/libnfcchips.la \