nfc-list.c

Go to the documentation of this file.
00001 /*-
00002  * Public platform independent Near Field Communication (NFC) library examples
00003  * 
00004  * Copyright (C) 2009, Roel Verdult
00005  * Copyright (C) 2010, Romuald Conty, Romain Tartière
00006  * 
00007  * Redistribution and use in source and binary forms, with or without
00008  * modification, are permitted provided that the following conditions are met:
00009  *  1) Redistributions of source code must retain the above copyright notice,
00010  *  this list of conditions and the following disclaimer. 
00011  *  2 )Redistributions in binary form must reproduce the above copyright
00012  *  notice, this list of conditions and the following disclaimer in the
00013  *  documentation and/or other materials provided with the distribution.
00014  *
00015  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00016  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00017  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00018  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
00019  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00020  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00021  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00022  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00023  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00024  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00025  * POSSIBILITY OF SUCH DAMAGE.
00026  * 
00027  * Note that this license only applies on the examples, NFC library itself is under LGPL
00028  *
00029  */
00030 
00036 #ifdef HAVE_CONFIG_H
00037 #  include "config.h"
00038 #endif // HAVE_CONFIG_H
00039 
00040 #ifdef HAVE_LIBUSB
00041 #  ifdef DEBUG
00042 #    include <sys/param.h>
00043 #    include <usb.h>
00044 #  endif
00045 #endif
00046 
00047 #include <err.h>
00048 #include <stdio.h>
00049 #include <stddef.h>
00050 #include <stdlib.h>
00051 #include <string.h>
00052 
00053 #include <nfc/nfc.h>
00054 #include <nfc/nfc-messages.h>
00055 #include "nfc-utils.h"
00056 
00057 #define MAX_DEVICE_COUNT 16
00058 #define MAX_TARGET_COUNT 16
00059 
00060 static nfc_device_t *pnd;
00061 
00062 int
00063 main (int argc, const char *argv[])
00064 {
00065   const char *acLibnfcVersion;
00066   size_t  szDeviceFound;
00067   size_t  szTargetFound;
00068   size_t  i;
00069   bool verbose = false;
00070   nfc_device_desc_t *pnddDevices;
00071 
00072   // Display libnfc version
00073   acLibnfcVersion = nfc_version ();
00074   printf ("%s use libnfc %s\n", argv[0], acLibnfcVersion);
00075 
00076   pnddDevices = parse_args (argc, argv, &szDeviceFound, &verbose);
00077 #ifdef HAVE_LIBUSB
00078 #  ifdef DEBUG
00079   usb_set_debug (4);
00080 #  endif
00081 #endif
00082 
00083   /* Lazy way to open an NFC device */
00084 #if 0
00085   pnd = nfc_connect (NULL);
00086 #endif
00087 
00088   /* If specific device is wanted, i.e. an ARYGON device on /dev/ttyUSB0 */
00089 #if 0
00090   nfc_device_desc_t ndd;
00091   ndd.pcDriver = "ARYGON";
00092   ndd.pcPort = "/dev/ttyUSB0";
00093   ndd.uiSpeed = 115200;
00094   pnd = nfc_connect (&ndd);
00095 #endif
00096 
00097   /* If specific device is wanted, i.e. a SCL3711 on USB */
00098 #if 0
00099   nfc_device_desc_t ndd;
00100   ndd.pcDriver = "PN533_USB";
00101   strcpy(ndd.acDevice, "SCM Micro / SCL3711-NFC&RW");
00102   pnd = nfc_connect (&ndd);
00103 #endif
00104 
00105   if (szDeviceFound == 0) {
00106     if (!(pnddDevices = malloc (MAX_DEVICE_COUNT * sizeof (*pnddDevices)))) {
00107       fprintf (stderr, "malloc() failed\n");
00108       return EXIT_FAILURE;
00109     }
00110 
00111     nfc_list_devices (pnddDevices, MAX_DEVICE_COUNT, &szDeviceFound);
00112   }
00113 
00114   if (szDeviceFound == 0) {
00115     printf ("No NFC device found.\n");
00116   }
00117 
00118   for (i = 0; i < szDeviceFound; i++) {
00119     nfc_target_t ant[MAX_TARGET_COUNT];
00120     pnd = nfc_connect (&(pnddDevices[i]));
00121 
00122     if (pnd == NULL) {
00123       ERR ("%s", "Unable to connect to NFC device.");
00124       return EXIT_FAILURE;
00125     }
00126     nfc_initiator_init (pnd);
00127 
00128     printf ("Connected to NFC device: %s\n", pnd->acName);
00129 
00130     // List ISO14443A targets
00131     nfc_modulation_t nm = {
00132       .nmt = NMT_ISO14443A,
00133       .nbr = NBR_106,
00134     };
00135     if (nfc_initiator_list_passive_targets (pnd, nm, ant, MAX_TARGET_COUNT, &szTargetFound)) {
00136       size_t  n;
00137       if (verbose || (szTargetFound > 0)) {
00138         printf ("%d ISO14443A passive target(s) was found%s\n", (int) szTargetFound, (szTargetFound == 0) ? ".\n" : ":");
00139       }
00140       for (n = 0; n < szTargetFound; n++) {
00141         print_nfc_iso14443a_info (ant[n].nti.nai, verbose);
00142         printf ("\n");
00143       }
00144     }
00145 
00146     nm.nmt = NMT_FELICA;
00147     nm.nbr = NBR_212;
00148     // List Felica tags
00149     if (nfc_initiator_list_passive_targets (pnd, nm, ant, MAX_TARGET_COUNT, &szTargetFound)) {
00150       size_t  n;
00151       if (verbose || (szTargetFound > 0)) {
00152         printf ("%d Felica (212 kbps) passive target(s) was found%s\n", (int) szTargetFound,
00153                 (szTargetFound == 0) ? ".\n" : ":");
00154       }
00155       for (n = 0; n < szTargetFound; n++) {
00156         print_nfc_felica_info (ant[n].nti.nfi, verbose);
00157         printf ("\n");
00158       }
00159     }
00160 
00161     nm.nbr = NBR_424;
00162     if (nfc_initiator_list_passive_targets (pnd, nm, ant, MAX_TARGET_COUNT, &szTargetFound)) {
00163       size_t  n;
00164       if (verbose || (szTargetFound > 0)) {
00165         printf ("%d Felica (424 kbps) passive target(s) was found%s\n", (int) szTargetFound,
00166                 (szTargetFound == 0) ? ".\n" : ":");
00167       }
00168       for (n = 0; n < szTargetFound; n++) {
00169         print_nfc_felica_info (ant[n].nti.nfi, verbose);
00170         printf ("\n");
00171       }
00172     }
00173 
00174     nm.nmt = NMT_ISO14443B;
00175     nm.nbr = NBR_106;
00176     // List ISO14443B targets
00177     if (nfc_initiator_list_passive_targets (pnd, nm, ant, MAX_TARGET_COUNT, &szTargetFound)) {
00178       size_t  n;
00179       if (verbose || (szTargetFound > 0)) {
00180         printf ("%d ISO14443B passive target(s) was found%s\n", (int) szTargetFound, (szTargetFound == 0) ? ".\n" : ":");
00181       }
00182       for (n = 0; n < szTargetFound; n++) {
00183         print_nfc_iso14443b_info (ant[n].nti.nbi, verbose);
00184         printf ("\n");
00185       }
00186     }
00187 
00188     nm.nmt = NMT_JEWEL;
00189     nm.nbr = NBR_106;
00190     // List Jewel targets
00191     if (nfc_initiator_list_passive_targets(pnd, nm, ant, MAX_TARGET_COUNT, &szTargetFound )) {
00192       size_t n;
00193       if (verbose || (szTargetFound > 0)) {
00194         printf("%d Jewel passive target(s) was found%s\n", (int)szTargetFound, (szTargetFound==0)?".\n":":");
00195       }
00196       for(n=0; n<szTargetFound; n++) {
00197         print_nfc_jewel_info (ant[n].nti.nji, verbose);
00198         printf("\n");
00199       }
00200     }
00201     nfc_disconnect (pnd);
00202   }
00203 
00204   free (pnddDevices);
00205   return 0;
00206 }