Merge branch 'master' into pn532_spi

* master:
  New connstring_decode() fix cppcheck warning "Non reentrant function 'strtok' called"
  Update Changelog
  Update NEWS & Changelog
  Forgot to commit new log internal header file
  Remove log-printf from CMakefile
  Add log_internal.h to Makefile
  Creating log_internal.h for log_*put_internal() prototypes
  log_win32: declare log_output_debug() static
  Merge log-printf.c into log.c, inline with prototypes in log.h & platform files log_*.c
This commit is contained in:
Philippe Teuwen 2013-03-10 00:22:27 +01:00
commit 759cd106e5
18 changed files with 275 additions and 400 deletions

View file

@ -1,3 +1,34 @@
TBD
---
Fixes:
- Fix bug when compiling without libusb
- Fix several memory leaks in error handling conditions
- Remove calls to exit() from the library
- Create safer snprint_nfc_*() instead of sprint_nfc_*() functions
- Fix warnings returned by cppcheck & clang/scan-build
- Obsolete function 'usleep' => nanosleep()
- Non reentrant function 'readdir' => readdir_r()
- Buffer may not be null-terminated after call to strncpy()
- scanf without field width limits can crash with huge input data
- Resource leaks: missing fclose()
- Dead code, unused vars & vars scopes warnings
- Windows: Fix compilation due to new usbbus file
- Windows: Clean up compiler/linker warnings
- Fixed the suppression of the auto-fixup for linking against MS built libs
- Fixed all the formatting warnings by shifting to inttypes.h specifiers
- shifted to %lu for DWORD printf
Improvements:
- Devels HACKING file: introduce clang/scan-build & cppcheck for better code
- Windows: logging via OutputDebugString(), ease debugging
Changes:
- Upon malloc error, nfc_init() doesn't force exit() anymore
so now you should test if context != NULL after nfc_init() call
- API: nfc_initiator_target_is_present() & str_nfc_target()
now take a pointer to nfc_target as argument instead of passing by value
Mar 03, 2013 - 1.7.0-rc6 (release candidate) Mar 03, 2013 - 1.7.0-rc6 (release candidate)
-------------------------------------------- --------------------------------------------

4
NEWS
View file

@ -3,8 +3,10 @@ New in 1.7.0-***:
API Changes: API Changes:
* Functions * Functions
- nfc_initiator_target_is_present() & str_nfc_target() - nfc_initiator_target_is_present() & str_nfc_target():
now take a pointer to nfc_target as argument now take a pointer to nfc_target as argument
- nfc_init(): upon malloc error, doesn't force exit() anymore
so now you should test if context != NULL after nfc_init() call
New in 1.7.0-rc5: New in 1.7.0-rc5:

View file

@ -48,9 +48,9 @@ INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR})
IF(LIBNFC_LOG) IF(LIBNFC_LOG)
IF(WIN32) IF(WIN32)
SET(CMAKE_C_FLAGS "-fgnu89-inline ${CMAKE_C_FLAGS}") SET(CMAKE_C_FLAGS "-fgnu89-inline ${CMAKE_C_FLAGS}")
LIST(APPEND LIBRARY_SOURCES log-printf log_win32) LIST(APPEND LIBRARY_SOURCES log_win32)
ELSE(WIN32) ELSE(WIN32)
LIST(APPEND LIBRARY_SOURCES log-printf log_posix) LIST(APPEND LIBRARY_SOURCES log_posix)
ENDIF(WIN32) ENDIF(WIN32)
ENDIF(LIBNFC_LOG) ENDIF(LIBNFC_LOG)
ADD_LIBRARY(nfc SHARED ${LIBRARY_SOURCES}) ADD_LIBRARY(nfc SHARED ${LIBRARY_SOURCES})

View file

@ -18,6 +18,7 @@ libnfc_la_SOURCES = \
drivers.h \ drivers.h \
iso7816.h \ iso7816.h \
log.h \ log.h \
log-internal.h \
mirror-subr.h \ mirror-subr.h \
nfc-internal.h \ nfc-internal.h \
target-subr.h target-subr.h
@ -40,11 +41,10 @@ if LIBUSB_ENABLED
endif endif
if WITH_LOG if WITH_LOG
libnfc_la_SOURCES += log-printf.c log_posix.c libnfc_la_SOURCES += log_posix.c
endif endif
EXTRA_DIST = \ EXTRA_DIST = \
CMakeLists.txt \ CMakeLists.txt \
log-printf.c \
log_posix.c \ log_posix.c \
log_win32.c log_win32.c

View file

