libnfc  1.7.1
nfc-poll.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 <inttypes.h>
48 #include <signal.h>
49 #include <stdio.h>
50 #include <stddef.h>
51 #include <stdlib.h>
52 #include <string.h>
53 
54 #include <nfc/nfc.h>
55 #include <nfc/nfc-types.h>
56 
57 #include "utils/nfc-utils.h"
58 
59 #define MAX_DEVICE_COUNT 16
60 
61 static nfc_device *pnd = NULL;
62 static nfc_context *context;
63 
64 static void stop_polling(int sig)
65 {
66  (void) sig;
67  if (pnd != NULL)
68  nfc_abort_command(pnd);
69  else {
70  nfc_exit(context);
71  exit(EXIT_FAILURE);
72  }
73 }
74 
75 static void
76 print_usage(const char *progname)
77 {
78  printf("usage: %s [-v]\n", progname);
79  printf(" -v\t verbose display\n");
80 }
81 
82 int
83 main(int argc, const char *argv[])
84 {
85  bool verbose = false;
86 
87  signal(SIGINT, stop_polling);
88 
89  // Display libnfc version
90  const char *acLibnfcVersion = nfc_version();
91 
92  printf("%s uses libnfc %s\n", argv[0], acLibnfcVersion);
93  if (argc != 1) {
94  if ((argc == 2) && (0 == strcmp("-v", argv[1]))) {
95  verbose = true;
96  } else {
97  print_usage(argv[0]);
98  exit(EXIT_FAILURE);
99  }
100  }
101 
102  const uint8_t uiPollNr = 20;
103  const uint8_t uiPeriod = 2;
104  const nfc_modulation nmModulations[5] = {
105  { .nmt = NMT_ISO14443A, .nbr = NBR_106 },
106  { .nmt = NMT_ISO14443B, .nbr = NBR_106 },
107  { .nmt = NMT_FELICA, .nbr = NBR_212 },
108  { .nmt = NMT_FELICA, .nbr = NBR_424 },
109  { .nmt = NMT_JEWEL, .nbr = NBR_106 },
110  };
111  const size_t szModulations = 5;
112 
113  nfc_target nt;
114  int res = 0;
115 
116  nfc_init(&context);
117  if (context == NULL) {
118  ERR("Unable to init libnfc (malloc)");
119  exit(EXIT_FAILURE);
120  }
121 
122  pnd = nfc_open(context, NULL);
123 
124  if (pnd == NULL) {
125  ERR("%s", "Unable to open NFC device.");
126  nfc_exit(context);
127  exit(EXIT_FAILURE);
128  }
129 
130  if (nfc_initiator_init(pnd) < 0) {
131  nfc_perror(pnd, "nfc_initiator_init");
132  nfc_close(pnd);
133  nfc_exit(context);
134  exit(EXIT_FAILURE);
135  }
136 
137  printf("NFC reader: %s opened\n", nfc_device_get_name(pnd));
138  printf("NFC device will poll during %ld ms (%u pollings of %lu ms for %" PRIdPTR " modulations)\n", (unsigned long) uiPollNr * szModulations * uiPeriod * 150, uiPollNr, (unsigned long) uiPeriod * 150, szModulations);
139  if ((res = nfc_initiator_poll_target(pnd, nmModulations, szModulations, uiPollNr, uiPeriod, &nt)) < 0) {
140  nfc_perror(pnd, "nfc_initiator_poll_target");
141  nfc_close(pnd);
142  nfc_exit(context);
143  exit(EXIT_FAILURE);
144  }
145 
146  if (res > 0) {
147  print_nfc_target(&nt, verbose);
148  } else {
149  printf("No target found.\n");
150  }
151 
152  printf("Waiting for card removing...");
153  while (0 == nfc_initiator_target_is_present(pnd, NULL)) {}
154  nfc_perror(pnd, "nfc_initiator_target_is_present");
155  printf("done.\n");
156 
157  nfc_close(pnd);
158  nfc_exit(context);
159  exit(EXIT_SUCCESS);
160 }