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 # define _GNU_SOURCE // for getline on system with glibc < 2.10
00040 # define _POSIX_C_SOURCE 200809L // for getline on system with glibc >= 2.10
00041 # include <stdio.h>
00042 #if defined(HAVE_READLINE)
00043 # include <readline/readline.h>
00044 # include <readline/history.h>
00045 #endif //HAVE_READLINE
00046
00047 #include <stdlib.h>
00048 #include <string.h>
00049 #include <ctype.h>
00050 #include <unistd.h>
00051
00052 #include <nfc/nfc.h>
00053 #include <nfc/nfc-messages.h>
00054
00055 #include "nfc-utils.h"
00056
00057 #include "chips/pn53x.h"
00058
00059 #define MAX_FRAME_LEN 264
00060
00061 int main(int argc, const char* argv[])
00062 {
00063 nfc_device_t* pnd;
00064 byte_t abtRx[MAX_FRAME_LEN];
00065 byte_t abtTx[MAX_FRAME_LEN] = { 0xD4 };
00066 size_t szRx;
00067 size_t szTx;
00068 extern FILE* stdin;
00069 FILE* input = NULL;
00070
00071 if (argc >= 2) {
00072 if((input=fopen(argv[1], "r"))==NULL) {
00073 ERR ("%s", "Cannot open file.");
00074 return EXIT_FAILURE;
00075 }
00076 }
00077
00078
00079 pnd = nfc_connect(NULL);
00080
00081 if (pnd == NULL) {
00082 ERR ("%s", "Unable to connect to NFC device.");
00083 return EXIT_FAILURE;
00084 }
00085
00086 printf ("Connected to NFC reader: %s\n", pnd->acName);
00087 nfc_initiator_init(pnd);
00088
00089 char * cmd;
00090 char * prompt="> ";
00091 while(1) {
00092 int offset=0;
00093 #if defined(HAVE_READLINE)
00094 if (input==NULL) {
00095 cmd=readline(prompt);
00096
00097 if (cmd==NULL) {
00098 printf("Bye!\n");
00099 break;
00100 }
00101 add_history(cmd);
00102 } else {
00103 #endif //HAVE_READLINE
00104 size_t n = 255;
00105 char * ret = NULL;
00106 cmd = malloc(n);
00107 printf("%s", prompt);
00108 fflush(0);
00109 if (input != NULL) {
00110 ret = fgets(cmd, n, input);
00111 } else {
00112 ret = fgets(cmd, n, stdin);
00113 }
00114 if (ret == NULL || strlen(cmd) <= 0) {
00115 printf("Bye!\n");
00116 free(cmd);
00117 break;
00118 }
00119
00120 printf("%s", cmd);
00121 #if defined(HAVE_READLINE)
00122 }
00123 #endif //HAVE_READLINE
00124 if (cmd[0]=='q') {
00125 printf("Bye!\n");
00126 free(cmd);
00127 break;
00128 }
00129 if (cmd[0]=='p') {
00130 int s=0;
00131 offset++;
00132 while (isspace(cmd[offset])) {
00133 offset++;
00134 }
00135 sscanf(cmd+offset, "%u", &s);
00136 printf("Pause for %i secs\n", s);
00137 if (s>0) {
00138 sleep(s);
00139 }
00140 free(cmd);
00141 continue;
00142 }
00143 szTx = 0;
00144 for(int i = 0; i<MAX_FRAME_LEN-10; i++) {
00145 int size;
00146 byte_t byte;
00147 while (isspace(cmd[offset])) {
00148 offset++;
00149 }
00150 size = sscanf(cmd+offset, "%2x", (unsigned int*)&byte);
00151 if (size<1) {
00152 break;
00153 }
00154 abtTx[i+1] = byte;
00155 szTx++;
00156 if (cmd[offset+1] == 0) {
00157 break;
00158 }
00159 offset += 2;
00160 }
00161
00162 if ((int)szTx < 1) {
00163 free(cmd);
00164 continue;
00165 }
00166 szTx++;
00167 printf("Tx: ");
00168 print_hex((byte_t*)abtTx+1,szTx-1);
00169
00170 if (!pn53x_transceive (pnd, abtTx, szTx, abtRx, &szRx)) {
00171 free(cmd);
00172 nfc_perror (pnd, "Rx");
00173 continue;
00174 }
00175
00176 printf("Rx: ");
00177 print_hex(abtRx, szRx);
00178 free(cmd);
00179 }
00180
00181 if (input != NULL) {
00182 fclose(input);
00183 }
00184 nfc_disconnect(pnd);
00185 return 1;
00186 }