libnfc  1.4.2
pn53x-tamashell.c
Go to the documentation of this file.
1 /*-
2  * Public platform independent Near Field Communication (NFC) library examples
3  *
4  * Copyright (C) 2010, Romuald Conty
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  * 1) Redistributions of source code must retain the above copyright notice,
9  * this list of conditions and the following disclaimer.
10  * 2 )Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
15  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
18  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24  * POSSIBILITY OF SUCH DAMAGE.
25  *
26  * Note that this license only applies on the examples, NFC library itself is under LGPL
27  *
28  */
29 
35 #ifdef HAVE_CONFIG_H
36 # include "config.h"
37 #endif // HAVE_CONFIG_H
38 
39 # define _GNU_SOURCE // for getline on system with glibc < 2.10
40 # define _POSIX_C_SOURCE 200809L // for getline on system with glibc >= 2.10
41 # include <stdio.h>
42 #if defined(HAVE_READLINE)
43 # include <readline/readline.h>
44 # include <readline/history.h>
45 #endif //HAVE_READLINE
46 
47 #include <stdlib.h>
48 #include <string.h>
49 #include <ctype.h>
50 #include <unistd.h>
51 
52 #include <nfc/nfc.h>
53 #include <nfc/nfc-messages.h>
54 
55 #include "nfc-utils.h"
56 
57 #include "chips/pn53x.h"
58 
59 #define MAX_FRAME_LEN 264
60 
61 int main(int argc, const char* argv[])
62 {
63  nfc_device_t* pnd;
64  byte_t abtRx[MAX_FRAME_LEN];
65  byte_t abtTx[MAX_FRAME_LEN] = { 0xD4 };
66  size_t szRx;
67  size_t szTx;
68  extern FILE* stdin;
69  FILE* input = NULL;
70 
71  if (argc >= 2) {
72  if((input=fopen(argv[1], "r"))==NULL) {
73  ERR ("%s", "Cannot open file.");
74  return EXIT_FAILURE;
75  }
76  }
77 
78  // Try to open the NFC reader
79  pnd = nfc_connect(NULL);
80 
81  if (pnd == NULL) {
82  ERR ("%s", "Unable to connect to NFC device.");
83  return EXIT_FAILURE;
84  }
85 
86  printf ("Connected to NFC reader: %s\n", pnd->acName);
87  nfc_initiator_init(pnd);
88 
89  char * cmd;
90  char * prompt="> ";
91  while(1) {
92  int offset=0;
93 #if defined(HAVE_READLINE)
94  if (input==NULL) { // means we use stdin
95  cmd=readline(prompt);
96  // NULL if ctrl-d
97  if (cmd==NULL) {
98  printf("Bye!\n");
99  break;
100  }
101  add_history(cmd);
102  } else {
103 #endif //HAVE_READLINE
104  size_t n = 255;
105  char * ret = NULL;
106  cmd = malloc(n);
107  printf("%s", prompt);
108  fflush(0);
109  if (input != NULL) {
110  ret = fgets(cmd, n, input);
111  } else {
112  ret = fgets(cmd, n, stdin);
113  }
114  if (ret == NULL || strlen(cmd) <= 0) {
115  printf("Bye!\n");
116  free(cmd);
117  break;
118  }
119  // FIXME print only if read from redirected stdin (i.e. script)
120  printf("%s", cmd);
121 #if defined(HAVE_READLINE)
122  }
123 #endif //HAVE_READLINE
124  if (cmd[0]=='q') {
125  printf("Bye!\n");
126  free(cmd);
127  break;
128  }
129  if (cmd[0]=='p') {
130  int s=0;
131  offset++;
132  while (isspace(cmd[offset])) {
133  offset++;
134  }
135  sscanf(cmd+offset, "%u", &s);
136  printf("Pause for %i secs\n", s);
137  if (s>0) {
138  sleep(s);
139  }
140  free(cmd);
141  continue;
142  }
143  szTx = 0;
144  for(int i = 0; i<MAX_FRAME_LEN-10; i++) {
145  int size;
146  byte_t byte;
147  while (isspace(cmd[offset])) {
148  offset++;
149  }
150  size = sscanf(cmd+offset, "%2x", (unsigned int*)&byte);
151  if (size<1) {
152  break;
153  }
154  abtTx[i+1] = byte;
155  szTx++;
156  if (cmd[offset+1] == 0) { // if last hex was only 1 symbol
157  break;
158  }
159  offset += 2;
160  }
161 
162  if ((int)szTx < 1) {
163  free(cmd);
164  continue;
165  }
166  szTx++;
167  printf("Tx: ");
168  print_hex((byte_t*)abtTx+1,szTx-1);
169 
170  if (!pn53x_transceive (pnd, abtTx, szTx, abtRx, &szRx)) {
171  free(cmd);
172  nfc_perror (pnd, "Rx");
173  continue;
174  }
175 
176  printf("Rx: ");
177  print_hex(abtRx, szRx);
178  free(cmd);
179  }
180 
181  if (input != NULL) {
182  fclose(input);
183  }
184  nfc_disconnect(pnd);
185  return 1;
186 }