2010-04-09 18:21:25 +02:00
|
|
|
/*-
|
|
|
|
* Public platform independent Near Field Communication (NFC) library
|
|
|
|
*
|
|
|
|
* Copyright (C) 2010, Romuald Conty
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU Lesser General Public License as published by the
|
|
|
|
* Free Software Foundation, either version 3 of the License, or (at your
|
|
|
|
* option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
|
|
|
* more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @file nfc-poll
|
|
|
|
* @brief Polling example
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
2010-06-07 11:51:31 +02:00
|
|
|
# include "config.h"
|
2010-04-09 18:21:25 +02:00
|
|
|
#endif // HAVE_CONFIG_H
|
|
|
|
|
2010-04-10 00:03:52 +02:00
|
|
|
#include <err.h>
|
2010-04-09 18:21:25 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stddef.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include <nfc/nfc.h>
|
|
|
|
#include <nfc/nfc-types.h>
|
|
|
|
#include <nfc/nfc-messages.h>
|
2010-04-09 23:57:03 +02:00
|
|
|
#include "nfc-utils.h"
|
2010-04-09 18:21:25 +02:00
|
|
|
|
|
|
|
#define MAX_DEVICE_COUNT 16
|
|
|
|
|
2010-06-07 11:51:31 +02:00
|
|
|
static nfc_device_t *pnd;
|
2010-04-09 18:21:25 +02:00
|
|
|
|
2010-06-07 11:51:31 +02:00
|
|
|
int
|
|
|
|
main (int argc, const char *argv[])
|
2010-04-09 18:21:25 +02:00
|
|
|
{
|
2010-09-07 19:51:03 +02:00
|
|
|
size_t szFound;
|
|
|
|
size_t i;
|
2010-10-19 14:50:52 +02:00
|
|
|
bool verbose = false;
|
2010-04-09 18:21:25 +02:00
|
|
|
nfc_device_desc_t *pnddDevices;
|
|
|
|
|
2010-10-19 14:50:52 +02:00
|
|
|
pnddDevices = parse_args (argc, argv, &szFound, &verbose);
|
|
|
|
|
2010-04-09 18:21:25 +02:00
|
|
|
// Display libnfc version
|
2010-06-07 11:51:31 +02:00
|
|
|
const char *acLibnfcVersion = nfc_version ();
|
2010-04-10 00:03:52 +02:00
|
|
|
|
|
|
|
if (argc > 1) {
|
|
|
|
errx (1, "usage: %s", argv[0]);
|
|
|
|
}
|
|
|
|
|
2010-06-07 11:51:31 +02:00
|
|
|
printf ("%s use libnfc %s\n", argv[0], acLibnfcVersion);
|
2010-04-09 18:21:25 +02:00
|
|
|
|
2010-10-19 14:50:52 +02:00
|
|
|
if (szFound == 0) {
|
|
|
|
if (!(pnddDevices = malloc (MAX_DEVICE_COUNT * sizeof (*pnddDevices)))) {
|
|
|
|
fprintf (stderr, "malloc() failed\n");
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
2010-04-09 18:21:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
nfc_list_devices (pnddDevices, MAX_DEVICE_COUNT, &szFound);
|
|
|
|
|
2010-06-07 11:51:31 +02:00
|
|
|
if (szFound == 0) {
|
2010-10-26 16:27:17 +02:00
|
|
|
printf ("No NFC device found.\n");
|
2010-04-09 18:21:25 +02:00
|
|
|
}
|
|
|
|
|
2010-06-07 11:51:31 +02:00
|
|
|
for (i = 0; i < szFound; i++) {
|
2010-04-09 18:21:25 +02:00
|
|
|
|
2010-08-10 21:50:29 +02:00
|
|
|
const byte_t btPollNr = 20;
|
|
|
|
const byte_t btPeriod = 2;
|
2010-10-13 19:43:23 +02:00
|
|
|
const nfc_modulation_t nmModulations[5] = {
|
|
|
|
{ .nmt = NMT_ISO14443A, .nbr = NBR_106 },
|
|
|
|
{ .nmt = NMT_ISO14443B, .nbr = NBR_106 },
|
|
|
|
{ .nmt = NMT_FELICA, .nbr = NBR_212 },
|
|
|
|
{ .nmt = NMT_FELICA, .nbr = NBR_424 },
|
|
|
|
{ .nmt = NMT_JEWEL, .nbr = NBR_106 },
|
|
|
|
};
|
|
|
|
const size_t szModulations = 5;
|
2010-08-10 21:50:29 +02:00
|
|
|
|
|
|
|
nfc_target_t antTargets[2];
|
2010-09-07 19:51:03 +02:00
|
|
|
size_t szTargetFound;
|
|
|
|
bool res;
|
2010-08-10 21:50:29 +02:00
|
|
|
|
|
|
|
pnd = nfc_connect (&(pnddDevices[i]));
|
2010-04-09 18:21:25 +02:00
|
|
|
|
2010-06-07 11:51:31 +02:00
|
|
|
if (pnd == NULL) {
|
|
|
|
ERR ("%s", "Unable to connect to NFC device.");
|
2010-04-09 18:21:25 +02:00
|
|
|
return 1;
|
|
|
|
}
|
2010-06-07 11:51:31 +02:00
|
|
|
nfc_initiator_init (pnd);
|
2010-04-09 18:21:25 +02:00
|
|
|
|
|
|
|
// Drop the field for a while
|
2010-08-18 19:22:13 +02:00
|
|
|
if (!nfc_configure (pnd, NDO_ACTIVATE_FIELD, false)) {
|
2010-09-07 19:51:03 +02:00
|
|
|
nfc_perror (pnd, "nfc_configure");
|
|
|
|
exit (EXIT_FAILURE);
|
2010-08-18 19:22:13 +02:00
|
|
|
}
|
2010-04-09 18:21:25 +02:00
|
|
|
// Let the reader only try once to find a tag
|
2010-08-18 19:22:13 +02:00
|
|
|
if (!nfc_configure (pnd, NDO_INFINITE_SELECT, false)) {
|
2010-09-07 19:51:03 +02:00
|
|
|
nfc_perror (pnd, "nfc_configure");
|
|
|
|
exit (EXIT_FAILURE);
|
2010-08-18 19:22:13 +02:00
|
|
|
}
|
2010-04-09 18:21:25 +02:00
|
|
|
// Enable field so more power consuming cards can power themselves up
|
2010-08-18 19:22:13 +02:00
|
|
|
if (!nfc_configure (pnd, NDO_ACTIVATE_FIELD, true)) {
|
2010-09-07 19:51:03 +02:00
|
|
|
nfc_perror (pnd, "nfc_configure");
|
|
|
|
exit (EXIT_FAILURE);
|
2010-08-18 19:22:13 +02:00
|
|
|
}
|
2010-04-09 18:21:25 +02:00
|
|
|
|
2010-06-07 11:51:31 +02:00
|
|
|
printf ("Connected to NFC reader: %s\n", pnd->acName);
|
2010-04-09 18:21:25 +02:00
|
|
|
|
2010-10-13 19:43:23 +02:00
|
|
|
printf ("PN532 will poll during %ld ms\n", (unsigned long) btPollNr * szModulations * btPeriod * 150);
|
|
|
|
res = nfc_initiator_poll_targets (pnd, nmModulations, szModulations, btPollNr, btPeriod, antTargets, &szTargetFound);
|
2010-06-07 11:51:31 +02:00
|
|
|
if (res) {
|
2010-08-10 21:50:29 +02:00
|
|
|
uint8_t n;
|
2010-06-07 11:51:31 +02:00
|
|
|
printf ("%ld target(s) have been found.\n", (unsigned long) szTargetFound);
|
2010-08-10 21:50:29 +02:00
|
|
|
for (n = 0; n < szTargetFound; n++) {
|
2010-10-13 21:17:51 +02:00
|
|
|
printf ("T%d: ", n + 1);
|
2010-10-19 14:50:52 +02:00
|
|
|
print_nfc_target ( antTargets[n], verbose );
|
2010-10-13 19:43:23 +02:00
|
|
|
|
2010-04-09 18:21:25 +02:00
|
|
|
}
|
|
|
|
} else {
|
2010-08-20 12:16:52 +02:00
|
|
|
nfc_perror (pnd, "nfc_initiator_poll_targets");
|
2010-09-23 11:40:23 +02:00
|
|
|
nfc_disconnect (pnd);
|
2010-08-20 12:16:52 +02:00
|
|
|
exit (EXIT_FAILURE);
|
2010-04-09 18:21:25 +02:00
|
|
|
}
|
2010-06-07 11:51:31 +02:00
|
|
|
nfc_disconnect (pnd);
|
|
|
|
}
|
2010-04-09 18:21:25 +02:00
|
|
|
|
|
|
|
free (pnddDevices);
|
|
|
|
return 0;
|
|
|
|
}
|