@ -191,49 +191,14 @@ acr122_pcsc_scan(const nfc_context *context, nfc_connstring connstrings[], const
} }
struct acr122_pcsc_descriptor { struct acr122_pcsc_descriptor {
char pcsc_device_name[512]; char *pcsc_device_name;
}; };
static int
acr122_pcsc_connstring_decode(const nfc_connstring connstring, struct acr122_pcsc_descriptor *desc)
{
char *cs = malloc(strlen(connstring) + 1);
if (!cs) {
perror("malloc");
return -1;
}
strcpy(cs, connstring);
const char *driver_name = strtok(cs, ":");
if (!driver_name) {
// Parse error
free(cs);
return -1;
}
if (0 != strcmp(driver_name, ACR122_PCSC_DRIVER_NAME)) {
// Driver name does not match.
free(cs);
return 0;
}
const char *device_name = strtok(NULL, ":");
if (!device_name) {
// Only driver name was specified (or parsing error)
free(cs);
return 1;
}
strncpy(desc->pcsc_device_name, device_name, sizeof(desc->pcsc_device_name) - 1);
desc->pcsc_device_name[sizeof(desc->pcsc_device_name) - 1] = '\0';
free(cs);
return 2;
}
static nfc_device * static nfc_device *
acr122_pcsc_open(const nfc_context *context, const nfc_connstring connstring) acr122_pcsc_open(const nfc_context *context, const nfc_connstring connstring)
{ {
struct acr122_pcsc_descriptor ndd; struct acr122_pcsc_descriptor ndd;
int connstring_decode_level = acr122_pcsc_connstring_decode(connstring, &ndd); int connstring_decode_level = connstring_decode(connstring, ACR122_PCSC_DRIVER_NAME, "pcsc", &ndd.pcsc_device_name, NULL);
if (connstring_decode_level < 1) { if (connstring_decode_level < 1) {
return NULL; return NULL;
@ -245,7 +210,7 @@ acr122_pcsc_open(const nfc_context *context, const nfc_connstring connstring)
size_t szDeviceFound = acr122_pcsc_scan(context, &fullconnstring, 1); size_t szDeviceFound = acr122_pcsc_scan(context, &fullconnstring, 1);
if (szDeviceFound < 1) if (szDeviceFound < 1)
return NULL; return NULL;
connstring_decode_level = acr122_pcsc_connstring_decode(fullconnstring, &ndd); connstring_decode_level = connstring_decode(fullconnstring, ACR122_PCSC_DRIVER_NAME, "pcsc", &ndd.pcsc_device_name, NULL);
if (connstring_decode_level < 2) { if (connstring_decode_level < 2) {
return NULL; return NULL;
} }
@ -255,23 +220,29 @@ acr122_pcsc_open(const nfc_context *context, const nfc_connstring connstring)
if (strlen(ndd.pcsc_device_name) < 5) { // We can assume it's a reader ID as pcsc_name always ends with "NN NN" if (strlen(ndd.pcsc_device_name) < 5) { // We can assume it's a reader ID as pcsc_name always ends with "NN NN"
// Device was not specified, only ID, retrieve it // Device was not specified, only ID, retrieve it
size_t index; size_t index;
if (sscanf(ndd.pcsc_device_name, "%4" SCNuPTR, &index) != 1) if (sscanf(ndd.pcsc_device_name, "%4" SCNuPTR, &index) != 1) {
free(ndd.pcsc_device_name);
return NULL; return NULL;
}
nfc_connstring *ncs = malloc(sizeof(nfc_connstring) * (index + 1)); nfc_connstring *ncs = malloc(sizeof(nfc_connstring) * (index + 1));
if (!ncs) { if (!ncs) {
perror("malloc"); perror("malloc");
free(ndd.pcsc_device_name);
return NULL; return NULL;
} }
size_t szDeviceFound = acr122_pcsc_scan(context, ncs, index + 1); size_t szDeviceFound = acr122_pcsc_scan(context, ncs, index + 1);
if (szDeviceFound < index + 1) { if (szDeviceFound < index + 1) {
free(ncs); free(ncs);
free(ndd.pcsc_device_name);
return NULL; return NULL;
} }
strncpy(fullconnstring, ncs[index], sizeof(nfc_connstring)); strncpy(fullconnstring, ncs[index], sizeof(nfc_connstring));
fullconnstring[sizeof(nfc_connstring) - 1] = '\0'; fullconnstring[sizeof(nfc_connstring) - 1] = '\0';
free(ncs); free(ncs);
connstring_decode_level = acr122_pcsc_connstring_decode(fullconnstring, &ndd); connstring_decode_level = connstring_decode(fullconnstring, ACR122_PCSC_DRIVER_NAME, "pcsc", &ndd.pcsc_device_name, NULL);
if (connstring_decode_level < 2) { if (connstring_decode_level < 2) {
free(ndd.pcsc_device_name);
return NULL; return NULL;
} }
} }
@ -324,12 +295,13 @@ acr122_pcsc_open(const nfc_context *context, const nfc_connstring connstring)
pn53x_init(pnd); pn53x_init(pnd);
free(ndd.pcsc_device_name);
return pnd; return pnd;
} }
error: error:
free(ndd.pcsc_device_name);
nfc_device_free(pnd); nfc_device_free(pnd);
return NULL; return NULL;
} }

