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

@ -66,16 +66,16 @@ main (int argc, const char *argv[])
size_t szDeviceFound;
byte_t abtTx[] = "Hello Mars!";
#define MAX_DEVICE_COUNT 2
nfc_device_desc_t pnddDevices[MAX_DEVICE_COUNT];
nfc_list_devices (pnddDevices, MAX_DEVICE_COUNT, &szDeviceFound);
nfc_connstring connstrings[MAX_DEVICE_COUNT];
nfc_list_devices (connstrings, MAX_DEVICE_COUNT, &szDeviceFound);
// Little hack to allow using nfc-dep-initiator & nfc-dep-target from
// the same machine: if there is more than one readers connected
// nfc-dep-target will connect to the second reader
// (we hope they're always detected in the same order)
if (szDeviceFound == 1) {
pnd = nfc_connect (&(pnddDevices[0]));
pnd = nfc_connect (connstrings[0]);
} else if (szDeviceFound > 1) {
pnd = nfc_connect (&(pnddDevices[1]));
pnd = nfc_connect (connstrings[1]);
} else {
printf("No device found.");
return EXIT_FAILURE;

View file

@ -65,15 +65,10 @@ void stop_polling (int sig)
int
main (int argc, const char *argv[])
{
size_t szFound;
size_t i;
bool verbose = false;
nfc_device_desc_t *pnddDevices;
signal (SIGINT, stop_polling);
pnddDevices = parse_args (argc, argv, &szFound, &verbose);
// Display libnfc version
const char *acLibnfcVersion = nfc_version ();
@ -83,59 +78,43 @@ main (int argc, const char *argv[])
printf ("%s uses libnfc %s\n", argv[0], acLibnfcVersion);
if (szFound == 0) {
if (!(pnddDevices = malloc (MAX_DEVICE_COUNT * sizeof (*pnddDevices)))) {
fprintf (stderr, "malloc() failed\n");
exit (EXIT_FAILURE);
}
const uint8_t uiPollNr = 20;
const uint8_t uiPeriod = 2;
const nfc_modulation_t nmModulations[5] = {
{ .nmt = NMT_ISO14443A, .nbr = NBR_106 },
{ .nmt = NMT_ISO14443B, .nbr = NBR_106 },
{ .nmt = NMT_FELICA, .nbr = NBR_212 },
{ .nmt = NMT_FELICA, .nbr = NBR_424 },
{ .nmt = NMT_JEWEL, .nbr = NBR_106 },
};
const size_t szModulations = 5;
nfc_target_t nt;
bool res;
pnd = nfc_connect (NULL);
if (pnd == NULL) {
ERR ("%s", "Unable to connect to NFC device.");
exit (EXIT_FAILURE);
}
nfc_list_devices (pnddDevices, MAX_DEVICE_COUNT, &szFound);
nfc_initiator_init (pnd);
if (szFound == 0) {
printf ("No NFC device found.\n");
}
for (i = 0; i < szFound; i++) {
const uint8_t uiPollNr = 20;
const uint8_t uiPeriod = 2;
const nfc_modulation_t nmModulations[5] = {
{ .nmt = NMT_ISO14443A, .nbr = NBR_106 },
{ .nmt = NMT_ISO14443B, .nbr = NBR_106 },
{ .nmt = NMT_FELICA, .nbr = NBR_212 },
{ .nmt = NMT_FELICA, .nbr = NBR_424 },
{ .nmt = NMT_JEWEL, .nbr = NBR_106 },
};
const size_t szModulations = 5;
nfc_target_t nt;
bool res;
pnd = nfc_connect (&(pnddDevices[i]));
if (pnd == NULL) {
ERR ("%s", "Unable to connect to NFC device.");
printf ("Connected to NFC reader: %s\n", pnd->acName);
printf ("NFC device will poll during %ld ms (%u pollings of %lu ms for %zd modulations)\n", (unsigned long) uiPollNr * szModulations * uiPeriod * 150, uiPollNr, (unsigned long) uiPeriod * 150, szModulations);
res = nfc_initiator_poll_target (pnd, nmModulations, szModulations, uiPollNr, uiPeriod, &nt);
if (res) {
print_nfc_target ( nt, verbose );
} else {
if (pnd->iLastError) {
nfc_perror (pnd, "nfc_initiator_poll_targets");
nfc_disconnect (pnd);
exit (EXIT_FAILURE);
}
nfc_initiator_init (pnd);
printf ("Connected to NFC reader: %s\n", pnd->acName);
printf ("NFC device will poll during %ld ms (%u pollings of %lu ms for %zd modulations)\n", (unsigned long) uiPollNr * szModulations * uiPeriod * 150, uiPollNr, (unsigned long) uiPeriod * 150, szModulations);
res = nfc_initiator_poll_target (pnd, nmModulations, szModulations, uiPollNr, uiPeriod, &nt);
if (res) {
print_nfc_target ( nt, verbose );
} else {
if (pnd->iLastError) {
nfc_perror (pnd, "nfc_initiator_poll_targets");
nfc_disconnect (pnd);
exit (EXIT_FAILURE);
} else {
printf ("No target found.\n");
}
printf ("No target found.\n");
}
nfc_disconnect (pnd);
}
free (pnddDevices);
nfc_disconnect (pnd);
exit (EXIT_SUCCESS);
}

View file

@ -83,7 +83,6 @@ main (int argc, char *argv[])
int arg;
bool quiet_output = false;
size_t szFound;
nfc_device_desc_t *pnddDevices;
const char *acLibnfcVersion = nfc_version ();
// Get commandline options
@ -109,20 +108,16 @@ 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 (szFound < 2) {
ERR ("%zd device found but two connected devices are needed to relay NFC.", szFound);
return EXIT_FAILURE;
}
// Try to open the NFC emulator device
pndTag = nfc_connect (&(pnddDevices[0]));
pndTag = nfc_connect (connstrings[0]);
if (pndTag == NULL) {
printf ("Error connecting NFC emulator device\n");
return EXIT_FAILURE;
@ -165,7 +160,7 @@ main (int argc, char *argv[])
printf ("%s", "Done, emulated tag is initialized");
// Try to open the NFC reader
pndReader = nfc_connect (&(pnddDevices[1]));
pndReader = nfc_connect (connstrings[1]);
printf ("Connected to the NFC reader device: %s", pndReader->acName);
printf ("%s", "Configuring NFC reader settings...");

View file

@ -55,7 +55,6 @@ main (int argc, const char *argv[])
size_t szFound;
size_t i;
nfc_device_t *pnd;
nfc_device_desc_t *pnddDevices;
const char *acLibnfcVersion;
bool result;
@ -72,19 +71,15 @@ main (int argc, const char *argv[])
acLibnfcVersion = nfc_version ();
printf ("%s uses libnfc %s\n", argv[0], acLibnfcVersion);
if (!(pnddDevices = malloc (MAX_DEVICE_COUNT * sizeof (*pnddDevices)))) {
fprintf (stderr, "malloc() failed\n");
return EXIT_FAILURE;
}
nfc_list_devices (pnddDevices, MAX_DEVICE_COUNT, &szFound);
nfc_connstring connstrings[MAX_DEVICE_COUNT];
nfc_list_devices (connstrings, MAX_DEVICE_COUNT, &szFound);
if (szFound == 0) {
printf ("No NFC device found.\n");
}
for (i = 0; i < szFound; i++) {
pnd = nfc_connect (&(pnddDevices[i]));
pnd = nfc_connect (connstrings[i]);
if (pnd == NULL) {
ERR ("%s", "Unable to connect to NFC device.");