00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
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
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
00112 if (!nfc_configure (pnd, NDO_ACTIVATE_FIELD, false)) {
00113 nfc_perror (pnd, "nfc_configure");
00114 exit (EXIT_FAILURE);
00115 }
00116
00117 if (!nfc_configure (pnd, NDO_INFINITE_SELECT, false)) {
00118 nfc_perror (pnd, "nfc_configure");
00119 exit (EXIT_FAILURE);
00120 }
00121
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 }