View file

@ -347,50 +347,6 @@ struct acr122_usb_descriptor {
char *filename; char *filename;
}; };
static int
acr122_usb_connstring_decode(const nfc_connstring connstring, struct acr122_usb_descriptor *desc)
{
int n = strlen(connstring) + 1;
char *driver_name = malloc(n);
if (!driver_name) {
perror("malloc");
return 0;
}
char *dirname = malloc(n);
if (!dirname) {
perror("malloc");
free(driver_name);
return 0;
}
char *filename = malloc(n);
if (!filename) {
perror("malloc");
free(driver_name);
free(dirname);
return 0;
}
driver_name[0] = '\0';
char format[32];
snprintf(format, sizeof(format), "%%%i[^:]:%%%i[^:]:%%%i[^:]", n - 1, n - 1, n - 1);
int res = sscanf(connstring, format, driver_name, dirname, filename);
if (!res || ((0 != strcmp(driver_name, ACR122_USB_DRIVER_NAME)) && (0 != strcmp(driver_name, "usb")))) {
// Driver name does not match.
res = 0;
} else {
desc->dirname = strdup(dirname);
desc->filename = strdup(filename);
}
free(driver_name);
free(dirname);
free(filename);
return res;
}
static bool static bool
acr122_usb_get_usb_device_name(struct usb_device *dev, usb_dev_handle *udev, char *buffer, size_t len) acr122_usb_get_usb_device_name(struct usb_device *dev, usb_dev_handle *udev, char *buffer, size_t len)
{ {
@ -424,7 +380,7 @@ acr122_usb_open(const nfc_context *context, const nfc_connstring connstring)
{ {
nfc_device *pnd = NULL; nfc_device *pnd = NULL;
struct acr122_usb_descriptor desc = { NULL, NULL }; struct acr122_usb_descriptor desc = { NULL, NULL };
int connstring_decode_level = acr122_usb_connstring_decode(connstring, &desc); int connstring_decode_level = connstring_decode(connstring, ACR122_USB_DRIVER_NAME, "usb", &desc.dirname, &desc.filename);
log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG, "%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) { if (connstring_decode_level < 1) {
goto free_mem; goto free_mem;

View file

@ -396,59 +396,10 @@ acr122s_get_firmware_version(nfc_device *pnd, char *version, size_t length)
} }
struct acr122s_descriptor { struct acr122s_descriptor {
char port[128]; char *port;
uint32_t speed; uint32_t speed;
}; };
static int
acr122s_connstring_decode(const nfc_connstring connstring, struct acr122s_descriptor *desc)
{
char *cs = malloc(strlen(connstring) + 1);
if (!cs) {
perror("malloc");
return -1;
}
strcpy(cs, connstring);
const char *driver_name = strtok(cs, ":");
if (!driver_name) {
// Parse error
free(cs);
return -1;
}
if (0 != strcmp(driver_name, ACR122S_DRIVER_NAME)) {
// Driver name does not match.
free(cs);
return 0;
}
const char *port = strtok(NULL, ":");
if (!port) {
// Only driver name was specified (or parsing error)
free(cs);
return 1;
}
strncpy(desc->port, port, sizeof(desc->port) - 1);
desc->port[sizeof(desc->port) - 1] = '\0';
const char *speed_s = strtok(NULL, ":");
if (!speed_s) {
// speed not specified (or parsing error)
free(cs);
return 2;
}
unsigned long speed;
if (sscanf(speed_s, "%10lu", &speed) != 1) {
// speed_s is not a number
free(cs);
return 2;
}
desc->speed = speed;
free(cs);
return 3;
}
static size_t static size_t
acr122s_scan(const nfc_context *context, nfc_connstring connstrings[], const size_t connstrings_len) acr122s_scan(const nfc_context *context, nfc_connstring connstrings[], const size_t connstrings_len)
{ {
@ -540,6 +491,7 @@ acr122s_close(nfc_device *pnd)
close(DRIVER_DATA(pnd)->abort_fds[1]); close(DRIVER_DATA(pnd)->abort_fds[1]);
#endif #endif
free(DRIVER_DATA(pnd)->port);
pn53x_data_free(pnd); pn53x_data_free(pnd);
nfc_device_free(pnd); nfc_device_free(pnd);
} }
@ -550,8 +502,18 @@ acr122s_open(const nfc_context *context, const nfc_connstring connstring)
serial_port sp; serial_port sp;
nfc_device *pnd; nfc_device *pnd;
struct acr122s_descriptor ndd; struct acr122s_descriptor ndd;
int connstring_decode_level = acr122s_connstring_decode(connstring, &ndd); char *speed_s;
int connstring_decode_level = connstring_decode(connstring, ACR122S_DRIVER_NAME, NULL, &ndd.port, &speed_s);
if (connstring_decode_level == 3) {
ndd.speed = 0;
if (sscanf(speed_s, "%10"PRIu32, &ndd.speed) != 1) {
// speed_s is not a number
free(ndd.port);
free(speed_s);
return NULL;
}
free(speed_s);
}
if (connstring_decode_level < 2) { if (connstring_decode_level < 2) {
return NULL; return NULL;
} }
@ -566,11 +528,13 @@ acr122s_open(const nfc_context *context, const nfc_connstring connstring)
if (sp == INVALID_SERIAL_PORT) { if (sp == INVALID_SERIAL_PORT) {
log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR,
"Invalid serial port: %s", ndd.port); "Invalid serial port: %s", ndd.port);
free(ndd.port);
return NULL; return NULL;
} }
if (sp == CLAIMED_SERIAL_PORT) { if (sp == CLAIMED_SERIAL_PORT) {
log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR,
"Serial port already claimed: %s", ndd.port); "Serial port already claimed: %s", ndd.port);
free(ndd.port);
return NULL; return NULL;
} }
@ -580,6 +544,7 @@ acr122s_open(const nfc_context *context, const nfc_connstring connstring)
pnd = nfc_device_new(context, connstring); pnd = nfc_device_new(context, connstring);
if (!pnd) { if (!pnd) {
perror("malloc"); perror("malloc");
acr122s_close(pnd);
return NULL; return NULL;
} }
pnd->driver = &acr122s_driver; pnd->driver = &acr122s_driver;
@ -597,6 +562,7 @@ acr122s_open(const nfc_context *context, const nfc_connstring connstring)
#ifndef WIN32 #ifndef WIN32
if (pipe(DRIVER_DATA(pnd)->abort_fds) < 0) { if (pipe(DRIVER_DATA(pnd)->abort_fds) < 0) {
acr122s_close(pnd);
return NULL; return NULL;
} }
#else #else

