nfc-list.c

Go to the documentation of this file.
00001 /*-
00002  * Public platform independent Near Field Communication (NFC) library
00003  * 
00004  * Copyright (C) 2009, Roel Verdult
00005  * 
00006  * This program is free software: you can redistribute it and/or modify it
00007  * under the terms of the GNU Lesser General Public License as published by the
00008  * Free Software Foundation, either version 3 of the License, or (at your
00009  * option) any later version.
00010  * 
00011  * This program is distributed in the hope that it will be useful, but WITHOUT
00012  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00013  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
00014  * more details.
00015  *
00016  * You should have received a copy of the GNU Lesser General Public License
00017  * along with this program.  If not, see <http://www.gnu.org/licenses/>
00018  */
00019 
00025 #ifdef HAVE_CONFIG_H
00026   #include "config.h"
00027 #endif // HAVE_CONFIG_H
00028 
00029 #ifdef HAVE_LIBUSB
00030   #ifdef DEBUG
00031     #include <sys/param.h>
00032     #include <usb.h>
00033   #endif
00034 #endif
00035 
00036 #include <stdio.h>
00037 #include <stddef.h>
00038 #include <stdlib.h>
00039 #include <string.h>
00040 
00041 #include <nfc/nfc.h>
00042 #include <nfc/nfc-messages.h>
00043 #include "bitutils.h"
00044 
00045 #define MAX_DEVICE_COUNT 16
00046 
00047 static nfc_device_t* pnd;
00048 static byte_t abtFelica[5] = { 0x00, 0xff, 0xff, 0x00, 0x00 };
00049 
00050 int main(int argc, const char* argv[])
00051 {
00052   size_t szFound;
00053   size_t i;
00054   nfc_target_info_t nti;
00055   nfc_device_desc_t *pnddDevices;
00056 
00057   // Display libnfc version
00058   const char* acLibnfcVersion = nfc_version();
00059   printf("%s use libnfc %s\n", argv[0], acLibnfcVersion);
00060 
00061   #ifdef HAVE_LIBUSB
00062     #ifdef DEBUG
00063       usb_set_debug(4);
00064     #endif
00065   #endif
00066 
00067   /* Lazy way to open an NFC device */
00068 #if 0
00069   pnd = nfc_connect(NULL);
00070 #endif
00071 
00072   /* If specific device is wanted, i.e. an ARYGON device on /dev/ttyUSB0 */
00073 #if 0
00074   nfc_device_desc_t ndd;
00075   ndd.pcDriver = "ARYGON";
00076   ndd.pcPort = "/dev/ttyUSB0";
00077   ndd.uiSpeed = 115200;
00078 
00079   pnd = nfc_connect(&ndd);
00080 #endif
00081 
00082   if (!(pnddDevices = malloc (MAX_DEVICE_COUNT * sizeof (*pnddDevices))))
00083   {
00084     fprintf (stderr, "malloc() failed\n");
00085     return EXIT_FAILURE;
00086   }
00087 
00088   nfc_list_devices (pnddDevices, MAX_DEVICE_COUNT, &szFound);
00089 
00090   if (szFound == 0)
00091   {
00092     INFO("%s", "No device found.");
00093   }
00094 
00095   for (i = 0; i < szFound; i++)
00096   {
00097     pnd = nfc_connect(&(pnddDevices[i]));
00098 
00099 
00100     if (pnd == NULL)
00101     {
00102       ERR("%s", "Unable to connect to NFC device.");
00103       return 1;
00104     }
00105     nfc_initiator_init(pnd);
00106 
00107     // Drop the field for a while
00108     nfc_configure(pnd,NDO_ACTIVATE_FIELD,false);
00109 
00110     // Let the reader only try once to find a tag
00111     nfc_configure(pnd,NDO_INFINITE_SELECT,false);
00112 
00113     // Configure the CRC and Parity settings
00114     nfc_configure(pnd,NDO_HANDLE_CRC,true);
00115     nfc_configure(pnd,NDO_HANDLE_PARITY,true);
00116 
00117     // Enable field so more power consuming cards can power themselves up
00118     nfc_configure(pnd,NDO_ACTIVATE_FIELD,true);
00119 
00120     printf("\nConnected to NFC reader: %s\n\n",pnd->acName);
00121 
00122     // Poll for a ISO14443A (MIFARE) tag
00123     if (nfc_initiator_select_tag(pnd,NM_ISO14443A_106,NULL,0,&nti))
00124     {
00125       printf("The following (NFC) ISO14443A tag was found:\n\n");
00126       printf("    ATQA (SENS_RES): "); print_hex(nti.nai.abtAtqa,2);
00127       printf("       UID (NFCID%c): ",(nti.nai.abtUid[0]==0x08?'3':'1')); print_hex(nti.nai.abtUid,nti.nai.szUidLen);
00128       printf("      SAK (SEL_RES): "); print_hex(&nti.nai.btSak,1);
00129       if (nti.nai.szAtsLen)
00130       {
00131         printf("          ATS (ATR): ");
00132         print_hex(nti.nai.abtAts,nti.nai.szAtsLen);
00133       }
00134     }
00135 
00136     // Poll for a Felica tag
00137     if (nfc_initiator_select_tag(pnd,NM_FELICA_212,abtFelica,5,&nti) || nfc_initiator_select_tag(pnd,NM_FELICA_424,abtFelica,5,&nti))
00138     {
00139       printf("The following (NFC) Felica tag was found:\n\n");
00140       printf("%18s","ID (NFCID2): "); print_hex(nti.nfi.abtId,8);
00141       printf("%18s","Parameter (PAD): "); print_hex(nti.nfi.abtPad,8);
00142     }
00143 
00144     // Poll for a ISO14443B tag
00145     if (nfc_initiator_select_tag(pnd,NM_ISO14443B_106,(byte_t*)"\x00",1,&nti))
00146     {
00147       printf("The following (NFC) ISO14443-B tag was found:\n\n");
00148       printf("  ATQB: "); print_hex(nti.nbi.abtAtqb,12);
00149       printf("    ID: "); print_hex(nti.nbi.abtId,4);
00150       printf("   CID: %02x\n",nti.nbi.btCid);
00151       if (nti.nbi.szInfLen>0)
00152       {
00153         printf("   INF: "); print_hex(nti.nbi.abtInf,nti.nbi.szInfLen);
00154       }
00155       printf("PARAMS: %02x %02x %02x %02x\n",nti.nbi.btParam1,nti.nbi.btParam2,nti.nbi.btParam3,nti.nbi.btParam4);
00156     }
00157 
00158     // Poll for a Jewel tag
00159     if (nfc_initiator_select_tag(pnd,NM_JEWEL_106,NULL,0,&nti))
00160     {
00161       // No test results yet
00162       printf("jewel\n");
00163     }
00164 
00165     nfc_disconnect(pnd);
00166     }
00167 
00168   free (pnddDevices);
00169   return 0;
00170 }

Generated on 4 Sep 2010 for libnfc by  doxygen 1.6.1