Commit 9b3947b8 authored by Romuald Conty's avatar Romuald Conty

Improves log feature:

  * Add --disable-log
  * Add a log level filter, configurable using conf file (ie. /etc/nfc/libnfc.conf) or environment var LIBNFC_LOG_LEVEL
parent d6c87908
......@@ -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
......
......@@ -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
......@@ -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
......
......@@ -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
......
......@@ -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"),
......
......@@ -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;
......
......@@ -15,6 +15,8 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>
*/
#include "conf.h"
#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>
......@@ -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);
}
}
......
......@@ -14,6 +14,7 @@
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>
*/
#include "nfc-internal.h"
void conf_load(nfc_context *context);
......@@ -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;
......
This diff is collapsed.
......@@ -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;
}
......
This diff is collapsed.
......@@ -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)");