View file

@ -165,59 +165,10 @@ arygon_scan(const nfc_context *context, nfc_connstring connstrings[], const size
} }
struct arygon_descriptor { struct arygon_descriptor {
char port[128]; char *port;
uint32_t speed; uint32_t speed;
}; };
static int
arygon_connstring_decode(const nfc_connstring connstring, struct arygon_descriptor *desc)
{
char *cs = malloc(strlen(connstring) + 1);
if (!cs) {
perror("malloc");
return -1;
}
strcpy(cs, connstring);
const char *driver_name = strtok(cs, ":");
if (!driver_name) {
// Parse error
free(cs);
return -1;
}
if (0 != strcmp(driver_name, ARYGON_DRIVER_NAME)) {
// Driver name does not match.
free(cs);
return 0;
}
const char *port = strtok(NULL, ":");
if (!port) {
// Only driver name was specified (or parsing error)
free(cs);
return 1;
}
strncpy(desc->port, port, sizeof(desc->port) - 1);
desc->port[sizeof(desc->port) - 1] = '\0';
const char *speed_s = strtok(NULL, ":");
if (!speed_s) {
// speed not specified (or parsing error)
free(cs);
return 2;
}
unsigned long speed;
if (sscanf(speed_s, "%10lu", &speed) != 1) {
// speed_s is not a number
free(cs);
return 2;
}
desc->speed = speed;
free(cs);
return 3;
}
static void static void
arygon_close(nfc_device *pnd) arygon_close(nfc_device *pnd)
{ {
@ -232,6 +183,7 @@ arygon_close(nfc_device *pnd)
close(DRIVER_DATA(pnd)->iAbortFds[1]); close(DRIVER_DATA(pnd)->iAbortFds[1]);
#endif #endif
free(DRIVER_DATA(pnd)->port);
pn53x_data_free(pnd); pn53x_data_free(pnd);
nfc_device_free(pnd); nfc_device_free(pnd);
} }
@ -240,8 +192,18 @@ static nfc_device *
arygon_open(const nfc_context *context, const nfc_connstring connstring) arygon_open(const nfc_context *context, const nfc_connstring connstring)
{ {
struct arygon_descriptor ndd; struct arygon_descriptor ndd;
int connstring_decode_level = arygon_connstring_decode(connstring, &ndd); char *speed_s;
int connstring_decode_level = connstring_decode(connstring, ARYGON_DRIVER_NAME, NULL, &ndd.port, &speed_s);
if (connstring_decode_level == 3) {
ndd.speed = 0;
if (sscanf(speed_s, "%10"PRIu32, &ndd.speed) != 1) {
// speed_s is not a number
free(ndd.port);
free(speed_s);
return NULL;
}
free(speed_s);
}
if (connstring_decode_level < 2) { if (connstring_decode_level < 2) {
return NULL; return NULL;
} }
@ -258,8 +220,10 @@ arygon_open(const nfc_context *context, const nfc_connstring connstring)
log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_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) if (sp == CLAIMED_SERIAL_PORT)
log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_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)) if ((sp == CLAIMED_SERIAL_PORT) || (sp == INVALID_SERIAL_PORT)) {
free(ndd.port);
return NULL; return NULL;
}
// We need to flush input to be sure first reply does not comes from older byte transceive // We need to flush input to be sure first reply does not comes from older byte transceive
uart_flush_input(sp); uart_flush_input(sp);
@ -269,6 +233,7 @@ arygon_open(const nfc_context *context, const nfc_connstring connstring)
pnd = nfc_device_new(context, connstring); pnd = nfc_device_new(context, connstring);
if (!pnd) { if (!pnd) {
perror("malloc"); perror("malloc");
free(ndd.port);
return NULL; return NULL;
} }
snprintf(pnd->name, sizeof(pnd->name), "%s:%s", ARYGON_DRIVER_NAME, ndd.port); snprintf(pnd->name, sizeof(pnd->name), "%s:%s", ARYGON_DRIVER_NAME, ndd.port);
@ -276,6 +241,7 @@ arygon_open(const nfc_context *context, const nfc_connstring connstring)
pnd->driver_data = malloc(sizeof(struct arygon_data)); pnd->driver_data = malloc(sizeof(struct arygon_data));
if (!pnd->driver_data) { if (!pnd->driver_data) {
perror("malloc"); perror("malloc");
free(ndd.port);
return NULL; return NULL;
} }
DRIVER_DATA(pnd)->port = sp; DRIVER_DATA(pnd)->port = sp;
@ -293,6 +259,7 @@ arygon_open(const nfc_context *context, const nfc_connstring connstring)
#ifndef WIN32 #ifndef WIN32
// pipe-based abort mecanism // pipe-based abort mecanism
if (pipe(DRIVER_DATA(pnd)->iAbortFds) < 0) { if (pipe(DRIVER_DATA(pnd)->iAbortFds) < 0) {
free(ndd.port);
return NULL; return NULL;
} }
#else #else

