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