nfc-poll.c

00001 /*-
00002  * Public platform independent Near Field Communication (NFC) library
00003  * 
00004  * Copyright (C) 2010, 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 #include <err.h>
00030 #include <stdio.h>
00031 #include <stddef.h>
00032 #include <stdlib.h>
00033 #include <string.h>
00034 
00035 #include <nfc/nfc.h>
00036 #include <nfc/nfc-types.h>
00037 #include <nfc/nfc-messages.h>
00038 #include "nfc-utils.h"
00039 
00040 #define MAX_DEVICE_COUNT 16
00041 
00042 static nfc_device_t *pnd;
00043 
00044 int
00045 main (int argc, const char *argv[])
00046 {
00047   size_t  szFound;
00048   size_t  i;
00049   nfc_device_desc_t *pnddDevices;
00050 
00051   // Display libnfc version
00052   const char *acLibnfcVersion = nfc_version ();
00053 
00054   if (argc > 1) {
00055     errx (1, "usage: %s", argv[0]);
00056   }
00057 
00058   printf ("%s use libnfc %s\n", argv[0], acLibnfcVersion);
00059 
00060   if (!(pnddDevices = malloc (MAX_DEVICE_COUNT * sizeof (*pnddDevices)))) {
00061     fprintf (stderr, "malloc() failed\n");
00062     return EXIT_FAILURE;
00063   }
00064 
00065   nfc_list_devices (pnddDevices, MAX_DEVICE_COUNT, &szFound);
00066 
00067   if (szFound == 0) {
00068     INFO ("%s", "No device found.");
00069   }
00070 
00071   for (i = 0; i < szFound; i++) {
00072 
00073     const byte_t btPollNr = 20;
00074     const byte_t btPeriod = 2;
00075     const nfc_target_type_t nttMifare = NTT_MIFARE;
00076     const size_t szTargetTypes = 1;
00077 
00078     nfc_target_t antTargets[2];
00079     size_t  szTargetFound;
00080     bool    res;
00081 
00082     pnd = nfc_connect (&(pnddDevices[i]));
00083 
00084     if (pnd == NULL) {
00085       ERR ("%s", "Unable to connect to NFC device.");
00086       return 1;
00087     }
00088     nfc_initiator_init (pnd);
00089 
00090     // Drop the field for a while
00091     if (!nfc_configure (pnd, NDO_ACTIVATE_FIELD, false)) {
00092       nfc_perror (pnd, "nfc_configure");
00093       exit (EXIT_FAILURE);
00094     }
00095     // Let the reader only try once to find a tag
00096     if (!nfc_configure (pnd, NDO_INFINITE_SELECT, false)) {
00097       nfc_perror (pnd, "nfc_configure");
00098       exit (EXIT_FAILURE);
00099     }
00100     // Configure the CRC and Parity settings
00101     if (!nfc_configure (pnd, NDO_HANDLE_CRC, true)) {
00102       nfc_perror (pnd, "nfc_configure");
00103       exit (EXIT_FAILURE);
00104     }
00105     if (!nfc_configure (pnd, NDO_HANDLE_PARITY, true)) {
00106       nfc_perror (pnd, "nfc_configure");
00107       exit (EXIT_FAILURE);
00108     }
00109     // Enable field so more power consuming cards can power themselves up
00110     if (!nfc_configure (pnd, NDO_ACTIVATE_FIELD, true)) {
00111       nfc_perror (pnd, "nfc_configure");
00112       exit (EXIT_FAILURE);
00113     }
00114 
00115     printf ("Connected to NFC reader: %s\n", pnd->acName);
00116 
00117     printf ("PN53x will poll during %ld ms\n", (unsigned long) btPollNr * szTargetTypes * btPeriod * 150);
00118     res = nfc_initiator_poll_targets (pnd, &nttMifare, 1, btPollNr, btPeriod, antTargets, &szTargetFound);
00119     if (res) {
00120       uint8_t n;
00121       printf ("%ld target(s) have been found.\n", (unsigned long) szTargetFound);
00122       for (n = 0; n < szTargetFound; n++) {
00123         printf ("T%d: targetType=%02x, ", n + 1, antTargets[n].ntt);
00124         printf ("targetData:\n");
00125         print_nfc_iso14443a_info (antTargets[n].nti.nai);
00126       }
00127     } else {
00128       nfc_perror (pnd, "nfc_initiator_poll_targets");
00129       exit (EXIT_FAILURE);
00130     }
00131 
00132     nfc_disconnect (pnd);
00133   }
00134 
00135   free (pnddDevices);
00136   return 0;
00137 }