libnfc 1.4.2
|
00001 /*- 00002 * Public platform independent Near Field Communication (NFC) library examples 00003 * 00004 * Copyright (C) 2010, Romuald Conty 00005 * 00006 * Redistribution and use in source and binary forms, with or without 00007 * modification, are permitted provided that the following conditions are met: 00008 * 1) Redistributions of source code must retain the above copyright notice, 00009 * this list of conditions and the following disclaimer. 00010 * 2 )Redistributions in binary form must reproduce the above copyright 00011 * notice, this list of conditions and the following disclaimer in the 00012 * documentation and/or other materials provided with the distribution. 00013 * 00014 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 00015 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00016 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 00017 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 00018 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 00019 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 00020 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 00021 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 00022 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 00023 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00024 * POSSIBILITY OF SUCH DAMAGE. 00025 * 00026 * Note that this license only applies on the examples, NFC library itself is under LGPL 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 // Try to open the NFC reader 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) { // means we use stdin 00095 cmd=readline(prompt); 00096 // NULL if ctrl-d 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 // FIXME print only if read from redirected stdin (i.e. script) 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) { // if last hex was only 1 symbol 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 }