View file

@ -142,59 +142,10 @@ pn532_uart_scan(const nfc_context *context, nfc_connstring connstrings[], const
} }
struct pn532_uart_descriptor { struct pn532_uart_descriptor {
char port[128]; char *port;
uint32_t speed; uint32_t speed;
}; };
static int
pn532_connstring_decode(const nfc_connstring connstring, struct pn532_uart_descriptor *desc)
{
char *cs = malloc(strlen(connstring) + 1);
if (!cs) {
perror("malloc");
return -1;
}
strcpy(cs, connstring);
const char *driver_name = strtok(cs, ":");
if (!driver_name) {
// Parse error
free(cs);
return -1;
}
if (0 != strcmp(driver_name, PN532_UART_DRIVER_NAME)) {
// Driver name does not match.
free(cs);
return 0;
}
const char *port = strtok(NULL, ":");
if (!port) {
// Only driver name was specified (or parsing error)
free(cs);
return 1;
}
strncpy(desc->port, port, sizeof(desc->port) - 1);
desc->port[sizeof(desc->port) - 1] = '\0';
const char *speed_s = strtok(NULL, ":");
if (!speed_s) {
// speed not specified (or parsing error)
free(cs);
return 2;
}
unsigned long speed;
if (sscanf(speed_s, "%10lu", &speed) != 1) {
// speed_s is not a number
free(cs);
return 2;
}
desc->speed = speed;
free(cs);
return 3;
}
static void static void
pn532_uart_close(nfc_device *pnd) pn532_uart_close(nfc_device *pnd)
{ {
@ -209,6 +160,7 @@ pn532_uart_close(nfc_device *pnd)
close(DRIVER_DATA(pnd)->iAbortFds[1]); close(DRIVER_DATA(pnd)->iAbortFds[1]);
#endif #endif
free(DRIVER_DATA(pnd)->port);
pn53x_data_free(pnd); pn53x_data_free(pnd);
nfc_device_free(pnd); nfc_device_free(pnd);
} }
@ -217,8 +169,18 @@ static nfc_device *
pn532_uart_open(const nfc_context *context, const nfc_connstring connstring) pn532_uart_open(const nfc_context *context, const nfc_connstring connstring)
{ {
struct pn532_uart_descriptor ndd; struct pn532_uart_descriptor ndd;
int connstring_decode_level = pn532_connstring_decode(connstring, &ndd); char *speed_s;
int connstring_decode_level = connstring_decode(connstring, PN532_UART_DRIVER_NAME, NULL, &ndd.port, &speed_s);
if (connstring_decode_level == 3) {
ndd.speed = 0;
if (sscanf(speed_s, "%10"PRIu32, &ndd.speed) != 1) {
// speed_s is not a number
free(ndd.port);
free(speed_s);
return NULL;
}
free(speed_s);
}
if (connstring_decode_level < 2) { if (connstring_decode_level < 2) {
return NULL; return NULL;
} }
@ -235,9 +197,10 @@ pn532_uart_open(const nfc_context *context, const nfc_connstring connstring)
log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_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) if (sp == CLAIMED_SERIAL_PORT)
log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_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)) if ((sp == CLAIMED_SERIAL_PORT) || (sp == INVALID_SERIAL_PORT)) {
free(ndd.port);
return NULL; return NULL;
}
// We need to flush input to be sure first reply does not comes from older byte transceive // We need to flush input to be sure first reply does not comes from older byte transceive
uart_flush_input(sp); uart_flush_input(sp);
uart_set_speed(sp, ndd.speed); uart_set_speed(sp, ndd.speed);
@ -246,6 +209,7 @@ pn532_uart_open(const nfc_context *context, const nfc_connstring connstring)
pnd = nfc_device_new(context, connstring); pnd = nfc_device_new(context, connstring);
if (!pnd) { if (!pnd) {
perror("malloc"); perror("malloc");
free(ndd.port);
return NULL; return NULL;
} }
snprintf(pnd->name, sizeof(pnd->name), "%s:%s", PN532_UART_DRIVER_NAME, ndd.port); snprintf(pnd->name, sizeof(pnd->name), "%s:%s", PN532_UART_DRIVER_NAME, ndd.port);
@ -253,6 +217,7 @@ pn532_uart_open(const nfc_context *context, const nfc_connstring connstring)
pnd->driver_data = malloc(sizeof(struct pn532_uart_data)); pnd->driver_data = malloc(sizeof(struct pn532_uart_data));
if (!pnd->driver_data) { if (!pnd->driver_data) {
perror("malloc"); perror("malloc");
free(ndd.port);
return NULL; return NULL;
} }
DRIVER_DATA(pnd)->port = sp; DRIVER_DATA(pnd)->port = sp;
@ -271,6 +236,7 @@ pn532_uart_open(const nfc_context *context, const nfc_connstring connstring)
#ifndef WIN32 #ifndef WIN32
// pipe-based abort mecanism // pipe-based abort mecanism
if (pipe(DRIVER_DATA(pnd)->iAbortFds) < 0) { if (pipe(DRIVER_DATA(pnd)->iAbortFds) < 0) {
free(ndd.port);
return NULL; return NULL;
} }
#else #else
@ -281,6 +247,7 @@ pn532_uart_open(const nfc_context *context, const nfc_connstring connstring)
if (pn53x_check_communication(pnd) < 0) { if (pn53x_check_communication(pnd) < 0) {
nfc_perror(pnd, "pn53x_check_communication"); nfc_perror(pnd, "pn53x_check_communication");
pn532_uart_close(pnd); pn532_uart_close(pnd);
free(ndd.port);
return NULL; return NULL;
} }

