libnfc  1.7.0
nfc-list.c
Go to the documentation of this file.
1 /*-
2  * Free/Libre Near Field Communication (NFC) library
3  *
4  * Libnfc historical contributors:
5  * Copyright (C) 2009 Roel Verdult
6  * Copyright (C) 2009-2013 Romuald Conty
7  * Copyright (C) 2010-2012 Romain Tartière
8  * Copyright (C) 2010-2013 Philippe Teuwen
9  * Copyright (C) 2012-2013 Ludovic Rousseau
10  * See AUTHORS file for a more comprehensive list of contributors.
11  * Additional contributors of this file:
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions are met:
15  * 1) Redistributions of source code must retain the above copyright notice,
16  * this list of conditions and the following disclaimer.
17  * 2 )Redistributions in binary form must reproduce the above copyright
18  * notice, this list of conditions and the following disclaimer in the
19  * documentation and/or other materials provided with the distribution.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
25  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31  * POSSIBILITY OF SUCH DAMAGE.
32  *
33  * Note that this license only applies on the examples, NFC library itself is under LGPL
34  *
35  */
36 
42 #ifdef HAVE_CONFIG_H
43 # include "config.h"
44 #endif // HAVE_CONFIG_H
45 
46 #include <err.h>
47 #include <stdio.h>
48 #include <stddef.h>
49 #include <stdlib.h>
50 #include <string.h>
51 
52 #include <nfc/nfc.h>
53 
54 #include "nfc-utils.h"
55 
56 #define MAX_DEVICE_COUNT 16
57 #define MAX_TARGET_COUNT 16
58 
59 static nfc_device *pnd;
60 
61 static void
62 print_usage(const char *progname)
63 {
64  printf("usage: %s [-v]\n", progname);
65  printf(" -v\t verbose display\n");
66 }
67 
68 int
69 main(int argc, const char *argv[])
70 {
71  (void) argc;
72  const char *acLibnfcVersion;
73  size_t i;
74  bool verbose = false;
75  int res = 0;
76 
77  nfc_context *context;
78  nfc_init(&context);
79  if (context == NULL) {
80  ERR("Unable to init libnfc (malloc)");
81  exit(EXIT_FAILURE);
82  }
83 
84  // Display libnfc version
85  acLibnfcVersion = nfc_version();
86  printf("%s uses libnfc %s\n", argv[0], acLibnfcVersion);
87  if (argc != 1) {
88  if ((argc == 2) && (0 == strcmp("-v", argv[1]))) {
89  verbose = true;
90  } else {
91  print_usage(argv[0]);
92  exit(EXIT_FAILURE);
93  }
94  }
95 
96  /* Lazy way to open an NFC device */
97 #if 0
98  pnd = nfc_open(context, NULL);
99 #endif
100 
101  /* Use connection string if specific device is wanted,
102  * i.e. PN532 UART device on /dev/ttyUSB1 */
103 #if 0
104  pnd = nfc_open(context, "pn532_uart:/dev/ttyUSB1");
105 #endif
106 
107  nfc_connstring connstrings[MAX_DEVICE_COUNT];
108  size_t szDeviceFound = nfc_list_devices(context, connstrings, MAX_DEVICE_COUNT);
109 
110  if (szDeviceFound == 0) {
111  printf("No NFC device found.\n");
112  }
113 
114  for (i = 0; i < szDeviceFound; i++) {
115  nfc_target ant[MAX_TARGET_COUNT];
116  pnd = nfc_open(context, connstrings[i]);
117 
118  if (pnd == NULL) {
119  ERR("Unable to open NFC device: %s", connstrings[i]);
120  continue;
121  }
122  if (nfc_initiator_init(pnd) < 0) {
123  nfc_perror(pnd, "nfc_initiator_init");
124  nfc_exit(context);
125  exit(EXIT_FAILURE);
126  }
127 
128  printf("NFC device: %s opened\n", nfc_device_get_name(pnd));
129 
130  nfc_modulation nm;
131 
132  nm.nmt = NMT_ISO14443A;
133  nm.nbr = NBR_106;
134  // List ISO14443A targets
135  if ((res = nfc_initiator_list_passive_targets(pnd, nm, ant, MAX_TARGET_COUNT)) >= 0) {
136  int n;
137  if (verbose || (res > 0)) {
138  printf("%d ISO14443A passive target(s) found%s\n", res, (res == 0) ? ".\n" : ":");
139  }
140  for (n = 0; n < res; n++) {
141  print_nfc_target(&ant[n], verbose);
142  printf("\n");
143  }
144  }
145 
146  nm.nmt = NMT_FELICA;
147  nm.nbr = NBR_212;
148  // List Felica tags
149  if ((res = nfc_initiator_list_passive_targets(pnd, nm, ant, MAX_TARGET_COUNT)) >= 0) {
150  int n;
151  if (verbose || (res > 0)) {
152  printf("%d Felica (212 kbps) passive target(s) found%s\n", res, (res == 0) ? ".\n" : ":");
153  }
154  for (n = 0; n < res; n++) {
155  print_nfc_target(&ant[n], verbose);
156  printf("\n");
157  }
158  }
159 
160  nm.nbr = NBR_424;
161  if ((res = nfc_initiator_list_passive_targets(pnd, nm, ant, MAX_TARGET_COUNT)) >= 0) {
162  int n;
163  if (verbose || (res > 0)) {
164  printf("%d Felica (424 kbps) passive target(s) found%s\n", res, (res == 0) ? ".\n" : ":");
165  }
166  for (n = 0; n < res; n++) {
167  print_nfc_target(&ant[n], verbose);
168  printf("\n");
169  }
170  }
171 
172  nm.nmt = NMT_ISO14443B;
173  nm.nbr = NBR_106;
174  // List ISO14443B targets
175  if ((res = nfc_initiator_list_passive_targets(pnd, nm, ant, MAX_TARGET_COUNT)) >= 0) {
176  int n;
177  if (verbose || (res > 0)) {
178  printf("%d ISO14443B passive target(s) found%s\n", res, (res == 0) ? ".\n" : ":");
179  }
180  for (n = 0; n < res; n++) {
181  print_nfc_target(&ant[n], verbose);
182  printf("\n");
183  }
184  }
185 
186  nm.nmt = NMT_ISO14443BI;
187  nm.nbr = NBR_106;
188  // List ISO14443B' targets
189  if ((res = nfc_initiator_list_passive_targets(pnd, nm, ant, MAX_TARGET_COUNT)) >= 0) {
190  int n;
191  if (verbose || (res > 0)) {
192  printf("%d ISO14443B' passive target(s) found%s\n", res, (res == 0) ? ".\n" : ":");
193  }
194  for (n = 0; n < res; n++) {
195  print_nfc_target(&ant[n], verbose);
196  printf("\n");
197  }
198  }
199 
200  nm.nmt = NMT_ISO14443B2SR;
201  nm.nbr = NBR_106;
202  // List ISO14443B-2 ST SRx family targets
203  if ((res = nfc_initiator_list_passive_targets(pnd, nm, ant, MAX_TARGET_COUNT)) >= 0) {
204  int n;
205  if (verbose || (res > 0)) {
206  printf("%d ISO14443B-2 ST SRx passive target(s) found%s\n", res, (res == 0) ? ".\n" : ":");
207  }
208  for (n = 0; n < res; n++) {
209  print_nfc_target(&ant[n], verbose);
210  printf("\n");
211  }
212  }
213 
214  nm.nmt = NMT_ISO14443B2CT;
215  nm.nbr = NBR_106;
216  // List ISO14443B-2 ASK CTx family targets
217  if ((res = nfc_initiator_list_passive_targets(pnd, nm, ant, MAX_TARGET_COUNT)) >= 0) {
218  int n;
219  if (verbose || (res > 0)) {
220  printf("%d ISO14443B-2 ASK CTx passive target(s) found%s\n", res, (res == 0) ? ".\n" : ":");
221  }
222  for (n = 0; n < res; n++) {
223  print_nfc_target(&ant[n], verbose);
224  printf("\n");
225  }
226  }
227 
228  nm.nmt = NMT_JEWEL;
229  nm.nbr = NBR_106;
230  // List Jewel targets
231  if ((res = nfc_initiator_list_passive_targets(pnd, nm, ant, MAX_TARGET_COUNT)) >= 0) {
232  int n;
233  if (verbose || (res > 0)) {
234  printf("%d Jewel passive target(s) found%s\n", res, (res == 0) ? ".\n" : ":");
235  }
236  for (n = 0; n < res; n++) {
237  print_nfc_target(&ant[n], verbose);
238  printf("\n");
239  }
240  }
241  nfc_close(pnd);
242  }
243 
244  nfc_exit(context);
245  exit(EXIT_SUCCESS);
246 }