add nfc_device_get_connstring() function and nfc-probe example to show devices connection strings

This commit is contained in:
Romuald Conty 2012-01-18 13:17:01 +00:00
parent 39216f9d7c
commit b366b8c027
10 changed files with 159 additions and 19 deletions

View file

@ -102,6 +102,7 @@ extern "C" {
/* Special data accessors */
NFC_EXPORT const char *nfc_device_get_name (nfc_device *pnd);
NFC_EXPORT const char *nfc_device_get_connstring (nfc_device *pnd);
/* Properties accessors */
NFC_EXPORT int nfc_device_set_property_int (nfc_device *pnd, const nfc_property property, const int value);

View file

@ -85,8 +85,8 @@ pn53x_init(struct nfc_device *pnd)
// Add the firmware revision to the device name
char *pcName;
pcName = strdup (pnd->acName);
snprintf (pnd->acName, DEVICE_NAME_LENGTH - 1, "%s - %s", pcName, abtFirmwareText);
pcName = strdup (pnd->name);
snprintf (pnd->name, DEVICE_NAME_LENGTH - 1, "%s - %s", pcName, abtFirmwareText);
free (pcName);
return NFC_SUCCESS;
}

View file

@ -120,7 +120,10 @@ arygon_probe (nfc_connstring connstrings[], size_t connstrings_len, size_t *pszD
uart_flush_input (sp);
uart_set_speed (sp, ARYGON_DEFAULT_SPEED);
nfc_device *pnd = nfc_device_new ();
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);
pnd->driver = &arygon_driver;
pnd->driver_data = malloc(sizeof(struct arygon_data));
DRIVER_DATA (pnd)->port = sp;
@ -144,7 +147,7 @@ arygon_probe (nfc_connstring connstrings[], size_t connstrings_len, size_t *pszD
}
// ARYGON reader is found
snprintf (connstrings[*pszDeviceFound], sizeof(nfc_connstring), "%s:%s:%"PRIu32, ARYGON_DRIVER_NAME, acPort, ARYGON_DEFAULT_SPEED);
memcpy (connstrings[*pszDeviceFound], connstring, sizeof(nfc_connstring));
(*pszDeviceFound)++;
// Test if we reach the maximum "wanted" devices
@ -245,8 +248,8 @@ arygon_open (const nfc_connstring connstring)
uart_set_speed (sp, ndd.speed);
// We have a connection
pnd = nfc_device_new ();
snprintf (pnd->acName, sizeof (pnd->acName), "%s:%s", ARYGON_DRIVER_NAME, ndd.port);
pnd = nfc_device_new (connstring);
snprintf (pnd->name, sizeof (pnd->name), "%s:%s", ARYGON_DRIVER_NAME, ndd.port);
pnd->driver_data = malloc(sizeof(struct arygon_data));
DRIVER_DATA (pnd)->port = sp;
@ -277,8 +280,8 @@ arygon_open (const nfc_connstring connstring)
char arygon_firmware_version[10];
arygon_firmware (pnd, arygon_firmware_version);
char *pcName;
pcName = strdup (pnd->acName);
snprintf (pnd->acName, sizeof (pnd->acName), "%s %s", pcName, arygon_firmware_version);
pcName = strdup (pnd->name);
snprintf (pnd->name, sizeof (pnd->name), "%s %s", pcName, arygon_firmware_version);
free (pcName);
pn53x_init(pnd);

View file

@ -94,7 +94,9 @@ pn532_uart_probe (nfc_connstring connstrings[], size_t connstrings_len, size_t *
// Serial port claimed but we need to check if a PN532_UART is opened.
uart_set_speed (sp, PN532_UART_DEFAULT_SPEED);
nfc_device *pnd = nfc_device_new ();
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);
pnd->driver = &pn532_uart_driver;
pnd->driver_data = malloc(sizeof(struct pn532_uart_data));
DRIVER_DATA (pnd)->port = sp;
@ -122,7 +124,7 @@ pn532_uart_probe (nfc_connstring connstrings[], size_t connstrings_len, size_t *
continue;
}
snprintf (connstrings[*pszDeviceFound], sizeof(nfc_connstring), "%s:%s:%"PRIu32, PN532_UART_DRIVER_NAME, acPort, PN532_UART_DEFAULT_SPEED);
memcpy (connstrings[*pszDeviceFound], connstring, sizeof (nfc_connstring));
(*pszDeviceFound)++;
// Test if we reach the maximum "wanted" devices
@ -223,8 +225,8 @@ pn532_uart_open (const nfc_connstring connstring)
uart_set_speed (sp, ndd.speed);
// We have a connection
pnd = nfc_device_new ();
snprintf (pnd->acName, sizeof (pnd->acName), "%s:%s", PN532_UART_DRIVER_NAME, ndd.port);
pnd = nfc_device_new (connstring);
snprintf (pnd->name, sizeof (pnd->name), "%s:%s", PN532_UART_DRIVER_NAME, ndd.port);
pnd->driver_data = malloc(sizeof(struct pn532_uart_data));
DRIVER_DATA (pnd)->port = sp;

View file

@ -382,8 +382,8 @@ pn53x_usb_open (const nfc_connstring connstring)
}
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 ();
pn53x_usb_get_usb_device_name (dev, data.pudh, pnd->acName, sizeof (pnd->acName));
pnd = nfc_device_new (connstring);
pn53x_usb_get_usb_device_name (dev, data.pudh, pnd->name, sizeof (pnd->name));
pnd->driver_data = malloc(sizeof(struct pn53x_usb_data));
*DRIVER_DATA (pnd) = data;

View file

