2010-01-15 11:18:11 +01:00
|
|
|
/*-
|
2010-11-17 15:27:11 +01:00
|
|
|
* Public platform independent Near Field Communication (NFC) library examples
|
2009-10-12 16:52:26 +02:00
|
|
|
*
|
2010-11-17 15:27:11 +01:00
|
|
|
* Copyright (C) 2009, Roel Verdult
|
|
|
|
* Copyright (C) 2010, Romuald Conty, Romain Tartière
|
2009-10-12 16:52:26 +02:00
|
|
|
*
|
2010-11-17 15:27:11 +01:00
|
|
|
* 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.
|
2009-10-12 16:52:26 +02:00
|
|
|
*
|
2010-11-17 15:27:11 +01:00
|
|
|
* Note that this license only applies on the examples, NFC library itself is under LGPL
|
2009-10-12 16:52:26 +02:00
|
|
|
*
|
2010-01-15 11:18:11 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2009-10-12 16:52:26 +02:00
|
|
|
* @file nfc-list.c
|
2010-10-14 13:53:27 +02:00
|
|
|
* @brief Lists the first target present of each founded device
|
2009-10-12 16:52:26 +02:00
|
|
|
*/
|
2009-05-27 14:18:21 +02:00
|
|
|
|
2010-01-15 11:18:11 +01:00
|
|
|
#ifdef HAVE_CONFIG_H
|
2010-09-07 19:51:03 +02:00
|
|
|
# include "config.h"
|
2010-01-15 11:18:11 +01:00
|
|
|
#endif // HAVE_CONFIG_H
|
|
|
|
|
2009-12-07 22:13:36 +01:00
|
|
|
#ifdef HAVE_LIBUSB
|
2010-09-07 19:51:03 +02:00
|
|
|
# ifdef DEBUG
|
|
|
|
# include <usb.h>
|
|
|
|
# endif
|
2009-12-07 22:13:36 +01:00
|
|
|
#endif
|
|
|
|
|
2010-04-07 17:08:04 +02:00
|
|
|
#include <err.h>
|
2009-05-27 14:18:21 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stddef.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2009-12-01 15:23:00 +01:00
|
|
|
#include <nfc/nfc.h>
|
2011-03-05 09:53:46 +01:00
|
|
|
|
2010-04-10 00:08:17 +02:00
|
|
|
#include "nfc-utils.h"
|
2009-05-27 14:18:21 +02:00
|
|
|
|
2009-11-24 18:49:24 +01:00
|
|
|
#define MAX_DEVICE_COUNT 16
|
2010-07-30 15:27:03 +02:00
|
|
|
#define MAX_TARGET_COUNT 16
|
2009-11-24 18:49:24 +01:00
|
|
|
|
2011-11-23 16:55:40 +01:00
|
|
|
static nfc_device *pnd;
|
2009-05-27 14:18:21 +02:00
|
|
|
|
2012-05-13 15:03:47 +02:00
|
|
|
static void
|
2012-01-05 15:49:02 +01:00
|
|
|
print_usage (const char* progname)
|
2012-01-04 22:29:43 +01:00
|
|
|
{
|
|
|
|
printf ("usage: %s [-v]\n", progname);
|
|
|
|
printf (" -v\t verbose display\n");
|
|
|
|
}
|
2009-05-27 14:18:21 +02:00
|
|
|
|
2010-09-07 19:51:03 +02:00
|
|
|
int
|
|
|
|
main (int argc, const char *argv[])
|
2009-08-28 18:54:04 +02:00
|
|
|
{
|
2011-10-17 15:03:56 +02:00
|
|
|
(void) argc;
|
2010-09-07 19:51:03 +02:00
|
|
|
const char *acLibnfcVersion;
|
|
|
|
size_t i;
|
2010-10-19 14:50:52 +02:00
|
|
|
bool verbose = false;
|
2011-12-15 12:46:14 +01:00
|
|
|
int res = 0;
|
2010-09-07 19:51:03 +02:00
|
|
|
|
2012-01-18 17:22:06 +01:00
|
|
|
nfc_init (NULL);
|
2012-01-18 12:09:01 +01:00
|
|
|
|
2009-11-24 18:49:24 +01:00
|
|
|
// Display libnfc version
|
2010-09-07 19:51:03 +02:00
|
|
|
acLibnfcVersion = nfc_version ();
|
2011-05-25 21:53:45 +02:00
|
|
|
printf ("%s uses libnfc %s\n", argv[0], acLibnfcVersion);
|
2012-01-04 22:29:43 +01:00
|
|
|
if (argc != 1) {
|
|
|
|
if ((argc == 2) && (0 == strcmp ("-v", argv[1]))) {
|
|
|
|
verbose = true;
|
|
|
|
} else {
|
|
|
|
print_usage (argv[0]);
|
|
|
|
exit (EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
}
|
2009-11-20 14:43:18 +01:00
|
|
|
|
2010-09-07 19:51:03 +02:00
|
|
|
#ifdef HAVE_LIBUSB
|
|
|
|
# ifdef DEBUG
|
|
|
|
usb_set_debug (4);
|
|
|
|
# endif
|
|
|
|
#endif
|
2009-12-07 22:13:36 +01:00
|
|
|
|
2010-01-12 12:50:09 +01:00
|
|
|
/* Lazy way to open an NFC device */
|
|
|
|
#if 0
|
2012-01-18 17:22:06 +01:00
|
|
|
pnd = nfc_open (NULL, NULL);
|
2010-01-12 12:50:09 +01:00
|
|
|
#endif
|
2009-09-04 15:24:34 +02:00
|
|
|
|
2010-01-12 12:50:09 +01:00
|
|
|
/* If specific device is wanted, i.e. an ARYGON device on /dev/ttyUSB0 */
|
|
|
|
#if 0
|
2009-10-02 11:52:02 +02:00
|
|
|
nfc_device_desc_t ndd;
|
|
|
|
ndd.pcDriver = "ARYGON";
|
|
|
|
ndd.pcPort = "/dev/ttyUSB0";
|
|
|
|
ndd.uiSpeed = 115200;
|
2012-01-18 17:22:06 +01:00
|
|
|
pnd = nfc_open (NULL, &ndd);
|
2010-10-05 09:53:19 +02:00
|
|
|
#endif
|
2009-09-04 15:24:34 +02:00
|
|
|
|
2010-10-05 09:53:19 +02:00
|
|
|
/* If specific device is wanted, i.e. a SCL3711 on USB */
|
|
|
|
#if 0
|
|
|
|
nfc_device_desc_t ndd;
|
|
|
|
ndd.pcDriver = "PN533_USB";
|
|
|
|
strcpy(ndd.acDevice, "SCM Micro / SCL3711-NFC&RW");
|
2012-01-18 17:22:06 +01:00
|
|
|
pnd = nfc_open (NULL, &ndd);
|
2010-01-12 12:50:09 +01:00
|
|
|
#endif
|
2011-10-17 15:03:56 +02:00
|
|
|
nfc_connstring connstrings[MAX_DEVICE_COUNT];
|
2012-01-18 17:22:06 +01:00
|
|
|
size_t szDeviceFound = nfc_list_devices (NULL, connstrings, MAX_DEVICE_COUNT);
|
2009-11-24 18:49:24 +01:00
|
|
|
|
2010-09-07 19:51:03 +02:00
|
|
|
if (szDeviceFound == 0) {
|
2010-10-26 16:27:17 +02:00
|
|
|
printf ("No NFC device found.\n");
|
2009-11-24 18:49:24 +01:00
|
|
|
}
|
|
|
|
|
2010-09-07 19:51:03 +02:00
|
|
|
for (i = 0; i < szDeviceFound; i++) {
|
2011-11-23 16:55:40 +01:00
|
|
|
nfc_target ant[MAX_TARGET_COUNT];
|
2012-01-18 17:22:06 +01:00
|
|
|
pnd = nfc_open (NULL, connstrings[i]);
|
2009-11-24 18:49:24 +01:00
|
|
|
|
2010-09-07 19:51:03 +02:00
|
|
|
if (pnd == NULL) {
|
2012-05-22 11:50:12 +02:00
|
|
|
ERR ("Unable to open NFC device: %s", connstrings[i]);
|
2011-10-27 15:46:13 +02:00
|
|
|
continue;
|
2009-11-24 19:05:31 +01:00
|
|
|
}
|
2012-01-05 11:33:50 +01:00
|
|
|
if (nfc_initiator_init (pnd) < 0) {
|
|
|
|
nfc_perror (pnd, "nfc_initiator_init");
|
|
|
|
exit (EXIT_FAILURE);
|
|
|
|
}
|
2009-05-27 14:18:21 +02:00
|
|
|
|
2012-01-17 16:21:56 +01:00
|
|
|
printf ("NFC device: %s opened\n", nfc_device_get_name (pnd));
|
2009-08-28 18:54:04 +02:00
|
|
|
|
2011-11-23 16:55:40 +01:00
|
|
|
nfc_modulation nm;
|
2011-05-11 01:44:27 +02:00
|
|
|
|
|
|
|
nm.nmt = NMT_ISO14443A;
|
|
|
|
nm.nbr = NBR_106;
|
2010-08-18 15:13:14 +02:00
|
|
|
// List ISO14443A targets
|
2011-12-15 12:46:14 +01:00
|
|
|
if ((res = nfc_initiator_list_passive_targets (pnd, nm, ant, MAX_TARGET_COUNT)) >= 0) {
|
|
|
|
int n;
|
2012-05-18 23:54:45 +02:00
|
|
|
if (verbose || (res > 0)) {
|
2011-12-15 12:46:14 +01:00
|
|
|
printf ("%d ISO14443A passive target(s) found%s\n", res, (res == 0) ? ".\n" : ":");
|
2010-10-19 14:50:52 +02:00
|
|
|
}
|
2011-12-15 12:46:14 +01:00
|
|
|
for (n = 0; n < res; n++) {
|
2010-10-19 14:50:52 +02:00
|
|
|
print_nfc_iso14443a_info (ant[n].nti.nai, verbose);
|
2010-09-07 19:51:03 +02:00
|
|
|
printf ("\n");
|
2010-07-30 15:27:03 +02:00
|
|
|
}
|
2009-05-27 14:18:21 +02:00
|
|
|
}
|
2010-10-13 19:43:23 +02:00
|
|
|
|
|
|
|
nm.nmt = NMT_FELICA;
|
|
|
|
nm.nbr = NBR_212;
|
2010-08-18 15:13:14 +02:00
|
|
|
// List Felica tags
|
2011-12-21 12:33:21 +01:00
|
|
|
if ((res = nfc_initiator_list_passive_targets (pnd, nm, ant, MAX_TARGET_COUNT)) >= 0) {
|
2011-12-15 12:46:14 +01:00
|
|
|
int n;
|
2012-05-18 23:54:45 +02:00
|
|
|
if (verbose || (res > 0)) {
|
2011-12-15 12:46:14 +01:00
|
|
|
printf ("%d Felica (212 kbps) passive target(s) found%s\n", res, (res == 0) ? ".\n" : ":");
|
2010-10-19 14:50:52 +02:00
|
|
|
}
|
2011-12-15 12:46:14 +01:00
|
|
|
for (n = 0; n < res; n++) {
|
2010-10-19 14:50:52 +02:00
|
|
|
print_nfc_felica_info (ant[n].nti.nfi, verbose);
|
2010-09-07 19:51:03 +02:00
|
|
|
printf ("\n");
|
2010-08-18 15:13:14 +02:00
|
|
|
}
|
2009-11-24 19:05:31 +01:00
|
|
|
}
|
2010-10-13 19:43:23 +02:00
|
|
|
|
|
|
|
nm.nbr = NBR_424;
|
2011-12-21 12:33:21 +01:00
|
|
|
if ((res = nfc_initiator_list_passive_targets (pnd, nm, ant, MAX_TARGET_COUNT)) >= 0) {
|
2011-12-15 12:46:14 +01:00
|
|
|
int n;
|
2012-05-18 23:54:45 +02:00
|
|
|
if (verbose || (res > 0)) {
|
2011-12-15 12:46:14 +01:00
|
|
|
printf ("%d Felica (424 kbps) passive target(s) found%s\n", res, (res == 0) ? ".\n" : ":");
|
2010-10-19 14:50:52 +02:00
|
|
|
}
|
2011-12-15 12:46:14 +01:00
|
|
|
for (n = 0; n < res; n++) {
|
2010-10-19 14:50:52 +02:00
|
|
|
print_nfc_felica_info (ant[n].nti.nfi, verbose);
|
2010-09-07 19:51:03 +02:00
|
|
|
printf ("\n");
|
2009-11-24 19:05:31 +01:00
|
|
|
}
|
2009-08-28 18:54:04 +02:00
|
|
|
}
|
2010-10-13 19:43:23 +02:00
|
|
|
|
|
|
|
nm.nmt = NMT_ISO14443B;
|
|
|
|
nm.nbr = NBR_106;
|
2010-08-18 15:13:14 +02:00
|
|
|
// List ISO14443B targets
|
2011-12-21 12:33:21 +01:00
|
|
|
if ((res = nfc_initiator_list_passive_targets (pnd, nm, ant, MAX_TARGET_COUNT)) >= 0) {
|
2011-12-15 12:46:14 +01:00
|
|
|
int n;
|
2012-05-18 23:54:45 +02:00
|
|
|
if (verbose || (res > 0)) {
|
2011-12-15 12:46:14 +01:00
|
|
|
printf ("%d ISO14443B passive target(s) found%s\n", res, (res == 0) ? ".\n" : ":");
|
2010-10-19 14:50:52 +02:00
|
|
|
}
|
2011-12-15 12:46:14 +01:00
|
|
|
for (n = 0; n < res; n++) {
|
2010-10-19 14:50:52 +02:00
|
|
|
print_nfc_iso14443b_info (ant[n].nti.nbi, verbose);
|
2010-09-07 19:51:03 +02:00
|
|
|
printf ("\n");
|
2010-08-18 15:13:14 +02:00
|
|
|
}
|
2009-11-24 19:05:31 +01:00
|
|
|
}
|
2009-05-27 14:18:21 +02:00
|
|
|
|
2011-05-06 16:14:06 +02:00
|
|
|
nm.nmt = NMT_ISO14443BI;
|
|
|
|
nm.nbr = NBR_106;
|
|
|
|
// List ISO14443B' targets
|
2011-12-21 12:33:21 +01:00
|
|
|
if ((res = nfc_initiator_list_passive_targets (pnd, nm, ant, MAX_TARGET_COUNT)) >= 0) {
|
2011-12-15 12:46:14 +01:00
|
|
|
int n;
|
2012-05-18 23:54:45 +02:00
|
|
|
if (verbose || (res > 0)) {
|
2011-12-15 12:46:14 +01:00
|
|
|
printf ("%d ISO14443B' passive target(s) found%s\n", res, (res == 0) ? ".\n" : ":");
|
2011-05-06 16:14:06 +02:00
|
|
|
}
|
2011-12-15 12:46:14 +01:00
|
|
|
for (n = 0; n < res; n++) {
|
2011-05-06 16:14:06 +02:00
|
|
|
print_nfc_iso14443bi_info (ant[n].nti.nii, verbose);
|
|
|
|
printf ("\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-05-12 01:00:54 +02:00
|
|
|
nm.nmt = NMT_ISO14443B2SR;
|
2011-05-06 21:11:03 +02:00
|
|
|
nm.nbr = NBR_106;
|
2011-05-12 01:00:54 +02:00
|
|
|
// List ISO14443B-2 ST SRx family targets
|
2011-12-21 12:33:21 +01:00
|
|
|
if ((res = nfc_initiator_list_passive_targets (pnd, nm, ant, MAX_TARGET_COUNT)) >= 0) {
|
2011-12-15 12:46:14 +01:00
|
|
|
int n;
|
2012-05-18 23:54:45 +02:00
|
|
|
if (verbose || (res > 0)) {
|
2011-12-15 12:46:14 +01:00
|
|
|
printf ("%d ISO14443B-2 ST SRx passive target(s) found%s\n", res, (res == 0) ? ".\n" : ":");
|
2011-05-06 21:11:03 +02:00
|
|
|
}
|
2011-12-15 12:46:14 +01:00
|
|
|
for (n = 0; n < res; n++) {
|
2011-05-12 01:00:54 +02:00
|
|
|
print_nfc_iso14443b2sr_info (ant[n].nti.nsi, verbose);
|
2011-05-06 21:11:03 +02:00
|
|
|
printf ("\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-05-12 01:00:54 +02:00
|
|
|
nm.nmt = NMT_ISO14443B2CT;
|
2011-05-06 21:11:03 +02:00
|
|
|
nm.nbr = NBR_106;
|
2011-05-12 01:00:54 +02:00
|
|
|
// List ISO14443B-2 ASK CTx family targets
|
2011-12-21 12:33:21 +01:00
|
|
|
if ((res = nfc_initiator_list_passive_targets (pnd, nm, ant, MAX_TARGET_COUNT)) >= 0) {
|
2011-12-15 12:46:14 +01:00
|
|
|
int n;
|
2012-05-18 23:54:45 +02:00
|
|
|
if (verbose || (res > 0)) {
|
2011-12-15 12:46:14 +01:00
|
|
|
printf ("%d ISO14443B-2 ASK CTx passive target(s) found%s\n", res, (res == 0) ? ".\n" : ":");
|
2011-05-06 21:11:03 +02:00
|
|
|
}
|
2011-12-15 12:46:14 +01:00
|
|
|
for (n = 0; n < res; n++) {
|
2011-05-12 01:00:54 +02:00
|
|
|
print_nfc_iso14443b2ct_info (ant[n].nti.nci, verbose);
|
2011-05-06 21:11:03 +02:00
|
|
|
printf ("\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-10-13 19:43:23 +02:00
|
|
|
nm.nmt = NMT_JEWEL;
|
|
|
|
nm.nbr = NBR_106;
|
2010-08-18 15:13:14 +02:00
|
|
|
// List Jewel targets
|
2011-12-21 12:33:21 +01:00
|
|
|
if ((res = nfc_initiator_list_passive_targets(pnd, nm, ant, MAX_TARGET_COUNT)) >= 0) {
|
2011-12-15 12:46:14 +01:00
|
|
|
int n;
|
2012-05-18 23:54:45 +02:00
|
|
|
if (verbose || (res > 0)) {
|
2011-12-15 12:46:14 +01:00
|
|
|
printf("%d Jewel passive target(s) found%s\n", res, (res == 0)?".\n":":");
|
2010-10-19 14:50:52 +02:00
|
|
|
}
|
2011-12-15 12:46:14 +01:00
|
|
|
for(n = 0; n < res; n++) {
|
2010-10-19 14:50:52 +02:00
|
|
|
print_nfc_jewel_info (ant[n].nti.nji, verbose);
|
2010-08-18 15:13:14 +02:00
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
}
|
2012-01-17 15:52:39 +01:00
|
|
|
nfc_close (pnd);
|
2010-09-07 19:51:03 +02:00
|
|
|
}
|
2012-05-18 23:54:45 +02:00
|
|
|
|
2012-01-18 17:22:06 +01:00
|
|
|
nfc_exit (NULL);
|
2009-11-24 14:03:48 +01:00
|
|
|
return 0;
|
2009-05-27 14:18:21 +02:00
|
|
|
}
|