libnfc 1.4.2

nfc-poll.c

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 #include <err.h>
00040 #include <stdio.h>
00041 #include <stddef.h>
00042 #include <stdlib.h>
00043 #include <string.h>
00044 
00045 #include <nfc/nfc.h>
00046 #include <nfc/nfc-types.h>
00047 #include <nfc/nfc-messages.h>
00048 #include "nfc-utils.h"
00049 
00050 #define MAX_DEVICE_COUNT 16
00051 
00052 static nfc_device_t *pnd;
00053 
00054 int
00055 main (int argc, const char *argv[])
00056 {
00057   size_t  szFound;
00058   size_t  i;
00059   bool verbose = false;
00060   nfc_device_desc_t *pnddDevices;
00061 
00062   pnddDevices = parse_args (argc, argv, &szFound, &verbose);
00063 
00064   // Display libnfc version
00065   const char *acLibnfcVersion = nfc_version ();
00066 
00067   if (argc > 1) {
00068     errx (1, "usage: %s", argv[0]);
00069   }
00070 
00071   printf ("%s use libnfc %s\n", argv[0], acLibnfcVersion);
00072 
00073   if (szFound == 0) {
00074     if (!(pnddDevices = malloc (MAX_DEVICE_COUNT * sizeof (*pnddDevices)))) {
00075       fprintf (stderr, "malloc() failed\n");
00076       return EXIT_FAILURE;
00077     }
00078   }
00079 
00080   nfc_list_devices (pnddDevices, MAX_DEVICE_COUNT, &szFound);
00081 
00082   if (szFound == 0) {
00083     printf ("No NFC device found.\n");
00084   }
00085 
00086   for (i = 0; i < szFound; i++) {
00087 
00088     const byte_t btPollNr = 20;
00089     const byte_t btPeriod = 2;
00090     const nfc_modulation_t nmModulations[5] = {
00091       { .nmt = NMT_ISO14443A, .nbr = NBR_106 },
00092       { .nmt = NMT_ISO14443B, .nbr = NBR_106 },
00093       { .nmt = NMT_FELICA, .nbr = NBR_212 },
00094       { .nmt = NMT_FELICA, .nbr = NBR_424 },
00095       { .nmt = NMT_JEWEL, .nbr = NBR_106 },
00096     };
00097     const size_t szModulations = 5;
00098 
00099     nfc_target_t antTargets[2];
00100     size_t  szTargetFound;
00101     bool    res;
00102 
00103     pnd = nfc_connect (&(pnddDevices[i]));
00104 
00105     if (pnd == NULL) {
00106       ERR ("%s", "Unable to connect to NFC device.");
00107       return 1;
00108     }
00109     nfc_initiator_init (pnd);
00110 
00111     // Drop the field for a while
00112     if (!nfc_configure (pnd, NDO_ACTIVATE_FIELD, false)) {
00113       nfc_perror (pnd, "nfc_configure");
00114       exit (EXIT_FAILURE);
00115     }
00116     // Let the reader only try once to find a tag
00117     if (!nfc_configure (pnd, NDO_INFINITE_SELECT, false)) {
00118       nfc_perror (pnd, "nfc_configure");
00119       exit (EXIT_FAILURE);
00120     }
00121     // Enable field so more power consuming cards can power themselves up
00122     if (!nfc_configure (pnd, NDO_ACTIVATE_FIELD, true)) {
00123       nfc_perror (pnd, "nfc_configure");
00124       exit (EXIT_FAILURE);
00125     }
00126 
00127     printf ("Connected to NFC reader: %s\n", pnd->acName);
00128 
00129     printf ("PN532 will poll during %ld ms\n", (unsigned long) btPollNr * szModulations * btPeriod * 150);
00130     res = nfc_initiator_poll_targets (pnd, nmModulations, szModulations, btPollNr, btPeriod, antTargets, &szTargetFound);
00131     if (res) {
00132       uint8_t n;
00133       printf ("%ld target(s) have been found.\n", (unsigned long) szTargetFound);
00134       for (n = 0; n < szTargetFound; n++) {
00135         printf ("T%d: ", n + 1);
00136         print_nfc_target ( antTargets[n], verbose );
00137 
00138       }
00139     } else {
00140       nfc_perror (pnd, "nfc_initiator_poll_targets");
00141       nfc_disconnect (pnd);
00142       exit (EXIT_FAILURE);
00143     }
00144     nfc_disconnect (pnd);
00145   }
00146 
00147   free (pnddDevices);
00148   return 0;
00149 }