Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00035 #ifdef HAVE_CONFIG_H
00036 # include "config.h"
00037 #endif // HAVE_CONFIG_H
00038
00039 #if defined(HAVE_READLINE)
00040 # include <stdio.h>
00041 # include <readline/readline.h>
00042 # include <readline/history.h>
00043 #else
00044 # define _GNU_SOURCE // for getline on system with glibc < 2.10
00045 # define _POSIX_C_SOURCE 200809L // for getline on system with glibc >= 2.10
00046 # include <stdio.h>
00047 extern FILE* stdin;
00048 #endif //HAVE_READLINE
00049
00050 #include <stdlib.h>
00051 #include <string.h>
00052 #include <ctype.h>
00053 #include <unistd.h>
00054
00055 #include <nfc/nfc.h>
00056 #include <nfc/nfc-messages.h>
00057
00058 #include "nfc-utils.h"
00059
00060 #include "chips/pn53x.h"
00061
00062 #define MAX_FRAME_LEN 264
00063
00064 int main(int argc, const char* argv[])
00065 {
00066 nfc_device_t* pnd;
00067 byte_t abtRx[MAX_FRAME_LEN];
00068 byte_t abtTx[MAX_FRAME_LEN] = { 0xD4 };
00069 size_t szRx;
00070 size_t szTx;
00071
00072
00073 pnd = nfc_connect(NULL);
00074
00075 if (pnd == NULL) {
00076 ERR ("%s", "Unable to connect to NFC device.");
00077 return EXIT_FAILURE;
00078 }
00079
00080 printf ("Connected to NFC reader: %s\n", pnd->acName);
00081
00082 nfc_initiator_init(pnd);
00083
00084 char * cmd;
00085 char * prompt="> ";
00086 while(1) {
00087 int offset=0;
00088 #if defined(HAVE_READLINE)
00089 cmd=readline(prompt);
00090
00091 if (cmd==NULL) {
00092 printf("Bye!\n");
00093 break;
00094 }
00095 add_history(cmd);
00096 #else
00097 cmd = NULL;
00098 printf("%s", prompt);
00099 fflush(0);
00100 size_t n;
00101 extern FILE* stdin;
00102 int s = getline(&cmd, &n, stdin);
00103 if (s <= 0) {
00104 printf("Bye!\n");
00105 free(cmd);
00106 break;
00107 }
00108
00109 printf("%s", cmd);
00110 #endif //HAVE_READLINE
00111 if (cmd[0]=='q') {
00112 printf("Bye!\n");
00113 free(cmd);
00114 break;
00115 }
00116 szTx = 0;
00117 for(int i = 0; i<MAX_FRAME_LEN-10; i++) {
00118 int size;
00119 byte_t byte;
00120 while (isspace(cmd[offset])) {
00121 offset++;
00122 }
00123 size = sscanf(cmd+offset, "%2x", (unsigned int*)&byte);
00124 if (size<1) {
00125 break;
00126 }
00127 abtTx[i+1] = byte;
00128 szTx++;
00129 if (cmd[offset+1] == 0) {
00130 break;
00131 }
00132 offset += 2;
00133 }
00134
00135 if ((int)szTx < 1) {
00136 free(cmd);
00137 continue;
00138 }
00139 szTx++;
00140 printf("Tx: ");
00141 print_hex((byte_t*)abtTx+1,szTx-1);
00142
00143 if (!pn53x_transceive (pnd, abtTx, szTx, abtRx, &szRx)) {
00144 free(cmd);
00145 nfc_perror (pnd, "Rx");
00146 continue;
00147 }
00148
00149 printf("Rx: ");
00150 print_hex(abtRx, szRx);
00151 free(cmd);
00152 }
00153
00154 nfc_disconnect(pnd);
00155 return 1;
00156 }