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, 2O1O, Roel Verdult, Romuald Conty
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 <err.h>
00037 #include <stdio.h>
00038 #include <stddef.h>
00039 #include <stdlib.h>
00040 #include <string.h>
00041 
00042 #include <nfc/nfc.h>
00043 #include <nfc/nfc-messages.h>
00044 #include "nfc-utils.h"
00045 
00046 #define MAX_DEVICE_COUNT 16
00047 #define MAX_TARGET_COUNT 16
00048 
00049 static nfc_device_t *pnd;
00050 
00051 int
00052 main (int argc, const char *argv[])
00053 {
00054   const char *acLibnfcVersion;
00055   size_t  szDeviceFound;
00056   size_t  szTargetFound;
00057   size_t  i;
00058   nfc_device_desc_t *pnddDevices;
00059 
00060   // Display libnfc version
00061   acLibnfcVersion = nfc_version ();
00062   printf ("%s use libnfc %s\n", argv[0], acLibnfcVersion);
00063 
00064   pnddDevices = parse_device_desc (argc, argv, &szDeviceFound);
00065 
00066   if (argc > 1 && szDeviceFound == 0) {
00067     errx (1, "usage: %s [--device driver:port:speed]", argv[0]);
00068   }
00069 #ifdef HAVE_LIBUSB
00070 #  ifdef DEBUG
00071   usb_set_debug (4);
00072 #  endif
00073 #endif
00074 
00075   /* Lazy way to open an NFC device */
00076 #if 0
00077   pnd = nfc_connect (NULL);
00078 #endif
00079 
00080   /* If specific device is wanted, i.e. an ARYGON device on /dev/ttyUSB0 */
00081 #if 0
00082   nfc_device_desc_t ndd;
00083   ndd.pcDriver = "ARYGON";
00084   ndd.pcPort = "/dev/ttyUSB0";
00085   ndd.uiSpeed = 115200;
00086 
00087   pnd = nfc_connect (&ndd);
00088 #endif
00089 
00090   if (szDeviceFound == 0) {
00091     if (!(pnddDevices = malloc (MAX_DEVICE_COUNT * sizeof (*pnddDevices)))) {
00092       fprintf (stderr, "malloc() failed\n");
00093       return EXIT_FAILURE;
00094     }
00095 
00096     nfc_list_devices (pnddDevices, MAX_DEVICE_COUNT, &szDeviceFound);
00097   }
00098 
00099   if (szDeviceFound == 0) {
00100     INFO ("%s", "No device found.");
00101   }
00102 
00103   for (i = 0; i < szDeviceFound; i++) {
00104     nfc_target_info_t anti[MAX_TARGET_COUNT];
00105     pnd = nfc_connect (&(pnddDevices[i]));
00106 
00107 
00108     if (pnd == NULL) {
00109       ERR ("%s", "Unable to connect to NFC device.");
00110       return 1;
00111     }
00112     nfc_initiator_init (pnd);
00113 
00114     // Drop the field for a while
00115     if (!nfc_configure (pnd, NDO_ACTIVATE_FIELD, false)) {
00116       nfc_perror (pnd, "nfc_configure");
00117       exit (EXIT_FAILURE);
00118     }
00119     // Let the reader only try once to find a tag
00120     if (!nfc_configure (pnd, NDO_INFINITE_SELECT, false)) {
00121       nfc_perror (pnd, "nfc_configure");
00122       exit (EXIT_FAILURE);
00123     }
00124     // Configure the CRC and Parity settings
00125     if (!nfc_configure (pnd, NDO_HANDLE_CRC, true)) {
00126       nfc_perror (pnd, "nfc_configure");
00127       exit (EXIT_FAILURE);
00128     }
00129     if (!nfc_configure (pnd, NDO_HANDLE_PARITY, true)) {
00130       nfc_perror (pnd, "nfc_configure");
00131       exit (EXIT_FAILURE);
00132     }
00133     // Enable field so more power consuming cards can power themselves up
00134     if (!nfc_configure (pnd, NDO_ACTIVATE_FIELD, true)) {
00135       nfc_perror (pnd, "nfc_configure");
00136       exit (EXIT_FAILURE);
00137     }
00138 
00139     if (!nfc_configure (pnd, NDO_AUTO_ISO14443_4, true)) {
00140       nfc_perror (pnd, "nfc_configure");
00141       exit (EXIT_FAILURE);
00142     }
00143 
00144     printf ("Connected to NFC reader: %s\n", pnd->acName);
00145 
00146     // List ISO14443A targets
00147     if (nfc_initiator_list_passive_targets (pnd, NM_ISO14443A_106, anti, MAX_TARGET_COUNT, &szTargetFound)) {
00148       size_t  n;
00149       printf ("%d ISO14443A passive target(s) was found%s\n", (int) szTargetFound, (szTargetFound == 0) ? ".\n" : ":");
00150       for (n = 0; n < szTargetFound; n++) {
00151         print_nfc_iso14443a_info (anti[n].nai);
00152         printf ("\n");
00153       }
00154     }
00155     // List Felica tags
00156     if (nfc_initiator_list_passive_targets (pnd, NM_FELICA_212, anti, MAX_TARGET_COUNT, &szTargetFound)) {
00157       size_t  n;
00158       printf ("%d Felica (212 kbps) passive target(s) was found%s\n", (int) szTargetFound,
00159               (szTargetFound == 0) ? ".\n" : ":");
00160       for (n = 0; n < szTargetFound; n++) {
00161         print_nfc_felica_info (anti[n].nfi);
00162         printf ("\n");
00163       }
00164     }
00165     if (nfc_initiator_list_passive_targets (pnd, NM_FELICA_424, anti, MAX_TARGET_COUNT, &szTargetFound)) {
00166       size_t  n;
00167       printf ("%d Felica (424 kbps) passive target(s) was found%s\n", (int) szTargetFound,
00168               (szTargetFound == 0) ? ".\n" : ":");
00169       for (n = 0; n < szTargetFound; n++) {
00170         print_nfc_felica_info (anti[n].nfi);
00171         printf ("\n");
00172       }
00173     }
00174     // List ISO14443B targets
00175     if (nfc_initiator_list_passive_targets (pnd, NM_ISO14443B_106, anti, MAX_TARGET_COUNT, &szTargetFound)) {
00176       size_t  n;
00177       printf ("%d ISO14443B passive target(s) was found%s\n", (int) szTargetFound, (szTargetFound == 0) ? ".\n" : ":");
00178       for (n = 0; n < szTargetFound; n++) {
00179         print_nfc_iso14443b_info (anti[n].nbi);
00180         printf ("\n");
00181       }
00182     }
00183 
00184 /*
00185     // List Jewel targets
00186     if (nfc_initiator_list_passive_targets(pnd, NM_JEWEL_106, anti, MAX_TARGET_COUNT, &szTargetFound )) {
00187       size_t n;
00188       printf("%d Jewel passive target(s) was found%s\n", (int)szTargetFound, (szTargetFound==0)?".\n":":"); for(n=0; n<szTargetFound; n++) {
00189         printf("Jewel support is missing in libnfc, feel free to contribute.\n");
00190         printf("\n");
00191       }
00192     }
00193 */
00194     nfc_disconnect (pnd);
00195   }
00196 
00197   free (pnddDevices);
00198   return 0;
00199 }