libnfc  1.7.1
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] [-t X]\n", progname);
65  printf(" -v\t verbose display\n");
66  printf(" -t X\t poll only for types according to bitfield X:\n");
67  printf("\t 1: ISO14443A\n");
68  printf("\t 2: Felica (212 kbps)\n");
69  printf("\t 4: Felica (424 kbps)\n");
70  printf("\t 8: ISO14443B\n");
71  printf("\t 16: ISO14443B'\n");
72  printf("\t 32: ISO14443B-2 ST SRx\n");
73  printf("\t 64: ISO14443B-2 ASK CTx\n");
74  printf("\t 128: Jewel\n");
75  printf("\tSo 255 (default) polls for all types.\n");
76  printf("\tNote that if 16, 32 or 64 then 8 is selected too.\n");
77 }
78 
79 int
80 main(int argc, const char *argv[])
81 {
82  (void) argc;
83  const char *acLibnfcVersion;
84  size_t i;
85  bool verbose = false;
86  int res = 0;
87  int mask = 0xff;
88  int arg;
89 
90  nfc_context *context;
91  nfc_init(&context);
92  if (context == NULL) {
93  ERR("Unable to init libnfc (malloc)");
94  exit(EXIT_FAILURE);
95  }
96 
97  // Display libnfc version
98  acLibnfcVersion = nfc_version();
99  printf("%s uses libnfc %s\n", argv[0], acLibnfcVersion);
100 
101  // Get commandline options
102  for (arg = 1; arg < argc; arg++) {
103  if (0 == strcmp(argv[arg], "-h")) {
104  print_usage(argv[0]);
105  exit(EXIT_SUCCESS);
106  } else if (0 == strcmp(argv[arg], "-v")) {
107  verbose = true;
108  } else if ((0 == strcmp(argv[arg], "-t")) && (arg + 1 < argc)) {
109  arg++;
110  mask = atoi(argv[arg]);
111  if ((mask < 1) || (mask > 255)) {
112  ERR("%i is invalid value for type bitfield.", mask);
113  print_usage(argv[0]);
114  exit(EXIT_FAILURE);
115  }
116  // Force TypeB for all derivatives of B
117  if (mask & 0x70)
118  mask |= 0x08;
119  } else {
120  ERR("%s is not supported option.", argv[arg]);
121  print_usage(argv[0]);
122  exit(EXIT_FAILURE);
123  }
124  }
125 
126  /* Lazy way to open an NFC device */
127 #if 0
128  pnd = nfc_open(context, NULL);
129 #endif
130 
131  /* Use connection string if specific device is wanted,
132  * i.e. PN532 UART device on /dev/ttyUSB1 */
133 #if 0
134  pnd = nfc_open(context, "pn532_uart:/dev/ttyUSB1");
135 #endif
136 
137  nfc_connstring connstrings[MAX_DEVICE_COUNT];
138  size_t szDeviceFound = nfc_list_devices(context, connstrings, MAX_DEVICE_COUNT);
139 
140  if (szDeviceFound == 0) {
141  printf("No NFC device found.\n");
142  }
143 
144  for (i = 0; i < szDeviceFound; i++) {
145  nfc_target ant[MAX_TARGET_COUNT];
146  pnd = nfc_open(context, connstrings[i]);
147 
148  if (pnd == NULL) {
149  ERR("Unable to open NFC device: %s", connstrings[i]);
150  continue;
151  }
152  if (nfc_initiator_init(pnd) < 0) {
153  nfc_perror(pnd, "nfc_initiator_init");
154  nfc_exit(context);
155  exit(EXIT_FAILURE);
156  }
157 
158  printf("NFC device: %s opened\n", nfc_device_get_name(pnd));
159 
160  nfc_modulation nm;
161 
162  if (mask & 0x1) {
163  nm.nmt = NMT_ISO14443A;
164  nm.nbr = NBR_106;
165  // List ISO14443A targets
166  if ((res = nfc_initiator_list_passive_targets(pnd, nm, ant, MAX_TARGET_COUNT)) >= 0) {
167  int n;
168  if (verbose || (res > 0)) {
169  printf("%d ISO14443A passive target(s) found%s\n", res, (res == 0) ? ".\n" : ":");
170  }
171  for (n = 0; n < res; n++) {
172  print_nfc_target(&ant[n], verbose);
173  printf("\n");
174  }
175  }
176  }
177 
178  if (mask & 0x02) {
179  nm.nmt = NMT_FELICA;
180  nm.nbr = NBR_212;
181  // List Felica tags
182  if ((res = nfc_initiator_list_passive_targets(pnd, nm, ant, MAX_TARGET_COUNT)) >= 0) {
183  int n;
184  if (verbose || (res > 0)) {
185  printf("%d Felica (212 kbps) passive target(s) found%s\n", res, (res == 0) ? ".\n" : ":");
186  }
187  for (n = 0; n < res; n++) {
188  print_nfc_target(&ant[n], verbose);
189  printf("\n");
190  }
191  }
192  }
193 
194  if (mask & 0x04) {
195  nm.nmt = NMT_FELICA;
196  nm.nbr = NBR_424;
197  if ((res = nfc_initiator_list_passive_targets(pnd, nm, ant, MAX_TARGET_COUNT)) >= 0) {
198  int n;
199  if (verbose || (res > 0)) {
200  printf("%d Felica (424 kbps) passive target(s) found%s\n", res, (res == 0) ? ".\n" : ":");
201  }
202  for (n = 0; n < res; n++) {
203  print_nfc_target(&ant[n], verbose);
204  printf("\n");
205  }
206  }
207  }
208 
209  if (mask & 0x08) {
210  nm.nmt = NMT_ISO14443B;
211  nm.nbr = NBR_106;
212  // List ISO14443B targets
213  if ((res = nfc_initiator_list_passive_targets(pnd, nm, ant, MAX_TARGET_COUNT)) >= 0) {
214  int n;
215  if (verbose || (res > 0)) {
216  printf("%d ISO14443B passive target(s) found%s\n", res, (res == 0) ? ".\n" : ":");
217  }
218  for (n = 0; n < res; n++) {
219  print_nfc_target(&ant[n], verbose);
220  printf("\n");
221  }
222  }
223  }
224 
225  if (mask & 0x10) {
226  nm.nmt = NMT_ISO14443BI;
227  nm.nbr = NBR_106;
228  // List ISO14443B' targets
229  if ((res = nfc_initiator_list_passive_targets(pnd, nm, ant, MAX_TARGET_COUNT)) >= 0) {
230  int n;
231  if (verbose || (res > 0)) {
232  printf("%d ISO14443B' passive target(s) found%s\n", res, (res == 0) ? ".\n" : ":");
233  }
234  for (n = 0; n < res; n++) {
235  print_nfc_target(&ant[n], verbose);
236  printf("\n");
237  }
238  }
239  }
240 
241  if (mask & 0x20) {
242  nm.nmt = NMT_ISO14443B2SR;
243  nm.nbr = NBR_106;
244  // List ISO14443B-2 ST SRx family targets
245  if ((res = nfc_initiator_list_passive_targets(pnd, nm, ant, MAX_TARGET_COUNT)) >= 0) {
246  int n;
247  if (verbose || (res > 0)) {
248  printf("%d ISO14443B-2 ST SRx passive target(s) found%s\n", res, (res == 0) ? ".\n" : ":");
249  }
250  for (n = 0; n < res; n++) {
251  print_nfc_target(&ant[n], verbose);
252  printf("\n");
253  }
254  }
255  }
256 
257  if (mask & 0x40) {
258  nm.nmt = NMT_ISO14443B2CT;
259  nm.nbr = NBR_106;
260  // List ISO14443B-2 ASK CTx family targets
261  if ((res = nfc_initiator_list_passive_targets(pnd, nm, ant, MAX_TARGET_COUNT)) >= 0) {
262  int n;
263  if (verbose || (res > 0)) {
264  printf("%d ISO14443B-2 ASK CTx passive target(s) found%s\n", res, (res == 0) ? ".\n" : ":");
265  }
266  for (n = 0; n < res; n++) {
267  print_nfc_target(&ant[n], verbose);
268  printf("\n");
269  }
270  }
271  }
272 
273  if (mask & 0x80) {
274  nm.nmt = NMT_JEWEL;
275  nm.nbr = NBR_106;
276  // List Jewel targets
277  if ((res = nfc_initiator_list_passive_targets(pnd, nm, ant, MAX_TARGET_COUNT)) >= 0) {
278  int n;
279  if (verbose || (res > 0)) {
280  printf("%d Jewel passive target(s) found%s\n", res, (res == 0) ? ".\n" : ":");
281  }
282  for (n = 0; n < res; n++) {
283  print_nfc_target(&ant[n], verbose);
284  printf("\n");
285  }
286  }
287  }
288  nfc_close(pnd);
289  }
290 
291  nfc_exit(context);
292  exit(EXIT_SUCCESS);
293 }
nfc_initiator_init
int nfc_initiator_init(nfc_device *pnd)
Initialize NFC device as initiator (reader)
Definition: nfc.c:452
nfc_init
void nfc_init(nfc_context **context)
Initialize libnfc. This function must be called before calling any other libnfc function.
Definition: nfc.c:192
nfc_context
NFC library context Struct which contains internal options, references, pointers, etc....
Definition: nfc-internal.h:175
nfc_device
NFC device information.
Definition: nfc-internal.h:190
nfc_version
const char * nfc_version(void)
Returns the library version.
Definition: nfc.c:1218
nfc_initiator_list_passive_targets
int nfc_initiator_list_passive_targets(nfc_device *pnd, const nfc_modulation nm, nfc_target ant[], const size_t szTargets)
List passive or emulated tags.
Definition: nfc.c:561
nfc_exit
void nfc_exit(nfc_context *context)
Deinitialize libnfc. Should be called after closing all open devices and before your application term...
Definition: nfc.c:209
nfc_target
NFC target structure.
Definition: nfc-types.h:328
nfc_modulation
NFC modulation structure.
Definition: nfc-types.h:319
nfc_connstring
char nfc_connstring[NFC_BUFSIZE_CONNSTRING]
Definition: nfc-types.h:62
nfc_device_get_name
const char * nfc_device_get_name(nfc_device *pnd)
Returns the device name.
Definition: nfc.c:1164
nfc_open
nfc_device * nfc_open(nfc_context *context, const nfc_connstring connstring)
Open a NFC device.
Definition: nfc.c:238
nfc_perror
void nfc_perror(const nfc_device *pnd, const char *pcString)
Display the last error occured on a nfc_device.
Definition: nfc.c:1138
nfc_list_devices
size_t nfc_list_devices(nfc_context *context, nfc_connstring connstrings[], const size_t connstrings_len)
Scan for discoverable supported devices (ie. only available for some drivers)
Definition: nfc.c:317
ERR
#define ERR(...)
Print a error message.
Definition: nfc-utils.h:85
nfc_close
void nfc_close(nfc_device *pnd)
Close from a NFC device.
Definition: nfc.c:300
nfc-utils.h
Provide some examples shared functions like print, parity calculation, options parsing.
nfc.h
libnfc interface