View file

@ -232,50 +232,6 @@ struct pn53x_usb_descriptor {
char *filename; char *filename;
}; };
static int
pn53x_usb_connstring_decode(const nfc_connstring connstring, struct pn53x_usb_descriptor *desc)
{
int n = strlen(connstring) + 1;
char *driver_name = malloc(n);
if (!driver_name) {
perror("malloc");
return 0;
}
char *dirname = malloc(n);
if (!dirname) {
perror("malloc");
free(driver_name);
return 0;
}
char *filename = malloc(n);
if (!filename) {
perror("malloc");
free(driver_name);
free(dirname);
return 0;
}
driver_name[0] = '\0';
char format[32];
snprintf(format, sizeof(format), "%%%i[^:]:%%%i[^:]:%%%i[^:]", n - 1, n - 1, n - 1);
int res = sscanf(connstring, format, driver_name, dirname, filename);
if (!res || ((0 != strcmp(driver_name, PN53X_USB_DRIVER_NAME)) && (0 != strcmp(driver_name, "usb")))) {
// Driver name does not match.
res = 0;
} else {
desc->dirname = strdup(dirname);
desc->filename = strdup(filename);
}
free(driver_name);
free(dirname);
free(filename);
return res;
}
bool bool
pn53x_usb_get_usb_device_name(struct usb_device *dev, usb_dev_handle *udev, char *buffer, size_t len) pn53x_usb_get_usb_device_name(struct usb_device *dev, usb_dev_handle *udev, char *buffer, size_t len)
{ {
@ -309,7 +265,7 @@ pn53x_usb_open(const nfc_context *context, const nfc_connstring connstring)
{ {
nfc_device *pnd = NULL; nfc_device *pnd = NULL;
struct pn53x_usb_descriptor desc = { NULL, NULL }; struct pn53x_usb_descriptor desc = { NULL, NULL };
int connstring_decode_level = pn53x_usb_connstring_decode(connstring, &desc); int connstring_decode_level = connstring_decode(connstring, PN53X_USB_DRIVER_NAME, "usb", &desc.dirname, &desc.filename);
log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG, "%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) { if (connstring_decode_level < 1) {
goto free_mem; goto free_mem;

33
libnfc/log-internal.h Normal file
View file

@ -0,0 +1,33 @@
/*-
* Copyright (C) 2013 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 <http://www.gnu.org/licenses/>
*/
#ifndef __LOG_INTERNAL_H__
#define __LOG_INTERNAL_H__
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif // HAVE_CONFIG_H
#include <stdarg.h>
// Internal methods so different platforms can route the logging
// Offering both forms of the variadic function
// These are implemented in the log_<platform> specific file
void log_put_internal(const char *format, ...);
void log_vput_internal(const char *format, va_list args);
#endif // __LOG_INTERNAL_H__

View file

@ -1,92 +0,0 @@
/*-
* Copyright (C) 2011 Romain Tartière
* Copyright (C) 2011, 2012 Romuald Conty
* Copyright (C) 2013 Alex Lian
*
* 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 <http://www.gnu.org/licenses/>
*/
#include "log.h"
#include <stdlib.h>
#include <stdint.h>
#include <inttypes.h>
#include <stdio.h>
#include <stdarg.h>
#include <fcntl.h>
#ifndef LOG
// Leaving in a preprocessor error, as the build system should skip this
// file otherwise.
#error "No logging defined, but log-printf.c still compiled."
#else // LOG
// Internal methods so different platforms can route the logging
// Offering both forms of the variadic function
// These are implemented in the log_<platform> specific file
void log_put_internal(const char *format, ...);
void log_vput_internal(const char *format, va_list args);
void
log_init(const nfc_context *context)
{
#ifdef ENVVARS
char str[32];
sprintf(str, "%"PRIu32, context->log_level);
setenv("LIBNFC_LOG_LEVEL", str, 1);
#else
(void)context;
#endif
}
void
log_exit(void)
{
}
void
log_put(const uint8_t group, const char *category, const uint8_t priority, const char *format, ...)
{
char *env_log_level = NULL;
#ifdef ENVVARS
env_log_level = getenv("LIBNFC_LOG_LEVEL");
#endif
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);
log_put_internal("%s\t%s\t", log_priority_to_str(priority), category);
log_vput_internal(format, va);
log_put_internal("\n");
va_end(va);
}
}
}
#endif // LOG

View file

@ -16,12 +16,13 @@
*/ */
#include "log.h" #include "log.h"
/* #include <string.h>
#include <stdlib.h>
#include <stdint.h> #include <stdint.h>
#include <inttypes.h>
#include <stdio.h> #include <stdio.h>
#include <stdarg.h> #include <stdarg.h>
*/ #include <fcntl.h>
#include <string.h>
/* /*
int int
@ -70,3 +71,60 @@ log_priority_to_str(const int priority)
return "unknown"; return "unknown";
} }
#ifdef LOG
#include "log-internal.h"
void
log_init(const nfc_context *context)
{
#ifdef ENVVARS
char str[32];
sprintf(str, "%"PRIu32, context->log_level);
setenv("LIBNFC_LOG_LEVEL", str, 1);
#else
(void)context;
#endif
}
void
log_exit(void)
{
}
void
log_put(const uint8_t group, const char *category, const uint8_t priority, const char *format, ...)
{
char *env_log_level = NULL;
#ifdef ENVVARS
env_log_level = getenv("LIBNFC_LOG_LEVEL");
#endif
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);
log_put_internal("%s\t%s\t", log_priority_to_str(priority), category);
log_vput_internal(format, va);
log_put_internal("\n");
va_end(va);
}
}
}
#endif // LOG

View file

@ -70,7 +70,6 @@ void log_put(const uint8_t group, const char *category, const uint8_t priority,
__attribute__((format(printf, 4, 5))) __attribute__((format(printf, 4, 5)))
# endif # endif
; ;
#else #else
// No logging // No logging
#define log_init(nfc_context) ((void) 0) #define log_init(nfc_context) ((void) 0)

View file

@ -17,11 +17,8 @@
* along with this program. If not, see <http://www.gnu.org/licenses/> * along with this program. If not, see <http://www.gnu.org/licenses/>
*/ */
#include "log.h" #include "log-internal.h"
#include <stdlib.h>
#include <stdint.h>
#include <inttypes.h>
#include <stdio.h> #include <stdio.h>
#include <stdarg.h> #include <stdarg.h>

View file

@ -17,16 +17,13 @@
* along with this program. If not, see <http://www.gnu.org/licenses/> * along with this program. If not, see <http://www.gnu.org/licenses/>
*/ */
#include "log.h" #include "log-internal.h"
#include <stdlib.h>
#include <stdint.h>
#include <inttypes.h>
#include <stdio.h> #include <stdio.h>
#include <stdarg.h> #include <stdarg.h>
#include <strsafe.h> #include <strsafe.h>
void static void
log_output_debug(const char *format, va_list args) log_output_debug(const char *format, va_list args)
{ {
char buffer[1024]; char buffer[1024];

View file

@ -185,3 +185,67 @@ prepare_initiator_data(const nfc_modulation nm, uint8_t **ppbtInitiatorData, siz
break; break;
} }
} }
int
connstring_decode(const nfc_connstring connstring, const char *driver_name, const char *bus_name, char **pparam1, char **pparam2)
{
if (driver_name == NULL) {
driver_name = "";
}
if (bus_name == NULL) {
bus_name = "";
}
int n = strlen(connstring) + 1;
char *param0 = malloc(n);
if (param0 == NULL) {
perror("malloc");
return 0;
}
char *param1 = malloc(n);
if (param1 == NULL) {
perror("malloc");
free(param0);
return 0;
}
char *param2 = malloc(n);
if (param2 == NULL) {
perror("malloc");
free(param0);
free(param1);
return 0;
}
char format[32];
snprintf(format, sizeof(format), "%%%i[^:]:%%%i[^:]:%%%i[^:]", n - 1, n - 1, n - 1);
int res = sscanf(connstring, format, param0, param1, param2);
if (res < 1 || ((0 != strcmp(param0, driver_name)) &&
(bus_name != NULL) &&
(0 != strcmp(param0, bus_name)))) {
// Driver name does not match.
res = 0;
}
if (pparam1 != NULL) {
if (res < 2) {
free(param1);
*pparam1 = NULL;
} else {
*pparam1 = param1;
}
} else {
free(param1);
}
if (pparam2 != NULL) {
if (res < 3) {
free(param2);
*pparam2 = NULL;
} else {
*pparam2 = param2;
}
} else {
free(param2);
}
free(param0);
return res;
}

View file

@ -215,4 +215,6 @@ void iso14443_cascade_uid(const uint8_t abtUID[], const size_t szUID, uint8_t *p
void prepare_initiator_data(const nfc_modulation nm, uint8_t **ppbtInitiatorData, size_t *pszInitiatorData); void prepare_initiator_data(const nfc_modulation nm, uint8_t **ppbtInitiatorData, size_t *pszInitiatorData);
int connstring_decode(const nfc_connstring connstring, const char *driver_name, const char *bus_name, char **pparam1, char **pparam2);
#endif // __NFC_INTERNAL_H__ #endif // __NFC_INTERNAL_H__