Allow to connect to a device using a connection string:

- Provide a nfc_get_default_device() that allow to grab the connstring stored
   in LIBNFC_DEFAULT_DEVICE environnement variable or returns the first
   available device if not set;
 - nfc_connect(NULL) now takes the default device (see
   nfc_get_default_device());
 - Removes nfc_driver_desc_t from public types
 - Defines nfc_connstring as char[1024]
 - examples/*: use nfc_connstring
 - examples/nfc-poll: now uses only the default device (instead of all devices
   availables)
 - Removes parse_args() from nfc-utils.[hc]
This commit is contained in:
Romuald Conty 2011-10-17 13:03:56 +00:00
parent dc842a844c
commit 55daa29a7c
20 changed files with 559 additions and 380 deletions

View file

@ -62,18 +62,16 @@ static nfc_device_t *pnd;
int
main (int argc, const char *argv[])
{
(void) argc;
const char *acLibnfcVersion;
size_t szDeviceFound;
size_t szTargetFound;
size_t i;
bool verbose = false;
nfc_device_desc_t *pnddDevices;
// Display libnfc version
acLibnfcVersion = nfc_version ();
printf ("%s uses libnfc %s\n", argv[0], acLibnfcVersion);
pnddDevices = parse_args (argc, argv, &szDeviceFound, &verbose);
#ifdef HAVE_LIBUSB
# ifdef DEBUG
usb_set_debug (4);
@ -101,15 +99,9 @@ main (int argc, const char *argv[])
strcpy(ndd.acDevice, "SCM Micro / SCL3711-NFC&RW");
pnd = nfc_connect (&ndd);
#endif
if (szDeviceFound == 0) {
if (!(pnddDevices = malloc (MAX_DEVICE_COUNT * sizeof (*pnddDevices)))) {
fprintf (stderr, "malloc() failed\n");
return EXIT_FAILURE;
}
nfc_list_devices (pnddDevices, MAX_DEVICE_COUNT, &szDeviceFound);
}
size_t szDeviceFound;
nfc_connstring connstrings[MAX_DEVICE_COUNT];
nfc_list_devices (connstrings, MAX_DEVICE_COUNT, &szDeviceFound);
if (szDeviceFound == 0) {
printf ("No NFC device found.\n");
@ -117,7 +109,7 @@ main (int argc, const char *argv[])
for (i = 0; i < szDeviceFound; i++) {
nfc_target_t ant[MAX_TARGET_COUNT];
pnd = nfc_connect (&(pnddDevices[i]));
pnd = nfc_connect (connstrings[i]);
if (pnd == NULL) {
ERR ("%s", "Unable to connect to NFC device.");
@ -243,6 +235,5 @@ main (int argc, const char *argv[])
nfc_disconnect (pnd);
}
free (pnddDevices);
return 0;
}

View file

@ -150,7 +150,6 @@ main (int argc, char *argv[])
{
int arg;
size_t szFound;
nfc_device_desc_t *pnddDevices;
const char *acLibnfcVersion = nfc_version ();
nfc_target_t ntRealTarget;
@ -192,13 +191,9 @@ main (int argc, char *argv[])
signal (SIGINT, (void (*)()) intr_hdlr);
#endif
// Allocate memory to put the result of available devices listing
if (!(pnddDevices = malloc (MAX_DEVICE_COUNT * sizeof (*pnddDevices)))) {
fprintf (stderr, "malloc() failed\n");
return EXIT_FAILURE;
}
nfc_connstring connstrings[MAX_DEVICE_COUNT];
// List available devices
nfc_list_devices (pnddDevices, MAX_DEVICE_COUNT, &szFound);
nfc_list_devices (connstrings, MAX_DEVICE_COUNT, &szFound);
if (initiator_only_mode || target_only_mode) {
if (szFound < 1) {
@ -222,9 +217,9 @@ main (int argc, char *argv[])
// if there is more than one readers connected we connect to the second reader
// (we hope they're always detected in the same order)
if (szFound == 1) {
pndInitiator = nfc_connect (&(pnddDevices[0]));
pndInitiator = nfc_connect (connstrings[0]);
} else {
pndInitiator = nfc_connect (&(pnddDevices[1]));
pndInitiator = nfc_connect (connstrings[1]);
}
if (!pndInitiator) {
@ -348,7 +343,7 @@ main (int argc, char *argv[])
print_nfc_iso14443a_info (ntEmulatedTarget.nti.nai, false);
// Try to open the NFC emulator device
pndTarget = nfc_connect (&(pnddDevices[0]));
pndTarget = nfc_connect (connstrings[0]);
if (pndTarget == NULL) {
printf ("Error connecting NFC emulator device\n");
if (!target_only_mode) {

View file

@ -670,51 +670,6 @@ print_nfc_dep_info (const nfc_dep_info_t ndi, bool verbose)
}
}
/**
* @brief Tries to parse arguments to find device descriptions.
* @return Returns the list of found device descriptions.
*/
nfc_device_desc_t *
parse_args (int argc, const char *argv[], size_t * szFound, bool * verbose)
{
nfc_device_desc_t *pndd = 0;
int arg;
*szFound = 0;
// Get commandline options
for (arg = 1; arg < argc; arg++) {
if (0 == strcmp (argv[arg], "--device")) {
// FIXME: this device selection by command line options is terrible & does not support USB/PCSC drivers
if (argc > arg + 1) {
char buffer[256];
pndd = malloc (sizeof (nfc_device_desc_t));
strncpy (buffer, argv[++arg], 256);
// Driver.
pndd->pcDriver = (char *) malloc (256);
strcpy (pndd->pcDriver, strtok (buffer, ":"));
// Port.
strcpy (pndd->acPort, strtok (NULL, ":"));
// Speed.
sscanf (strtok (NULL, ":"), "%u", &pndd->uiSpeed);
*szFound = 1;
} else {
errx (1, "usage: %s [--device driver:port:speed]", argv[0]);
}
}
if ((0 == strcmp (argv[arg], "-v")) || (0 == strcmp (argv[arg], "--verbose"))) {
*verbose = true;
}
}
return pndd;
}
const char *
str_nfc_baud_rate (const nfc_baud_rate_t nbr)
{

View file

@ -97,6 +97,4 @@ void print_nfc_dep_info (const nfc_dep_info_t ndi, bool verbose);
void print_nfc_target (const nfc_target_t nt, bool verbose);
nfc_device_desc_t *parse_args (int argc, const char *argv[], size_t * szFound, bool * verbose);
#endif