diff --git a/configure.ac b/configure.ac index 435b03a..1125855 100644 --- a/configure.ac +++ b/configure.ac @@ -69,16 +69,27 @@ AC_TYPE_OFF_T LIBNFC_CFLAGS='-I$(top_srcdir)/libnfc -I$(top_builddir)/include -I$(top_srcdir)/include' AC_SUBST(LIBNFC_CFLAGS) -# Debug support (default:no) -AC_ARG_ENABLE([debug],AS_HELP_STRING([--enable-debug],[Enable debug output]),[enable_debug=$enableval],[enable_debug="no"]) +# Log support (default:yes) +AC_ARG_ENABLE([log],AS_HELP_STRING([--disable-log],[Disable any logs]),[enable_log=$enableval],[enable_log="yes"]) +AC_MSG_CHECKING(for log flag) +AC_MSG_RESULT($enable_log) +AM_CONDITIONAL([WITH_LOG], [test "$enable_log" != "no"]) +if test x"$enable_log" = "xyes" +then + AC_DEFINE([LOG], [1], [Enable log]) +fi + +# Debug support (default:no) +AC_ARG_ENABLE([debug],AS_HELP_STRING([--enable-debug],[Enable debug mode]),[enable_debug=$enableval],[enable_debug="no"]) AC_MSG_CHECKING(for debug flag) AC_MSG_RESULT($enable_debug) AM_CONDITIONAL([WITH_DEBUG], [test "$enable_debug" != "no"]) if test x"$enable_debug" = "xyes" then - CFLAGS="$CFLAGS -g -DDEBUG -O0 -ggdb" + AC_DEFINE([DEBUG], [1], [Enable debug flag]) + CFLAGS="$CFLAGS -g -O0 -ggdb" fi # Handle --with-drivers option diff --git a/libnfc.conf.sample b/libnfc.conf.sample index 1d81c9c..c9ebd8f 100644 --- a/libnfc.conf.sample +++ b/libnfc.conf.sample @@ -6,3 +6,8 @@ # Allow intrusive auto-detection (default: false) # Warning: intrusive auto-detection can seriously disturb other devices #allow_intrusive_autoscan = false + +# Set log level (default: error) +# Valid log levels are (in order of verbosity): 0 (none), 1 (error), 2 (info), 3 (debug) +# Note: if you set --enable-debug option, the default log level is "debug" +#log_level = 1 diff --git a/libnfc/Makefile.am b/libnfc/Makefile.am index 6effdd9..c0b31ce 100644 --- a/libnfc/Makefile.am +++ b/libnfc/Makefile.am @@ -16,6 +16,7 @@ lib_LTLIBRARIES = libnfc.la libnfc_la_SOURCES = \ conf.c \ iso14443-subr.c \ + log.c \ mirror-subr.c \ nfc.c \ nfc-device.c \ @@ -40,7 +41,7 @@ if LIBUSB_ENABLED libnfc_la_LIBADD += @libusb_LIBS@ endif -if WITH_DEBUG +if WITH_LOG libnfc_la_SOURCES += log-printf.c endif diff --git a/libnfc/buses/uart_posix.c b/libnfc/buses/uart_posix.c index 133e428..78ca95b 100644 --- a/libnfc/buses/uart_posix.c +++ b/libnfc/buses/uart_posix.c @@ -42,6 +42,7 @@ #include "nfc-internal.h" +#define LOG_GROUP NFC_LOG_GROUP_COM #define LOG_CATEGORY "libnfc.bus.uart" # if defined(__APPLE__) @@ -127,14 +128,14 @@ uart_flush_input(serial_port sp) char *rx = malloc(available_bytes_count); // There is something available, read the data res = read(UART_DATA(sp)->fd, rx, available_bytes_count); - log_put(LOG_CATEGORY, NFC_PRIORITY_TRACE, "%d bytes have eatten.", available_bytes_count); + log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG, "%d bytes have eatten.", available_bytes_count); free(rx); } 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); + log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG, "Serial port speed requested to be set to %d bauds.", uiPortSpeed); // Portability note: on some systems, B9600 != 9600 so we have to do // uint32_t <=> speed_t associations by hand. @@ -170,7 +171,7 @@ uart_set_speed(serial_port sp, const uint32_t uiPortSpeed) break; # endif default: - 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).", + log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "Unable to set serial port speed to %d bauds. Speed value must be one of those defined in termios(3).", uiPortSpeed); return; }; @@ -179,7 +180,7 @@ uart_set_speed(serial_port sp, const uint32_t uiPortSpeed) 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."); + log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "%s", "Unable to apply new speed settings."); } } @@ -279,18 +280,18 @@ select: // Read error if (res < 0) { - log_put(LOG_CATEGORY, NFC_PRIORITY_TRACE, "Error: %s", strerror(errno)); + log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG, "Error: %s", strerror(errno)); return NFC_EIO; } // Read time-out if (res == 0) { - log_put(LOG_CATEGORY, NFC_PRIORITY_TRACE, "%s", "Timeout!"); + log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG, "%s", "Timeout!"); return NFC_ETIMEOUT; } if (FD_ISSET(iAbortFd, &rfds)) { // Abort requested - log_put(LOG_CATEGORY, NFC_PRIORITY_TRACE, "%s", "Abort!"); + log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG, "%s", "Abort!"); close(iAbortFd); return NFC_EOPABORTED; } @@ -309,7 +310,7 @@ select: received_bytes_count += res; } while (expected_bytes_count > received_bytes_count); - LOG_HEX("RX", pbtRx, szRx); + LOG_HEX(LOG_GROUP, "RX", pbtRx, szRx); return NFC_SUCCESS; } @@ -322,7 +323,7 @@ int uart_send(serial_port sp, const uint8_t *pbtTx, const size_t szTx, int timeout) { (void) timeout; - LOG_HEX("TX", pbtTx, szTx); + LOG_HEX(LOG_GROUP, "TX", pbtTx, szTx); if ((int) szTx == write(UART_DATA(sp)->fd, pbtTx, szTx)) return NFC_SUCCESS; else diff --git a/libnfc/chips/pn53x-internal.h b/libnfc/chips/pn53x-internal.h index debbc27..d0cd01e 100644 --- a/libnfc/chips/pn53x-internal.h +++ b/libnfc/chips/pn53x-internal.h @@ -2,7 +2,7 @@ * Public platform independent Near Field Communication (NFC) library * * Copyright (C) 2011 Romain Tartière - * Copyright (C) 2011 Romuald Conty + * Copyright (C) 2011, 2012 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 @@ -115,7 +115,7 @@ typedef struct { uint8_t ui8Code; uint8_t ui8CompatFlags; -#ifdef LOGGING +#ifdef LOG const char *abtCommandText; #endif } pn53x_command; @@ -128,7 +128,7 @@ typedef enum { RCS360 = 0x08 } pn53x_type; -#ifndef LOGGING +#ifndef LOG # define PNCMD( X, Y ) { X , Y } # define PNCMD_TRACE( X ) do {} while(0) #else @@ -136,7 +136,7 @@ typedef enum { # define PNCMD_TRACE( X ) do { \ for (size_t i=0; i<(sizeof(pn53x_commands)/sizeof(pn53x_command)); i++) { \ if ( X == pn53x_commands[i].ui8Code ) { \ - log_put( LOG_CATEGORY, NFC_PRIORITY_TRACE, "%s", pn53x_commands[i].abtCommandText ); \ + log_put( LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG, "%s", pn53x_commands[i].abtCommandText ); \ break; \ } \ } \ @@ -201,7 +201,7 @@ static const pn53x_command pn53x_commands[] = { #define P35 5 // Registers part -#ifdef LOGGING +#ifdef LOG typedef struct { uint16_t ui16Address; const char *abtRegisterText; @@ -210,17 +210,17 @@ typedef struct { # define PNREG( X, Y ) { X , #X, Y } -#endif /* LOGGING */ +#endif /* LOG */ -#ifndef LOGGING +#ifndef LOG # define PNREG_TRACE( X ) do { \ } while(0) #else # define PNREG_TRACE( X ) do { \ for (size_t i=0; i<(sizeof(pn53x_registers)/sizeof(pn53x_register)); i++) { \ if ( X == pn53x_registers[i].ui16Address ) { \ - log_put( LOG_CATEGORY, NFC_PRIORITY_TRACE, "%s (%s)", pn53x_registers[i].abtRegisterText, pn53x_registers[i].abtRegisterDescription ); \ + log_put( LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG, "%s (%s)", pn53x_registers[i].abtRegisterText, pn53x_registers[i].abtRegisterDescription ); \ break; \ } \ } \ @@ -329,7 +329,7 @@ typedef struct { #define EOVCURRENT 0x2d #define ENAD 0x2e -#ifdef LOGGING +#ifdef LOG 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 cb04523..eb8ce55 100644 --- a/libnfc/chips/pn53x.c +++ b/libnfc/chips/pn53x.c @@ -3,7 +3,7 @@ * * Copyright (C) 2009, 2010 Roel Verdult * Copyright (C) 2010, 2011 Romain Tartière - * Copyright (C) 2009, 2010, 2011 Romuald Conty + * Copyright (C) 2009, 2010, 2011, 2012 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 @@ -41,6 +41,7 @@ #include "mirror-subr.h" #define LOG_CATEGORY "libnfc.chip.pn53x" +#define LOG_GROUP NFC_LOG_GROUP_CHIP const uint8_t pn53x_ack_frame[] = { 0x00, 0x00, 0xff, 0x00, 0xff, 0x00 }; const uint8_t pn53x_nack_frame[] = { 0x00, 0x00, 0xff, 0xff, 0x00, 0x00 }; @@ -154,13 +155,13 @@ pn53x_transceive(struct nfc_device *pnd, const uint8_t *pbtTx, const size_t szTx PNCMD_TRACE(pbtTx[0]); if (timeout > 0) { - log_put(LOG_CATEGORY, NFC_PRIORITY_TRACE, "Timeout values: %d", timeout); + log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG, "Timeout values: %d", timeout); } else if (timeout == 0) { - log_put(LOG_CATEGORY, NFC_PRIORITY_TRACE, "%s", "No timeout"); + log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG, "%s", "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); + log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "Invalid timeout value: %d", timeout); } uint8_t abtRx[PN53x_EXTENDED_FRAME__DATA_MAX_LEN]; @@ -294,7 +295,7 @@ pn53x_transceive(struct nfc_device *pnd, const uint8_t *pbtTx, const size_t szTx 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); + log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG, "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; } @@ -1359,7 +1360,7 @@ pn53x_initiator_transceive_bytes(struct nfc_device *pnd, const uint8_t *pbtTx, c const size_t szRxLen = (size_t)res - 1; if (pbtRx != NULL) { if (szRxLen > szRx) { - log_put(LOG_CATEGORY, NFC_PRIORITY_ERROR, "Buffer size is too short: %zuo available(s), %zuo needed", szRx, szRxLen); + log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "Buffer size is too short: %zuo available(s), %zuo needed", szRx, szRxLen); return NFC_EOVFLOW; } // Copy the received bytes @@ -1634,7 +1635,7 @@ pn53x_initiator_transceive_bytes_timed(struct nfc_device *pnd, const uint8_t *pb } if (pbtRx != NULL) { if ((szRxLen + sz) > szRx) { - log_put(LOG_CATEGORY, NFC_PRIORITY_ERROR, "Buffer size is too short: %zuo available(s), %zuo needed", szRx, szRxLen + sz); + log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "Buffer size is too short: %zuo available(s), %zuo needed", szRx, szRxLen + sz); return NFC_EOVFLOW; } // Copy the received bytes @@ -2635,12 +2636,12 @@ pn53x_check_ack_frame(struct nfc_device *pnd, const uint8_t *pbtRxFrame, const s { if (szRxFrameLen >= sizeof(pn53x_ack_frame)) { if (0 == memcmp(pbtRxFrame, pn53x_ack_frame, sizeof(pn53x_ack_frame))) { - log_put(LOG_CATEGORY, NFC_PRIORITY_TRACE, "%s", "PN53x ACKed"); + log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG, "%s", "PN53x ACKed"); return NFC_SUCCESS; } } pnd->last_error = NFC_EIO; - log_put(LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Unexpected PN53x reply!"); + log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "%s", "Unexpected PN53x reply!"); return pnd->last_error; } @@ -2649,7 +2650,7 @@ pn53x_check_error_frame(struct nfc_device *pnd, const uint8_t *pbtRxFrame, const { if (szRxFrameLen >= sizeof(pn53x_error_frame)) { if (0 == memcmp(pbtRxFrame, pn53x_error_frame, sizeof(pn53x_error_frame))) { - log_put(LOG_CATEGORY, NFC_PRIORITY_TRACE, "%s", "PN53x sent an error frame"); + log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG, "%s", "PN53x sent an error frame"); pnd->last_error = NFC_EIO; return pnd->last_error; } @@ -2714,7 +2715,7 @@ pn53x_build_frame(uint8_t *pbtFrame, size_t *pszFrame, const uint8_t *pbtData, c (*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); + log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "We can't send more than %d bytes in a raw (requested: %zd)", PN53x_EXTENDED_FRAME__DATA_MAX_LEN, szData); return NFC_ECHIP; } return NFC_SUCCESS; diff --git a/libnfc/conf.c b/libnfc/conf.c index 9e27fc8..4a10aa8 100644 --- a/libnfc/conf.c +++ b/libnfc/conf.c @@ -15,6 +15,8 @@ * along with this program. If not, see */ +#include "conf.h" + #include #include #include @@ -27,13 +29,14 @@ #include "log.h" #define LOG_CATEGORY "libnfc.config" +#define LOG_GROUP NFC_LOG_GROUP_CONFIG #define LIBNFC_SYSCONFDIR "/etc/nfc" #define LIBNFC_CONFFILE LIBNFC_SYSCONFDIR"/libnfc.conf" #define LIBNFC_DEVICECONFDIR LIBNFC_SYSCONFDIR"/devices.d" - -bool conf_parse_file(const char* filename, void (*conf_keyvalue)(void* data, const char* key, const char* value), void* data) +static bool +conf_parse_file(const char* filename, void (*conf_keyvalue)(void* data, const char* key, const char* value), void* data) { FILE *f = fopen (filename, "r"); if (!f) { @@ -73,7 +76,7 @@ bool conf_parse_file(const char* filename, void (*conf_keyvalue)(void* data, con strncpy(value, line+(pmatch[value_pmatch].rm_so), value_size); value[value_size]='\0'; conf_keyvalue(data, key, value); } else { - log_put( LOG_CATEGORY, NFC_PRIORITY_TRACE, "parse error on line #%d: %s", lineno, line); + log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG, "parse error on line #%d: %s", lineno, line); } } break; @@ -82,7 +85,7 @@ bool conf_parse_file(const char* filename, void (*conf_keyvalue)(void* data, con return false; } -void +static void conf_keyvalue_context(void *data, const char* key, const char* value) { nfc_context *context = (nfc_context*)data; @@ -91,8 +94,10 @@ conf_keyvalue_context(void *data, const char* key, const char* value) string_as_boolean(value, &(context->allow_autoscan)); } else if (strcmp(key, "allow_intrusive_scan") == 0) { string_as_boolean(value, &(context->allow_intrusive_scan)); + } else if (strcmp(key, "log_level") == 0) { + context->log_level = atoi(value); } else { - log_put( LOG_CATEGORY, NFC_PRIORITY_INFO, "unknown key in config line: %s = %s", key, value); + log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_INFO, "unknown key in config line: %s = %s", key, value); } } diff --git a/libnfc/conf.h b/libnfc/conf.h index 4717768..7ba1c72 100644 --- a/libnfc/conf.h +++ b/libnfc/conf.h @@ -14,6 +14,7 @@ * You should have received a copy of the GNU Lesser General Public License * along with this program. If not, see */ +#include "nfc-internal.h" void conf_load(nfc_context *context); diff --git a/libnfc/drivers/acr122_pcsc.c b/libnfc/drivers/acr122_pcsc.c index bbd9935..f189167 100644 --- a/libnfc/drivers/acr122_pcsc.c +++ b/libnfc/drivers/acr122_pcsc.c @@ -79,6 +79,7 @@ #define ACR122_PCSC_COMMAND_LEN 266 #define ACR122_PCSC_RESPONSE_LEN 268 +#define LOG_GROUP NFC_LOG_GROUP_DRIVER #define LOG_CATEGORY "libnfc.driver.acr122" // Internal data struct @@ -142,8 +143,9 @@ acr122_pcsc_free_scardcontext(void) * @return number of devices found. */ static size_t -acr122_pcsc_scan(nfc_connstring connstrings[], const size_t connstrings_len) +acr122_pcsc_scan(const nfc_context *context, nfc_connstring connstrings[], const size_t connstrings_len) { + (void) context; size_t szPos = 0; char acDeviceNames[256 + 64 * PCSC_MAX_DEVICES]; size_t szDeviceNamesLen = sizeof(acDeviceNames); @@ -156,7 +158,7 @@ acr122_pcsc_scan(nfc_connstring connstrings[], const size_t connstrings_len) // Test if context succeeded if (!(pscc = acr122_pcsc_get_scardcontext())) { - log_put(LOG_CATEGORY, NFC_PRIORITY_WARN, "%s", "PCSC context not found (make sure PCSC daemon is running)."); + log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_INFO, "Warning: %s", "PCSC context not found (make sure PCSC daemon is running)."); return 0; } // Retrieve the string array of all available pcsc readers @@ -177,7 +179,7 @@ acr122_pcsc_scan(nfc_connstring connstrings[], const size_t connstrings_len) snprintf(connstrings[device_found], sizeof(nfc_connstring), "%s:%s", ACR122_PCSC_DRIVER_NAME, acDeviceNames + szPos); device_found++; } else { - log_put(LOG_CATEGORY, NFC_PRIORITY_TRACE, "PCSC device [%s] is not NFC capable or not supported by libnfc.", acDeviceNames + szPos); + log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG, "PCSC device [%s] is not NFC capable or not supported by libnfc.", acDeviceNames + szPos); } // Find next device name position @@ -231,7 +233,7 @@ acr122_pcsc_connstring_decode(const nfc_connstring connstring, struct acr122_pcs } static nfc_device * -acr122_pcsc_open(const nfc_connstring connstring) +acr122_pcsc_open(const nfc_context *context, const nfc_connstring connstring) { struct acr122_pcsc_descriptor ndd; int connstring_decode_level = acr122_pcsc_connstring_decode(connstring, &ndd); @@ -243,7 +245,7 @@ acr122_pcsc_open(const nfc_connstring connstring) nfc_connstring fullconnstring; if (connstring_decode_level == 1) { // Device was not specified, take the first one we can find - size_t szDeviceFound = acr122_pcsc_scan(&fullconnstring, 1); + size_t szDeviceFound = acr122_pcsc_scan(context, &fullconnstring, 1); if (szDeviceFound < 1) return NULL; connstring_decode_level = acr122_pcsc_connstring_decode(fullconnstring, &ndd); @@ -259,7 +261,7 @@ acr122_pcsc_open(const nfc_connstring connstring) if (sscanf(ndd.pcsc_device_name, "%lu", &index) != 1) return NULL; nfc_connstring *ncs = malloc(sizeof(nfc_connstring) * (index + 1)); - size_t szDeviceFound = acr122_pcsc_scan(ncs, index + 1); + size_t szDeviceFound = acr122_pcsc_scan(context, ncs, index + 1); if (szDeviceFound < index + 1) return NULL; strncpy(fullconnstring, ncs[index], sizeof(nfc_connstring)); @@ -271,7 +273,7 @@ acr122_pcsc_open(const nfc_connstring connstring) } char *pcFirmware; - nfc_device *pnd = nfc_device_new(fullconnstring); + nfc_device *pnd = nfc_device_new(context, fullconnstring); pnd->driver_data = malloc(sizeof(struct acr122_pcsc_data)); // Alloc and init chip's data @@ -279,7 +281,7 @@ acr122_pcsc_open(const nfc_connstring connstring) SCARDCONTEXT *pscc; - log_put(LOG_CATEGORY, NFC_PRIORITY_TRACE, "Attempt to open %s", ndd.pcsc_device_name); + log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG, "Attempt to open %s", ndd.pcsc_device_name); // Test if context succeeded if (!(pscc = acr122_pcsc_get_scardcontext())) goto error; @@ -288,7 +290,7 @@ acr122_pcsc_open(const nfc_connstring connstring) // Connect to ACR122 firmware version >2.0 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"); + log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG, "%s", "PCSC connect failed"); goto error; } } @@ -345,7 +347,7 @@ acr122_pcsc_send(nfc_device *pnd, const uint8_t *pbtData, const size_t szData, i const size_t szTxBuf = szData + 6; uint8_t abtTxBuf[ACR122_PCSC_WRAP_LEN + ACR122_PCSC_COMMAND_LEN] = { 0xFF, 0x00, 0x00, 0x00, szData + 1, 0xD4 }; memcpy(abtTxBuf + 6, pbtData, szData); - LOG_HEX("TX", abtTxBuf, szTxBuf); + LOG_HEX(NFC_LOG_GROUP_COM, "TX", abtTxBuf, szTxBuf); DRIVER_DATA(pnd)->szRx = 0; @@ -425,7 +427,7 @@ acr122_pcsc_receive(nfc_device *pnd, uint8_t *pbtData, const size_t szData, int * We already have the PN532 answer, it was saved by acr122_pcsc_send(). */ } - LOG_HEX("RX", DRIVER_DATA(pnd)->abtRx, DRIVER_DATA(pnd)->szRx); + LOG_HEX(NFC_LOG_GROUP_COM, "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) { @@ -455,7 +457,7 @@ acr122_pcsc_firmware(nfc_device *pnd) } if (uiResult != SCARD_S_SUCCESS) { - log_put(LOG_CATEGORY, NFC_PRIORITY_ERROR, "No ACR122 firmware received, Error: %08x", uiResult); + log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "No ACR122 firmware received, Error: %08x", uiResult); } return abtFw; diff --git a/libnfc/drivers/acr122_usb.c b/libnfc/drivers/acr122_usb.c index b8a9fc1..300db3c 100644 --- a/libnfc/drivers/acr122_usb.c +++ b/libnfc/drivers/acr122_usb.c @@ -77,6 +77,8 @@ Thanks to d18c7db and Okko for example code #include "drivers/acr122_usb.h" #define ACR122_USB_DRIVER_NAME "acr122_usb" + +#define LOG_GROUP NFC_LOG_GROUP_DRIVER #define LOG_CATEGORY "libnfc.driver.acr122_usb" #define USB_INFINITE_TIMEOUT 0 @@ -216,11 +218,11 @@ acr122_usb_bulk_read(struct acr122_usb_data *data, uint8_t abtRx[], const size_t { int res = usb_bulk_read(data->pudh, data->uiEndPointIn, (char *) abtRx, szRx, timeout); if (res > 0) { - LOG_HEX("RX", abtRx, res); + LOG_HEX(NFC_LOG_GROUP_COM, "RX", abtRx, res); } else if (res < 0) { if (res != -USB_TIMEDOUT) { res = NFC_EIO; - log_put(LOG_CATEGORY, NFC_PRIORITY_ERROR, "Unable to read from USB (%s)", _usb_strerror(res)); + log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "Unable to read from USB (%s)", _usb_strerror(res)); } else { res = NFC_ETIMEOUT; } @@ -231,7 +233,7 @@ acr122_usb_bulk_read(struct acr122_usb_data *data, uint8_t abtRx[], const size_t static int acr122_usb_bulk_write(struct acr122_usb_data *data, uint8_t abtTx[], const size_t szTx, const int timeout) { - LOG_HEX("TX", abtTx, szTx); + LOG_HEX(NFC_LOG_GROUP_COM, "TX", abtTx, szTx); 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 @@ -239,7 +241,7 @@ acr122_usb_bulk_write(struct acr122_usb_data *data, uint8_t abtTx[], const size_ usb_bulk_write(data->pudh, data->uiEndPointOut, "\0", 0, timeout); } } else if (res < 0) { - log_put(LOG_CATEGORY, NFC_PRIORITY_ERROR, "Unable to write to USB (%s)", _usb_strerror(res)); + log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "Unable to write to USB (%s)", _usb_strerror(res)); if (res == -USB_TIMEDOUT) { res = NFC_ETIMEOUT; } else { @@ -304,8 +306,9 @@ acr122_usb_get_end_points(struct usb_device *dev, struct acr122_usb_data *data) } static size_t -acr122_usb_scan(nfc_connstring connstrings[], const size_t connstrings_len) +acr122_usb_scan(const nfc_context *context, nfc_connstring connstrings[], const size_t connstrings_len) { + (void)context; usb_init(); int res; @@ -313,14 +316,14 @@ acr122_usb_scan(nfc_connstring connstrings[], const size_t connstrings_len) // 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)); + log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "Unable to find USB busses (%s)", _usb_strerror(res)); return 0; } // 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)); + log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "Unable to find USB devices (%s)", _usb_strerror(res)); return 0; } @@ -349,7 +352,7 @@ acr122_usb_scan(nfc_connstring connstrings[], const size_t connstrings_len) // Set configuration // acr122_usb_get_usb_device_name (dev, udev, pnddDevices[device_found].acDevice, sizeof (pnddDevices[device_found].acDevice)); - log_put(LOG_CATEGORY, NFC_PRIORITY_TRACE, "device found: Bus %s Device %s Name %s", bus->dirname, dev->filename, acr122_usb_supported_devices[n].name); + log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG, "device found: Bus %s Device %s Name %s", bus->dirname, dev->filename, acr122_usb_supported_devices[n].name); usb_close(udev); snprintf(connstrings[device_found], sizeof(nfc_connstring), "%s:%s:%s", ACR122_USB_DRIVER_NAME, bus->dirname, dev->filename); device_found++; @@ -425,12 +428,12 @@ acr122_usb_get_usb_device_name(struct usb_device *dev, usb_dev_handle *udev, cha } static nfc_device * -acr122_usb_open(const nfc_connstring connstring) +acr122_usb_open(const nfc_context *context, const nfc_connstring connstring) { nfc_device *pnd = NULL; struct acr122_usb_descriptor desc = { NULL, NULL }; int connstring_decode_level = acr122_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_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG, "%d element(s) have been decoded from \"%s\"", connstring_decode_level, connstring); if (connstring_decode_level < 1) { goto free_mem; } @@ -450,14 +453,14 @@ acr122_usb_open(const nfc_connstring connstring) // 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)); + log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_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)); + log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "Unable to find USB devices (%s)", _usb_strerror(res)); goto free_mem; } @@ -482,7 +485,7 @@ acr122_usb_open(const nfc_connstring connstring) // Claim interface 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)); + log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "Unable to claim USB interface (%s)", _usb_strerror(res)); usb_close(data.pudh); // we failed to use the specified device goto free_mem; @@ -490,7 +493,7 @@ acr122_usb_open(const nfc_connstring connstring) res = usb_set_altinterface(data.pudh, 0); if (res < 0) { - log_put(LOG_CATEGORY, NFC_PRIORITY_ERROR, "Unable to set alternate setting on USB interface (%s)", _usb_strerror(res)); + log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "Unable to set alternate setting on USB interface (%s)", _usb_strerror(res)); usb_close(data.pudh); // we failed to use the specified device goto free_mem; @@ -498,7 +501,7 @@ acr122_usb_open(const nfc_connstring connstring) data.model = acr122_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(connstring); + pnd = nfc_device_new(context, connstring); acr122_usb_get_usb_device_name(dev, data.pudh, pnd->name, sizeof(pnd->name)); pnd->driver_data = malloc(sizeof(struct acr122_usb_data)); @@ -553,11 +556,11 @@ acr122_usb_close(nfc_device *pnd) int res; if ((res = usb_release_interface(DRIVER_DATA(pnd)->pudh, 0)) < 0) { - log_put(LOG_CATEGORY, NFC_PRIORITY_ERROR, "Unable to release USB interface (%s)", _usb_strerror(res)); + log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "Unable to release USB interface (%s)", _usb_strerror(res)); } if ((res = usb_close(DRIVER_DATA(pnd)->pudh)) < 0) { - log_put(LOG_CATEGORY, NFC_PRIORITY_ERROR, "Unable to close USB connection (%s)", _usb_strerror(res)); + log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "Unable to close USB connection (%s)", _usb_strerror(res)); } pn53x_data_free(pnd); nfc_device_free(pnd); @@ -668,7 +671,7 @@ read: } } if (abtRxBuf[offset] != attempted_response) { - log_put(LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Frame header mismatch"); + log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "%s", "Frame header mismatch"); pnd->last_error = NFC_EIO; return pnd->last_error; } @@ -676,7 +679,7 @@ read: len = abtRxBuf[offset++]; if (len != 2) { - log_put(LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Wrong reply"); + log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "%s", "Wrong reply"); pnd->last_error = NFC_EIO; return pnd->last_error; } @@ -707,7 +710,7 @@ read: } if (abtRxBuf[offset] != attempted_response) { - log_put(LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Frame header mismatch"); + log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "%s", "Frame header mismatch"); pnd->last_error = NFC_EIO; return pnd->last_error; } @@ -716,21 +719,21 @@ read: // XXX In CCID specification, len is a 32-bits (dword), do we need to decode more than 1 byte ? (0-255 bytes for PN532 reply) len = abtRxBuf[offset++]; if ((abtRxBuf[offset] != 0x00) && (abtRxBuf[offset+1] != 0x00) && (abtRxBuf[offset+2] != 0x00)) { - log_put(LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Not implemented: only 1-byte length is supported, please report this bug with a full trace."); + 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; return pnd->last_error; } offset += 3; if (len < 4) { - log_put(LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Too small reply"); + log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "%s", "Too small reply"); pnd->last_error = NFC_EIO; return pnd->last_error; } len -= 4; // We skip 2 bytes for PN532 direction byte (D5) and command byte (CMD+1), then 2 bytes for APDU status (90 00). if (len > szDataLen) { - log_put(LOG_CATEGORY, NFC_PRIORITY_ERROR, "Unable to receive data: buffer too small. (szDataLen: %zu, len: %zu)", szDataLen, len); + log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "Unable to receive data: buffer too small. (szDataLen: %zu, len: %zu)", szDataLen, len); pnd->last_error = NFC_EOVFLOW; return pnd->last_error; } @@ -742,14 +745,14 @@ read: // TFI + PD0 (CC+1) if (abtRxBuf[offset] != 0xD5) { - log_put(LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "TFI Mismatch"); + log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "%s", "TFI Mismatch"); pnd->last_error = NFC_EIO; return pnd->last_error; } offset += 1; if (abtRxBuf[offset] != CHIP_DATA(pnd)->last_command + 1) { - log_put(LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Command Code verification failed"); + log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "%s", "Command Code verification failed"); pnd->last_error = NFC_EIO; return pnd->last_error; } @@ -767,7 +770,7 @@ acr122_usb_ack(nfc_device *pnd) (void) pnd; int res = 0; uint8_t acr122_ack_frame[] = { GetFirmwareVersion }; // We can't send a PN532's ACK frame, so we use a normal command to cancel current command - log_put(LOG_CATEGORY, NFC_PRIORITY_DEBUG, "%s", "ACR122 Abort"); + log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG, "%s", "ACR122 Abort"); if ((res = acr122_build_frame_from_tama(pnd, acr122_ack_frame, sizeof(acr122_ack_frame))) < 0) return res; @@ -812,7 +815,7 @@ acr122_usb_init(nfc_device *pnd) 0x00, 0x00, 0x00, 0x00, // Blinking duration control }; - log_put (LOG_CATEGORY, NFC_PRIORITY_DEBUG, "%s", "ACR122 Get LED state"); + log_put (LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG, "%s", "ACR122 Get LED state"); if ((res = acr122_usb_bulk_write (DRIVER_DATA (pnd), (uint8_t *) acr122u_get_led_state_frame, sizeof (acr122u_get_led_state_frame), 1000)) < 0) return res; @@ -837,7 +840,7 @@ acr122_usb_init(nfc_device *pnd) if ((res = acr122_usb_bulk_read(DRIVER_DATA(pnd), abtRxBuf, sizeof(abtRxBuf), 1000)) < 0) return res; - log_put(LOG_CATEGORY, NFC_PRIORITY_DEBUG, "%s", "ACR122 PICC Operating Parameters"); + log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG, "%s", "ACR122 PICC Operating Parameters"); if ((res = acr122_usb_send_apdu(pnd, 0x00, 0x51, 0x00, NULL, 0, 0, abtRxBuf, sizeof(abtRxBuf))) < 0) return res; diff --git a/libnfc/drivers/acr122s.c b/libnfc/drivers/acr122s.c index a111608..a2f4fa3 100644 --- a/libnfc/drivers/acr122s.c +++ b/libnfc/drivers/acr122s.c @@ -45,7 +45,9 @@ #define ACR122S_DEFAULT_SPEED 9600 #define ACR122S_DRIVER_NAME "ACR122S" + #define LOG_CATEGORY "libnfc.driver.acr122s" +#define LOG_GROUP NFC_LOG_GROUP_DRIVER // Internal data structs struct acr122s_data { @@ -252,7 +254,7 @@ acr122s_recv_frame(nfc_device *pnd, uint8_t *frame, size_t frame_size, void *abo struct xfr_block_res *res = (struct xfr_block_res *) &frame[1]; if ((uint8_t)(res->seq + 1) != DRIVER_DATA(pnd)->seq) { - log_put(LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Invalid response sequence number."); + log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "%s", "Invalid response sequence number."); pnd->last_error = NFC_EIO; return pnd->last_error; } @@ -448,7 +450,7 @@ acr122s_connstring_decode(const nfc_connstring connstring, struct acr122s_descri } static size_t -acr122s_scan(nfc_connstring connstrings[], const size_t connstrings_len) +acr122s_scan(const nfc_context *context, nfc_connstring connstrings[], const size_t connstrings_len) { size_t device_found = 0; serial_port sp; @@ -458,7 +460,7 @@ acr122s_scan(nfc_connstring connstrings[], const size_t connstrings_len) while ((acPort = acPorts[iDevice++])) { sp = uart_open(acPort); - log_put(LOG_CATEGORY, NFC_PRIORITY_TRACE, "Trying to find ACR122S device on serial port: %s at %d bauds.", acPort, ACR122S_DEFAULT_SPEED); + log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG, "Trying to find ACR122S device on serial port: %s at %d bauds.", acPort, ACR122S_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 @@ -467,7 +469,7 @@ acr122s_scan(nfc_connstring connstrings[], const size_t connstrings_len) nfc_connstring connstring; snprintf(connstring, sizeof(nfc_connstring), "%s:%s:%"PRIu32, ACR122S_DRIVER_NAME, acPort, ACR122S_DEFAULT_SPEED); - nfc_device *pnd = nfc_device_new(connstring); + nfc_device *pnd = nfc_device_new(context, connstring); pnd->driver = &acr122s_driver; pnd->driver_data = malloc(sizeof(struct acr122s_data)); @@ -531,7 +533,7 @@ acr122s_close(nfc_device *pnd) } static nfc_device * -acr122s_open(const nfc_connstring connstring) +acr122s_open(const nfc_context *context, const nfc_connstring connstring) { serial_port sp; nfc_device *pnd; @@ -545,17 +547,17 @@ acr122s_open(const nfc_connstring connstring) ndd.speed = ACR122S_DEFAULT_SPEED; } - log_put(LOG_CATEGORY, NFC_PRIORITY_TRACE, + log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG, "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, + log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "Invalid serial port: %s", ndd.port); return NULL; } if (sp == CLAIMED_SERIAL_PORT) { - log_put(LOG_CATEGORY, NFC_PRIORITY_ERROR, + log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "Serial port already claimed: %s", ndd.port); return NULL; } @@ -563,7 +565,7 @@ acr122s_open(const nfc_connstring connstring) uart_flush_input(sp); uart_set_speed(sp, ndd.speed); - pnd = nfc_device_new(connstring); + pnd = nfc_device_new(context, connstring); pnd->driver = &acr122s_driver; strcpy(pnd->name, ACR122S_DRIVER_NAME); @@ -586,13 +588,13 @@ acr122s_open(const nfc_connstring connstring) // Retrieve firmware version char version[DEVICE_NAME_LENGTH]; if (acr122s_get_firmware_version(pnd, version, sizeof(version)) != 0) { - log_put(LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Cannot get reader firmware."); + log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "%s", "Cannot get reader firmware."); acr122s_close(pnd); return NULL; } if (strncmp(version, "ACR122S", 7) != 0) { - log_put(LOG_CATEGORY, NFC_PRIORITY_ERROR, "Invalid firmware version: %s", + log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "Invalid firmware version: %s", version); acr122s_close(pnd); return NULL; @@ -602,14 +604,14 @@ acr122s_open(const nfc_connstring connstring) // Activate SAM before operating if (acr122s_activate_sam(pnd) != 0) { - log_put(LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Cannot activate SAM."); + log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "%s", "Cannot activate SAM."); acr122s_close(pnd); return NULL; } #endif if (pn53x_init(pnd) < 0) { - log_put(LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Failed initializing PN532 chip."); + log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "%s", "Failed initializing PN532 chip."); acr122s_close(pnd); return NULL; } @@ -626,7 +628,7 @@ acr122s_send(nfc_device *pnd, const uint8_t *buf, const size_t buf_len, int time acr122s_build_frame(pnd, cmd, sizeof(cmd), 0, 0, buf, buf_len, 1); int ret; if ((ret = acr122s_send_frame(pnd, cmd, timeout)) != 0) { - log_put(LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Unable to transmit data. (TX)"); + log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "%s", "Unable to transmit data. (TX)"); pnd->last_error = ret; return pnd->last_error; } @@ -654,13 +656,13 @@ acr122s_receive(nfc_device *pnd, uint8_t *buf, size_t buf_len, int timeout) } if (pnd->last_error < 0) { - log_put(LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Unable to receive data. (RX)"); + log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "%s", "Unable to receive data. (RX)"); return -1; } size_t data_len = FRAME_SIZE(tmp) - 17; if (data_len > buf_len) { - log_put(LOG_CATEGORY, NFC_PRIORITY_ERROR, "Receive buffer too small. (buf_len: %zu, data_len: %zu)", buf_len, data_len); + log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "Receive buffer too small. (buf_len: %zu, data_len: %zu)", buf_len, data_len); pnd->last_error = NFC_EIO; return pnd->last_error; } diff --git a/libnfc/drivers/arygon.c b/libnfc/drivers/arygon.c index eb92873..94fcda0 100644 --- a/libnfc/drivers/arygon.c +++ b/libnfc/drivers/arygon.c @@ -66,7 +66,9 @@ #define ARYGON_DEFAULT_SPEED 9600 #define ARYGON_DRIVER_NAME "arygon" + #define LOG_CATEGORY "libnfc.driver.arygon" +#define LOG_GROUP NFC_LOG_GROUP_DRIVER #define DRIVER_DATA(pnd) ((struct arygon_data*)(pnd->driver_data)) @@ -92,7 +94,7 @@ int arygon_reset_tama(nfc_device *pnd); void arygon_firmware(nfc_device *pnd, char *str); static size_t -arygon_scan(nfc_connstring connstrings[], const size_t connstrings_len) +arygon_scan(const nfc_context *context, nfc_connstring connstrings[], const size_t connstrings_len) { size_t device_found = 0; serial_port sp; @@ -102,7 +104,7 @@ arygon_scan(nfc_connstring connstrings[], const size_t connstrings_len) while ((acPort = acPorts[iDevice++])) { sp = uart_open(acPort); - log_put(LOG_CATEGORY, NFC_PRIORITY_TRACE, "Trying to find ARYGON device on serial port: %s at %d bauds.", acPort, ARYGON_DEFAULT_SPEED); + log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG, "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 @@ -111,7 +113,7 @@ arygon_scan(nfc_connstring connstrings[], const size_t connstrings_len) 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); + nfc_device *pnd = nfc_device_new(context, connstring); pnd->driver = &arygon_driver; pnd->driver_data = malloc(sizeof(struct arygon_data)); @@ -223,7 +225,7 @@ arygon_close(nfc_device *pnd) } static nfc_device * -arygon_open(const nfc_connstring connstring) +arygon_open(const nfc_context *context, const nfc_connstring connstring) { struct arygon_descriptor ndd; int connstring_decode_level = arygon_connstring_decode(connstring, &ndd); @@ -237,13 +239,13 @@ arygon_open(const nfc_connstring connstring) serial_port sp; nfc_device *pnd = NULL; - log_put(LOG_CATEGORY, NFC_PRIORITY_TRACE, "Attempt to open: %s at %d bauds.", ndd.port, ndd.speed); + log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG, "Attempt to open: %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", ndd.port); + log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_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", ndd.port); + log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "Serial port already claimed: %s", ndd.port); if ((sp == CLAIMED_SERIAL_PORT) || (sp == INVALID_SERIAL_PORT)) return NULL; @@ -252,7 +254,7 @@ arygon_open(const nfc_connstring connstring) uart_set_speed(sp, ndd.speed); // We have a connection - pnd = nfc_device_new(connstring); + pnd = nfc_device_new(context, connstring); snprintf(pnd->name, sizeof(pnd->name), "%s:%s", ARYGON_DRIVER_NAME, ndd.port); pnd->driver_data = malloc(sizeof(struct arygon_data)); @@ -308,7 +310,7 @@ arygon_tama_send(nfc_device *pnd, const uint8_t *pbtData, const size_t szData, i 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?) - 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); + log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG, "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 pnd->last_error; } @@ -319,14 +321,14 @@ arygon_tama_send(nfc_device *pnd, const uint8_t *pbtData, const size_t szData, i } 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)"); + log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "%s", "Unable to transmit data. (TX)"); pnd->last_error = res; return pnd->last_error; } uint8_t abtRxBuf[6]; 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"); + log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "%s", "Unable to read ACK"); pnd->last_error = res; return pnd->last_error; } @@ -334,7 +336,7 @@ arygon_tama_send(nfc_device *pnd, const uint8_t *pbtData, const size_t szData, i 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, "%s", "Bad frame format."); + log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "%s", "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. pnd->last_error = uart_receive(DRIVER_DATA(pnd)->port, abtRxBuf, 4, 0, timeout); @@ -381,13 +383,13 @@ arygon_tama_receive(nfc_device *pnd, uint8_t *pbtData, const size_t szDataLen, i } if (pnd->last_error != 0) { - log_put(LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Unable to receive data. (RX)"); + log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "%s", "Unable to receive data. (RX)"); 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"); + log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "%s", "Frame preamble+start code mismatch"); pnd->last_error = NFC_EIO; return pnd->last_error; } @@ -395,7 +397,7 @@ arygon_tama_receive(nfc_device *pnd, uint8_t *pbtData, const size_t szDataLen, i 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"); + log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "%s", "Application level error detected"); pnd->last_error = NFC_EIO; return pnd->last_error; } else if ((0xff == abtRxBuf[3]) && (0xff == abtRxBuf[4])) { @@ -406,7 +408,7 @@ arygon_tama_receive(nfc_device *pnd, uint8_t *pbtData, const size_t szDataLen, i // Normal frame if (256 != (abtRxBuf[3] + abtRxBuf[4])) { // TODO: Retry - log_put(LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Length checksum mismatch"); + log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "%s", "Length checksum mismatch"); pnd->last_error = NFC_EIO; return pnd->last_error; } @@ -416,7 +418,7 @@ arygon_tama_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); + log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "Unable to receive data: buffer too small. (szDataLen: %zu, len: %zu)", szDataLen, len); pnd->last_error = NFC_EIO; return pnd->last_error; } @@ -424,18 +426,18 @@ arygon_tama_receive(nfc_device *pnd, uint8_t *pbtData, const size_t szDataLen, i // 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)"); + log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "%s", "Unable to receive data. (RX)"); return pnd->last_error; } if (abtRxBuf[0] != 0xD5) { - log_put(LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "TFI Mismatch"); + log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "%s", "TFI Mismatch"); pnd->last_error = NFC_EIO; return pnd->last_error; } if (abtRxBuf[1] != CHIP_DATA(pnd)->last_command + 1) { - log_put(LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Command Code verification failed"); + log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "%s", "Command Code verification failed"); pnd->last_error = NFC_EIO; return pnd->last_error; } @@ -443,14 +445,14 @@ arygon_tama_receive(nfc_device *pnd, uint8_t *pbtData, const size_t szDataLen, i 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)"); + log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "%s", "Unable to receive data. (RX)"); 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)"); + log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "%s", "Unable to receive data. (RX)"); return pnd->last_error; } @@ -461,13 +463,13 @@ arygon_tama_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"); + log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "%s", "Data checksum mismatch"); pnd->last_error = NFC_EIO; return pnd->last_error; } if (0x00 != abtRxBuf[1]) { - log_put(LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Frame postamble mismatch"); + log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "%s", "Frame postamble mismatch"); pnd->last_error = NFC_EIO; return pnd->last_error; } @@ -485,12 +487,12 @@ arygon_firmware(nfc_device *pnd, char *str) 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, "%s", "Unable to send ARYGON firmware command."); + log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG, "%s", "Unable to send ARYGON firmware command."); return; } res = uart_receive(DRIVER_DATA(pnd)->port, abtRx, szRx, 0, 0); if (res != 0) { - log_put(LOG_CATEGORY, NFC_PRIORITY_TRACE, "%s", "Unable to retrieve ARYGON firmware version."); + log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG, "%s", "Unable to retrieve ARYGON firmware version."); return; } @@ -517,7 +519,7 @@ arygon_reset_tama(nfc_device *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, 1000); if (res != 0) { - log_put(LOG_CATEGORY, NFC_PRIORITY_TRACE, "%s", "No reply to 'reset TAMA' command."); + log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG, "%s", "No reply to 'reset TAMA' command."); pnd->last_error = res; return pnd->last_error; } diff --git a/libnfc/drivers/pn532_uart.c b/libnfc/drivers/pn532_uart.c index dc20f83..05f30b8 100644 --- a/libnfc/drivers/pn532_uart.c +++ b/libnfc/drivers/pn532_uart.c @@ -45,7 +45,9 @@ #define PN532_UART_DEFAULT_SPEED 115200 #define PN532_UART_DRIVER_NAME "pn532_uart" + #define LOG_CATEGORY "libnfc.driver.pn532_uart" +#define LOG_GROUP NFC_LOG_GROUP_DRIVER // Internal data structs const struct pn53x_io pn532_uart_io; @@ -65,7 +67,7 @@ int pn532_uart_wakeup(nfc_device *pnd); #define DRIVER_DATA(pnd) ((struct pn532_uart_data*)(pnd->driver_data)) static size_t -pn532_uart_scan(nfc_connstring connstrings[], const size_t connstrings_len) +pn532_uart_scan(const nfc_context *context, nfc_connstring connstrings[], const size_t connstrings_len) { size_t device_found = 0; serial_port sp; @@ -75,7 +77,7 @@ pn532_uart_scan(nfc_connstring connstrings[], const size_t connstrings_len) while ((acPort = acPorts[iDevice++])) { sp = uart_open(acPort); - log_put(LOG_CATEGORY, NFC_PRIORITY_TRACE, "Trying to find PN532 device on serial port: %s at %d bauds.", acPort, PN532_UART_DEFAULT_SPEED); + log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG, "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 @@ -85,7 +87,7 @@ pn532_uart_scan(nfc_connstring connstrings[], const size_t connstrings_len) 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); + nfc_device *pnd = nfc_device_new(context, connstring); pnd->driver = &pn532_uart_driver; pnd->driver_data = malloc(sizeof(struct pn532_uart_data)); DRIVER_DATA(pnd)->port = sp; @@ -200,7 +202,7 @@ pn532_uart_close(nfc_device *pnd) } static nfc_device * -pn532_uart_open(const nfc_connstring connstring) +pn532_uart_open(const nfc_context *context, const nfc_connstring connstring) { struct pn532_uart_descriptor ndd; int connstring_decode_level = pn532_connstring_decode(connstring, &ndd); @@ -214,13 +216,13 @@ pn532_uart_open(const nfc_connstring connstring) serial_port sp; nfc_device *pnd = NULL; - log_put(LOG_CATEGORY, NFC_PRIORITY_TRACE, "Attempt to open: %s at %d bauds.", ndd.port, ndd.speed); + log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG, "Attempt to open: %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", ndd.port); + log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_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", ndd.port); + log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "Serial port already claimed: %s", ndd.port); if ((sp == CLAIMED_SERIAL_PORT) || (sp == INVALID_SERIAL_PORT)) return NULL; @@ -229,7 +231,7 @@ pn532_uart_open(const nfc_connstring connstring) uart_set_speed(sp, ndd.speed); // We have a connection - pnd = nfc_device_new(connstring); + pnd = nfc_device_new(context, connstring); snprintf(pnd->name, sizeof(pnd->name), "%s:%s", PN532_UART_DRIVER_NAME, ndd.port); pnd->driver_data = malloc(sizeof(struct pn532_uart_data)); @@ -315,7 +317,7 @@ pn532_uart_send(nfc_device *pnd, const uint8_t *pbtData, const size_t szData, in 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)"); + log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "%s", "Unable to transmit data. (TX)"); pnd->last_error = res; return pnd->last_error; } @@ -323,7 +325,7 @@ pn532_uart_send(nfc_device *pnd, const uint8_t *pbtData, const size_t szData, in 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"); + log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "%s", "Unable to read ACK"); pnd->last_error = res; return pnd->last_error; } @@ -361,7 +363,7 @@ pn532_uart_receive(nfc_device *pnd, uint8_t *pbtData, const size_t szDataLen, in 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"); + log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "%s", "Frame preamble+start code mismatch"); pnd->last_error = NFC_EIO; goto error; } @@ -369,20 +371,20 @@ pn532_uart_receive(nfc_device *pnd, uint8_t *pbtData, const size_t szDataLen, in 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"); + log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "%s", "Application level error detected"); pnd->last_error = NFC_EIO; goto error; } 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 != 0) { - log_put(LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Unable to receive data. (RX)"); + log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "%s", "Unable to receive data. (RX)"); goto error; } // (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) { - log_put(LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Length checksum mismatch"); + log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "%s", "Length checksum mismatch"); pnd->last_error = NFC_EIO; goto error; } @@ -390,7 +392,7 @@ pn532_uart_receive(nfc_device *pnd, uint8_t *pbtData, const size_t szDataLen, in // Normal frame if (256 != (abtRxBuf[3] + abtRxBuf[4])) { // TODO: Retry - log_put(LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Length checksum mismatch"); + log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "%s", "Length checksum mismatch"); pnd->last_error = NFC_EIO; goto error; } @@ -400,7 +402,7 @@ pn532_uart_receive(nfc_device *pnd, uint8_t *pbtData, const size_t szDataLen, in } if (len > szDataLen) { - log_put(LOG_CATEGORY, NFC_PRIORITY_ERROR, "Unable to receive data: buffer too small. (szDataLen: %zu, len: %zu)", szDataLen, len); + log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "Unable to receive data: buffer too small. (szDataLen: %zu, len: %zu)", szDataLen, len); pnd->last_error = NFC_EIO; goto error; } @@ -408,18 +410,18 @@ pn532_uart_receive(nfc_device *pnd, uint8_t *pbtData, const size_t szDataLen, in // 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)"); + log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "%s", "Unable to receive data. (RX)"); goto error; } if (abtRxBuf[0] != 0xD5) { - log_put(LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "TFI Mismatch"); + log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "%s", "TFI Mismatch"); pnd->last_error = NFC_EIO; goto error; } if (abtRxBuf[1] != CHIP_DATA(pnd)->last_command + 1) { - log_put(LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Command Code verification failed"); + log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "%s", "Command Code verification failed"); pnd->last_error = NFC_EIO; goto error; } @@ -427,14 +429,14 @@ pn532_uart_receive(nfc_device *pnd, uint8_t *pbtData, const size_t szDataLen, in 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)"); + log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "%s", "Unable to receive data. (RX)"); goto 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)"); + log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "%s", "Unable to receive data. (RX)"); goto error; } @@ -445,13 +447,13 @@ pn532_uart_receive(nfc_device *pnd, uint8_t *pbtData, const size_t szDataLen, in } if (btDCS != abtRxBuf[0]) { - log_put(LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Data checksum mismatch"); + log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "%s", "Data checksum mismatch"); pnd->last_error = NFC_EIO; goto error; } if (0x00 != abtRxBuf[1]) { - log_put(LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Frame postamble mismatch"); + log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "%s", "Frame postamble mismatch"); pnd->last_error = NFC_EIO; goto error; } diff --git a/libnfc/drivers/pn53x_usb.c b/libnfc/drivers/pn53x_usb.c index 23db81f..6098427 100644 --- a/libnfc/drivers/pn53x_usb.c +++ b/libnfc/drivers/pn53x_usb.c @@ -61,6 +61,7 @@ Thanks to d18c7db and Okko for example code #define PN53X_USB_DRIVER_NAME "pn53x_usb" #define LOG_CATEGORY "libnfc.driver.pn53x_usb" +#define LOG_GROUP NFC_LOG_GROUP_DRIVER #define USB_INFINITE_TIMEOUT 0 @@ -97,10 +98,10 @@ pn53x_usb_bulk_read(struct pn53x_usb_data *data, uint8_t abtRx[], const size_t s { int res = usb_bulk_read(data->pudh, data->uiEndPointIn, (char *) abtRx, szRx, timeout); if (res > 0) { - LOG_HEX("RX", abtRx, res); + LOG_HEX(NFC_LOG_GROUP_COM, "RX", abtRx, res); } else if (res < 0) { if (res != -USB_TIMEDOUT) - log_put(LOG_CATEGORY, NFC_PRIORITY_ERROR, "Unable to read from USB (%s)", _usb_strerror(res)); + log_put(NFC_LOG_GROUP_COM, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "Unable to read from USB (%s)", _usb_strerror(res)); } return res; } @@ -108,7 +109,7 @@ pn53x_usb_bulk_read(struct pn53x_usb_data *data, uint8_t abtRx[], const size_t s static int pn53x_usb_bulk_write(struct pn53x_usb_data *data, uint8_t abtTx[], const size_t szTx, const int timeout) { - LOG_HEX("TX", abtTx, szTx); + LOG_HEX(NFC_LOG_GROUP_COM, "TX", abtTx, szTx); 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 @@ -116,7 +117,7 @@ pn53x_usb_bulk_write(struct pn53x_usb_data *data, uint8_t abtTx[], const size_t 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)); + log_put(NFC_LOG_GROUP_COM, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "Unable to write to USB (%s)", _usb_strerror(res)); } return res; } @@ -182,22 +183,23 @@ pn53x_usb_get_end_points(struct usb_device *dev, struct pn53x_usb_data *data) } static size_t -pn53x_usb_scan(nfc_connstring connstrings[], const size_t connstrings_len) +pn53x_usb_scan(const nfc_context *context, nfc_connstring connstrings[], const size_t connstrings_len) { + (void)context; 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)); + log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "Unable to find USB busses (%s)", _usb_strerror(res)); return 0; } // 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)); + log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "Unable to find USB devices (%s)", _usb_strerror(res)); return 0; } @@ -227,14 +229,14 @@ pn53x_usb_scan(nfc_connstring connstrings[], const size_t connstrings_len) // Set configuration res = usb_set_configuration(udev, 1); if (res < 0) { - log_put(LOG_CATEGORY, NFC_PRIORITY_ERROR, "Unable to set USB configuration (%s)", _usb_strerror(res)); + log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "Unable to set USB configuration (%s)", _usb_strerror(res)); usb_close(udev); // we failed to use the device continue; } // pn53x_usb_get_usb_device_name (dev, udev, pnddDevices[device_found].acDevice, sizeof (pnddDevices[device_found].acDevice)); - log_put(LOG_CATEGORY, NFC_PRIORITY_TRACE, "device found: Bus %s Device %s", bus->dirname, dev->filename); + log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG, "device found: Bus %s Device %s", bus->dirname, dev->filename); usb_close(udev); snprintf(connstrings[device_found], sizeof(nfc_connstring), "%s:%s:%s", PN53X_USB_DRIVER_NAME, bus->dirname, dev->filename); device_found++; @@ -310,12 +312,12 @@ pn53x_usb_get_usb_device_name(struct usb_device *dev, usb_dev_handle *udev, char } static nfc_device * -pn53x_usb_open(const nfc_connstring connstring) +pn53x_usb_open(const nfc_context *context, const nfc_connstring connstring) { 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_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG, "%d element(s) have been decoded from \"%s\"", connstring_decode_level, connstring); if (connstring_decode_level < 1) { goto free_mem; } @@ -335,14 +337,14 @@ pn53x_usb_open(const nfc_connstring connstring) // 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)); + log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_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)); + log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "Unable to find USB devices (%s)", _usb_strerror(res)); goto free_mem; } @@ -365,9 +367,9 @@ pn53x_usb_open(const nfc_connstring connstring) // Set configuration 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)); + log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_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); + log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_INFO, "Warning: 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 @@ -376,14 +378,14 @@ pn53x_usb_open(const nfc_connstring connstring) 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)); + log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "Unable to claim USB interface (%s)", _usb_strerror(res)); usb_close(data.pudh); // we failed to use the specified device 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 - pnd = nfc_device_new(connstring); + pnd = nfc_device_new(context, connstring); pn53x_usb_get_usb_device_name(dev, data.pudh, pnd->name, sizeof(pnd->name)); pnd->driver_data = malloc(sizeof(struct pn53x_usb_data)); @@ -455,11 +457,11 @@ pn53x_usb_close(nfc_device *pnd) int res; if ((res = usb_release_interface(DRIVER_DATA(pnd)->pudh, 0)) < 0) { - log_put(LOG_CATEGORY, NFC_PRIORITY_ERROR, "Unable to release USB interface (%s)", _usb_strerror(res)); + log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "Unable to release USB interface (%s)", _usb_strerror(res)); } if ((res = usb_close(DRIVER_DATA(pnd)->pudh)) < 0) { - log_put(LOG_CATEGORY, NFC_PRIORITY_ERROR, "Unable to close USB connection (%s)", _usb_strerror(res)); + log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "Unable to close USB connection (%s)", _usb_strerror(res)); } pn53x_data_free(pnd); nfc_device_free(pnd); @@ -562,7 +564,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"); + log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "%s", "Frame preamble+start code mismatch"); pnd->last_error = NFC_EIO; return pnd->last_error; } @@ -570,7 +572,7 @@ read: if ((0x01 == abtRxBuf[offset]) && (0xff == abtRxBuf[offset + 1])) { // Error frame - log_put(LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Application level error detected"); + log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "%s", "Application level error detected"); pnd->last_error = NFC_EIO; return pnd->last_error; } else if ((0xff == abtRxBuf[offset]) && (0xff == abtRxBuf[offset + 1])) { @@ -581,7 +583,7 @@ read: len = (abtRxBuf[offset] << 8) + abtRxBuf[offset + 1] - 2; if (((abtRxBuf[offset] + abtRxBuf[offset + 1] + abtRxBuf[offset + 2]) % 256) != 0) { // TODO: Retry - log_put(LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Length checksum mismatch"); + log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "%s", "Length checksum mismatch"); pnd->last_error = NFC_EIO; return pnd->last_error; } @@ -590,7 +592,7 @@ read: // Normal frame if (256 != (abtRxBuf[offset] + abtRxBuf[offset + 1])) { // TODO: Retry - log_put(LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Length checksum mismatch"); + log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "%s", "Length checksum mismatch"); pnd->last_error = NFC_EIO; return pnd->last_error; } @@ -601,21 +603,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); + log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "Unable to receive data: buffer too small. (szDataLen: %zu, len: %zu)", szDataLen, len); pnd->last_error = NFC_EIO; return pnd->last_error; } // TFI + PD0 (CC+1) if (abtRxBuf[offset] != 0xD5) { - log_put(LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "TFI Mismatch"); + log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "%s", "TFI Mismatch"); pnd->last_error = NFC_EIO; return pnd->last_error; } offset += 1; if (abtRxBuf[offset] != CHIP_DATA(pnd)->last_command + 1) { - log_put(LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Command Code verification failed"); + log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "%s", "Command Code verification failed"); pnd->last_error = NFC_EIO; return pnd->last_error; } @@ -631,14 +633,14 @@ read: } if (btDCS != abtRxBuf[offset]) { - log_put(LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Data checksum mismatch"); + log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "%s", "Data checksum mismatch"); pnd->last_error = NFC_EIO; return pnd->last_error; } offset += 1; if (0x00 != abtRxBuf[offset]) { - log_put(LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Frame postamble mismatch"); + log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "%s", "Frame postamble mismatch"); pnd->last_error = NFC_EIO; return pnd->last_error; } @@ -664,7 +666,7 @@ pn53x_usb_init(nfc_device *pnd) // ...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, "%s", "SONY RC-S360 initialization."); + log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG, "%s", "SONY RC-S360 initialization."); const uint8_t abtCmd2[] = { 0x18, 0x01 }; pn53x_transceive(pnd, abtCmd2, sizeof(abtCmd2), NULL, 0, -1); pn53x_usb_ack(pnd); @@ -674,7 +676,7 @@ pn53x_usb_init(nfc_device *pnd) return res; if (ASK_LOGO == DRIVER_DATA(pnd)->model) { - log_put(LOG_CATEGORY, NFC_PRIORITY_TRACE, "%s", "ASK LoGO initialization."); + log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG, "%s", "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); @@ -722,7 +724,7 @@ pn53x_usb_set_property_bool(nfc_device *pnd, const nfc_property property, const case ASK_LOGO: 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"); + log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG, "Switch progressive field %s", bEnable ? "On" : "Off"); if ((res = pn53x_write_register(pnd, PN53X_SFR_P3, _BV(P31) | _BV(P34), bEnable ? _BV(P34) : _BV(P31))) < 0) return NFC_ECHIP; } diff --git a/libnfc/log-printf.c b/libnfc/log-printf.c index 6d27ab3..9251c9c 100644 --- a/libnfc/log-printf.c +++ b/libnfc/log-printf.c @@ -1,6 +1,6 @@ /*- * Copyright (C) 2011 Romain Tartière - * Copyright (C) 2011 Romuald Conty + * Copyright (C) 2011, 2012 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 @@ -16,53 +16,54 @@ * along with this program. If not, see */ -#include "config.h" +#include "log.h" +#include #include +#include #include #include #include -#include "log.h" - -static uint8_t __log_init_counter = 0; - -int -log_init(void) +void +log_init(const nfc_context *context) { - 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; + char str[32]; + sprintf(str, "%"PRIu32, context->log_level); + setenv("LIBNFC_LOG_LEVEL", str, 1); } void -log_put(const char *category, const char *priority, const char *format, ...) +log_exit(void) { - va_list va; - va_start(va, format); - fprintf(stderr, "%s\t%s\t", priority, category); - vfprintf(stderr, format, va); - fprintf(stderr, "\n"); - va_end(va); +} + +void +log_put(const uint8_t group, const char *category, const uint8_t priority, const char *format, ...) +{ + char *env_log_level = getenv("LIBNFC_LOG_LEVEL"); + uint32_t log_level; + if (NULL == env_log_level) { + // LIBNFC_LOG_LEVEL is not set +#ifdef DEBUG + log_level = 3; +#else + log_level = 1; +#endif + } else { + log_level = atoi(env_log_level); + } + + // printf("log_level = %"PRIu32" group = %"PRIu8" priority = %"PRIu8"\n", log_level, group, priority); + if (log_level) { // If log is not disabled by log_level=none + if ( ((log_level & 0x00000003) >= priority) || // Global log level + (((log_level >> (group*2)) & 0x00000003) >= priority) ) { // Group log level + va_list va; + va_start(va, format); + fprintf(stderr, "%s\t%s\t", log_priority_to_str(priority), category); + vfprintf(stderr, format, va); + fprintf(stderr, "\n"); + va_end(va); + } + } } diff --git a/libnfc/log.c b/libnfc/log.c new file mode 100644 index 0000000..2455788 --- /dev/null +++ b/libnfc/log.c @@ -0,0 +1,72 @@ +/*- + * Copyright (C) 2012 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 "log.h" +/* +#include +#include +#include +*/ +#include + +/* +int +log_priority_to_int(const char* priority) +{ + if (strcmp("none", priority) == 0) { + return -1; + } else if (strcmp("fatal", priority) == 0) { + return NFC_LOG_PRIORITY_FATAL; + } else if (strcmp("alert", priority) == 0) { + return NFC_LOG_PRIORITY_ALERT; + } else if (strcmp("critical", priority) == 0) { + return NFC_LOG_PRIORITY_CRIT; + } else if (strcmp("error", priority) == 0) { + return NFC_LOG_PRIORITY_ERROR; + } else if (strcmp("warning", priority) == 0) { + return NFC_LOG_PRIORITY_WARN; + } else if (strcmp("notice", priority) == 0) { + return NFC_LOG_PRIORITY_NOTICE; + } else if (strcmp("info", priority) == 0) { + return NFC_LOG_PRIORITY_INFO; + } else if (strcmp("debug", priority) == 0) { + return NFC_LOG_PRIORITY_DEBUG; + } else if (strcmp("trace", priority) == 0) { + return NFC_LOG_PRIORITY_TRACE; + } + + // if priority is string is not recognized, we set maximal verbosity + return NFC_LOG_PRIORITY_TRACE; +} +*/ + +const char* +log_priority_to_str(const int priority) +{ + switch(priority) { + case NFC_LOG_PRIORITY_ERROR: + return "error"; + case NFC_LOG_PRIORITY_INFO: + return "info"; + case NFC_LOG_PRIORITY_DEBUG: + return "debug"; + default: + break; + } + return "unkown"; +} + diff --git a/libnfc/log.h b/libnfc/log.h index 379aee1..34d0b85 100644 --- a/libnfc/log.h +++ b/libnfc/log.h @@ -1,6 +1,6 @@ /*- * Copyright (C) 2011 Romain Tartière - * Copyright (C) 2011 Romuald Conty + * Copyright (C) 2011, 2012 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 @@ -23,7 +23,36 @@ # include "config.h" #endif // HAVE_CONFIG_H -#if defined DEBUG +#include "nfc-internal.h" + +#define NFC_LOG_PRIORITY_NONE 0 +#define NFC_LOG_PRIORITY_ERROR 1 +#define NFC_LOG_PRIORITY_INFO 2 +#define NFC_LOG_PRIORITY_DEBUG 3 + +#define NFC_LOG_GROUP_GENERAL 1 +#define NFC_LOG_GROUP_CONFIG 2 +#define NFC_LOG_GROUP_CHIP 3 +#define NFC_LOG_GROUP_DRIVER 4 +#define NFC_LOG_GROUP_COM 5 + +/* + To enable log only for one (or more) group, you can use this formula: + log_level = NFC_LOG_PRIORITY(main) + NFC_LOG_PRIORITY(group) * 2 ^ (NFC_LOG_GROUP(group) * 2) + + Examples: + * Main log level is NONE and only communication group log is set to DEBUG verbosity (for rx/tx trace): + LIBNFC_LOG_LEVEL=3072 // 0+3072 + * Main log level is ERROR and driver layer log is set to DEBUG level: + LIBNFC_LOG_LEVEL=769 // 1+768 + * Main log level is ERROR, driver layer is set to INFO and communication is set to DEBUG: + LIBNFC_LOG_LEVEL=3585 // 1+512+3072 +*/ + +//int log_priority_to_int(const char* priority); +const char* log_priority_to_str(const int priority); + +#if defined LOG # ifndef __has_attribute # define __has_attribute(x) 0 @@ -33,57 +62,34 @@ # define __has_attribute_format 1 # endif -// User want debug features -#define LOGGING 1 -int log_init(void); -int log_fini(void); -void log_put(const char *category, const char *priority, const char *format, ...) +void log_init(const nfc_context *context); +void log_exit(void); +void log_put(const uint8_t group, const char *category, const uint8_t priority, const char *format, ...) # if __has_attribute_format -__attribute__((format(printf, 3, 4))) +__attribute__((format(printf, 4, 5))) # endif ; - -#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() ((void) 0) -#define log_fini() ((void) 0) -#define log_msg(category, priority, message) do {} while (0) -#define log_set_appender(category, appender) do {} while (0) +#define log_exit() ((void) 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 */ +#endif // LOG /** * @macro LOG_HEX * @brief Log a byte-array in hexadecimal format */ -# ifdef LOGGING -# define LOG_HEX(pcTag, pbtData, szBytes) do { \ +# ifdef LOG +# define LOG_HEX(group, pcTag, pbtData, szBytes) do { \ size_t __szPos; \ char __acBuf[1024]; \ size_t __szBuf = 0; \ if ((int)szBytes < 0) { \ fprintf (stderr, "%s:%d: Attempt to print %d bytes!\n", __FILE__, __LINE__, (int)szBytes); \ - log_put (LOG_CATEGORY, NFC_PRIORITY_FATAL, "%s:%d: Attempt to print %d bytes!\n", __FILE__, __LINE__, (int)szBytes); \ + log_put (group, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "%s:%d: Attempt to print %d bytes!\n", __FILE__, __LINE__, (int)szBytes); \ abort(); \ break; \ } \ @@ -93,7 +99,7 @@ __attribute__((format(printf, 3, 4))) snprintf (__acBuf + __szBuf, sizeof(__acBuf) - __szBuf, "%02x ",((uint8_t *)(pbtData))[__szPos]); \ __szBuf += 4; \ } \ - log_put (LOG_CATEGORY, NFC_PRIORITY_TRACE, "%s", __acBuf); \ + log_put (group, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG, "%s", __acBuf); \ } while (0); # else # define LOG_HEX(pcTag, pbtData, szBytes) do { \ @@ -103,4 +109,4 @@ __attribute__((format(printf, 3, 4))) } while (0); # endif -#endif +#endif // __LOG_H__ diff --git a/libnfc/nfc-device.c b/libnfc/nfc-device.c index 1a20934..138c3d1 100644 --- a/libnfc/nfc-device.c +++ b/libnfc/nfc-device.c @@ -2,7 +2,7 @@ * Public platform independent Near Field Communication (NFC) library * * Copyright (C) 2011 Romain Tartière - * Copyright (C) 2011 Romuald Conty + * Copyright (C) 2011, 2012 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 @@ -33,7 +33,7 @@ #include "nfc-internal.h" nfc_device * -nfc_device_new(const nfc_connstring connstring) +nfc_device_new(const nfc_context *context, const nfc_connstring connstring) { nfc_device *res = malloc(sizeof(*res)); @@ -41,6 +41,9 @@ nfc_device_new(const nfc_connstring connstring) err(EXIT_FAILURE, "nfc_device_new: malloc"); } + // Store associated context + res->context = context; + // Variables initiatialization // Note: Actually, these initialization will be overwritten while the device // will be setup. Putting them to _false_ while the default is _true_ ensure we diff --git a/libnfc/nfc-internal.c b/libnfc/nfc-internal.c index 7ebcdb3..f4c2e93 100644 --- a/libnfc/nfc-internal.c +++ b/libnfc/nfc-internal.c @@ -28,6 +28,10 @@ #include #include +#include + +#define LOG_GROUP NFC_LOG_GROUP_GENERAL +#define LOG_CATEGORY "libnfc.general" void string_as_boolean(const char* s, bool *value) @@ -63,6 +67,11 @@ nfc_context_new(void) // Set default context values res->allow_autoscan = true; res->allow_intrusive_scan = false; +#ifdef DEBUG + res->log_level = 3; +#else + res->log_level = 1; +#endif // Load options from configuration file (ie. /etc/nfc/libnfc.conf) conf_load(res); @@ -72,9 +81,16 @@ nfc_context_new(void) char *envvar = getenv("LIBNFC_INTRUSIVE_SCAN"); string_as_boolean(envvar, &(res->allow_intrusive_scan)); + // log level + envvar = getenv("LIBNFC_LOG_LEVEL"); + if (envvar) { + res->log_level = atoi(envvar); + } + // Debug context state - log_put ("libnfc", NFC_PRIORITY_DEBUG, "allow_autoscan is set to %s", (res->allow_autoscan)?"true":"false"); - log_put ("libnfc", NFC_PRIORITY_DEBUG, "allow_intrusive_scan is set to %s", (res->allow_intrusive_scan)?"true":"false"); + log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_NONE, "log_level is set to %"PRIu32, res->log_level); + log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG, "allow_autoscan is set to %s", (res->allow_autoscan)?"true":"false"); + log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG, "allow_intrusive_scan is set to %s", (res->allow_intrusive_scan)?"true":"false"); return res; } diff --git a/libnfc/nfc-internal.h b/libnfc/nfc-internal.h index 5edebf1..8a061ea 100644 --- a/libnfc/nfc-internal.h +++ b/libnfc/nfc-internal.h @@ -2,7 +2,7 @@ * Public platform independent Near Field Communication (NFC) library * * Copyright (C) 2011 Romain Tartière - * Copyright (C) 2011 Romuald Conty + * Copyright (C) 2011, 2012 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 @@ -116,8 +116,8 @@ typedef enum { struct nfc_driver { const char *name; const scan_type_enum scan_type; - size_t (*scan)(nfc_connstring connstrings[], const size_t connstrings_len); - struct nfc_device *(*open)(const nfc_connstring connstring); + size_t (*scan)(const nfc_context *context, nfc_connstring connstrings[], const size_t connstrings_len); + struct nfc_device *(*open)(const nfc_context *context, const nfc_connstring connstring); void (*close)(struct nfc_device *pnd); const char *(*strerror)(const struct nfc_device *pnd); @@ -160,6 +160,7 @@ struct nfc_driver { struct nfc_context { bool allow_autoscan; bool allow_intrusive_scan; + int log_level; }; nfc_context *nfc_context_new(void); @@ -170,6 +171,7 @@ void nfc_context_free(nfc_context *context); * @brief NFC device information */ struct nfc_device { + nfc_context *context; const struct nfc_driver *driver; void *driver_data; void *chip_data; @@ -193,7 +195,7 @@ struct nfc_device { int last_error; }; -nfc_device *nfc_device_new(const nfc_connstring connstring); +nfc_device *nfc_device_new(const nfc_context *context, const nfc_connstring connstring); void nfc_device_free(nfc_device *dev); void string_as_boolean(const char* s, bool *value); diff --git a/libnfc/nfc.c b/libnfc/nfc.c index 4e7d589..9825b84 100644 --- a/libnfc/nfc.c +++ b/libnfc/nfc.c @@ -82,6 +82,7 @@ #include "drivers.h" #define LOG_CATEGORY "libnfc.general" +#define LOG_GROUP NFC_LOG_GROUP_GENERAL const struct nfc_driver *nfc_drivers[] = { # if defined (DRIVER_PN53X_USB_ENABLED) @@ -118,7 +119,7 @@ nfc_init(nfc_context **context) exit(EXIT_FAILURE); } *context = nfc_context_new(); - log_init(); + log_init(*context); } /** @ingroup lib @@ -129,8 +130,8 @@ nfc_init(nfc_context **context) void nfc_exit(nfc_context *context) { - if(context) nfc_context_free(context); - log_fini(); + nfc_context_free(context); + log_exit(); } /** @ingroup dev @@ -186,13 +187,11 @@ nfc_get_default_device(nfc_connstring *connstring) nfc_device * nfc_open(nfc_context *context, const nfc_connstring connstring) { - (void) context; nfc_device *pnd = NULL; nfc_connstring ncs; if (connstring == NULL) { if (!nfc_get_default_device(&ncs)) { - log_fini(); return NULL; } } else { @@ -212,7 +211,7 @@ nfc_open(nfc_context *context, const nfc_connstring connstring) } } - pnd = ndr->open(ncs); + pnd = ndr->open(context, ncs); // Test if the opening was successful if (pnd == NULL) { if (0 == strncmp("usb", ncs, strlen("usb"))) { @@ -220,19 +219,16 @@ nfc_open(nfc_context *context, const nfc_connstring connstring) pndr++; continue; } - log_put(LOG_CATEGORY, NFC_PRIORITY_TRACE, "Unable to open \"%s\".", ncs); - log_fini(); + log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG, "Unable to open \"%s\".", ncs); return pnd; } - log_put(LOG_CATEGORY, NFC_PRIORITY_TRACE, "\"%s\" (%s) has been claimed.", pnd->name, pnd->connstring); - log_fini(); + log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG, "\"%s\" (%s) has been claimed.", pnd->name, pnd->connstring); 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(); + log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG, "No driver available to handle \"%s\".", ncs); return NULL; } @@ -280,8 +276,8 @@ nfc_list_devices(nfc_context *context, nfc_connstring connstrings[], const size_ while ((ndr = *pndr)) { size_t _device_found = 0; if((ndr->scan_type == NOT_INTRUSIVE) || ((context->allow_intrusive_scan) && (ndr->scan_type == INTRUSIVE))) { - _device_found = ndr->scan(connstrings + (device_found), connstrings_len - (device_found)); - log_put(LOG_CATEGORY, NFC_PRIORITY_TRACE, "%ld device(s) found using %s driver", (unsigned long) _device_found, ndr->name); + _device_found = ndr->scan(context, connstrings + (device_found), connstrings_len - (device_found)); + log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG, "%ld device(s) found using %s driver", (unsigned long) _device_found, ndr->name); if (_device_found > 0) { device_found += _device_found; if (device_found == connstrings_len)