libnfc  1.7.0-rc7
pn53x-sam.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  * Copyright (C) 2010 Emanuele Bertoldi
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 
44 #ifdef HAVE_CONFIG_H
45 # include "config.h"
46 #endif // HAVE_CONFIG_H
47 
48 #include <stdio.h>
49 #include <stdlib.h>
50 #include <time.h>
51 #include <unistd.h>
52 
53 #include <nfc/nfc.h>
54 
55 #include "utils/nfc-utils.h"
56 #include "libnfc/chips/pn53x.h"
57 
58 #define MAX_FRAME_LEN 264
59 #define TIMEOUT 60 // secs.
60 
61 static void
62 wait_one_minute(void)
63 {
64  int secs = 0;
65 
66  printf("|");
67  fflush(stdout);
68 
69  while (secs < TIMEOUT) {
70  sleep(1);
71  secs++;
72  printf(".");
73  fflush(stdout);
74  }
75 
76  printf("|\n");
77 }
78 
79 int
80 main(int argc, const char *argv[])
81 {
82  (void) argc;
83  (void) argv;
84 
85  nfc_context *context;
86  nfc_init(&context);
87  if (context == NULL) {
88  ERR("Unable to init libnfc (malloc)");
89  exit(EXIT_FAILURE);
90  }
91 
92  // Display libnfc version
93  const char *acLibnfcVersion = nfc_version();
94  printf("%s uses libnfc %s\n", argv[0], acLibnfcVersion);
95 
96  // Open using the first available NFC device
97  nfc_device *pnd;
98  pnd = nfc_open(context, NULL);
99 
100  if (pnd == NULL) {
101  ERR("%s", "Unable to open NFC device.");
102  nfc_exit(context);
103  exit(EXIT_FAILURE);
104  }
105 
106  printf("NFC device: %s opened\n", nfc_device_get_name(pnd));
107 
108  // Print the example's menu
109  printf("\nSelect the communication mode:\n");
110  printf("[1] Virtual card mode.\n");
111  printf("[2] Wired card mode.\n");
112  printf("[3] Dual card mode.\n");
113  printf(">> ");
114 
115  // Take user's choice
116  char input = getchar();
117  printf("\n");
118  if ((input < '1') || (input > '3')) {
119  ERR("%s", "Invalid selection.");
120  nfc_close(pnd);
121  nfc_exit(context);
122  exit(EXIT_FAILURE);
123  }
124 
125  /*
126  * '1' -> "Virtual mode" (0x02)
127  * '2' -> "Wired card" (0x03)
128  * '3' -> "Dual card" (0x04)
129  */
130  int iMode = input - '0' + 0x01;
131  pn532_sam_mode mode = iMode;
132 
133  // Connect with the SAM
134 
135  switch (mode) {
136  case PSM_VIRTUAL_CARD: {
137  // FIXME Its a private pn53x function
138  if (pn532_SAMConfiguration(pnd, mode, 0) < 0) {
139  nfc_perror(pnd, "pn53x_SAMConfiguration");
140  nfc_close(pnd);
141  nfc_exit(context);
142  exit(EXIT_FAILURE);
143  }
144  printf("Now the SAM is readable for 1 minute from an external reader.\n");
145  wait_one_minute();
146  }
147  break;
148 
149  case PSM_WIRED_CARD: {
150  // Set opened NFC device to initiator mode
151  if (nfc_initiator_init_secure_element(pnd) < 0) {
152  nfc_perror(pnd, "nfc_initiator_init_secure_element");
153  nfc_close(pnd);
154  nfc_exit(context);
155  exit(EXIT_FAILURE);
156  }
157 
158  // Let the reader only try once to find a tag
159  if (nfc_device_set_property_bool(pnd, NP_INFINITE_SELECT, false) < 0) {
160  nfc_perror(pnd, "nfc_device_set_property_bool");
161  nfc_close(pnd);
162  nfc_exit(context);
163  exit(EXIT_FAILURE);
164  }
165  // Read the SAM's info
166  const nfc_modulation nmSAM = {
167  .nmt = NMT_ISO14443A,
168  .nbr = NBR_106,
169  };
170  nfc_target nt;
171 
172  int res;
173  if ((res = nfc_initiator_select_passive_target(pnd, nmSAM, NULL, 0, &nt)) < 0) {
174  nfc_perror(pnd, "nfc_initiator_select_passive_target");
175  nfc_close(pnd);
176  nfc_exit(context);
177  exit(EXIT_FAILURE);
178  } else if (res == 0) {
179  ERR("No SAM found.");
180  nfc_close(pnd);
181  nfc_exit(context);
182  exit(EXIT_FAILURE);
183  } else if (res == 1) {
184  printf("The following ISO14443A tag (SAM) was found:\n");
185  print_nfc_target(&nt, true);
186  } else {
187  ERR("%s", "More than one ISO14442 tag found as SAM.");
188  nfc_close(pnd);
189  nfc_exit(context);
190  exit(EXIT_FAILURE);
191  }
192  }
193  break;
194 
195  case PSM_DUAL_CARD: {
196  // FIXME Its a private pn53x function
197  if (pn532_SAMConfiguration(pnd, mode, 0) < 0) {
198  nfc_perror(pnd, "pn53x_SAMConfiguration");
199  nfc_close(pnd);
200  nfc_exit(context);
201  exit(EXIT_FAILURE);
202  }
203  uint8_t abtRx[MAX_FRAME_LEN];
204 
205  nfc_target nt = {
206  .nm = {
207  .nmt = NMT_ISO14443A,
208  .nbr = NBR_UNDEFINED,
209  },
210  .nti = {
211  .nai = {
212  .abtAtqa = { 0x04, 0x00 },
213  .abtUid = { 0x08, 0xad, 0xbe, 0xef },
214  .btSak = 0x20,
215  .szUidLen = 4,
216  .szAtsLen = 0,
217  },
218  },
219  };
220  printf("Now both, NFC device (configured as target) and SAM are readables from an external NFC initiator.\n");
221  printf("Please note that NFC device (configured as target) stay in target mode until it receive RATS, ATR_REQ or proprietary command.\n");
222  if (nfc_target_init(pnd, &nt, abtRx, sizeof(abtRx), 0) < 0) {
223  nfc_perror(pnd, "nfc_target_init");
224  nfc_close(pnd);
225  nfc_exit(context);
226  exit(EXIT_FAILURE);
227  }
228  // wait_one_minute ();
229  }
230  break;
231  case PSM_NORMAL:
232  // This should not happend... nothing to do.
233  break;
234  }
235 
236  // Disconnect from the SAM
237  pn532_SAMConfiguration(pnd, PSM_NORMAL, -1);
238 
239  // Close NFC device
240  nfc_close(pnd);
241  nfc_exit(context);
242  exit(EXIT_SUCCESS);
243 }