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