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 }
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_poll_target
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:627
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_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
ERR
#define ERR(...)
Print a error message.
Definition: nfc-utils.h:85
nfc_abort_command
int nfc_abort_command(nfc_device *pnd)
Abort current running command.
Definition: nfc.c:991
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-types.h
Define NFC types.
nfc_initiator_target_is_present
int nfc_initiator_target_is_present(nfc_device *pnd, const nfc_target *pnt)
Check target presence.
Definition: nfc.c:862
nfc.h
libnfc interface