libnfc  1.4.2
nfc-list.c
Go to the documentation of this file.
1 /*-
2  * Public platform independent Near Field Communication (NFC) library examples
3  *
4  * Copyright (C) 2009, Roel Verdult
5  * Copyright (C) 2010, Romuald Conty, Romain Tartière
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions are met:
9  * 1) Redistributions of source code must retain the above copyright notice,
10  * this list of conditions and the following disclaimer.
11  * 2 )Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in the
13  * documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
19  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25  * POSSIBILITY OF SUCH DAMAGE.
26  *
27  * Note that this license only applies on the examples, NFC library itself is under LGPL
28  *
29  */
30 
36 #ifdef HAVE_CONFIG_H
37 # include "config.h"
38 #endif // HAVE_CONFIG_H
39 
40 #ifdef HAVE_LIBUSB
41 # ifdef DEBUG
42 # include <sys/param.h>
43 # include <usb.h>
44 # endif
45 #endif
46 
47 #include <err.h>
48 #include <stdio.h>
49 #include <stddef.h>
50 #include <stdlib.h>
51 #include <string.h>
52 
53 #include <nfc/nfc.h>
54 #include <nfc/nfc-messages.h>
55 #include "nfc-utils.h"
56 
57 #define MAX_DEVICE_COUNT 16
58 #define MAX_TARGET_COUNT 16
59 
60 static nfc_device_t *pnd;
61 
62 int
63 main (int argc, const char *argv[])
64 {
65  const char *acLibnfcVersion;
66  size_t szDeviceFound;
67  size_t szTargetFound;
68  size_t i;
69  bool verbose = false;
70  nfc_device_desc_t *pnddDevices;
71 
72  // Display libnfc version
73  acLibnfcVersion = nfc_version ();
74  printf ("%s use libnfc %s\n", argv[0], acLibnfcVersion);
75 
76  pnddDevices = parse_args (argc, argv, &szDeviceFound, &verbose);
77 #ifdef HAVE_LIBUSB
78 # ifdef DEBUG
79  usb_set_debug (4);
80 # endif
81 #endif
82 
83  /* Lazy way to open an NFC device */
84 #if 0
85  pnd = nfc_connect (NULL);
86 #endif
87 
88  /* If specific device is wanted, i.e. an ARYGON device on /dev/ttyUSB0 */
89 #if 0
91  ndd.pcDriver = "ARYGON";
92  ndd.pcPort = "/dev/ttyUSB0";
93  ndd.uiSpeed = 115200;
94  pnd = nfc_connect (&ndd);
95 #endif
96 
97  /* If specific device is wanted, i.e. a SCL3711 on USB */
98 #if 0
100  ndd.pcDriver = "PN533_USB";
101  strcpy(ndd.acDevice, "SCM Micro / SCL3711-NFC&RW");
102  pnd = nfc_connect (&ndd);
103 #endif
104 
105  if (szDeviceFound == 0) {
106  if (!(pnddDevices = malloc (MAX_DEVICE_COUNT * sizeof (*pnddDevices)))) {
107  fprintf (stderr, "malloc() failed\n");
108  return EXIT_FAILURE;
109  }
110 
111  nfc_list_devices (pnddDevices, MAX_DEVICE_COUNT, &szDeviceFound);
112  }
113 
114  if (szDeviceFound == 0) {
115  printf ("No NFC device found.\n");
116  }
117 
118  for (i = 0; i < szDeviceFound; i++) {
119  nfc_target_t ant[MAX_TARGET_COUNT];
120  pnd = nfc_connect (&(pnddDevices[i]));
121 
122  if (pnd == NULL) {
123  ERR ("%s", "Unable to connect to NFC device.");
124  return EXIT_FAILURE;
125  }
126  nfc_initiator_init (pnd);
127 
128  printf ("Connected to NFC device: %s\n", pnd->acName);
129 
130  // List ISO14443A targets
131  nfc_modulation_t nm = {
132  .nmt = NMT_ISO14443A,
133  .nbr = NBR_106,
134  };
135  if (nfc_initiator_list_passive_targets (pnd, nm, ant, MAX_TARGET_COUNT, &szTargetFound)) {
136  size_t n;
137  if (verbose || (szTargetFound > 0)) {
138  printf ("%d ISO14443A passive target(s) was found%s\n", (int) szTargetFound, (szTargetFound == 0) ? ".\n" : ":");
139  }
140  for (n = 0; n < szTargetFound; n++) {
141  print_nfc_iso14443a_info (ant[n].nti.nai, verbose);
142  printf ("\n");
143  }
144  }
145 
146  nm.nmt = NMT_FELICA;
147  nm.nbr = NBR_212;
148  // List Felica tags
149  if (nfc_initiator_list_passive_targets (pnd, nm, ant, MAX_TARGET_COUNT, &szTargetFound)) {
150  size_t n;
151  if (verbose || (szTargetFound > 0)) {
152  printf ("%d Felica (212 kbps) passive target(s) was found%s\n", (int) szTargetFound,
153  (szTargetFound == 0) ? ".\n" : ":");
154  }
155  for (n = 0; n < szTargetFound; n++) {
156  print_nfc_felica_info (ant[n].nti.nfi, verbose);
157  printf ("\n");
158  }
159  }
160 
161  nm.nbr = NBR_424;
162  if (nfc_initiator_list_passive_targets (pnd, nm, ant, MAX_TARGET_COUNT, &szTargetFound)) {
163  size_t n;
164  if (verbose || (szTargetFound > 0)) {
165  printf ("%d Felica (424 kbps) passive target(s) was found%s\n", (int) szTargetFound,
166  (szTargetFound == 0) ? ".\n" : ":");
167  }
168  for (n = 0; n < szTargetFound; n++) {
169  print_nfc_felica_info (ant[n].nti.nfi, verbose);
170  printf ("\n");
171  }
172  }
173 
174  nm.nmt = NMT_ISO14443B;
175  nm.nbr = NBR_106;
176  // List ISO14443B targets
177  if (nfc_initiator_list_passive_targets (pnd, nm, ant, MAX_TARGET_COUNT, &szTargetFound)) {
178  size_t n;
179  if (verbose || (szTargetFound > 0)) {
180  printf ("%d ISO14443B passive target(s) was found%s\n", (int) szTargetFound, (szTargetFound == 0) ? ".\n" : ":");
181  }
182  for (n = 0; n < szTargetFound; n++) {
183  print_nfc_iso14443b_info (ant[n].nti.nbi, verbose);
184  printf ("\n");
185  }
186  }
187 
188  nm.nmt = NMT_JEWEL;
189  nm.nbr = NBR_106;
190  // List Jewel targets
191  if (nfc_initiator_list_passive_targets(pnd, nm, ant, MAX_TARGET_COUNT, &szTargetFound )) {
192  size_t n;
193  if (verbose || (szTargetFound > 0)) {
194  printf("%d Jewel passive target(s) was found%s\n", (int)szTargetFound, (szTargetFound==0)?".\n":":");
195  }
196  for(n=0; n<szTargetFound; n++) {
197  print_nfc_jewel_info (ant[n].nti.nji, verbose);
198  printf("\n");
199  }
200  }
201  nfc_disconnect (pnd);
202  }
203 
204  free (pnddDevices);
205  return 0;
206 }