diff --git a/CMakeLists.txt b/CMakeLists.txt index a8605fa40..edc365163 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -16,6 +16,8 @@ SET(PACKAGE_NAME "libnfc") SET(PACKAGE_VERSION ${VERSION}) SET(PACKAGE_STRING "${PACKAGE_NAME} ${PACKAGE_VERSION}") +SET(CMAKE_C_STANDARD 99) + # config.h IF(WIN32) SET(LIBNFC_SYSCONFDIR "./config" CACHE PATH "libnfc configuration directory") @@ -52,9 +54,7 @@ ENDIF(LIBNFC_ENVVARS) SET(LIBNFC_DEBUG_MODE OFF CACHE BOOL "Debug mode") IF(LIBNFC_DEBUG_MODE) ADD_DEFINITIONS(-DDEBUG) - SET(CMAKE_C_FLAGS "-g3 ${CMAKE_C_FLAGS}") SET(WIN32_MODE "debug") - SET(CMAKE_RC_FLAGS "-D_DEBUG ${CMAKE_RC_FLAGS}") ELSE(LIBNFC_DEBUG_MODE) SET(WIN32_MODE "release") ENDIF(LIBNFC_DEBUG_MODE) @@ -93,12 +93,6 @@ IF(NOT DEFINED SHARE_INSTALL_PREFIX) SET(SHARE_INSTALL_PREFIX share) ENDIF(NOT DEFINED SHARE_INSTALL_PREFIX) -# Additonnal GCC flags -IF(CMAKE_COMPILER_IS_GNUCC) - # Make sure we will not miss some warnings ;) - SET(CMAKE_C_FLAGS "-Wall -pedantic -std=c99 ${CMAKE_C_FLAGS}") -ENDIF(CMAKE_COMPILER_IS_GNUCC) - # Workarounds for libusb in C99 ADD_DEFINITIONS(-Du_int8_t=uint8_t -Du_int16_t=uint16_t) @@ -152,20 +146,20 @@ ENDIF(NOT WIN32) INCLUDE(LibnfcDrivers) IF(${CMAKE_SYSTEM_NAME} MATCHES "Linux") - IF(I2C_REQUIRED) - # Inspired from http://cmake.3232098.n2.nabble.com/RFC-cmake-analog-to-AC-SEARCH-LIBS-td7585423.html - INCLUDE (CheckFunctionExists) - INCLUDE (CheckLibraryExists) - CHECK_FUNCTION_EXISTS (clock_gettime HAVE_CLOCK_GETTIME) - IF (NOT HAVE_CLOCK_GETTIME) - CHECK_LIBRARY_EXISTS (rt clock_gettime "" HAVE_CLOCK_GETTIME_IN_RT) - IF (HAVE_CLOCK_GETTIME_IN_RT) - SET(LIBRT_FOUND TRUE) - SET(LIBRT_LIBRARIES "rt") - ENDIF (HAVE_CLOCK_GETTIME_IN_RT) - ENDIF (NOT HAVE_CLOCK_GETTIME) - ENDIF(I2C_REQUIRED) - ENDIF(${CMAKE_SYSTEM_NAME} MATCHES "Linux") + IF(I2C_REQUIRED) + # Inspired from http://cmake.3232098.n2.nabble.com/RFC-cmake-analog-to-AC-SEARCH-LIBS-td7585423.html + INCLUDE (CheckFunctionExists) + INCLUDE (CheckLibraryExists) + CHECK_FUNCTION_EXISTS (clock_gettime HAVE_CLOCK_GETTIME) + IF (NOT HAVE_CLOCK_GETTIME) + CHECK_LIBRARY_EXISTS (rt clock_gettime "" HAVE_CLOCK_GETTIME_IN_RT) + IF (HAVE_CLOCK_GETTIME_IN_RT) + SET(LIBRT_FOUND TRUE) + SET(LIBRT_LIBRARIES "rt") + ENDIF (HAVE_CLOCK_GETTIME_IN_RT) + ENDIF (NOT HAVE_CLOCK_GETTIME) + ENDIF(I2C_REQUIRED) +ENDIF(${CMAKE_SYSTEM_NAME} MATCHES "Linux") IF(PCSC_INCLUDE_DIRS) INCLUDE_DIRECTORIES(${PCSC_INCLUDE_DIRS}) diff --git a/contrib/win32/libnfc/buses/uart.c b/contrib/win32/libnfc/buses/uart.c index ba7ddfe80..6b2478d5d 100644 --- a/contrib/win32/libnfc/buses/uart.c +++ b/contrib/win32/libnfc/buses/uart.c @@ -45,9 +45,7 @@ #define LOG_GROUP NFC_LOG_GROUP_COM #define LOG_CATEGORY "libnfc.bus.uart_win32" -// Handle platform specific includes #include "contrib/windows.h" -#define delay_ms( X ) Sleep( X ) struct serial_port_windows { HANDLE hPort; // Serial port handle diff --git a/examples/nfc-emulate-tag.c b/examples/nfc-emulate-tag.c index a2adbe67f..a3f96e265 100644 --- a/examples/nfc-emulate-tag.c +++ b/examples/nfc-emulate-tag.c @@ -189,11 +189,7 @@ main(int argc, char *argv[]) (void) argc; const char *acLibnfcVersion; -#ifdef WIN32 - signal(SIGINT, (void (__cdecl *)(int)) intr_hdlr); -#else signal(SIGINT, intr_hdlr); -#endif nfc_init(&context); if (context == NULL) { diff --git a/examples/nfc-emulate-uid.c b/examples/nfc-emulate-uid.c index 690388ed8..ff29d879f 100644 --- a/examples/nfc-emulate-uid.c +++ b/examples/nfc-emulate-uid.c @@ -130,11 +130,7 @@ main(int argc, char *argv[]) } } -#ifdef WIN32 - signal(SIGINT, (void (__cdecl *)(int)) intr_hdlr); -#else signal(SIGINT, intr_hdlr); -#endif nfc_init(&context); if (context == NULL) { diff --git a/examples/nfc-poll.c b/examples/nfc-poll.c index 4cea5d635..2acacc60f 100644 --- a/examples/nfc-poll.c +++ b/examples/nfc-poll.c @@ -44,7 +44,6 @@ # include "config.h" #endif // HAVE_CONFIG_H -#include #include #include #include diff --git a/examples/nfc-relay.c b/examples/nfc-relay.c index e03c8cd20..6c0948c8e 100644 --- a/examples/nfc-relay.c +++ b/examples/nfc-relay.c @@ -109,11 +109,7 @@ main(int argc, char *argv[]) // Display libnfc version printf("%s uses libnfc %s\n", argv[0], acLibnfcVersion); -#ifdef WIN32 - signal(SIGINT, (void (__cdecl *)(int)) intr_hdlr); -#else signal(SIGINT, intr_hdlr); -#endif nfc_context *context; nfc_init(&context); diff --git a/examples/nfc-st25tb.c b/examples/nfc-st25tb.c index 7eb5deeb6..47c7263fc 100644 --- a/examples/nfc-st25tb.c +++ b/examples/nfc-st25tb.c @@ -64,11 +64,7 @@ #include #include -#if defined(WIN32) /* mingw compiler */ -#include -#endif - -#define ST25TB_SR_BLOCK_MAX_SIZE ((uint8_t) 4) // for static arrays +#define ST25TB_SR_BLOCK_MAX_SIZE ((size_t) 4) // for static arrays typedef void(*get_info_specific) (uint8_t * systemArea); typedef struct _st_data { diff --git a/examples/pn53x-diagnose.c b/examples/pn53x-diagnose.c index b5b0e5252..50f5169b7 100644 --- a/examples/pn53x-diagnose.c +++ b/examples/pn53x-diagnose.c @@ -43,7 +43,6 @@ # include "config.h" #endif // HAVE_CONFIG_H -#include #include #include diff --git a/examples/pn53x-tamashell.c b/examples/pn53x-tamashell.c index 52beee16a..d384098a1 100644 --- a/examples/pn53x-tamashell.c +++ b/examples/pn53x-tamashell.c @@ -54,24 +54,11 @@ #include #include -#ifndef _WIN32 -# include -# define msleep(x) do { \ - struct timespec xsleep; \ - xsleep.tv_sec = x / 1000; \ - xsleep.tv_nsec = (x - xsleep.tv_sec * 1000) * 1000 * 1000; \ - nanosleep(&xsleep, NULL); \ - } while (0) -#else -# include -# define msleep Sleep -#endif - - #include #include "utils/nfc-utils.h" #include "libnfc/chips/pn53x.h" +#include "libnfc/nfc-internal.h" #define MAX_FRAME_LEN 264 diff --git a/libnfc/buses/i2c.c b/libnfc/buses/i2c.c index dc1ad1f02..b400bc7e8 100644 --- a/libnfc/buses/i2c.c +++ b/libnfc/buses/i2c.c @@ -41,10 +41,10 @@ #include #include #include -#include #include #include #include +#include #include #include #include diff --git a/libnfc/buses/spi.c b/libnfc/buses/spi.c index 0e68f9b47..4a2bc308c 100644 --- a/libnfc/buses/spi.c +++ b/libnfc/buses/spi.c @@ -45,7 +45,6 @@ #include #include #include -#include #include #include #include diff --git a/libnfc/buses/uart.c b/libnfc/buses/uart.c index ffe64aa56..1e8054b97 100644 --- a/libnfc/buses/uart.c +++ b/libnfc/buses/uart.c @@ -57,22 +57,6 @@ #define LOG_GROUP NFC_LOG_GROUP_COM #define LOG_CATEGORY "libnfc.bus.uart" -#ifndef _WIN32 -// Needed by sleep() under Unix -# include -# include -# define msleep(x) do { \ - struct timespec xsleep; \ - xsleep.tv_sec = x / 1000; \ - xsleep.tv_nsec = (x - xsleep.tv_sec * 1000) * 1000 * 1000; \ - nanosleep(&xsleep, NULL); \ - } while (0) -#else -// Needed by Sleep() under Windows -# include -# define msleep Sleep -#endif - # if defined(__APPLE__) const char *serial_ports_device_radix[] = { "tty.SLAB_USBtoUART", "tty.usbserial", "tty.usbmodem", NULL }; # elif defined (__FreeBSD__) || defined (__OpenBSD__) || defined(__FreeBSD_kernel__) diff --git a/libnfc/chips/pn53x.c b/libnfc/chips/pn53x.c index e5d9d9406..a2c72d4f5 100644 --- a/libnfc/chips/pn53x.c +++ b/libnfc/chips/pn53x.c @@ -50,6 +50,9 @@ #define LOG_CATEGORY "libnfc.chip.pn53x" #define LOG_GROUP NFC_LOG_GROUP_CHIP +#define SAK_ISO14443_4_COMPLIANT 0x20 +#define SAK_ISO18092_COMPLIANT 0x40 + 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 }; @@ -2367,8 +2370,6 @@ pn53x_initiator_target_is_present(struct nfc_device *pnd, const nfc_target *pnt) return pnd->last_error = ret; } -#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, const size_t szRxLen, int timeout) { diff --git a/libnfc/drivers/acr122_usb.c b/libnfc/drivers/acr122_usb.c index fc545014b..4041e5640 100644 --- a/libnfc/drivers/acr122_usb.c +++ b/libnfc/drivers/acr122_usb.c @@ -697,7 +697,7 @@ acr122_usb_receive(nfc_device *pnd, uint8_t *pbtData, const size_t szDataLen, co len = abtRxBuf[offset++]; if ((abtRxBuf[offset] != 0x00) && (abtRxBuf[offset + 1] != 0x00) && (abtRxBuf[offset + 2] != 0x00)) { log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "%s", "Not implemented: only 1-byte length is supported, please report this bug with a full trace."); - pnd->last_error = NFC_EIO; + pnd->last_error = NFC_ENOTIMPL; return pnd->last_error; } offset += 3; diff --git a/libnfc/drivers/acr122s.c b/libnfc/drivers/acr122s.c index 7a6a9f45a..d97752211 100644 --- a/libnfc/drivers/acr122s.c +++ b/libnfc/drivers/acr122s.c @@ -225,7 +225,7 @@ acr122s_send_frame(nfc_device *pnd, uint8_t *frame, int timeout) } /** - * Receive response frame after a successfull acr122s_send_command(). + * Receive response frame after a successful acr122s_send_command(). * * @param: pnd is target nfc device * @param: frame is buffer where received response frame will be stored @@ -272,7 +272,7 @@ acr122s_recv_frame(nfc_device *pnd, uint8_t *frame, size_t frame_size, void *abo #define APDU_OVERHEAD (FRAME_OVERHEAD + 5) /** - * Convert host uint32 to litle endian uint32 + * Convert host uint32 to little endian uint32 */ static uint32_t le32(uint32_t val) @@ -417,7 +417,7 @@ acr122s_scan(const nfc_context *context, nfc_connstring connstrings[], const siz size_t device_found = 0; serial_port sp; char **acPorts = uart_list_ports(); - const char *acPort; + char *acPort; int iDevice = 0; while ((acPort = acPorts[iDevice++])) { @@ -621,7 +621,6 @@ acr122s_open(const nfc_context *context, const nfc_connstring connstring) } CHIP_DATA(pnd)->type = PN532; -#if 1 // Retrieve firmware version char version[DEVICE_NAME_LENGTH]; if (acr122s_get_firmware_version(pnd, version, sizeof(version)) != 0) { @@ -637,7 +636,7 @@ acr122s_open(const nfc_context *context, const nfc_connstring connstring) return NULL; } - snprintf(pnd->name, sizeof(pnd->name), "%s", version); + strncpy(pnd->name, version, DEVICE_NAME_LENGTH); // Activate SAM before operating if (acr122s_activate_sam(pnd) != 0) { @@ -645,7 +644,6 @@ acr122s_open(const nfc_context *context, const nfc_connstring connstring) acr122s_close(pnd); return NULL; } -#endif if (pn53x_init(pnd) < 0) { log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "%s", "Failed initializing PN532 chip."); diff --git a/libnfc/drivers/arygon.c b/libnfc/drivers/arygon.c index acaab4652..1a95f8f30 100644 --- a/libnfc/drivers/arygon.c +++ b/libnfc/drivers/arygon.c @@ -103,7 +103,7 @@ arygon_scan(const nfc_context *context, nfc_connstring connstrings[], const size size_t device_found = 0; serial_port sp; char **acPorts = uart_list_ports(); - const char *acPort; + char *acPort; int iDevice = 0; while ((acPort = acPorts[iDevice++])) { diff --git a/libnfc/drivers/pcsc.c b/libnfc/drivers/pcsc.c index 106d99ac4..5b087cca5 100644 --- a/libnfc/drivers/pcsc.c +++ b/libnfc/drivers/pcsc.c @@ -67,17 +67,12 @@ #define SCARD_ATTR_VENDOR_IFD_SERIAL_NO SCARD_ATTR_VALUE(SCARD_CLASS_VENDOR_INFO, 0x0103) /**< Vendor-supplied interface device serial number. */ #define SCARD_ATTR_ICC_TYPE_PER_ATR SCARD_ATTR_VALUE(SCARD_CLASS_ICC_STATE, 0x0304) /**< Single byte indicating smart card type */ #else -#ifndef _Win32 +#ifndef _WIN32 #include #endif #include #endif -#ifdef WIN32 -#include -#define usleep(x) Sleep((x + 999) / 1000) -#endif - #define PCSC_DRIVER_NAME "pcsc" #include @@ -549,7 +544,7 @@ pcsc_open(const nfc_context *context, const nfc_connstring connstring) DRIVER_DATA(pnd)->dwShareMode = SCARD_SHARE_DIRECT; // Done, we found the reader we are looking for - snprintf(pnd->name, sizeof(pnd->name), "%s", ndd.pcsc_device_name); + strcpy(pnd->name, ndd.pcsc_device_name); pnd->driver = &pcsc_driver; @@ -713,14 +708,14 @@ static const char *stringify_error(const LONG pcscError) msg = "Feature not supported."; break; default: - (void)snprintf(strError, sizeof(strError) - 1, "Unknown error: 0x%08lX", + snprintf(strError, sizeof(strError) - 1, "Unknown error: 0x%08lX", pcscError); }; if (msg) - (void)strncpy(strError, msg, sizeof(strError)); + strncpy(strError, msg, sizeof(strError)); else - (void)snprintf(strError, sizeof(strError) - 1, "Unknown error: 0x%08lX", + snprintf(strError, sizeof(strError) - 1, "Unknown error: 0x%08lX", pcscError); /* add a null byte */ @@ -826,7 +821,7 @@ static int pcsc_initiator_transceive_bytes(struct nfc_device *pnd, const uint8_t pnd->last_error = pcsc_transmit(pnd, apdu_data, send_size, resp, &resp_len); memset(apdu_data, 0, sizeof(apdu_data)); memset(resp, 0, sizeof(resp)); - usleep(500000);//delay 500ms + msleep(500); } // then auth apdu_data[0] = 0xFF; diff --git a/libnfc/drivers/pn532_spi.c b/libnfc/drivers/pn532_spi.c index 39a8b51ab..4d243c588 100644 --- a/libnfc/drivers/pn532_spi.c +++ b/libnfc/drivers/pn532_spi.c @@ -54,22 +54,6 @@ #define LOG_CATEGORY "libnfc.driver.pn532_spi" #define LOG_GROUP NFC_LOG_GROUP_DRIVER -#ifndef _WIN32 -// Needed by sleep() under Unix -# include -# include -# define msleep(x) do { \ - struct timespec xsleep; \ - xsleep.tv_sec = x / 1000; \ - xsleep.tv_nsec = (x - xsleep.tv_sec * 1000) * 1000 * 1000; \ - nanosleep(&xsleep, NULL); \ - } while (0) -#else -// Needed by Sleep() under Windows -# include -# define msleep Sleep -#endif - // Internal data structs const struct pn53x_io pn532_spi_io; struct pn532_spi_data { diff --git a/libnfc/drivers/pn532_uart.c b/libnfc/drivers/pn532_uart.c index d82051d7f..680b80684 100644 --- a/libnfc/drivers/pn532_uart.c +++ b/libnfc/drivers/pn532_uart.c @@ -77,7 +77,7 @@ pn532_uart_scan(const nfc_context *context, nfc_connstring connstrings[], const size_t device_found = 0; serial_port sp; char **acPorts = uart_list_ports(); - const char *acPort; + char *acPort; int iDevice = 0; while ((acPort = acPorts[iDevice++])) { diff --git a/libnfc/drivers/pn53x_usb.c b/libnfc/drivers/pn53x_usb.c index 52c40f082..8d80c82bd 100644 --- a/libnfc/drivers/pn53x_usb.c +++ b/libnfc/drivers/pn53x_usb.c @@ -257,7 +257,11 @@ pn53x_usb_get_end_points_default(struct usb_device *dev, struct pn53x_usb_data * return false; } -int pn53x_usb_ack(nfc_device *pnd); +int +pn53x_usb_ack(nfc_device *pnd) +{ + return pn53x_usb_bulk_write(DRIVER_DATA(pnd), (uint8_t *) pn53x_ack_frame, sizeof(pn53x_ack_frame), 1000); +} // Find transfer endpoints for bulk transfers static void @@ -741,12 +745,6 @@ pn53x_usb_receive(nfc_device *pnd, uint8_t *pbtData, const size_t szDataLen, con return len; } -int -pn53x_usb_ack(nfc_device *pnd) -{ - return pn53x_usb_bulk_write(DRIVER_DATA(pnd), (uint8_t *) pn53x_ack_frame, sizeof(pn53x_ack_frame), 1000); -} - int pn53x_usb_init(nfc_device *pnd) { diff --git a/libnfc/nfc-internal.h b/libnfc/nfc-internal.h index 74c05c210..6f1407315 100644 --- a/libnfc/nfc-internal.h +++ b/libnfc/nfc-internal.h @@ -33,13 +33,8 @@ #define __NFC_INTERNAL_H__ #include -#include -#if !defined(_MSC_VER) -# include -#endif #include "nfc/nfc.h" - #include "log.h" /** @@ -63,6 +58,22 @@ #define MAX(a,b) (((a) > (b)) ? (a) : (b)) #endif +#ifndef _WIN32 +// Needed by sleep() under Unix +# include +# include +# define msleep(x) do { \ + struct timespec xsleep; \ + xsleep.tv_sec = x / 1000; \ + xsleep.tv_nsec = (x - xsleep.tv_sec * 1000) * 1000 * 1000; \ + nanosleep(&xsleep, NULL); \ + } while (0) +#else +// Needed by Sleep() under Windows +# include +# define msleep Sleep +#endif + /* * Buffer management macros. * diff --git a/utils/nfc-list.c b/utils/nfc-list.c index bb6aaef9b..ebc53bc35 100644 --- a/utils/nfc-list.c +++ b/utils/nfc-list.c @@ -44,7 +44,6 @@ # include "config.h" #endif // HAVE_CONFIG_H -#include #include #include #include diff --git a/utils/nfc-mfclassic.c b/utils/nfc-mfclassic.c index 244af45cd..0e9c2f6ab 100644 --- a/utils/nfc-mfclassic.c +++ b/utils/nfc-mfclassic.c @@ -50,7 +50,6 @@ #include #include #include - #include #include diff --git a/utils/nfc-read-forum-tag3.c b/utils/nfc-read-forum-tag3.c index 52c6c2f8d..890cecd1c 100644 --- a/utils/nfc-read-forum-tag3.c +++ b/utils/nfc-read-forum-tag3.c @@ -62,10 +62,6 @@ #include "nfc-utils.h" -#if defined(WIN32) /* mingw compiler */ -#include -#endif - static nfc_device *pnd; static nfc_context *context; diff --git a/utils/nfc-relay-picc.c b/utils/nfc-relay-picc.c index d694f8a6c..9199edfe0 100644 --- a/utils/nfc-relay-picc.c +++ b/utils/nfc-relay-picc.c @@ -55,7 +55,6 @@ #include #include #include - #include #include @@ -199,11 +198,7 @@ main(int argc, char *argv[]) // Display libnfc version printf("%s uses libnfc %s\n", argv[0], acLibnfcVersion); -#ifdef WIN32 - signal(SIGINT, (void (__cdecl *)(int)) intr_hdlr); -#else signal(SIGINT, intr_hdlr); -#endif nfc_context *context; nfc_init(&context); diff --git a/utils/nfc-scan-device.c b/utils/nfc-scan-device.c index bdc100de8..24482eed4 100644 --- a/utils/nfc-scan-device.c +++ b/utils/nfc-scan-device.c @@ -43,7 +43,6 @@ # include "config.h" #endif // HAVE_CONFIG_H -#include #include #include #include diff --git a/utils/nfc-utils.c b/utils/nfc-utils.c index 72a33eda2..c9e94ac8f 100644 --- a/utils/nfc-utils.c +++ b/utils/nfc-utils.c @@ -38,7 +38,6 @@ * @brief Provide some examples shared functions like print, parity calculation, options parsing. */ #include -#include #include "nfc-utils.h"