pn53x-tamashell.c

Go to the documentation of this file.
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 #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   // Try to open the NFC reader
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     // NULL if ctrl-d
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     // FIXME print only if read from redirected stdin (i.e. script)
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) { // if last hex was only 1 symbol
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 }