@ -25,6 +25,7 @@
/* vim:set et sw=2 ts=2: */
#include <stdlib.h>
#include <string.h>
#ifdef HAVE_CONFIG_H
# include "config.h"
@ -33,7 +34,7 @@
#include "nfc-internal.h"
nfc_device *
nfc_device_new (void)
nfc_device_new (const nfc_connstring connstring)
{
nfc_device *res = malloc (sizeof (*res));
@ -50,6 +51,7 @@ nfc_device_new (void)
res->bEasyFraming = false;
res->bAutoIso14443_4 = false;
res->last_error = 0;
memcpy (res->connstring, connstring, sizeof (res->connstring));
res->driver_data = NULL;
res->chip_data = NULL;

View file

@ -168,7 +168,9 @@ struct nfc_device {
void *chip_data;
/** Device name string, including device wrapper firmware */
char acName[DEVICE_NAME_LENGTH];
char name[DEVICE_NAME_LENGTH];
/** Device connection string */
nfc_connstring connstring;
/** Is the CRC automaticly added, checked and removed from the frames */
bool bCrc;
/** Does the chip handle parity bits, all parities are handled as data */
@ -184,7 +186,7 @@ struct nfc_device {
int last_error;
};
nfc_device *nfc_device_new (void);
nfc_device *nfc_device_new (const nfc_connstring connstring);
void nfc_device_free (nfc_device *dev);
void iso14443_cascade_uid (const uint8_t abtUID[], const size_t szUID, uint8_t * pbtCascadedUID, size_t * pszCascadedUID);

View file

@ -163,7 +163,7 @@ nfc_open (const nfc_connstring connstring)
return pnd;
}
log_put (LOG_CATEGORY, NFC_PRIORITY_TRACE, "[%s] has been claimed.", pnd->acName);
log_put (LOG_CATEGORY, NFC_PRIORITY_TRACE, "\"%s\" (%s) has been claimed.", pnd->name, pnd->connstring);
log_fini ();
return pnd;
}
@ -898,7 +898,17 @@ nfc_device_get_last_error (const nfc_device *pnd)
const char *
nfc_device_get_name (nfc_device *pnd)
{
return pnd->acName;
return pnd->name;
}
/**
* @brief Returns the device connection string
* @return Returns a string with the device connstring
*/
const char *
nfc_device_get_connstring (nfc_device *pnd)
{
return pnd->connstring;
}
/* Misc. functions */

View file

@ -4,6 +4,7 @@ bin_PROGRAMS = \
nfc-mfclassic \
nfc-mfsetuid \
nfc-mfultralight \
nfc-probe \
nfc-read-forum-tag3 \
nfc-relay-picc
@ -36,6 +37,10 @@ nfc_mfsetuid_LDADD = $(top_builddir)/libnfc/libnfc.la \
nfc_mfultralight_SOURCES = nfc-mfultralight.c mifare.c mifare.h
nfc_mfultralight_LDADD = $(top_builddir)/libnfc/libnfc.la
nfc_probe_SOURCES = nfc-probe.c
nfc_probe_LDADD = $(top_builddir)/libnfc/libnfc.la \
libnfcutils.la
nfc_read_forum_tag3_SOURCES = nfc-read-forum-tag3.c
nfc_read_forum_tag3_LDADD = $(top_builddir)/libnfc/libnfc.la \
libnfcutils.la

115
utils/nfc-probe.c Normal file
View file

@ -0,0 +1,115 @@
/*-
* Public platform independent Near Field Communication (NFC) library examples
*
* Copyright (C) 2009, Roel Verdult
* Copyright (C) 2010, Romuald Conty, Romain Tartière
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* 1) Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2 )Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* Note that this license only applies on the examples, NFC library itself is under LGPL
*
*/
/**
* @file nfc-probe.c
* @brief Lists each available NFC device
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif // HAVE_CONFIG_H
#ifdef HAVE_LIBUSB
# ifdef DEBUG
# include <sys/param.h>
# include <usb.h>
# endif
#endif
#include <err.h>
#include <stdio.h>
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <nfc/nfc.h>
#include "nfc-utils.h"
#define MAX_DEVICE_COUNT 16
#define MAX_TARGET_COUNT 16
static nfc_device *pnd;
void
print_usage (const char* progname)
{
printf ("usage: %s [-v]\n", progname);
printf (" -v\t verbose display\n");
}
int
main (int argc, const char *argv[])
{
(void) argc;
const char *acLibnfcVersion;
size_t i;
bool verbose = false;
nfc_init ();
// Display libnfc version
acLibnfcVersion = nfc_version ();
printf ("%s uses libnfc %s\n", argv[0], acLibnfcVersion);
if (argc != 1) {
if ((argc == 2) && (0 == strcmp ("-v", argv[1]))) {
verbose = true;
} else {
print_usage (argv[0]);
exit (EXIT_FAILURE);
}
}
#ifdef HAVE_LIBUSB
# ifdef DEBUG
usb_set_debug (4);
# endif
#endif
nfc_connstring connstrings[MAX_DEVICE_COUNT];
size_t szDeviceFound = nfc_list_devices (connstrings, MAX_DEVICE_COUNT);
if (szDeviceFound == 0) {
printf ("No NFC device found.\n");
}
printf ("%d NFC device(s) found:\n", szDeviceFound);
for (i = 0; i < szDeviceFound; i++) {
pnd = nfc_open (connstrings[i]);
if (pnd != NULL) {
printf ("- %s:\n %s\n", nfc_device_get_name (pnd), nfc_device_get_connstring (pnd));
}
nfc_close (pnd);
}
nfc_exit ();
return 0;
}