libnfc  1.7.0-rc7
pn53x-tamashell.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 <stdio.h>
46 #if defined(HAVE_READLINE)
47 # include <readline/readline.h>
48 # include <readline/history.h>
49 #endif //HAVE_READLINE
50 
51 #include <stdlib.h>
52 #include <string.h>
53 #include <ctype.h>
54 #include <time.h>
55 
56 #ifndef _WIN32
57 # include <time.h>
58 # define msleep(x) do { \
59  struct timespec xsleep; \
60  xsleep.tv_sec = x / 1000; \
61  xsleep.tv_nsec = (x - xsleep.tv_sec * 1000) * 1000 * 1000; \
62  nanosleep(&xsleep, NULL); \
63  } while (0)
64 #else
65 # include <winbase.h>
66 # define msleep Sleep
67 #endif
68 
69 
70 #include <nfc/nfc.h>
71 
72 #include "utils/nfc-utils.h"
73 #include "libnfc/chips/pn53x.h"
74 
75 #define MAX_FRAME_LEN 264
76 
77 int main(int argc, const char *argv[])
78 {
79  nfc_device *pnd;
80  uint8_t abtRx[MAX_FRAME_LEN];
81  uint8_t abtTx[MAX_FRAME_LEN];
82  size_t szRx = sizeof(abtRx);
83  size_t szTx;
84  FILE *input = NULL;
85 
86  if (argc >= 2) {
87  if ((input = fopen(argv[1], "r")) == NULL) {
88  ERR("%s", "Cannot open file.");
89  exit(EXIT_FAILURE);
90  }
91  }
92 
93  nfc_context *context;
94  nfc_init(&context);
95  if (context == NULL) {
96  ERR("Unable to init libnfc (malloc)");
97  exit(EXIT_FAILURE);
98  }
99 
100  // Try to open the NFC reader
101  pnd = nfc_open(context, NULL);
102 
103  if (pnd == NULL) {
104  ERR("%s", "Unable to open NFC device.");
105  if (input != NULL) {
106  fclose(input);
107  }
108  nfc_exit(context);
109  exit(EXIT_FAILURE);
110  }
111 
112  printf("NFC reader: %s opened\n", nfc_device_get_name(pnd));
113  if (nfc_initiator_init(pnd) < 0) {
114  nfc_perror(pnd, "nfc_initiator_init");
115  if (input != NULL) {
116  fclose(input);
117  }
118  nfc_close(pnd);
119  nfc_exit(context);
120  exit(EXIT_FAILURE);
121  }
122 
123  const char *prompt = "> ";
124  while (1) {
125  int offset = 0;
126  char *cmd;
127 #if defined(HAVE_READLINE)
128  if (input == NULL) { // means we use stdin
129  cmd = readline(prompt);
130  // NULL if ctrl-d
131  if (cmd == NULL) {
132  printf("Bye!\n");
133  break;
134  }
135  add_history(cmd);
136  } else {
137 #endif //HAVE_READLINE
138  size_t n = 512;
139  char *ret = NULL;
140  cmd = malloc(n);
141  printf("%s", prompt);
142  fflush(0);
143  if (input != NULL) {
144  ret = fgets(cmd, n, input);
145  } else {
146  ret = fgets(cmd, n, stdin);
147  }
148  if (ret == NULL || strlen(cmd) <= 0) {
149  printf("Bye!\n");
150  free(cmd);
151  break;
152  }
153  // FIXME print only if read from redirected stdin (i.e. script)
154  printf("%s", cmd);
155 #if defined(HAVE_READLINE)
156  }
157 #endif //HAVE_READLINE
158  if (cmd[0] == 'q') {
159  printf("Bye!\n");
160  free(cmd);
161  break;
162  }
163  if (cmd[0] == 'p') {
164  int ms = 0;
165  offset++;
166  while (isspace(cmd[offset])) {
167  offset++;
168  }
169  sscanf(cmd + offset, "%10d", &ms);
170  printf("Pause for %i msecs\n", ms);
171  if (ms > 0) {
172  msleep(ms);
173  }
174  free(cmd);
175  continue;
176  }
177  szTx = 0;
178  for (int i = 0; i < MAX_FRAME_LEN; i++) {
179  int size;
180  unsigned int byte;
181  while (isspace(cmd[offset])) {
182  offset++;
183  }
184  size = sscanf(cmd + offset, "%2x", &byte);
185  if (size < 1) {
186  break;
187  }
188  abtTx[i] = byte;
189  szTx++;
190  if (cmd[offset + 1] == 0) { // if last hex was only 1 symbol
191  break;
192  }
193  offset += 2;
194  }
195 
196  if ((int)szTx < 1) {
197  free(cmd);
198  continue;
199  }
200  printf("Tx: ");
201  print_hex(abtTx, szTx);
202 
203  szRx = sizeof(abtRx);
204  int res = 0;
205  if ((res = pn53x_transceive(pnd, abtTx, szTx, abtRx, szRx, 0)) < 0) {
206  free(cmd);
207  nfc_perror(pnd, "Rx");
208  continue;
209  }
210  szRx = (size_t) res;
211 
212  printf("Rx: ");
213  print_hex(abtRx, szRx);
214  free(cmd);
215  }
216 
217  if (input != NULL) {
218  fclose(input);
219  }
220  nfc_close(pnd);
221  nfc_exit(context);
222  exit(EXIT_SUCCESS);
223 }