libnfc  1.4.2
nfc-poll.c
1 /*-
2  * Public platform independent Near Field Communication (NFC) library examples
3  *
4  * Copyright (C) 2010, Romuald Conty
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  * 1) Redistributions of source code must retain the above copyright notice,
9  * this list of conditions and the following disclaimer.
10  * 2 )Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
15  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
18  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24  * POSSIBILITY OF SUCH DAMAGE.
25  *
26  * Note that this license only applies on the examples, NFC library itself is under LGPL
27  *
28  */
29 
35 #ifdef HAVE_CONFIG_H
36 # include "config.h"
37 #endif // HAVE_CONFIG_H
38 
39 #include <err.h>
40 #include <stdio.h>
41 #include <stddef.h>
42 #include <stdlib.h>
43 #include <string.h>
44 
45 #include <nfc/nfc.h>
46 #include <nfc/nfc-types.h>
47 #include <nfc/nfc-messages.h>
48 #include "nfc-utils.h"
49 
50 #define MAX_DEVICE_COUNT 16
51 
52 static nfc_device_t *pnd;
53 
54 int
55 main (int argc, const char *argv[])
56 {
57  size_t szFound;
58  size_t i;
59  bool verbose = false;
60  nfc_device_desc_t *pnddDevices;
61 
62  pnddDevices = parse_args (argc, argv, &szFound, &verbose);
63 
64  // Display libnfc version
65  const char *acLibnfcVersion = nfc_version ();
66 
67  if (argc > 1) {
68  errx (1, "usage: %s", argv[0]);
69  }
70 
71  printf ("%s use libnfc %s\n", argv[0], acLibnfcVersion);
72 
73  if (szFound == 0) {
74  if (!(pnddDevices = malloc (MAX_DEVICE_COUNT * sizeof (*pnddDevices)))) {
75  fprintf (stderr, "malloc() failed\n");
76  return EXIT_FAILURE;
77  }
78  }
79 
80  nfc_list_devices (pnddDevices, MAX_DEVICE_COUNT, &szFound);
81 
82  if (szFound == 0) {
83  printf ("No NFC device found.\n");
84  }
85 
86  for (i = 0; i < szFound; i++) {
87 
88  const byte_t btPollNr = 20;
89  const byte_t btPeriod = 2;
90  const nfc_modulation_t nmModulations[5] = {
91  { .nmt = NMT_ISO14443A, .nbr = NBR_106 },
92  { .nmt = NMT_ISO14443B, .nbr = NBR_106 },
93  { .nmt = NMT_FELICA, .nbr = NBR_212 },
94  { .nmt = NMT_FELICA, .nbr = NBR_424 },
95  { .nmt = NMT_JEWEL, .nbr = NBR_106 },
96  };
97  const size_t szModulations = 5;
98 
99  nfc_target_t antTargets[2];
100  size_t szTargetFound;
101  bool res;
102 
103  pnd = nfc_connect (&(pnddDevices[i]));
104 
105  if (pnd == NULL) {
106  ERR ("%s", "Unable to connect to NFC device.");
107  return 1;
108  }
109  nfc_initiator_init (pnd);
110 
111  // Drop the field for a while
112  if (!nfc_configure (pnd, NDO_ACTIVATE_FIELD, false)) {
113  nfc_perror (pnd, "nfc_configure");
114  exit (EXIT_FAILURE);
115  }
116  // Let the reader only try once to find a tag
117  if (!nfc_configure (pnd, NDO_INFINITE_SELECT, false)) {
118  nfc_perror (pnd, "nfc_configure");
119  exit (EXIT_FAILURE);
120  }
121  // Enable field so more power consuming cards can power themselves up
122  if (!nfc_configure (pnd, NDO_ACTIVATE_FIELD, true)) {
123  nfc_perror (pnd, "nfc_configure");
124  exit (EXIT_FAILURE);
125  }
126 
127  printf ("Connected to NFC reader: %s\n", pnd->acName);
128 
129  printf ("PN532 will poll during %ld ms\n", (unsigned long) btPollNr * szModulations * btPeriod * 150);
130  res = nfc_initiator_poll_targets (pnd, nmModulations, szModulations, btPollNr, btPeriod, antTargets, &szTargetFound);
131  if (res) {
132  uint8_t n;
133  printf ("%ld target(s) have been found.\n", (unsigned long) szTargetFound);
134  for (n = 0; n < szTargetFound; n++) {
135  printf ("T%d: ", n + 1);
136  print_nfc_target ( antTargets[n], verbose );
137 
138  }
139  } else {
140  nfc_perror (pnd, "nfc_initiator_poll_targets");
141  nfc_disconnect (pnd);
142  exit (EXIT_FAILURE);
143  }
144  nfc_disconnect (pnd);
145  }
146 
147  free (pnddDevices);
148  return 0;
149 }