libnfc 1.8.0
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 * Copyright (C) 2020 Adam Laurie
13 *
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions are met:
16 * 1) Redistributions of source code must retain the above copyright notice,
17 * this list of conditions and the following disclaimer.
18 * 2 )Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
23 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
26 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
33 *
34 * Note that this license only applies on the examples, NFC library itself is under LGPL
35 *
36 */
37
43#ifdef HAVE_CONFIG_H
44# include "config.h"
45#endif // HAVE_CONFIG_H
46
47#include <err.h>
48#include <inttypes.h>
49#include <signal.h>
50#include <stdio.h>
51#include <stddef.h>
52#include <stdlib.h>
53#include <string.h>
54
55#include <nfc/nfc.h>
56#include <nfc/nfc-types.h>
57
58#include "utils/nfc-utils.h"
59
60#define MAX_DEVICE_COUNT 16
61
62static nfc_device *pnd = NULL;
63static nfc_context *context;
64
65static void stop_polling(int sig)
66{
67 (void) sig;
68 if (pnd != NULL)
70 else {
71 nfc_exit(context);
72 exit(EXIT_FAILURE);
73 }
74}
75
76static void
77print_usage(const char *progname)
78{
79 printf("usage: %s [-v]\n", progname);
80 printf(" -v\t verbose display\n");
81}
82
83int
84main(int argc, const char *argv[])
85{
86 bool verbose = false;
87
88 signal(SIGINT, stop_polling);
89
90 // Display libnfc version
91 const char *acLibnfcVersion = nfc_version();
92
93 printf("%s uses libnfc %s\n", argv[0], acLibnfcVersion);
94 if (argc != 1) {
95 if ((argc == 2) && (0 == strcmp("-v", argv[1]))) {
96 verbose = true;
97 } else {
98 print_usage(argv[0]);
99 exit(EXIT_FAILURE);
100 }
101 }
102
103 const uint8_t uiPollNr = 20;
104 const uint8_t uiPeriod = 2;
105 const nfc_modulation nmModulations[6] = {
106 { .nmt = NMT_ISO14443A, .nbr = NBR_106 },
107 { .nmt = NMT_ISO14443B, .nbr = NBR_106 },
108 { .nmt = NMT_FELICA, .nbr = NBR_212 },
109 { .nmt = NMT_FELICA, .nbr = NBR_424 },
110 { .nmt = NMT_JEWEL, .nbr = NBR_106 },
111 { .nmt = NMT_ISO14443BICLASS, .nbr = NBR_106 },
112 };
113 const size_t szModulations = 6;
114
115 nfc_target nt;
116 int res = 0;
117
118 nfc_init(&context);
119 if (context == NULL) {
120 ERR("Unable to init libnfc (malloc)");
121 exit(EXIT_FAILURE);
122 }
123
124 pnd = nfc_open(context, NULL);
125
126 if (pnd == NULL) {
127 ERR("%s", "Unable to open NFC device.");
128 nfc_exit(context);
129 exit(EXIT_FAILURE);
130 }
131
132 if (nfc_initiator_init(pnd) < 0) {
133 nfc_perror(pnd, "nfc_initiator_init");
134 nfc_close(pnd);
135 nfc_exit(context);
136 exit(EXIT_FAILURE);
137 }
138
139 printf("NFC reader: %s opened\n", nfc_device_get_name(pnd));
140 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);
141 if ((res = nfc_initiator_poll_target(pnd, nmModulations, szModulations, uiPollNr, uiPeriod, &nt)) < 0) {
142 nfc_perror(pnd, "nfc_initiator_poll_target");
143 nfc_close(pnd);
144 nfc_exit(context);
145 exit(EXIT_FAILURE);
146 }
147
148 if (res > 0) {
149 print_nfc_target(&nt, verbose);
150 printf("Waiting for card removing...");
151 fflush(stdout);
152 while (0 == nfc_initiator_target_is_present(pnd, NULL)) {}
153 nfc_perror(pnd, "nfc_initiator_target_is_present");
154 printf("done.\n");
155 } else {
156 printf("No target found.\n");
157 }
158
159 nfc_close(pnd);
160 nfc_exit(context);
161 exit(EXIT_SUCCESS);
162}
const char * nfc_device_get_name(nfc_device *pnd)
Returns the device name.
Definition nfc.c:1209
void nfc_close(nfc_device *pnd)
Close from a NFC device.
Definition nfc.c:339
nfc_device * nfc_open(nfc_context *context, const nfc_connstring connstring)
Open a NFC device.
Definition nfc.c:277
int nfc_abort_command(nfc_device *pnd)
Abort current running command.
Definition nfc.c:1036
void nfc_perror(const nfc_device *pnd, const char *pcString)
Display the last error occured on a nfc_device.
Definition nfc.c:1183
int nfc_initiator_init(nfc_device *pnd)
Initialize NFC device as initiator (reader)
Definition nfc.c:493
int nfc_initiator_target_is_present(nfc_device *pnd, const nfc_target *pnt)
Check target presence.
Definition nfc.c:907
int nfc_initiator_poll_target(nfc_device *pnd, const nfc_modulation *pnmModulations, const size_t szModulations, const uint8_t uiPollNr, const uint8_t uiPeriod, nfc_target *pnt)
Polling for NFC targets.
Definition nfc.c:672
void nfc_exit(nfc_context *context)
Deinitialize libnfc. Should be called after closing all open devices and before your application term...
Definition nfc.c:248
void nfc_init(nfc_context **context)
Initialize libnfc. This function must be called before calling any other libnfc function.
Definition nfc.c:231
const char * nfc_version(void)
Returns the library version.
Definition nfc.c:1319
Define NFC types.
Provide some examples shared functions like print, parity calculation, options parsing.
#define ERR(...)
Print a error message.
Definition nfc-utils.h:85
libnfc interface
NFC library context Struct which contains internal options, references, pointers, etc....
NFC device information.
NFC modulation structure.
Definition nfc-types.h:342
NFC target structure.
Definition nfc-types.h:351