diff --git a/configure.ac b/configure.ac index 8454f18..36a75b5 100644 --- a/configure.ac +++ b/configure.ac @@ -69,6 +69,17 @@ 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/libnfc/Makefile.am b/libnfc/Makefile.am index 2599aa3..5af2476 100644 --- a/libnfc/Makefile.am +++ b/libnfc/Makefile.am @@ -6,6 +6,7 @@ INCLUDES = $(all_includes) $(LIBNFC_CFLAGS) noinst_HEADERS = \ drivers.h \ iso7816.h \ + log.h \ mirror-subr.h \ nfc-internal.h lib_LTLIBRARIES = libnfc.la @@ -32,4 +33,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.c +endif + EXTRA_DIST = CMakeLists.txt diff --git a/libnfc/buses/uart_posix.c b/libnfc/buses/uart_posix.c index b430287..a27a783 100644 --- a/libnfc/buses/uart_posix.c +++ b/libnfc/buses/uart_posix.c @@ -45,6 +45,8 @@ #include "nfc-internal.h" +#define LOG_CATEGORY "libnfc.bus.uart" + # if defined(__APPLE__) // FIXME: find UART connection string for PN53X device on Mac OS X when multiples devices are used char *serial_ports_device_radix[] = { "tty.SLAB_USBtoUART", NULL }; @@ -127,14 +129,14 @@ 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); - DBG ("%d bytes have eatten.", available_bytes_count); + log_put (LOG_CATEGORY, NFC_PRIORITY_TRACE, "%d bytes have eatten.", available_bytes_count); free (rx); } void uart_set_speed (serial_port sp, const uint32_t uiPortSpeed) { - DBG ("Serial port speed requested to be set to %d bauds.", 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; // Portability note: on some systems, B9600 != 9600 so we have to do @@ -171,7 +173,7 @@ uart_set_speed (serial_port sp, const uint32_t uiPortSpeed) break; # endif default: - ERR ("Unable to set serial port speed to %d bauds. Speed value must be one of those defined in termios(3).", + log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "Unable to set serial port speed to %d bauds. Speed value must be one of those defined in termios(3).", uiPortSpeed); return; }; @@ -180,7 +182,7 @@ uart_set_speed (serial_port sp, const uint32_t uiPortSpeed) cfsetispeed (&(spu->termios_new), stPortSpeed); cfsetospeed (&(spu->termios_new), stPortSpeed); if (tcsetattr (spu->fd, TCSADRAIN, &(spu->termios_new)) == -1) { - ERR ("%s", "Unable to apply new speed settings."); + log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Unable to apply new speed settings."); } } @@ -283,18 +285,18 @@ select: // Read error if (res < 0) { - DBG ("%s", "RX error."); + log_put (LOG_CATEGORY, NFC_PRIORITY_TRACE, "%s", "RX error."); return ECOMIO; } // Read time-out if (res == 0) { - DBG ("Timeout!"); + log_put (LOG_CATEGORY, NFC_PRIORITY_TRACE, "Timeout!"); return ECOMTIMEOUT; } if (FD_ISSET (iAbortFd, &rfds)) { // Abort requested - DBG ("Abort!"); + log_put (LOG_CATEGORY, NFC_PRIORITY_TRACE, "Abort!"); close (iAbortFd); return EOPABORT; } @@ -305,7 +307,6 @@ select: return ECOMIO; } // There is something available, read the data - // DBG ("expected bytes: %zu, received bytes: %d, available bytes: %d", szRx, received_bytes_count, available_bytes_count); res = read (((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) { @@ -314,7 +315,7 @@ select: received_bytes_count += res; } while (expected_bytes_count > received_bytes_count); - PRINT_HEX ("RX", pbtRx, szRx); + LOG_HEX ("RX", pbtRx, szRx); return 0; } @@ -326,7 +327,7 @@ select: int uart_send (serial_port sp, const byte_t * pbtTx, const size_t szTx) { - PRINT_HEX ("TX", pbtTx, szTx); + LOG_HEX ("TX", pbtTx, szTx); if ((int) szTx == write (((serial_port_unix *) sp)->fd, pbtTx, szTx)) return 0; else diff --git a/libnfc/chips/pn53x-internal.h b/libnfc/chips/pn53x-internal.h index db89ecb..b133d38 100644 --- a/libnfc/chips/pn53x-internal.h +++ b/libnfc/chips/pn53x-internal.h @@ -25,6 +25,8 @@ #ifndef __PN53X_INTERNAL_H__ #define __PN53X_INTERNAL_H__ +#include "log.h" + // Miscellaneous #define Diagnose 0x00 #define GetFirmwareVersion 0x02 @@ -112,7 +114,7 @@ typedef struct { uint8_t ui8Code; uint8_t ui8CompatFlags; -#ifdef DEBUG +#ifdef LOGGING const char * abtCommandText; #endif } pn53x_command; @@ -125,16 +127,15 @@ typedef enum { RCS360 = 0x08 } pn53x_type; -#ifndef DEBUG +#ifndef LOGGING # define PNCMD( X, Y ) { X , Y } -# define PNCMD_DBG( X ) do { \ - } while(0) +# define PNCMD_TRACE( X ) do {} while(0) #else # define PNCMD( X, Y ) { X , Y, #X } -# define PNCMD_DBG( X ) do { \ +# define PNCMD_TRACE( X ) do { \ for (size_t i=0; i<(sizeof(pn53x_commands)/sizeof(pn53x_command)); i++) { \ if ( X == pn53x_commands[i].ui8Code ) { \ - DBG( "%s", pn53x_commands[i].abtCommandText ); \ + log_put( LOG_CATEGORY, NFC_PRIORITY_TRACE, "%s", pn53x_commands[i].abtCommandText ); \ break; \ } \ } \ @@ -199,23 +200,26 @@ static const pn53x_command pn53x_commands[] = { #define P35 5 // Registers part -#ifdef DEBUG +#ifdef LOGGING typedef struct { uint16_t ui16Address; const char * abtRegisterText; const char * abtRegisterDescription; } pn53x_register; -#endif -#ifndef DEBUG -# define PNREG_DBG( X ) do { \ +# define PNREG( X, Y ) { X , #X, Y } + +#endif /* LOGGING */ + + +#ifndef LOGGING +# define PNREG_TRACE( X ) do { \ } while(0) #else -# define PNREG( X, Y ) { X , #X, Y } -# define PNREG_DBG( X ) do { \ +# define PNREG_TRACE( X ) do { \ for (size_t i=0; i<(sizeof(pn53x_registers)/sizeof(pn53x_register)); i++) { \ if ( X == pn53x_registers[i].ui16Address ) { \ - DBG( "%s (%s)", pn53x_registers[i].abtRegisterText, pn53x_registers[i].abtRegisterDescription ); \ + log_put( LOG_CATEGORY, NFC_PRIORITY_TRACE, "%s (%s)", pn53x_registers[i].abtRegisterText, pn53x_registers[i].abtRegisterDescription ); \ break; \ } \ } \ @@ -296,7 +300,7 @@ typedef struct { #define PN53X_SFR_P7 0xFFF7 -#ifdef DEBUG +#ifdef LOGGING static const pn53x_register pn53x_registers[] = { PNREG (PN53X_REG_CIU_Mode, "Defines general modes for transmitting and receiving"), PNREG (PN53X_REG_CIU_TxMode, "Defines the transmission data rate and framing during transmission"), diff --git a/libnfc/chips/pn53x.c b/libnfc/chips/pn53x.c index 1b84dfd..8010787 100644 --- a/libnfc/chips/pn53x.c +++ b/libnfc/chips/pn53x.c @@ -36,6 +36,7 @@ #include #include +#include #include "pn53x.h" #include "pn53x-internal.h" @@ -43,7 +44,7 @@ #include "mirror-subr.h" #include "nfc-internal.h" -#include +#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 }; @@ -60,6 +61,8 @@ pn53x_target_type_t pn53x_nm_to_ptt (const nfc_modulation_t nm); bool pn53x_init(nfc_device_t * pnd) { + log_put ("LOG_CATEGORY", NFC_PRIORITY_TRACE, "%s", "pn53x_init"); + // GetFirmwareVersion command is used to set PN53x chips type (PN531, PN532 or PN533) char abtFirmwareText[22]; if (!pn53x_get_firmware_version (pnd, abtFirmwareText)) { @@ -107,7 +110,7 @@ pn53x_transceive (nfc_device_t * pnd, const byte_t * pbtTx, const size_t szTx, b } } - PNCMD_DBG (pbtTx[0]); + PNCMD_TRACE (pbtTx[0]); byte_t abtRx[PN53x_EXTENDED_FRAME__DATA_MAX_LEN]; size_t szRx = sizeof(abtRx); @@ -463,7 +466,7 @@ pn53x_ReadRegister (nfc_device_t * pnd, uint16_t ui16RegisterAddress, uint8_t * byte_t abtRegValue[2]; size_t szRegValue = sizeof (abtRegValue); - DBG ("ReadRegister (%04x)", ui16RegisterAddress); + PNREG_TRACE (ui16RegisterAddress); if (!pn53x_transceive (pnd, abtCmd, sizeof (abtCmd), abtRegValue, &szRegValue)) { return false; } @@ -485,8 +488,7 @@ bool pn53x_WriteRegister (nfc_device_t * pnd, const uint16_t ui16RegisterAddress, const uint8_t ui8Value) { byte_t abtCmd[] = { WriteRegister, ui16RegisterAddress >> 8, ui16RegisterAddress & 0xff, ui8Value }; - PNREG_DBG (ui16RegisterAddress); - DBG ("WriteRegister (%04x, %02x)", ui16RegisterAddress, ui8Value); + PNREG_TRACE (ui16RegisterAddress); return pn53x_transceive (pnd, abtCmd, sizeof (abtCmd), NULL, NULL); } @@ -512,7 +514,6 @@ pn53x_write_register (nfc_device_t * pnd, const uint16_t ui16RegisterAddress, co CHIP_DATA (pnd)->wb_data[internal_address] = (CHIP_DATA (pnd)->wb_data[internal_address] & CHIP_DATA (pnd)->wb_mask[internal_address] & (~ui8SymbolMask)) | (ui8Value & ui8SymbolMask); CHIP_DATA (pnd)->wb_mask[internal_address] = CHIP_DATA (pnd)->wb_mask[internal_address] | ui8SymbolMask; CHIP_DATA (pnd)->wb_trigged = true; - DBG ("WriteBackRegister (%04x, %02x, %02x)", ui16RegisterAddress, CHIP_DATA (pnd)->wb_data[internal_address], CHIP_DATA (pnd)->wb_mask[internal_address]); } return true; } @@ -567,10 +568,10 @@ pn53x_writeback_register (nfc_device_t * pnd) for (size_t n = 0; n < PN53X_CACHE_REGISTER_SIZE; n++) { if (CHIP_DATA (pnd)->wb_mask[n] == 0xff) { const uint16_t pn53x_register_address = PN53X_CACHE_REGISTER_MIN_ADDRESS + n; + PNREG_TRACE (pn53x_register_address); BUFFER_APPEND (abtWriteRegisterCmd, pn53x_register_address >> 8); BUFFER_APPEND (abtWriteRegisterCmd, pn53x_register_address & 0xff); BUFFER_APPEND (abtWriteRegisterCmd, CHIP_DATA (pnd)->wb_data[n]); - DBG ("WriteBackRegister will write (%04x, %02x)", pn53x_register_address, CHIP_DATA (pnd)->wb_data[n]); // This register is handled, we reset the mask to prevent CHIP_DATA (pnd)->wb_mask[n] = 0x00; } @@ -2000,7 +2001,7 @@ bool pn53x_InDeselect (nfc_device_t * pnd, const uint8_t ui8Target) { if (CHIP_DATA(pnd)->type == RCS360) { - // We should do act here *only* if a target was previsouly selected + // We should do act here *only* if a target was previously selected byte_t abtStatus[PN53x_EXTENDED_FRAME__DATA_MAX_LEN]; size_t szStatus = sizeof(abtStatus); byte_t abtCmdGetStatus[] = { GetGeneralStatus }; @@ -2022,7 +2023,7 @@ bool pn53x_InRelease (nfc_device_t * pnd, const uint8_t ui8Target) { if (CHIP_DATA(pnd)->type == RCS360) { - // We should do act here *only* if a target was previsouly selected + // We should do act here *only* if a target was previously selected byte_t abtStatus[PN53x_EXTENDED_FRAME__DATA_MAX_LEN]; size_t szStatus = sizeof(abtStatus); byte_t abtCmdGetStatus[] = { GetGeneralStatus }; @@ -2272,12 +2273,12 @@ pn53x_check_ack_frame (nfc_device_t * pnd, const byte_t * pbtRxFrame, const size { if (szRxFrameLen >= sizeof (pn53x_ack_frame)) { if (0 == memcmp (pbtRxFrame, pn53x_ack_frame, sizeof (pn53x_ack_frame))) { - // DBG ("%s", "PN53x ACKed"); + log_put ("LOG_CATEGORY", NFC_PRIORITY_TRACE, "PN53x ACKed"); return true; } } pnd->iLastError = EFRAACKMISMATCH; - ERR ("%s", "Unexpected PN53x reply!"); + log_put ("LOG_CATEGORY", NFC_PRIORITY_ERROR, "Unexpected PN53x reply!"); return false; } @@ -2286,7 +2287,7 @@ pn53x_check_error_frame (nfc_device_t * pnd, const byte_t * pbtRxFrame, const si { if (szRxFrameLen >= sizeof (pn53x_error_frame)) { if (0 == memcmp (pbtRxFrame, pn53x_error_frame, sizeof (pn53x_error_frame))) { - DBG ("%s", "PN53x sent an error frame"); + log_put ("LOG_CATEGORY", NFC_PRIORITY_TRACE, "PN53x sent an error frame"); pnd->iLastError = EFRAISERRFRAME; return false; } @@ -2351,7 +2352,7 @@ pn53x_build_frame (byte_t * pbtFrame, size_t * pszFrame, const byte_t * pbtData, (*pszFrame) = szData + PN53x_EXTENDED_FRAME__OVERHEAD; } else { - ERR ("We can't send more than %d bytes in a raw (requested: %zd)", PN53x_EXTENDED_FRAME__DATA_MAX_LEN, szData); + 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 true; diff --git a/libnfc/drivers/acr122.c b/libnfc/drivers/acr122.c index 540a9f5..b32ce9d 100644 --- a/libnfc/drivers/acr122.c +++ b/libnfc/drivers/acr122.c @@ -72,6 +72,8 @@ #define ACR122_COMMAND_LEN 266 #define ACR122_RESPONSE_LEN 268 +#define LOG_CATEGORY "libnfc.driver.acr122" + const struct pn53x_io acr122_io; char *acr122_firmware (nfc_device_t *pnd); @@ -148,7 +150,7 @@ acr122_probe (nfc_device_desc_t pnddDevices[], size_t szDevices, size_t * pszDev // Test if context succeeded if (!(pscc = acr122_get_scardcontext ())) { - DBG ("%s", "PCSC context not found"); + log_put (LOG_CATEGORY, NFC_PRIORITY_TRACE, "%s", "PCSC context not found"); return false; } // Retrieve the string array of all available pcsc readers @@ -176,7 +178,7 @@ acr122_probe (nfc_device_desc_t pnddDevices[], size_t szDevices, size_t * pszDev pnddDevices[*pszDeviceFound].uiBusIndex = uiBusIndex; (*pszDeviceFound)++; } else { - DBG ("PCSC device [%s] is not NFC capable or not supported by libnfc.", acDeviceNames + szPos); + log_put (LOG_CATEGORY, NFC_PRIORITY_TRACE, "PCSC device [%s] is not NFC capable or not supported by libnfc.", acDeviceNames + szPos); } // Find next device name position @@ -199,7 +201,7 @@ acr122_connect (const nfc_device_desc_t * pndd) SCARDCONTEXT *pscc; - DBG ("Attempt to connect to %s", pndd->acDevice); + log_put (LOG_CATEGORY, NFC_PRIORITY_TRACE, "Attempt to connect to %s", pndd->acDevice); // Test if context succeeded if (!(pscc = acr122_get_scardcontext ())) goto error; @@ -208,7 +210,7 @@ acr122_connect (const nfc_device_desc_t * pndd) // 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) { // We can not connect to this device. - DBG ("%s", "PCSC connect failed"); + log_put (LOG_CATEGORY, NFC_PRIORITY_TRACE, "%s", "PCSC connect failed"); goto error; } } @@ -262,9 +264,7 @@ acr122_send (nfc_device_t * pnd, const byte_t * pbtData, const size_t szData) const size_t szTxBuf = szData + 6; byte_t abtTxBuf[ACR122_WRAP_LEN + ACR122_COMMAND_LEN] = { 0xFF, 0x00, 0x00, 0x00, szData + 1, 0xD4 }; memcpy (abtTxBuf + 6, pbtData, szData); -#ifdef DEBUG - PRINT_HEX ("TX", abtTxBuf, szTxBuf); -#endif + LOG_HEX ("TX", abtTxBuf, szTxBuf); DRIVER_DATA (pnd)->szRx = 0; @@ -341,9 +341,7 @@ acr122_receive (nfc_device_t * pnd, byte_t * pbtData, const size_t szData) * We already have the PN532 answer, it was saved by acr122_send(). */ } -#ifdef DEBUG - PRINT_HEX ("RX", DRIVER_DATA (pnd)->abtRx, DRIVER_DATA (pnd)->szRx); -#endif + LOG_HEX ("RX", DRIVER_DATA (pnd)->abtRx, DRIVER_DATA (pnd)->szRx); // Make sure we have an emulated answer that fits the return buffer if (DRIVER_DATA (pnd)->szRx < 4 || (DRIVER_DATA (pnd)->szRx - 4) > szData) { @@ -375,7 +373,7 @@ acr122_firmware (nfc_device_t *pnd) } if (uiResult != SCARD_S_SUCCESS) { - ERR ("No ACR122 firmware received, Error: %08x", uiResult); + log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "No ACR122 firmware received, Error: %08x", uiResult); } return abtFw; diff --git a/libnfc/drivers/arygon.c b/libnfc/drivers/arygon.c index 6503d72..dcfa574 100644 --- a/libnfc/drivers/arygon.c +++ b/libnfc/drivers/arygon.c @@ -65,6 +65,7 @@ #define ARYGON_DEFAULT_SPEED 9600 #define ARYGON_DRIVER_NAME "ARYGON" +#define LOG_CATEGORY "libnfc.driver.arygon" #define DRIVER_DATA(pnd) ((struct arygon_data*)(pnd->driver_data)) @@ -96,7 +97,7 @@ arygon_probe (nfc_device_desc_t pnddDevices[], size_t szDevices, size_t * pszDev (void) pnddDevices; (void) szDevices; *pszDeviceFound = 0; - DBG ("%s", "Serial auto-probing have been disabled at compile time. Skipping autoprobe."); + log_put (LOG_CATEGORY, NFC_PRIORITY_TRACE, "%s", "Serial auto-probing have been disabled at compile time. Skipping autoprobe."); return false; #else /* SERIAL_AUTOPROBE_ENABLED */ *pszDeviceFound = 0; @@ -108,7 +109,7 @@ arygon_probe (nfc_device_desc_t pnddDevices[], size_t szDevices, size_t * pszDev while ((acPort = acPorts[iDevice++])) { sp = uart_open (acPort); - DBG ("Trying to find ARYGON device on serial port: %s at %d bauds.", acPort, ARYGON_DEFAULT_SPEED); + log_put (LOG_CATEGORY, NFC_PRIORITY_TRACE, "Trying to find ARYGON device on serial port: %s at %d bauds.", acPort, ARYGON_DEFAULT_SPEED); 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 @@ -141,13 +142,10 @@ arygon_probe (nfc_device_desc_t pnddDevices[], size_t szDevices, size_t * pszDev if ((*pszDeviceFound) >= szDevices) break; } -# ifdef DEBUG if (sp == INVALID_SERIAL_PORT) - DBG ("Invalid serial port: %s", acPort); + log_put (LOG_CATEGORY, NFC_PRIORITY_TRACE, "Invalid serial port: %s", acPort); if (sp == CLAIMED_SERIAL_PORT) - DBG ("Serial port already claimed: %s", acPort); -# endif - /* DEBUG */ + log_put (LOG_CATEGORY, NFC_PRIORITY_TRACE, "Serial port already claimed: %s", acPort); } iDevice = 0; while ((acPort = acPorts[iDevice++])) { @@ -164,13 +162,13 @@ arygon_connect (const nfc_device_desc_t * pndd) serial_port sp; nfc_device_t *pnd = NULL; - DBG ("Attempt to connect to: %s at %d bauds.", pndd->acPort, pndd->uiSpeed); + log_put (LOG_CATEGORY, NFC_PRIORITY_TRACE, "Attempt to connect to: %s at %d bauds.", pndd->acPort, pndd->uiSpeed); sp = uart_open (pndd->acPort); if (sp == INVALID_SERIAL_PORT) - ERR ("Invalid serial port: %s", pndd->acPort); + log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "Invalid serial port: %s", pndd->acPort); if (sp == CLAIMED_SERIAL_PORT) - ERR ("Serial port already claimed: %s", pndd->acPort); + log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "Serial port already claimed: %s", pndd->acPort); if ((sp == CLAIMED_SERIAL_PORT) || (sp == INVALID_SERIAL_PORT)) return NULL; @@ -248,7 +246,7 @@ arygon_tama_send (nfc_device_t * pnd, const byte_t * pbtData, const size_t szDat size_t szFrame = 0; if (szData > PN53x_NORMAL_FRAME__DATA_MAX_LEN) { // ARYGON Reader with PN532 equipped does not support extended frame (bug in ARYGON firmware?) - DBG ("ARYGON device does not support more than %d bytes as payload (requested: %zd)", PN53x_NORMAL_FRAME__DATA_MAX_LEN, szData); + 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; return false; } @@ -260,7 +258,7 @@ arygon_tama_send (nfc_device_t * pnd, const byte_t * pbtData, const size_t szDat int res = uart_send (DRIVER_DATA (pnd)->port, abtFrame, szFrame + 1); if (res != 0) { - ERR ("%s", "Unable to transmit data. (TX)"); + log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Unable to transmit data. (TX)"); pnd->iLastError = res; return false; } @@ -268,7 +266,7 @@ arygon_tama_send (nfc_device_t * pnd, const byte_t * pbtData, const size_t szDat byte_t abtRxBuf[6]; res = uart_receive (DRIVER_DATA (pnd)->port, abtRxBuf, sizeof (abtRxBuf), 0); if (res != 0) { - ERR ("%s", "Unable to read ACK"); + log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Unable to read ACK"); pnd->iLastError = res; return false; } @@ -276,7 +274,7 @@ arygon_tama_send (nfc_device_t * pnd, const byte_t * pbtData, const size_t szDat if (pn53x_check_ack_frame (pnd, abtRxBuf, sizeof(abtRxBuf))) { // The PN53x is running the sent command } else if (0 == memcmp(arygon_error_unknown_mode, abtRxBuf, sizeof(abtRxBuf))) { - ERR( "Bad frame format." ); + 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); @@ -333,13 +331,13 @@ arygon_tama_receive (nfc_device_t * pnd, byte_t * pbtData, const size_t szDataLe } if (pnd->iLastError != 0) { - ERR ("%s", "Unable to receive data. (RX)"); + log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Unable to receive data. (RX)"); return -1; } const byte_t pn53x_preamble[3] = { 0x00, 0x00, 0xff }; if (0 != (memcmp (abtRxBuf, pn53x_preamble, 3))) { - ERR ("%s", "Frame preamble+start code mismatch"); + log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Frame preamble+start code mismatch"); pnd->iLastError = ECOMIO; return -1; } @@ -347,7 +345,7 @@ arygon_tama_receive (nfc_device_t * pnd, byte_t * pbtData, const size_t szDataLe if ((0x01 == abtRxBuf[3]) && (0xff == abtRxBuf[4])) { // Error frame uart_receive (DRIVER_DATA (pnd)->port, abtRxBuf, 3, 0); - ERR ("%s", "Application level error detected"); + log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Application level error detected"); pnd->iLastError = EFRAISERRFRAME; return -1; } else if ((0xff == abtRxBuf[3]) && (0xff == abtRxBuf[4])) { @@ -358,7 +356,7 @@ arygon_tama_receive (nfc_device_t * pnd, byte_t * pbtData, const size_t szDataLe // Normal frame if (256 != (abtRxBuf[3] + abtRxBuf[4])) { // TODO: Retry - ERR ("%s", "Length checksum mismatch"); + log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Length checksum mismatch"); pnd->iLastError = ECOMIO; return -1; } @@ -368,7 +366,7 @@ arygon_tama_receive (nfc_device_t * pnd, byte_t * pbtData, const size_t szDataLe } if (len > szDataLen) { - ERR ("Unable to receive data: buffer too small. (szDataLen: %zu, len: %zu)", szDataLen, len); + log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "Unable to receive data: buffer too small. (szDataLen: %zu, len: %zu)", szDataLen, len); pnd->iLastError = ECOMIO; return -1; } @@ -376,18 +374,18 @@ arygon_tama_receive (nfc_device_t * pnd, byte_t * pbtData, const size_t szDataLe // TFI + PD0 (CC+1) pnd->iLastError = uart_receive (DRIVER_DATA (pnd)->port, abtRxBuf, 2, 0); if (pnd->iLastError != 0) { - ERR ("%s", "Unable to receive data. (RX)"); + log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Unable to receive data. (RX)"); return -1; } if (abtRxBuf[0] != 0xD5) { - ERR ("%s", "TFI Mismatch"); + log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "TFI Mismatch"); pnd->iLastError = ECOMIO; return -1; } if (abtRxBuf[1] != CHIP_DATA (pnd)->ui8LastCommand + 1) { - ERR ("%s", "Command Code verification failed"); + log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Command Code verification failed"); pnd->iLastError = ECOMIO; return -1; } @@ -395,14 +393,14 @@ arygon_tama_receive (nfc_device_t * pnd, byte_t * pbtData, const size_t szDataLe if (len) { pnd->iLastError = uart_receive (DRIVER_DATA (pnd)->port, pbtData, len, 0); if (pnd->iLastError != 0) { - ERR ("%s", "Unable to receive data. (RX)"); + 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); if (pnd->iLastError != 0) { - ERR ("%s", "Unable to receive data. (RX)"); + log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Unable to receive data. (RX)"); return -1; } @@ -413,13 +411,13 @@ arygon_tama_receive (nfc_device_t * pnd, byte_t * pbtData, const size_t szDataLe } if (btDCS != abtRxBuf[0]) { - ERR ("%s", "Data checksum mismatch"); + log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Data checksum mismatch"); pnd->iLastError = ECOMIO; return -1; } if (0x00 != abtRxBuf[1]) { - ERR ("%s", "Frame postamble mismatch"); + log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Frame postamble mismatch"); pnd->iLastError = ECOMIO; return -1; } @@ -437,12 +435,12 @@ arygon_firmware (nfc_device_t * pnd, char * str) int res = uart_send (DRIVER_DATA (pnd)->port, arygon_firmware_version_cmd, sizeof (arygon_firmware_version_cmd)); if (res != 0) { - DBG ("Unable to send ARYGON firmware command."); + log_put (LOG_CATEGORY, NFC_PRIORITY_TRACE, "Unable to send ARYGON firmware command."); return; } res = uart_receive (DRIVER_DATA (pnd)->port, abtRx, szRx, 0); if (res != 0) { - DBG ("Unable to retrieve ARYGON firmware version."); + log_put (LOG_CATEGORY, NFC_PRIORITY_TRACE, "Unable to retrieve ARYGON firmware version."); return; } @@ -469,7 +467,7 @@ arygon_reset_tama (nfc_device_t * pnd) // 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); if (res != 0) { - DBG ("No reply to 'reset TAMA' command."); + log_put (LOG_CATEGORY, NFC_PRIORITY_TRACE, "No reply to 'reset TAMA' command."); return false; } diff --git a/libnfc/drivers/pn532_uart.c b/libnfc/drivers/pn532_uart.c index 2bc1588..e8f14af 100644 --- a/libnfc/drivers/pn532_uart.c +++ b/libnfc/drivers/pn532_uart.c @@ -45,6 +45,7 @@ #define PN532_UART_DEFAULT_SPEED 115200 #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); @@ -72,7 +73,7 @@ pn532_uart_probe (nfc_device_desc_t pnddDevices[], size_t szDevices, size_t * ps (void) pnddDevices; (void) szDevices; *pszDeviceFound = 0; - DBG ("%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; @@ -84,7 +85,7 @@ pn532_uart_probe (nfc_device_desc_t pnddDevices[], size_t szDevices, size_t * ps while ((acPort = acPorts[iDevice++])) { sp = uart_open (acPort); - DBG ("Trying to find PN532 device on serial port: %s at %d bauds.", acPort, PN532_UART_DEFAULT_SPEED); + log_put (LOG_CATEGORY, NFC_PRIORITY_TRACE, "Trying to find PN532 device on serial port: %s at %d bauds.", acPort, PN532_UART_DEFAULT_SPEED); 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 @@ -126,13 +127,6 @@ pn532_uart_probe (nfc_device_desc_t pnddDevices[], size_t szDevices, size_t * ps if ((*pszDeviceFound) >= szDevices) break; } -# ifdef DEBUG - if (sp == INVALID_SERIAL_PORT) - DBG ("Invalid serial port: %s", acPort); - if (sp == CLAIMED_SERIAL_PORT) - DBG ("Serial port already claimed: %s", acPort); -# endif - /* DEBUG */ } iDevice = 0; @@ -150,13 +144,13 @@ pn532_uart_connect (const nfc_device_desc_t * pndd) serial_port sp; nfc_device_t *pnd = NULL; - DBG ("Attempt to connect to: %s at %d bauds.", pndd->acPort, pndd->uiSpeed); + log_put (LOG_CATEGORY, NFC_PRIORITY_TRACE, "Attempt to connect to: %s at %d bauds.", pndd->acPort, pndd->uiSpeed); sp = uart_open (pndd->acPort); if (sp == INVALID_SERIAL_PORT) - ERR ("Invalid serial port: %s", pndd->acPort); + log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "Invalid serial port: %s", pndd->acPort); if (sp == CLAIMED_SERIAL_PORT) - ERR ("Serial port already claimed: %s", pndd->acPort); + log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "Serial port already claimed: %s", pndd->acPort); if ((sp == CLAIMED_SERIAL_PORT) || (sp == INVALID_SERIAL_PORT)) return NULL; @@ -266,7 +260,7 @@ pn532_uart_send (nfc_device_t * pnd, const byte_t * pbtData, const size_t szData int res = uart_send (DRIVER_DATA(pnd)->port, abtFrame, szFrame); if (res != 0) { - ERR ("%s", "Unable to transmit data. (TX)"); + log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Unable to transmit data. (TX)"); pnd->iLastError = res; return false; } @@ -274,7 +268,7 @@ pn532_uart_send (nfc_device_t * pnd, const byte_t * pbtData, const size_t szData byte_t abtRxBuf[6]; res = uart_receive (DRIVER_DATA(pnd)->port, abtRxBuf, 6, 0); if (res != 0) { - ERR ("%s", "Unable to read ACK"); + log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Unable to read ACK"); pnd->iLastError = res; return false; } @@ -318,13 +312,13 @@ pn532_uart_receive (nfc_device_t * pnd, byte_t * pbtData, const size_t szDataLen } if (pnd->iLastError != 0) { - ERR ("%s", "Unable to receive data. (RX)"); + log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Unable to receive data. (RX)"); return -1; } const byte_t pn53x_preamble[3] = { 0x00, 0x00, 0xff }; if (0 != (memcmp (abtRxBuf, pn53x_preamble, 3))) { - ERR ("%s", "Frame preamble+start code mismatch"); + log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Frame preamble+start code mismatch"); pnd->iLastError = ECOMIO; return -1; } @@ -332,7 +326,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); - ERR ("%s", "Application level error detected"); + log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Application level error detected"); pnd->iLastError = EFRAISERRFRAME; return -1; } else if ((0xff == abtRxBuf[3]) && (0xff == abtRxBuf[4])) { @@ -344,7 +338,7 @@ pn532_uart_receive (nfc_device_t * pnd, byte_t * pbtData, const size_t szDataLen len = (abtRxBuf[0] << 8) + abtRxBuf[1] - 2; if (((abtRxBuf[0] + abtRxBuf[1] + abtRxBuf[2]) % 256) != 0) { // TODO: Retry - ERR ("%s", "Length checksum mismatch"); + log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Length checksum mismatch"); pnd->iLastError = ECOMIO; return -1; } @@ -352,7 +346,7 @@ pn532_uart_receive (nfc_device_t * pnd, byte_t * pbtData, const size_t szDataLen // Normal frame if (256 != (abtRxBuf[3] + abtRxBuf[4])) { // TODO: Retry - ERR ("%s", "Length checksum mismatch"); + log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Length checksum mismatch"); pnd->iLastError = ECOMIO; return -1; } @@ -362,7 +356,7 @@ pn532_uart_receive (nfc_device_t * pnd, byte_t * pbtData, const size_t szDataLen } if (len > szDataLen) { - ERR ("Unable to receive data: buffer too small. (szDataLen: %zu, len: %zu)", szDataLen, len); + log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "Unable to receive data: buffer too small. (szDataLen: %zu, len: %zu)", szDataLen, len); pnd->iLastError = ECOMIO; return -1; } @@ -370,18 +364,18 @@ 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); if (pnd->iLastError != 0) { - ERR ("%s", "Unable to receive data. (RX)"); + log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Unable to receive data. (RX)"); return -1; } if (abtRxBuf[0] != 0xD5) { - ERR ("%s", "TFI Mismatch"); + log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "TFI Mismatch"); pnd->iLastError = ECOMIO; return -1; } if (abtRxBuf[1] != CHIP_DATA (pnd)->ui8LastCommand + 1) { - ERR ("%s", "Command Code verification failed"); + log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Command Code verification failed"); pnd->iLastError = ECOMIO; return -1; } @@ -389,14 +383,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); if (pnd->iLastError != 0) { - ERR ("%s", "Unable to receive data. (RX)"); + 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); if (pnd->iLastError != 0) { - ERR ("%s", "Unable to receive data. (RX)"); + log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Unable to receive data. (RX)"); return -1; } @@ -407,13 +401,13 @@ pn532_uart_receive (nfc_device_t * pnd, byte_t * pbtData, const size_t szDataLen } if (btDCS != abtRxBuf[0]) { - ERR ("%s", "Data checksum mismatch"); + log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Data checksum mismatch"); pnd->iLastError = ECOMIO; return -1; } if (0x00 != abtRxBuf[1]) { - ERR ("%s", "Frame postamble mismatch"); + log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Frame postamble mismatch"); pnd->iLastError = ECOMIO; return -1; } diff --git a/libnfc/drivers/pn53x_usb.c b/libnfc/drivers/pn53x_usb.c index 44695fd..f08192f 100644 --- a/libnfc/drivers/pn53x_usb.c +++ b/libnfc/drivers/pn53x_usb.c @@ -59,6 +59,7 @@ Thanks to d18c7db and Okko for example code #include "drivers/pn53x_usb.h" #define PN53X_USB_DRIVER_NAME "PN53x USB" +#define LOG_CATEGORY "libnfc.driver.pn53x_usb" #define USB_INFINITE_TIMEOUT 0 @@ -92,9 +93,9 @@ pn53x_usb_bulk_read (struct pn53x_usb_data *data, byte_t abtRx[], const size_t s { int res = usb_bulk_read (data->pudh, data->uiEndPointIn, (char *) abtRx, szRx, USB_INFINITE_TIMEOUT); if (res > 0) { - PRINT_HEX ("RX", abtRx, res); + LOG_HEX ("RX", abtRx, res); } else if (res < 0) { - ERR ("Unable to read from USB (%s", _usb_strerror (res)); + log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "Unable to read from USB (%s", _usb_strerror (res)); } return res; } @@ -104,10 +105,10 @@ pn53x_usb_bulk_read_ex (struct pn53x_usb_data *data, byte_t abtRx[], const size_ { int res = usb_bulk_read (data->pudh, data->uiEndPointIn, (char *) abtRx, szRx, timeout); if (res > 0) { - PRINT_HEX ("RX", abtRx, res); + LOG_HEX ("RX", abtRx, res); } else if (res < 0) { if (-res != USB_TIMEDOUT) { - ERR ("Unable to read from USB (%s)", _usb_strerror (res)); + log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "Unable to read from USB (%s)", _usb_strerror (res)); } } return res; @@ -116,7 +117,7 @@ pn53x_usb_bulk_read_ex (struct pn53x_usb_data *data, byte_t abtRx[], const size_ int pn53x_usb_bulk_write (struct pn53x_usb_data *data, byte_t abtTx[], const size_t szTx) { - PRINT_HEX ("TX", abtTx, szTx); + LOG_HEX ("TX", abtTx, szTx); int res = usb_bulk_write (data->pudh, data->uiEndPointOut, (char *) abtTx, szTx, USB_INFINITE_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 @@ -124,7 +125,7 @@ pn53x_usb_bulk_write (struct pn53x_usb_data *data, byte_t abtTx[], const size_t usb_bulk_write (data->pudh, data->uiEndPointOut, "\0", 0, USB_INFINITE_TIMEOUT); } } else { - ERR ("Unable to write to USB (%s)", _usb_strerror (res)); + log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "Unable to write to USB (%s)", _usb_strerror (res)); } return res; } @@ -199,14 +200,14 @@ pn53x_usb_probe (nfc_device_desc_t pnddDevices[], size_t szDevices, size_t * psz // number of changes since previous call to this function (total of new // busses and busses removed). if ((res = usb_find_busses () < 0)) { - ERR ("Unable to find USB busses (%s)", _usb_strerror (res)); + log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "Unable to find USB busses (%s)", _usb_strerror (res)); return false; } // 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)) { - ERR ("Unable to find USB devices (%s)", _usb_strerror (res)); + log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "Unable to find USB devices (%s)", _usb_strerror (res)); return false; } @@ -219,7 +220,6 @@ pn53x_usb_probe (nfc_device_desc_t pnddDevices[], size_t szDevices, size_t * psz for (dev = bus->devices; dev; dev = dev->next, uiBusIndex++) { for (size_t n = 0; n < sizeof (pn53x_usb_supported_devices) / sizeof (struct pn53x_usb_supported_device); n++) { - //DBG("Checking device %04x:%04x (%04x:%04x)",dev->descriptor.idVendor,dev->descriptor.idProduct); if ((pn53x_usb_supported_devices[n].vendor_id == dev->descriptor.idVendor) && (pn53x_usb_supported_devices[n].product_id == dev->descriptor.idProduct)) { // Make sure there are 2 endpoints available @@ -238,7 +238,7 @@ pn53x_usb_probe (nfc_device_desc_t pnddDevices[], size_t szDevices, size_t * psz // Set configuration int res = usb_set_configuration (udev, 1); if (res < 0) { - ERR ("Unable to set USB configuration (%s)", _usb_strerror (res)); + log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "Unable to set USB configuration (%s)", _usb_strerror (res)); usb_close (udev); // we failed to use the device continue; @@ -307,7 +307,7 @@ pn53x_usb_connect (const nfc_device_desc_t *pndd) for (bus = usb_get_busses (); bus; bus = bus->next) { for (dev = bus->devices; dev; dev = dev->next, uiBusIndex--) { - DBG ("Checking device %04x:%04x", dev->descriptor.idVendor, dev->descriptor.idProduct); + 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); @@ -316,9 +316,9 @@ pn53x_usb_connect (const nfc_device_desc_t *pndd) // Set configuration int res = usb_set_configuration (data.pudh, 1); if (res < 0) { - ERR ("Unable to set USB configuration (%s)", _usb_strerror (res)); + log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "Unable to set USB configuration (%s)", _usb_strerror (res)); if (EPERM == -res) { - WARN ("Please double check USB permissions for device %04x:%04x", dev->descriptor.idVendor, dev->descriptor.idProduct); + 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 @@ -327,7 +327,7 @@ pn53x_usb_connect (const nfc_device_desc_t *pndd) res = usb_claim_interface (data.pudh, 0); if (res < 0) { - ERR ("Unable to claim USB interface (%s)", _usb_strerror (res)); + 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; @@ -401,11 +401,11 @@ pn53x_usb_disconnect (nfc_device_t * pnd) int res; if ((res = usb_release_interface (DRIVER_DATA (pnd)->pudh, 0)) < 0) { - ERR ("Unable to release USB interface (%s)", _usb_strerror (res)); + log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "Unable to release USB interface (%s)", _usb_strerror (res)); } if ((res = usb_close (DRIVER_DATA (pnd)->pudh)) < 0) { - ERR ("Unable to close USB connection (%s)", _usb_strerror (res)); + log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "Unable to close USB connection (%s)", _usb_strerror (res)); } pn53x_data_free (pnd); nfc_device_free (pnd); @@ -502,7 +502,7 @@ read: const byte_t pn53x_preamble[3] = { 0x00, 0x00, 0xff }; if (0 != (memcmp (abtRxBuf, pn53x_preamble, 3))) { - ERR ("%s", "Frame preamble+start code mismatch"); + log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Frame preamble+start code mismatch"); pnd->iLastError = ECOMIO; return -1; } @@ -510,7 +510,7 @@ read: if ((0x01 == abtRxBuf[offset]) && (0xff == abtRxBuf[offset + 1])) { // Error frame - ERR ("%s", "Application level error detected"); + log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Application level error detected"); pnd->iLastError = EFRAISERRFRAME; return -1; } else if ((0xff == abtRxBuf[offset]) && (0xff == abtRxBuf[offset + 1])) { @@ -521,7 +521,7 @@ read: len = (abtRxBuf[offset] << 8) + abtRxBuf[offset + 1] - 2; if (((abtRxBuf[offset] + abtRxBuf[offset + 1] + abtRxBuf[offset + 2]) % 256) != 0) { // TODO: Retry - ERR ("%s", "Length checksum mismatch"); + log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Length checksum mismatch"); pnd->iLastError = ECOMIO; return -1; } @@ -530,7 +530,7 @@ read: // Normal frame if (256 != (abtRxBuf[offset] + abtRxBuf[offset + 1])) { // TODO: Retry - ERR ("%s", "Length checksum mismatch"); + log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Length checksum mismatch"); pnd->iLastError = ECOMIO; return -1; } @@ -541,21 +541,21 @@ read: } if (len > szDataLen) { - ERR ("Unable to receive data: buffer too small. (szDataLen: %zu, len: %zu)", szDataLen, len); + log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "Unable to receive data: buffer too small. (szDataLen: %zu, len: %zu)", szDataLen, len); pnd->iLastError = ECOMIO; return -1; } // TFI + PD0 (CC+1) if (abtRxBuf[offset] != 0xD5) { - ERR ("%s", "TFI Mismatch"); + log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "TFI Mismatch"); pnd->iLastError = ECOMIO; return -1; } offset += 1; if (abtRxBuf[offset] != CHIP_DATA (pnd)->ui8LastCommand + 1) { - ERR ("%s", "Command Code verification failed"); + log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Command Code verification failed"); pnd->iLastError = ECOMIO; return -1; } @@ -571,14 +571,14 @@ read: } if (btDCS != abtRxBuf[offset]) { - ERR ("%s", "Data checksum mismatch"); + log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Data checksum mismatch"); pnd->iLastError = ECOMIO; return -1; } offset += 1; if (0x00 != abtRxBuf[offset]) { - ERR ("%s", "Frame postamble mismatch"); + log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Frame postamble mismatch"); pnd->iLastError = ECOMIO; return -1; } @@ -603,7 +603,7 @@ pn53x_usb_init (nfc_device_t *pnd) // ...and we don't care about error pnd->iLastError = 0; if (SONY_RCS360 == DRIVER_DATA (pnd)->model) { - DBG ("SONY RC-S360 initialization."); + log_put (LOG_CATEGORY, NFC_PRIORITY_TRACE, "SONY RC-S360 initialization."); const byte_t abtCmd2[] = { 0x18, 0x01 }; pn53x_transceive (pnd, abtCmd2, sizeof (abtCmd2), NULL, NULL); pn53x_usb_ack (pnd); @@ -613,7 +613,7 @@ pn53x_usb_init (nfc_device_t *pnd) return false; if (ASK_LOGO == DRIVER_DATA (pnd)->model) { - DBG ("ASK LoGO initialization."); + log_put (LOG_CATEGORY, NFC_PRIORITY_TRACE, "ASK LoGO initialization."); /* Internal registers */ /* Disable 100mA current limit, Power on Secure IC (SVDD) */ pn53x_write_register (pnd, PN53X_REG_Control_switch_rng, 0xFF, SYMBOL_CURLIMOFF | SYMBOL_SIC_SWITCH_EN | SYMBOL_RANDOM_DATAREADY); @@ -660,7 +660,7 @@ pn53x_usb_configure (nfc_device_t * pnd, const nfc_device_option_t ndo, const bo case ASK_LOGO: if (NDO_ACTIVATE_FIELD == ndo) { /* Switch on/off LED2 and Progressive Field GPIO according to ACTIVATE_FIELD option */ - DBG ("Switch progressive field %s", bEnable ? "On" : "Off"); + 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; } diff --git a/libnfc/log.c b/libnfc/log.c new file mode 100644 index 0000000..f4e5db7 --- /dev/null +++ b/libnfc/log.c @@ -0,0 +1,79 @@ +/*- + * 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" + +sem_t *__log_sem; +const char *__sem_name = "/libnfc"; + +static uint8_t __log_init_counter = 0; + +int +log_init (void) +{ + if ((__log_sem = sem_open (__sem_name, O_CREAT, 0666, 1)) == SEM_FAILED) { + perror ("sem_open"); + return -1; + } + 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) { + sem_close (__log_sem); + sem_unlink (__sem_name); + res = log4c_fini (); + } + __log_init_counter--; + } else { + res = -1; + } + return res; +} + +void +log_put (char *category, int priority, char *format, ...) +{ + sem_wait (__log_sem); + + 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); + } + + sem_post (__log_sem); +} diff --git a/libnfc/log.h b/libnfc/log.h new file mode 100644 index 0000000..c12f2b0 --- /dev/null +++ b/libnfc/log.h @@ -0,0 +1,90 @@ +/*- + * 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 + */ + +#ifndef __LOG_H__ +#define __LOG_H__ + +#include + +extern sem_t *log_sem; + +#ifdef 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 */ + +/** + * @macro LOG_HEX + * @brief Log a byte-array in hexadecimal format + */ +# ifdef LOGGING +# define LOG_HEX(pcTag, pbtData, szBytes) do { \ + size_t __szPos; \ + char __acBuf[1024]; \ + size_t __szBuf = 0; \ + snprintf (__acBuf + __szBuf, sizeof(__acBuf) - __szBuf, "%s: ", pcTag); \ + __szBuf += strlen (pcTag) + 2; \ + for (__szPos=0; __szPos < (size_t)(szBytes); __szPos++) { \ + snprintf (__acBuf + __szBuf, sizeof(__acBuf) - __szBuf, "%02x ",((uint8_t *)(pbtData))[__szPos]); \ + __szBuf += 4; \ + } \ + log_put (LOG_CATEGORY, NFC_PRIORITY_TRACE, __acBuf); \ + } while (0); +# else +# define LOG_HEX(pcTag, pbtData, szBytes) do { \ + (void) pcTag; \ + (void) pbtData; \ + (void) szBytes; \ + } while (0); +# endif + +#endif diff --git a/libnfc/nfc-internal.h b/libnfc/nfc-internal.h index bbfaf8c..09247e4 100644 --- a/libnfc/nfc-internal.h +++ b/libnfc/nfc-internal.h @@ -29,66 +29,12 @@ # include # include -/** - * @macro PRINT_HEX - * @brief Print a byte-array in hexadecimal format (only in DEBUG mode) - */ -# ifdef DEBUG -# define PRINT_HEX(pcTag, pbtData, szBytes) do { \ - size_t __szPos; \ - fprintf(stderr, " %s: ", pcTag); \ - for (__szPos=0; __szPos < (size_t)(szBytes); __szPos++) { \ - fprintf(stderr, "%02x ",((uint8_t *)(pbtData))[__szPos]); \ - } \ - fprintf(stderr, "\n"); \ - } while (0); -# else -# define PRINT_HEX(pcTag, pbtData, szBytes) do { \ - (void) pcTag; \ - (void) pbtData; \ - (void) szBytes; \ - } while (0); -# endif +#include "log.h" /** - * @macro DBG - * @brief Print a message of standard output only in DEBUG mode + * @macro HAL + * @brief Execute corresponding driver function if exists. */ -#ifdef DEBUG -# define DBG(...) do { \ - warnx ("DBG %s:%d", __FILE__, __LINE__); \ - warnx (" " __VA_ARGS__ ); \ - } while (0) -#else -# define DBG(...) {} -#endif - -/** - * @macro WARN - * @brief Print a warn message - */ -#ifdef DEBUG -# define WARN(...) do { \ - warnx ("WARNING %s:%d", __FILE__, __LINE__); \ - warnx (" " __VA_ARGS__ ); \ - } while (0) -#else -# define WARN(...) warnx ("WARNING: " __VA_ARGS__ ) -#endif - -/** - * @macro ERR - * @brief Print a error message - */ -#ifdef DEBUG -# define ERR(...) do { \ - warnx ("ERROR %s:%d", __FILE__, __LINE__); \ - warnx (" " __VA_ARGS__ ); \ - } while (0) -#else -# define ERR(...) warnx ("ERROR: " __VA_ARGS__ ) -#endif - #define HAL( FUNCTION, ... ) pnd->iLastError = 0; \ if (pnd->driver->FUNCTION) { \ return pnd->driver->FUNCTION( __VA_ARGS__ ); \ diff --git a/libnfc/nfc.c b/libnfc/nfc.c index a517375..ef96ae3 100644 --- a/libnfc/nfc.c +++ b/libnfc/nfc.c @@ -38,11 +38,13 @@ #include -#include "drivers.h" #include "nfc-internal.h" +#include "drivers.h" #include +#define LOG_CATEGORY "libnfc.general" + const struct nfc_driver_t *nfc_drivers[] = { # if defined (DRIVER_PN53X_USB_ENABLED) &pn53x_usb_driver, @@ -92,6 +94,7 @@ nfc_connect (nfc_device_desc_t * pndd) if (pndd == NULL) return NULL; + log_init (); // Search through the device list for an available device const struct nfc_driver_t *ndr; const struct nfc_driver_t **pndr = nfc_drivers; @@ -106,13 +109,14 @@ nfc_connect (nfc_device_desc_t * pndd) // Test if the connection was successful if (pnd != NULL) { - DBG ("[%s] has been claimed.", pnd->acName); + log_put (LOG_CATEGORY, NFC_PRIORITY_TRACE, "[%s] has been claimed.", pnd->acName); return pnd; } else { - DBG ("No device found using driver: %s", ndr->name); + log_put (LOG_CATEGORY, NFC_PRIORITY_TRACE, "No device found using driver: %s", ndr->name); } pndr++; } + log_fini (); // Too bad, no reader is ready to be claimed return NULL; } @@ -131,6 +135,8 @@ nfc_disconnect (nfc_device_t * pnd) nfc_idle (pnd); // Disconnect, clean up and release the device pnd->driver->disconnect (pnd); + + log_fini (); } } @@ -144,21 +150,22 @@ void nfc_list_devices (nfc_device_desc_t pnddDevices[], size_t szDevices, size_t * pszDeviceFound) { size_t szN; - *pszDeviceFound = 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 (pnddDevices + (*pszDeviceFound), szDevices - (*pszDeviceFound), &szN)) { *pszDeviceFound += szN; - DBG ("%ld device(s) found using %s driver", (unsigned long) szN, ndr->name); + log_put (LOG_CATEGORY, NFC_PRIORITY_TRACE, "%ld device(s) found using %s driver", (unsigned long) szN, ndr->name); if (*pszDeviceFound == szDevices) break; } pndr++; } + log_fini (); } /** diff --git a/log4crc b/log4crc new file mode 100644 index 0000000..1b94d68 --- /dev/null +++ b/log4crc @@ -0,0 +1,25 @@ + + + + + + 0 + + 0 + + + + + + + + + + + + + + + + +