Merge libnfc-1.5-new-api branch to trunk (r1168:1303).

This commit is contained in:
Audrey Diacre 2012-01-25 09:56:05 +00:00
commit 26245add73
82 changed files with 4481 additions and 3212 deletions

View file

@ -1,4 +1,4 @@
SET(EXAMPLES-SOURCES nfc-anticol nfc-dep-initiator nfc-dep-target nfc-emulate-tag nfc-emulate-uid nfc-poll nfc-relay)
SET(EXAMPLES-SOURCES nfc-anticol nfc-dep-initiator nfc-dep-target nfc-emulate-forum-tag2 nfc-emulate-tag nfc-emulate-uid nfc-poll nfc-relay)
INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}/../libnfc)

View file

@ -4,6 +4,7 @@ bin_PROGRAMS = \
nfc-anticol \
nfc-dep-initiator \
nfc-dep-target \
nfc-emulate-forum-tag2 \
nfc-emulate-tag \
nfc-emulate-uid \
nfc-poll \
@ -16,6 +17,9 @@ bin_PROGRAMS += \
pn53x-tamashell
endif
check_PROGRAMS = \
quick_start_example1
# set the include path found by configure
INCLUDES = $(all_includes) $(LIBNFC_CFLAGS)
@ -33,6 +37,10 @@ nfc_relay_SOURCES = nfc-relay.c
nfc_relay_LDADD = $(top_builddir)/libnfc/libnfc.la \
$(top_builddir)/utils/libnfcutils.la
nfc_emulate_forum_tag2_SOURCES = nfc-emulate-forum-tag2.c
nfc_emulate_forum_tag2_LDADD = $(top_builddir)/libnfc/libnfc.la \
$(top_builddir)/utils/libnfcutils.la
nfc_emulate_tag_SOURCES = nfc-emulate-tag.c
nfc_emulate_tag_LDADD = $(top_builddir)/libnfc/libnfc.la \
$(top_builddir)/utils/libnfcutils.la
@ -62,6 +70,11 @@ pn53x_tamashell_LDADD = $(top_builddir)/libnfc/libnfc.la \
$(top_builddir)/utils/libnfcutils.la
pn53x_tamashell_LDFLAGS = @READLINE_LIBS@
quick_start_example1_SOURCES = doc/quick_start_example1.c
quick_start_example1_LDADD = $(top_builddir)/libnfc/libnfc.la \
$(top_builddir)/utils/libnfcutils.la
dist_man_MANS = \
nfc-anticol.1 \
nfc-dep-initiator.1 \
@ -74,9 +87,4 @@ dist_man_MANS = \
pn53x-sam.1 \
pn53x-tamashell.1
if HAS_LOG4C
AM_CFLAGS += @log4c_CFLAGS@
LIBADD = @log4c_LIBS@
endif
EXTRA_DIST = CMakeLists.txt

View file

@ -5,45 +5,42 @@
#include <stdlib.h>
#include <nfc/nfc.h>
void
print_hex (const byte_t * pbtData, const size_t szBytes)
{
size_t szPos;
for (szPos = 0; szPos < szBytes; szPos++) {
printf ("%02x ", pbtData[szPos]);
}
printf ("\n");
}
#include "utils/nfc-utils.h"
#include "libnfc/chips/pn53x.h"
int
main (int argc, const char *argv[])
{
nfc_device_t *pnd;
nfc_target_t nt;
nfc_device *pnd;
nfc_target nt;
nfc_init (NULL);
// Display libnfc version
const char *acLibnfcVersion = nfc_version ();
printf ("%s uses libnfc %s\n", argv[0], acLibnfcVersion);
// Connect using the first available NFC device
pnd = nfc_connect (NULL);
// Open, using the first available NFC device
pnd = nfc_open (NULL, NULL);
if (pnd == NULL) {
fprintf (stderr, "Unable to connect to NFC device.");
ERR ("%s", "Unable to open NFC device.");
return EXIT_FAILURE;
}
// Set connected NFC device to initiator mode
nfc_initiator_init (pnd);
// Set opened NFC device to initiator mode
if (nfc_initiator_init (pnd) < 0) {
nfc_perror (pnd, "nfc_initiator_init");
exit (EXIT_FAILURE);
}
printf ("Connected to NFC reader: %s\n", pnd->acName);
printf ("NFC reader: %s opened\n", nfc_device_get_name (pnd));
// Poll for a ISO14443A (MIFARE) tag
const nfc_modulation_t nmMifare = {
const nfc_modulation nmMifare = {
.nmt = NMT_ISO14443A,
.nbr = NBR_106,
};
if (nfc_initiator_select_passive_target (pnd, nmMifare, NULL, 0, &nt)) {
if (nfc_initiator_select_passive_target (pnd, nmMifare, NULL, 0, &nt) > 0) {
printf ("The following (NFC) ISO14443A tag was found:\n");
printf (" ATQA (SENS_RES): ");
print_hex (nt.nti.nai.abtAtqa, 2);
@ -56,7 +53,8 @@ main (int argc, const char *argv[])
print_hex (nt.nti.nai.abtAts, nt.nti.nai.szAtsLen);
}
}
// Disconnect from NFC device
nfc_disconnect (pnd);
// Close NFC device
nfc_close (pnd);
nfc_exit (NULL);
return EXIT_SUCCESS;
}

View file

@ -51,31 +51,31 @@
#define MAX_FRAME_LEN 264
static byte_t abtRx[MAX_FRAME_LEN];
static size_t szRxBits;
static uint8_t abtRx[MAX_FRAME_LEN];
static int szRxBits;
static size_t szRx = sizeof(abtRx);
static byte_t abtRawUid[12];
static byte_t abtAtqa[2];
static byte_t abtSak;
static byte_t abtAts[MAX_FRAME_LEN];
static byte_t szAts = 0;
static uint8_t abtRawUid[12];
static uint8_t abtAtqa[2];
static uint8_t abtSak;
static uint8_t abtAts[MAX_FRAME_LEN];
static uint8_t szAts = 0;
static size_t szCL = 1;//Always start with Cascade Level 1 (CL1)
static nfc_device_t *pnd;
static nfc_device *pnd;
bool quiet_output = false;
bool force_rats = false;
bool iso_ats_supported = false;
// ISO14443A Anti-Collision Commands
byte_t abtReqa[1] = { 0x26 };
byte_t abtSelectAll[2] = { 0x93, 0x20 };
byte_t abtSelectTag[9] = { 0x93, 0x70, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
byte_t abtRats[4] = { 0xe0, 0x50, 0x00, 0x00 };
byte_t abtHalt[4] = { 0x50, 0x00, 0x00, 0x00 };
uint8_t abtReqa[1] = { 0x26 };
uint8_t abtSelectAll[2] = { 0x93, 0x20 };
uint8_t abtSelectTag[9] = { 0x93, 0x70, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
uint8_t abtRats[4] = { 0xe0, 0x50, 0x00, 0x00 };
uint8_t abtHalt[4] = { 0x50, 0x00, 0x00, 0x00 };
#define CASCADE_BIT 0x04
static bool
transmit_bits (const byte_t * pbtTx, const size_t szTxBits)
transmit_bits (const uint8_t *pbtTx, const size_t szTxBits)
{
// Show transmitted command
if (!quiet_output) {
@ -83,7 +83,7 @@ transmit_bits (const byte_t * pbtTx, const size_t szTxBits)
print_hex_bits (pbtTx, szTxBits);
}
// Transmit the bit frame command, we don't use the arbitrary parity feature
if (!nfc_initiator_transceive_bits (pnd, pbtTx, szTxBits, NULL, abtRx, &szRxBits, NULL))
if ((szRxBits = nfc_initiator_transceive_bits (pnd, pbtTx, szTxBits, NULL, abtRx, NULL)) < 0)
return false;
// Show received answer
@ -97,7 +97,7 @@ transmit_bits (const byte_t * pbtTx, const size_t szTxBits)
static bool
transmit_bytes (const byte_t * pbtTx, const size_t szTx)
transmit_bytes (const uint8_t *pbtTx, const size_t szTx)
{
// Show transmitted command
if (!quiet_output) {
@ -105,7 +105,7 @@ transmit_bytes (const byte_t * pbtTx, const size_t szTx)
print_hex (pbtTx, szTx);
}
// Transmit the command bytes
if (!nfc_initiator_transceive_bytes (pnd, pbtTx, szTx, abtRx, &szRx, NULL))
if (nfc_initiator_transceive_bytes (pnd, pbtTx, szTx, abtRx, &szRx, 0) < 0)
return false;
// Show received answer
@ -148,39 +148,45 @@ main (int argc, char *argv[])
}
}
nfc_init (NULL);
// Try to open the NFC reader
pnd = nfc_connect (NULL);
pnd = nfc_open (NULL, NULL);
if (!pnd) {
printf ("Error connecting NFC reader\n");
printf ("Error opening NFC reader\n");
exit(EXIT_FAILURE);
}
// Initialise NFC device as "initiator"
nfc_initiator_init (pnd);
if (nfc_initiator_init (pnd) < 0) {
nfc_perror (pnd, "nfc_initiator_init");
exit (EXIT_FAILURE);
}
// Configure the CRC
if (!nfc_configure (pnd, NDO_HANDLE_CRC, false)) {
nfc_perror (pnd, "nfc_configure");
if (nfc_device_set_property_bool (pnd, NP_HANDLE_CRC, false) < 0) {
nfc_perror (pnd, "nfc_device_set_property_bool");
exit (EXIT_FAILURE);
}
// Use raw send/receive methods
if (!nfc_configure (pnd, NDO_EASY_FRAMING, false)) {
nfc_perror (pnd, "nfc_configure");
if (nfc_device_set_property_bool (pnd, NP_EASY_FRAMING, false) < 0) {
nfc_perror (pnd, "nfc_device_set_property_bool");
exit (EXIT_FAILURE);
}
// Disable 14443-4 autoswitching
if (!nfc_configure (pnd, NDO_AUTO_ISO14443_4, false)) {
nfc_perror (pnd, "nfc_configure");
if (nfc_device_set_property_bool (pnd, NP_AUTO_ISO14443_4, false) < 0) {
nfc_perror (pnd, "nfc_device_set_property_bool");
exit (EXIT_FAILURE);
}
printf ("Connected to NFC reader: %s\n\n", pnd->acName);
printf ("NFC reader: %s opened\n\n", nfc_device_get_name (pnd));
// Send the 7 bits request command specified in ISO 14443A (0x26)
if (!transmit_bits (abtReqa, 7)) {
printf ("Error: No tag available\n");
nfc_disconnect (pnd);
nfc_close (pnd);
nfc_exit (NULL);
return 1;
}
memcpy (abtAtqa, abtRx, 2);
@ -309,6 +315,7 @@ main (int argc, char *argv[])
print_hex (abtAts, szAts);
}
nfc_disconnect (pnd);
nfc_close (pnd);
nfc_exit (NULL);
return 0;
}

View file

@ -48,7 +48,7 @@
#define MAX_FRAME_LEN 264
static nfc_device_t *pnd;
static nfc_device *pnd;
void stop_dep_communication (int sig)
{
@ -62,38 +62,40 @@ void stop_dep_communication (int sig)
int
main (int argc, const char *argv[])
{
nfc_target_t nt;
byte_t abtRx[MAX_FRAME_LEN];
nfc_target nt;
uint8_t abtRx[MAX_FRAME_LEN];
size_t szRx = sizeof(abtRx);
byte_t abtTx[] = "Hello World!";
uint8_t abtTx[] = "Hello World!";
if (argc > 1) {
printf ("Usage: %s\n", argv[0]);
return EXIT_FAILURE;
}
nfc_init (NULL);
pnd = nfc_connect (NULL);
pnd = nfc_open (NULL, NULL);
if (!pnd) {
printf("Unable to connect to NFC device.\n");
printf("Unable to open NFC device.\n");
return EXIT_FAILURE;
}
printf ("Connected to NFC device: %s\n", pnd->acName);
printf ("NFC device: %s\n opened", nfc_device_get_name (pnd));
signal (SIGINT, stop_dep_communication);
if (!nfc_initiator_init (pnd)) {
if (nfc_initiator_init (pnd) < 0) {
nfc_perror(pnd, "nfc_initiator_init");
return EXIT_FAILURE;
goto error;
}
if(!nfc_initiator_select_dep_target (pnd, NDM_PASSIVE, NBR_212, NULL, &nt)) {
if(nfc_initiator_select_dep_target (pnd, NDM_PASSIVE, NBR_212, NULL, &nt, 1000) < 0) {
nfc_perror(pnd, "nfc_initiator_select_dep_target");
goto error;
}
print_nfc_target (nt, false);
printf ("Sending: %s\n", abtTx);
if (!nfc_initiator_transceive_bytes (pnd, abtTx, sizeof(abtTx), abtRx, &szRx, NULL)) {
if (nfc_initiator_transceive_bytes (pnd, abtTx, sizeof(abtTx), abtRx, &szRx, 0) < 0) {
nfc_perror(pnd, "nfc_initiator_transceive_bytes");
goto error;
}
@ -101,9 +103,13 @@ main (int argc, const char *argv[])
abtRx[szRx] = 0;
printf ("Received: %s\n", abtRx);
nfc_initiator_deselect_target (pnd);
if (nfc_initiator_deselect_target (pnd) < 0) {
nfc_perror(pnd, "nfc_initiator_deselect_target");
goto error;
}
error:
nfc_disconnect (pnd);
nfc_close (pnd);
nfc_exit (NULL);
return EXIT_SUCCESS;
}

View file

@ -47,7 +47,7 @@
#define MAX_FRAME_LEN 264
static nfc_device_t *pnd;
static nfc_device *pnd;
void stop_dep_communication (int sig)
{
@ -61,23 +61,23 @@ void stop_dep_communication (int sig)
int
main (int argc, const char *argv[])
{
byte_t abtRx[MAX_FRAME_LEN];
size_t szRx = sizeof(abtRx);
size_t szDeviceFound;
byte_t abtTx[] = "Hello Mars!";
uint8_t abtRx[MAX_FRAME_LEN];
int szRx;
uint8_t abtTx[] = "Hello Mars!";
#define MAX_DEVICE_COUNT 2
nfc_device_desc_t pnddDevices[MAX_DEVICE_COUNT];
nfc_list_devices (pnddDevices, MAX_DEVICE_COUNT, &szDeviceFound);
nfc_connstring connstrings[MAX_DEVICE_COUNT];
size_t szDeviceFound = nfc_list_devices (NULL, connstrings, MAX_DEVICE_COUNT);
// Little hack to allow using nfc-dep-initiator & nfc-dep-target from
// the same machine: if there is more than one readers connected
// nfc-dep-target will connect to the second reader
// the same machine: if there is more than one readers opened
// nfc-dep-target will open the second reader
// (we hope they're always detected in the same order)
nfc_init (NULL);
if (szDeviceFound == 1) {
pnd = nfc_connect (&(pnddDevices[0]));
pnd = nfc_open (NULL, connstrings[0]);
} else if (szDeviceFound > 1) {
pnd = nfc_connect (&(pnddDevices[1]));
pnd = nfc_open (NULL, connstrings[1]);
} else {
printf("No device found.");
printf("No device found.\n");
return EXIT_FAILURE;
}
@ -86,7 +86,7 @@ main (int argc, const char *argv[])
return EXIT_FAILURE;
}
nfc_target_t nt = {
nfc_target nt = {
.nm = {
.nmt = NMT_DEP,
.nbr = NBR_UNDEFINED
@ -108,10 +108,10 @@ main (int argc, const char *argv[])
};
if (!pnd) {
printf("Unable to connect to NFC device.\n");
printf("Unable to open NFC device.\n");
return EXIT_FAILURE;
}
printf ("Connected to NFC device: %s\n", pnd->acName);
printf ("NFC device: %s opened\n", nfc_device_get_name (pnd));
signal (SIGINT, stop_dep_communication);
@ -119,27 +119,28 @@ main (int argc, const char *argv[])
print_nfc_target (nt, false);
printf ("Waiting for initiator request...\n");
if(!nfc_target_init (pnd, &nt, abtRx, &szRx)) {
if ((szRx = nfc_target_init (pnd, &nt, abtRx, sizeof(abtRx), 0)) < 0) {
nfc_perror(pnd, "nfc_target_init");
goto error;
}
printf("Initiator request received. Waiting for data...\n");
if (!nfc_target_receive_bytes (pnd, abtRx, &szRx, NULL)) {
if ((szRx = nfc_target_receive_bytes (pnd, abtRx, sizeof (abtRx), 0)) < 0) {
nfc_perror(pnd, "nfc_target_receive_bytes");
goto error;
}
abtRx[szRx] = '\0';
abtRx[(size_t) szRx] = '\0';
printf ("Received: %s\n", abtRx);
printf ("Sending: %s\n", abtTx);
if (!nfc_target_send_bytes (pnd, abtTx, sizeof(abtTx), NULL)) {
if (nfc_target_send_bytes (pnd, abtTx, sizeof(abtTx), 0) < 0) {
nfc_perror(pnd, "nfc_target_send_bytes");
goto error;
}
printf("Data sent.\n");
error:
nfc_disconnect (pnd);
nfc_close (pnd);
nfc_exit (NULL);
return EXIT_SUCCESS;
}

View file

@ -0,0 +1,211 @@
/*-
* Public platform independent Near Field Communication (NFC) library examples
*
* Copyright (C) 2011, Romuald Conty
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* 1) Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2 )Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* Note that this license only applies on the examples, NFC library itself is under LGPL
*
*/
/**
* @file nfc-emulate-forum-tag2.c
* @brief Emulates a NFC Forum Tag Type 2 with a NDEF message
* This example allow to emulate an NFC Forum Tag Type 2 that contains a read-only NDEF message.
*
* It have been developed using PN533 USB hardware as target and Google Nexus S phone as initiator.
*
* This is know to NOT work with Nokia 6212 Classic and could not work with
* several NFC Forum compiliant devices due to these reasons:
* - The emulated target only have a 4 bytes UID where 7 bytes UID (as a real
* Mifare Ultralight tag) are usually attempted;
* - The chip is emulating a ISO/IEC 14443-3 tag, without any hardware helper.
* If the initiator have too short timeouts for software-based emulation
* (which is usually the case), this example will failed, this is not a bug
* and we can't do anything using this kind of hardware (PN531/PN533).
*/
/*
* This implementation was written based on information provided by the
* following documents:
*
* NFC Forum Type 2 Tag Operation
* Technical Specification
* NFCForum-TS-Type-2-Tag_1.0 - 2007-07-09
*
* ISO/IEC 14443-3
* First edition - 2001-02-01
* Identification cards Contactless integrated circuit(s) cards Proximity cards
* Part 3: Initialization and anticollision
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif // HAVE_CONFIG_H
#include <errno.h>
#include <signal.h>
#include <stdlib.h>
#include <nfc/nfc.h>
#include <nfc/nfc-emulation.h>
#include "utils/nfc-utils.h"
static nfc_device *pnd;
void
stop_emulation (int sig)
{
(void)sig;
if (pnd) {
nfc_abort_command(pnd);
} else {
exit (EXIT_FAILURE);
}
}
static uint8_t __nfcforum_tag2_memory_area[] = {
0x00, 0x00, 0x00, 0x00, // Block 0
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0xFF, 0xFF, // Block 2 (Static lock bytes: CC area and data area are read-only locked)
0xE1, 0x10, 0x06, 0x0F, // Block 3 (CC - NFC-Forum Tag Type 2 version 1.0, Data area (from block 4 to the end) is 48 bytes, Read-only mode)
0x03, 33, 0xd1, 0x02, // Block 4 (NDEF)
0x1c, 0x53, 0x70, 0x91,
0x01, 0x09, 0x54, 0x02,
0x65, 0x6e, 0x4c, 0x69,
0x62, 0x6e, 0x66, 0x63,
0x51, 0x01, 0x0b, 0x55,
0x03, 0x6c, 0x69, 0x62,
0x6e, 0x66, 0x63, 0x2e,
0x6f, 0x72, 0x67, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
};
#define READ 0x30
#define WRITE 0xA2
#define SECTOR_SELECT 0xC2
#define HALT 0x50
int
nfcforum_tag2_io (struct nfc_emulator *emulator, const uint8_t *data_in, const size_t data_in_len, uint8_t *data_out, const size_t data_out_len)
{
int res = 0;
uint8_t *nfcforum_tag2_memory_area = (uint8_t *)(emulator->user_data);
printf (" In: ");
print_hex (data_in, data_in_len);
switch (data_in[0]) {
case READ:
if (data_out_len >= 16) {
memcpy(data_out, nfcforum_tag2_memory_area + (data_in[1] * 4), 16);
res = 16;
} else {
res = -ENOSPC;
}
break;
case HALT:
printf ("HALT sent\n");
res = -ECONNABORTED;
break;
default:
printf ("Unknown command: 0x%02x\n", data_in[0]);
res = -ENOTSUP;
}
if (res < 0) {
ERR ("%s (%d)", strerror (-res), -res);
} else {
printf (" Out: ");
print_hex (data_out, res);
}
return res;
}
int
main(int argc, char *argv[])
{
(void)argc;
(void)argv;
nfc_target nt = {
.nm = {
.nmt = NMT_ISO14443A,
.nbr = NBR_UNDEFINED, // Will be updated by nfc_target_init()
},
.nti = {
.nai = {
.abtAtqa = { 0x00, 0x04 },
.abtUid = { 0x08, 0x00, 0xb0, 0x0b },
.szUidLen = 4,
.btSak = 0x00,
.szAtsLen = 0,
},
}
};
struct nfc_emulation_state_machine state_machine = {
.io = nfcforum_tag2_io
};
struct nfc_emulator emulator = {
.target = &nt,
.state_machine = &state_machine,
.user_data = __nfcforum_tag2_memory_area,
};
signal (SIGINT, stop_emulation);
nfc_init (NULL);
pnd = nfc_open (NULL, NULL);
if (pnd == NULL) {
ERR("Unable to open NFC device");
exit (EXIT_FAILURE);
}
printf ("NFC device: %s opened\n", nfc_device_get_name (pnd));
printf ("Emulating NDEF tag now, please touch it with a second NFC device\n");
if (nfc_emulate_target (pnd, &emulator) < 0) {
goto error;
}
nfc_close(pnd);
nfc_exit (NULL);
exit (EXIT_SUCCESS);
error:
if (pnd) {
nfc_perror (pnd, argv[0]);
nfc_close (pnd);
nfc_exit (NULL);
}
}

View file

@ -54,9 +54,9 @@
#define MAX_FRAME_LEN (264)
#define SAK_ISO14443_4_COMPLIANT 0x20
static byte_t abtRx[MAX_FRAME_LEN];
static size_t szRx = sizeof(abtRx);
static nfc_device_t *pnd;
static uint8_t abtRx[MAX_FRAME_LEN];
static int szRx;
static nfc_device *pnd;
static bool quiet_output = false;
static bool init_mfc_auth = false;
@ -65,13 +65,14 @@ intr_hdlr (void)
{
printf ("\nQuitting...\n");
if (pnd != NULL) {
nfc_disconnect(pnd);
nfc_close(pnd);
}
nfc_exit (NULL);
exit (EXIT_FAILURE);
}
bool
target_io( nfc_target_t * pnt, const byte_t * pbtInput, const size_t szInput, byte_t * pbtOutput, size_t *pszOutput )
target_io( nfc_target *pnt, const uint8_t *pbtInput, const size_t szInput, uint8_t *pbtOutput, size_t *pszOutput )
{
bool loop = true;
*pszOutput = 0;
@ -134,31 +135,31 @@ target_io( nfc_target_t * pnt, const byte_t * pbtInput, const size_t szInput, by
}
bool
nfc_target_emulate_tag(nfc_device_t* pnd, nfc_target_t * pnt)
nfc_target_emulate_tag(nfc_device *pnd, nfc_target *pnt)
{
size_t szTx;
byte_t abtTx[MAX_FRAME_LEN];
uint8_t abtTx[MAX_FRAME_LEN];
bool loop = true;
if (!nfc_target_init (pnd, pnt, abtRx, &szRx)) {
if ((szRx = nfc_target_init (pnd, pnt, abtRx, sizeof(abtRx), 0)) < 0) {
nfc_perror (pnd, "nfc_target_init");
return false;
}
while ( loop ) {
loop = target_io( pnt, abtRx, szRx, abtTx, &szTx );
loop = target_io( pnt, abtRx, (size_t) szRx, abtTx, &szTx );
if (szTx) {
if (!nfc_target_send_bytes(pnd, abtTx, szTx, NULL)) {
if (nfc_target_send_bytes(pnd, abtTx, szTx, 0) < 0) {
nfc_perror (pnd, "nfc_target_send_bytes");
return false;
}
}
if ( loop ) {
if ( init_mfc_auth ) {
nfc_configure (pnd, NDO_HANDLE_CRC, false);
nfc_device_set_property_bool (pnd, NP_HANDLE_CRC, false);
init_mfc_auth = false;
}
if (!nfc_target_receive_bytes(pnd, abtRx, &szRx, NULL)) {
if ((szRx = nfc_target_receive_bytes(pnd, abtRx, sizeof (abtRx), 0)) < 0) {
nfc_perror (pnd, "nfc_target_receive_bytes");
return false;
}
@ -179,19 +180,21 @@ main (int argc, char *argv[])
signal (SIGINT, (void (*)()) intr_hdlr);
#endif
nfc_init (NULL);
// Try to open the NFC reader
pnd = nfc_connect (NULL);
pnd = nfc_open (NULL, NULL);
// Display libnfc version
acLibnfcVersion = nfc_version ();
printf ("%s uses libnfc %s\n", argv[0], acLibnfcVersion);
if (pnd == NULL) {
ERR("Unable to connect to NFC device");
ERR("Unable to open NFC device");
exit (EXIT_FAILURE);
}
printf ("Connected to NFC device: %s\n", pnd->acName);
printf ("NFC device: %s opened\n", nfc_device_get_name (pnd));
// Notes for ISO14443-A emulated tags:
// * Only short UIDs are supported
@ -203,7 +206,7 @@ main (int argc, char *argv[])
// Example of a Mifare Classic Mini
// Note that crypto1 is not implemented in this example
nfc_target_t nt = {
nfc_target nt = {
.nm = {
.nmt = NMT_ISO14443A,
.nbr = NBR_UNDEFINED,
@ -220,40 +223,53 @@ main (int argc, char *argv[])
};
/*
// Example of a FeliCa
nfc_target_t nt = {
.nm.nmt = NMT_FELICA,
.nm.nbr = NBR_UNDEFINED,
.nti.nfi.abtId = { 0x01, 0xFE, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xFF },
.nti.nfi.abtPad = { 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xFF },
.nti.nfi.abtSysCode = { 0xFF, 0xFF },
nfc_target nt = {
.nm = {
.nmt = NMT_FELICA,
.nbr = NBR_UNDEFINED,
},
.nti = {
.nfi = {
.abtId = { 0x01, 0xFE, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xFF },
.abtPad = { 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xFF },
.abtSysCode = { 0xFF, 0xFF },
},
},
};
*/
/*
// Example of a ISO14443-4 (DESfire)
nfc_target_t nt = {
.nm.nmt = NMT_ISO14443A,
.nm.nbr = NBR_UNDEFINED,
.nti.nai.abtAtqa = { 0x03, 0x44 },
.nti.nai.abtUid = { 0x08, 0xab, 0xcd, 0xef },
.nti.nai.btSak = 0x20,
.nti.nai.szUidLen = 4,
.nti.nai.abtAts = { 0x75, 0x77, 0x81, 0x02, 0x80 },
.nti.nai.szAtsLen = 5,
nfc_target nt = {
.nm = {
.nmt = NMT_ISO14443A,
.nbr = NBR_UNDEFINED,
},
.nti = {
.nai = {
abtAtqa = { 0x03, 0x44 },
abtUid = { 0x08, 0xab, 0xcd, 0xef },
btSak = 0x20,
.szUidLen = 4,
.abtAts = { 0x75, 0x77, 0x81, 0x02, 0x80 },
.szAtsLen = 5,
},
},
};
*/
printf ("%s will emulate this ISO14443-A tag:\n", argv[0]);
print_nfc_iso14443a_info (nt.nti.nai, true);
// Switch off NDO_EASY_FRAMING if target is not ISO14443-4
nfc_configure (pnd, NDO_EASY_FRAMING, (nt.nti.nai.btSak & SAK_ISO14443_4_COMPLIANT));
// Switch off NP_EASY_FRAMING if target is not ISO14443-4
nfc_device_set_property_bool (pnd, NP_EASY_FRAMING, (nt.nti.nai.btSak & SAK_ISO14443_4_COMPLIANT));
printf ("NFC device (configured as target) is now emulating the tag, please touch it with a second NFC device (initiator)\n");
if (!nfc_target_emulate_tag (pnd, &nt)) {
nfc_perror (pnd, "nfc_target_emulate_tag");
exit (EXIT_FAILURE);
}
nfc_disconnect(pnd);
nfc_close(pnd);
nfc_exit (NULL);
exit (EXIT_SUCCESS);
}

View file

@ -56,14 +56,14 @@
#define MAX_FRAME_LEN 264
static byte_t abtRecv[MAX_FRAME_LEN];
static size_t szRecvBits;
static nfc_device_t *pnd;
static uint8_t abtRecv[MAX_FRAME_LEN];
static int szRecvBits;
static nfc_device *pnd;
// ISO14443A Anti-Collision response
byte_t abtAtqa[2] = { 0x04, 0x00 };
byte_t abtUidBcc[5] = { 0xDE, 0xAD, 0xBE, 0xEF, 0x62 };
byte_t abtSak[9] = { 0x08, 0xb6, 0xdd };
uint8_t abtAtqa[2] = { 0x04, 0x00 };
uint8_t abtUidBcc[5] = { 0xDE, 0xAD, 0xBE, 0xEF, 0x62 };
uint8_t abtSak[9] = { 0x08, 0xb6, 0xdd };
void
intr_hdlr (void)
@ -88,7 +88,7 @@ print_usage (char *argv[])
int
main (int argc, char *argv[])
{
byte_t *pbtTx = NULL;
uint8_t *pbtTx = NULL;
size_t szTxBits;
bool quiet_output = false;
@ -104,12 +104,12 @@ main (int argc, char *argv[])
printf ("Quiet mode.\n");
quiet_output = true;
} else if ((arg == argc - 1) && (strlen (argv[arg]) == 8)) { // See if UID was specified as HEX string
byte_t abtTmp[3] = { 0x00, 0x00, 0x00 };
uint8_t abtTmp[3] = { 0x00, 0x00, 0x00 };
printf ("[+] Using UID: %s\n", argv[arg]);
abtUidBcc[4] = 0x00;
for (i = 0; i < 4; ++i) {
memcpy (abtTmp, argv[arg] + i * 2, 2);
abtUidBcc[i] = (byte_t) strtol ((char *) abtTmp, NULL, 16);
abtUidBcc[i] = (uint8_t) strtol ((char *) abtTmp, NULL, 16);
abtUidBcc[4] ^= abtUidBcc[i];
}
} else {
@ -125,22 +125,24 @@ main (int argc, char *argv[])
signal (SIGINT, (void (*)()) intr_hdlr);
#endif
nfc_init (NULL);
// Try to open the NFC device
pnd = nfc_connect (NULL);
pnd = nfc_open (NULL, NULL);
if (pnd == NULL) {
printf ("Unable to connect to NFC device\n");
printf ("Unable to open NFC device\n");
exit(EXIT_FAILURE);
}
printf ("\n");
printf ("Connected to NFC device: %s\n", pnd->acName);
printf ("NFC device: %s opened\n", nfc_device_get_name (pnd));
printf ("[+] Try to break out the auto-emulation, this requires a second NFC device!\n");
printf ("[+] To do this, please send any command after the anti-collision\n");
printf ("[+] For example, send a RATS command or use the \"nfc-anticol\" or \"nfc-list\" tool.\n");
// Note: We have to build a "fake" nfc_target_t in order to do exactly the same that was done before the new nfc_target_init() was introduced.
nfc_target_t nt = {
// Note: We have to build a "fake" nfc_target in order to do exactly the same that was done before the new nfc_target_init() was introduced.
nfc_target nt = {
.nm = {
.nmt = NMT_ISO14443A,
.nbr = NBR_UNDEFINED,
@ -155,16 +157,16 @@ main (int argc, char *argv[])
},
},
};
if (!nfc_target_init (pnd, &nt, abtRecv, &szRecvBits)) {
if ((szRecvBits = nfc_target_init (pnd, &nt, abtRecv, sizeof (abtRecv), 0)) < 0) {
nfc_perror (pnd, "nfc_target_init");
ERR ("Could not come out of auto-emulation, no command was received");
goto error;
}
printf ("[+] Received initiator command: ");
print_hex_bits (abtRecv, szRecvBits);
print_hex_bits (abtRecv, (size_t) szRecvBits);
printf ("[+] Configuring communication\n");
if (!nfc_configure (pnd, NDO_HANDLE_CRC, false) || !nfc_configure (pnd, NDO_HANDLE_PARITY, true)) {
nfc_perror (pnd, "nfc_configure");
if ((nfc_device_set_property_bool (pnd, NP_HANDLE_CRC, false) < 0) || (nfc_device_set_property_bool (pnd, NP_HANDLE_PARITY, true) < 0)) {
nfc_perror (pnd, "nfc_device_set_property_bool");
exit (EXIT_FAILURE);
}
printf ("[+] Done, the emulated tag is initialized with UID: %02X%02X%02X%02X\n\n", abtUidBcc[0], abtUidBcc[1],
@ -172,7 +174,7 @@ main (int argc, char *argv[])
while (true) {
// Test if we received a frame
if (nfc_target_receive_bits (pnd, abtRecv, &szRecvBits, NULL)) {
if ((szRecvBits = nfc_target_receive_bits (pnd, abtRecv, sizeof (abtRecv), 0)) > 0) {
// Prepare the command to send back for the anti-collision request
switch (szRecvBits) {
case 7: // Request or Wakeup
@ -200,12 +202,12 @@ main (int argc, char *argv[])
if (!quiet_output) {
printf ("R: ");
print_hex_bits (abtRecv, szRecvBits);
print_hex_bits (abtRecv, (size_t) szRecvBits);
}
// Test if we know how to respond
if (szTxBits) {
// Send and print the command to the screen
if (!nfc_target_send_bits (pnd, pbtTx, szTxBits, NULL)) {
if (nfc_target_send_bits (pnd, pbtTx, szTxBits, NULL) < 0) {
nfc_perror (pnd, "nfc_target_send_bits");
goto error;
}
@ -216,10 +218,12 @@ main (int argc, char *argv[])
}
}
}
nfc_disconnect (pnd);
nfc_close (pnd);
nfc_exit (NULL);
exit (EXIT_SUCCESS);
error:
nfc_disconnect (pnd);
nfc_close (pnd);
nfc_exit (NULL);
exit (EXIT_FAILURE);
}

View file

@ -1,6 +1,6 @@
.TH nfc-poll 1 "June 26, 2009" "libnfc" "libnfc's examples"
.SH NAME
nfc-poll \- Poll for one NFC target
nfc-poll \- poll first available NFC target
.SH SYNOPSIS
.B nfc-poll
.SH DESCRIPTION
@ -8,8 +8,18 @@ nfc-poll \- Poll for one NFC target
is a utility for polling any available target (tags but also NFCIP targets)
using ISO14443-A, FeliCa, Jewel and ISO14443-B modulations.
This tool relies on a hardware polling feature of the PN532, it will display
available information retrieved from the tag.
This tool uses hardware polling feature if available (ie. PN532) or switch back
to software polling, it will display available information retrieved from the
tag.
.SH OPTIONS
.TP
.B \-v
Tells
.I
nfc-poll
to be verbose and display detailed information about the targets shown.
This includes SAK decoding and fingerprinting is available.
.SH IMPORTANT
There are some well-know limits with this example:

View file

@ -51,7 +51,7 @@
#define MAX_DEVICE_COUNT 16
static nfc_device_t *pnd = NULL;
static nfc_device *pnd = NULL;
void stop_polling (int sig)
{
@ -62,80 +62,76 @@ void stop_polling (int sig)
exit (EXIT_FAILURE);
}
void
print_usage (const char* progname)
{
printf ("usage: %s [-v]\n", progname);
printf (" -v\t verbose display\n");
}
int
main (int argc, const char *argv[])
{
size_t szFound;
size_t i;
bool verbose = false;
nfc_device_desc_t *pnddDevices;
signal (SIGINT, stop_polling);
pnddDevices = parse_args (argc, argv, &szFound, &verbose);
// Display libnfc version
const char *acLibnfcVersion = nfc_version ();
if (argc > 1) {
errx (1, "usage: %s", argv[0]);
}
printf ("%s uses libnfc %s\n", argv[0], acLibnfcVersion);
if (szFound == 0) {
if (!(pnddDevices = malloc (MAX_DEVICE_COUNT * sizeof (*pnddDevices)))) {
fprintf (stderr, "malloc() failed\n");
exit (EXIT_FAILURE);
}
}
nfc_list_devices (pnddDevices, MAX_DEVICE_COUNT, &szFound);
if (szFound == 0) {
printf ("No NFC device found.\n");
}
for (i = 0; i < szFound; i++) {
const uint8_t uiPollNr = 20;
const uint8_t uiPeriod = 2;
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;
nfc_target_t nt;
bool res;
pnd = nfc_connect (&(pnddDevices[i]));
if (pnd == NULL) {
ERR ("%s", "Unable to connect to NFC device.");
exit (EXIT_FAILURE);
}
nfc_initiator_init (pnd);
printf ("Connected to NFC reader: %s\n", pnd->acName);
printf ("NFC device will poll during %ld ms (%u pollings of %lu ms for %zd modulations)\n", (unsigned long) uiPollNr * szModulations * uiPeriod * 150, uiPollNr, (unsigned long) uiPeriod * 150, szModulations);
res = nfc_initiator_poll_target (pnd, nmModulations, szModulations, uiPollNr, uiPeriod, &nt);
if (res) {
print_nfc_target ( nt, verbose );
if (argc != 1) {
if ((argc == 2) && (0 == strcmp ("-v", argv[1]))) {
verbose = true;
} else {
if (pnd->iLastError) {
nfc_perror (pnd, "nfc_initiator_poll_targets");
nfc_disconnect (pnd);
exit (EXIT_FAILURE);
} else {
printf ("No target found.\n");
}
print_usage (argv[0]);
exit (EXIT_FAILURE);
}
nfc_disconnect (pnd);
}
free (pnddDevices);
const uint8_t uiPollNr = 20;
const uint8_t uiPeriod = 2;
const nfc_modulation 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;
nfc_target nt;
int res = 0;
nfc_init (NULL);
pnd = nfc_open (NULL, NULL);
if (pnd == NULL) {
ERR ("%s", "Unable to open NFC device.");
exit (EXIT_FAILURE);
}
if (nfc_initiator_init (pnd) < 0) {
nfc_perror (pnd, "nfc_initiator_init");
exit (EXIT_FAILURE);
}
printf ("NFC reader: %s opened\n", nfc_device_get_name (pnd));
printf ("NFC device will poll during %ld ms (%u pollings of %lu ms for %zd modulations)\n", (unsigned long) uiPollNr * szModulations * uiPeriod * 150, uiPollNr, (unsigned long) uiPeriod * 150, szModulations);
if ((res = nfc_initiator_poll_target (pnd, nmModulations, szModulations, uiPollNr, uiPeriod, &nt)) < 0) {
nfc_perror (pnd, "nfc_initiator_poll_target");
nfc_close (pnd);
nfc_exit (NULL);
exit (EXIT_FAILURE);
}
if (res > 0) {
print_nfc_target ( nt, verbose );
} else {
printf ("No target found.\n");
}
nfc_close (pnd);
nfc_exit (NULL);
exit (EXIT_SUCCESS);
}

View file

@ -50,14 +50,14 @@
#define MAX_FRAME_LEN 264
#define MAX_DEVICE_COUNT 2
static byte_t abtReaderRx[MAX_FRAME_LEN];
static byte_t abtReaderRxPar[MAX_FRAME_LEN];
static size_t szReaderRxBits;
static byte_t abtTagRx[MAX_FRAME_LEN];
static byte_t abtTagRxPar[MAX_FRAME_LEN];
static size_t szTagRxBits;
static nfc_device_t *pndReader;
static nfc_device_t *pndTag;
static uint8_t abtReaderRx[MAX_FRAME_LEN];
static uint8_t abtReaderRxPar[MAX_FRAME_LEN];
static int szReaderRxBits;
static uint8_t abtTagRx[MAX_FRAME_LEN];
static uint8_t abtTagRxPar[MAX_FRAME_LEN];
static int szTagRxBits;
static nfc_device *pndReader;
static nfc_device *pndTag;
static bool quitting = false;
void
@ -82,8 +82,6 @@ main (int argc, char *argv[])
{
int arg;
bool quiet_output = false;
size_t szFound;
nfc_device_desc_t *pnddDevices;
const char *acLibnfcVersion = nfc_version ();
// Get commandline options
@ -109,33 +107,32 @@ main (int argc, char *argv[])
signal (SIGINT, (void (*)()) intr_hdlr);
#endif
// Allocate memory to put the result of available devices listing
if (!(pnddDevices = malloc (MAX_DEVICE_COUNT * sizeof (*pnddDevices)))) {
fprintf (stderr, "malloc() failed\n");
return EXIT_FAILURE;
}
nfc_connstring connstrings[MAX_DEVICE_COUNT];
// List available devices
nfc_list_devices (pnddDevices, MAX_DEVICE_COUNT, &szFound);
size_t szFound = nfc_list_devices (NULL, connstrings, MAX_DEVICE_COUNT);
if (szFound < 2) {
ERR ("%zd device found but two connected devices are needed to relay NFC.", szFound);
ERR ("%zd device found but two opened devices are needed to relay NFC.", szFound);
return EXIT_FAILURE;
}
nfc_init (NULL);
// Try to open the NFC emulator device
pndTag = nfc_connect (&(pnddDevices[0]));
pndTag = nfc_open (NULL, connstrings[0]);
if (pndTag == NULL) {
printf ("Error connecting NFC emulator device\n");
printf ("Error opening NFC emulator device\n");
return EXIT_FAILURE;
}
printf ("Hint: tag <---> initiator (relay) <---> target (relay) <---> original reader\n\n");
printf ("Connected to the NFC emulator device: %s\n", pndTag->acName);
printf ("NFC emulator device: %s opened\n", nfc_device_get_name (pndTag));
printf ("[+] Try to break out the auto-emulation, this requires a second reader!\n");
printf ("[+] To do this, please send any command after the anti-collision\n");
printf ("[+] For example, send a RATS command or use the \"nfc-anticol\" tool\n");
nfc_target_t nt = {
nfc_target nt = {
.nm = {
.nmt = NMT_ISO14443A,
.nbr = NBR_UNDEFINED,
@ -151,60 +148,65 @@ main (int argc, char *argv[])
},
};
if (!nfc_target_init (pndTag, &nt, abtReaderRx, &szReaderRxBits)) {
if ((szReaderRxBits = nfc_target_init (pndTag, &nt, abtReaderRx, sizeof (abtReaderRx), 0)) < 0) {
ERR ("%s", "Initialization of NFC emulator failed");
nfc_disconnect (pndTag);
nfc_close (pndTag);
nfc_exit (NULL);
return EXIT_FAILURE;
}
printf ("%s", "Configuring emulator settings...");
if (!nfc_configure (pndTag, NDO_HANDLE_CRC, false) ||
!nfc_configure (pndTag, NDO_HANDLE_PARITY, false) || !nfc_configure (pndTag, NDO_ACCEPT_INVALID_FRAMES, true)) {
nfc_perror (pndTag, "nfc_configure");
if ((nfc_device_set_property_bool (pndTag, NP_HANDLE_CRC, false) < 0) ||
(nfc_device_set_property_bool (pndTag, NP_HANDLE_PARITY, false) < 0) || (nfc_device_set_property_bool (pndTag, NP_ACCEPT_INVALID_FRAMES, true)) < 0) {
nfc_perror (pndTag, "nfc_device_set_property_bool");
exit (EXIT_FAILURE);
}
printf ("%s", "Done, emulated tag is initialized");
// Try to open the NFC reader
pndReader = nfc_connect (&(pnddDevices[1]));
pndReader = nfc_open (NULL, connstrings[1]);
printf ("Connected to the NFC reader device: %s", pndReader->acName);
printf ("NFC reader device: %s opened", nfc_device_get_name (pndReader));
printf ("%s", "Configuring NFC reader settings...");
nfc_initiator_init (pndReader);
if (!nfc_configure (pndReader, NDO_HANDLE_CRC, false) ||
!nfc_configure (pndReader, NDO_HANDLE_PARITY, false) ||
!nfc_configure (pndReader, NDO_ACCEPT_INVALID_FRAMES, true)) {
nfc_perror (pndReader, "nfc_configure");
if (nfc_initiator_init (pndReader) < 0) {
nfc_perror (pndReader, "nfc_initiator_init");
exit (EXIT_FAILURE);
}
if ((nfc_device_set_property_bool (pndReader, NP_HANDLE_CRC, false) < 0) ||
(nfc_device_set_property_bool (pndReader, NP_HANDLE_PARITY, false) < 0) ||
(nfc_device_set_property_bool (pndReader, NP_ACCEPT_INVALID_FRAMES, true)) < 0) {
nfc_perror (pndReader, "nfc_device_set_property_bool");
exit (EXIT_FAILURE);
}
printf ("%s", "Done, relaying frames now!");
while (!quitting) {
// Test if we received a frame from the reader
if (nfc_target_receive_bits (pndTag, abtReaderRx, &szReaderRxBits, abtReaderRxPar)) {
if ((szReaderRxBits = nfc_target_receive_bits (pndTag, abtReaderRx, sizeof (abtReaderRx), abtReaderRxPar)) > 0) {
// Drop down the field before sending a REQA command and start a new session
if (szReaderRxBits == 7 && abtReaderRx[0] == 0x26) {
// Drop down field for a very short time (original tag will reboot)
if (!nfc_configure (pndReader, NDO_ACTIVATE_FIELD, false)) {
nfc_perror (pndReader, "nfc_configure");
if (nfc_device_set_property_bool (pndReader, NP_ACTIVATE_FIELD, false) < 0) {
nfc_perror (pndReader, "nfc_device_set_property_bool");
exit (EXIT_FAILURE);
}
if (!quiet_output)
printf ("\n");
if (!nfc_configure (pndReader, NDO_ACTIVATE_FIELD, true)) {
nfc_perror (pndReader, "nfc_configure");
if (nfc_device_set_property_bool (pndReader, NP_ACTIVATE_FIELD, true) < 0) {
nfc_perror (pndReader, "nfc_device_set_property_bool");
exit (EXIT_FAILURE);
}
}
// Print the reader frame to the screen
if (!quiet_output) {
printf ("R: ");
print_hex_par (abtReaderRx, szReaderRxBits, abtReaderRxPar);
print_hex_par (abtReaderRx, (size_t) szReaderRxBits, abtReaderRxPar);
}
// Forward the frame to the original tag
if (nfc_initiator_transceive_bits
(pndReader, abtReaderRx, szReaderRxBits, abtReaderRxPar, abtTagRx, &szTagRxBits, abtTagRxPar)) {
if ((szTagRxBits = nfc_initiator_transceive_bits
(pndReader, abtReaderRx, (size_t) szReaderRxBits, abtReaderRxPar, abtTagRx, abtTagRxPar)) > 0) {
// Redirect the answer back to the reader
if (!nfc_target_send_bits (pndTag, abtTagRx, szTagRxBits, abtTagRxPar)) {
if (nfc_target_send_bits (pndTag, abtTagRx, szTagRxBits, abtTagRxPar) < 0) {
nfc_perror (pndTag, "nfc_target_send_bits");
exit (EXIT_FAILURE);
}
@ -217,7 +219,8 @@ main (int argc, char *argv[])
}
}
nfc_disconnect (pndTag);
nfc_disconnect (pndReader);
nfc_close (pndTag);
nfc_close (pndReader);
nfc_exit (NULL);
exit (EXIT_SUCCESS);
}

View file

@ -52,49 +52,48 @@
int
main (int argc, const char *argv[])
{
size_t szFound;
size_t i;
nfc_device_t *pnd;
nfc_device_desc_t *pnddDevices;
nfc_device *pnd;
const char *acLibnfcVersion;
bool result;
int res = 0;
byte_t abtRx[PN53x_EXTENDED_FRAME__DATA_MAX_LEN];
uint8_t abtRx[PN53x_EXTENDED_FRAME__DATA_MAX_LEN];
size_t szRx = sizeof(abtRx);
const byte_t pncmd_diagnose_communication_line_test[] = { Diagnose, 0x00, 0x06, 'l', 'i', 'b', 'n', 'f', 'c' };
const byte_t pncmd_diagnose_rom_test[] = { Diagnose, 0x01 };
const byte_t pncmd_diagnose_ram_test[] = { Diagnose, 0x02 };
const uint8_t pncmd_diagnose_communication_line_test[] = { Diagnose, 0x00, 0x06, 'l', 'i', 'b', 'n', 'f', 'c' };
const uint8_t pncmd_diagnose_rom_test[] = { Diagnose, 0x01 };
const uint8_t pncmd_diagnose_ram_test[] = { Diagnose, 0x02 };
if (argc > 1) {
errx (1, "usage: %s", argv[0]);
}
nfc_init (NULL);
// Display libnfc version
acLibnfcVersion = nfc_version ();
printf ("%s uses libnfc %s\n", argv[0], acLibnfcVersion);
if (!(pnddDevices = malloc (MAX_DEVICE_COUNT * sizeof (*pnddDevices)))) {
fprintf (stderr, "malloc() failed\n");
return EXIT_FAILURE;
}
nfc_list_devices (pnddDevices, MAX_DEVICE_COUNT, &szFound);
nfc_connstring connstrings[MAX_DEVICE_COUNT];
size_t szFound = nfc_list_devices (NULL, connstrings, MAX_DEVICE_COUNT);
if (szFound == 0) {
printf ("No NFC device found.\n");
}
for (i = 0; i < szFound; i++) {
pnd = nfc_connect (&(pnddDevices[i]));
pnd = nfc_open (NULL, connstrings[i]);
if (pnd == NULL) {
ERR ("%s", "Unable to connect to NFC device.");
ERR ("%s", "Unable to open NFC device.");
return EXIT_FAILURE;
}
printf ("NFC device [%s] connected.\n", pnd->acName);
printf ("NFC device [%s] opened.\n", nfc_device_get_name (pnd));
result = pn53x_transceive (pnd, pncmd_diagnose_communication_line_test, sizeof (pncmd_diagnose_communication_line_test), abtRx, &szRx, NULL);
if (result) {
res = pn53x_transceive (pnd, pncmd_diagnose_communication_line_test, sizeof (pncmd_diagnose_communication_line_test), abtRx, szRx, 0);
if (res > 0) {
szRx = (size_t) res;
// Result of Diagnose ping for RC-S360 doesn't contain status byte so we've to handle both cases
result = (memcmp (pncmd_diagnose_communication_line_test + 1, abtRx, sizeof (pncmd_diagnose_communication_line_test) - 1) == 0) ||
(memcmp (pncmd_diagnose_communication_line_test + 2, abtRx, sizeof (pncmd_diagnose_communication_line_test) - 2) == 0);
@ -103,16 +102,18 @@ main (int argc, const char *argv[])
}
printf (" Communication line test: %s\n", result ? "OK" : "Failed");
result = pn53x_transceive (pnd, pncmd_diagnose_rom_test, sizeof (pncmd_diagnose_rom_test), abtRx, &szRx, NULL);
if (result) {
res = pn53x_transceive (pnd, pncmd_diagnose_rom_test, sizeof (pncmd_diagnose_rom_test), abtRx, szRx, 0);
if (res > 0) {
szRx = (size_t) res;
result = ((szRx == 1) && (abtRx[0] == 0x00));
} else {
nfc_perror (pnd, "pn53x_transceive");
}
printf (" ROM test: %s\n", result ? "OK" : "Failed");
result = pn53x_transceive (pnd, pncmd_diagnose_ram_test, sizeof (pncmd_diagnose_ram_test), abtRx, &szRx, NULL);
if (result) {
res = pn53x_transceive (pnd, pncmd_diagnose_ram_test, sizeof (pncmd_diagnose_ram_test), abtRx, szRx, 0);
if (res > 0) {
szRx = (size_t) res;
result = ((szRx == 1) && (abtRx[0] == 0x00));
} else {
nfc_perror (pnd, "pn53x_transceive");

View file

@ -72,24 +72,26 @@ wait_one_minute (void)
int
main (int argc, const char *argv[])
{
nfc_device_t *pnd;
nfc_device *pnd;
(void) argc;
(void) argv;
nfc_init (NULL);
// Display libnfc version
const char *acLibnfcVersion = nfc_version ();
printf ("%s uses libnfc %s\n", argv[0], acLibnfcVersion);
// Connect using the first available NFC device
pnd = nfc_connect (NULL);
// Open using the first available NFC device
pnd = nfc_open (NULL, NULL);
if (pnd == NULL) {
ERR ("%s", "Unable to connect to NFC device.");
ERR ("%s", "Unable to open NFC device.");
return EXIT_FAILURE;
}
printf ("Connected to NFC device: %s\n", pnd->acName);
printf ("NFC device: %s opened\n", nfc_device_get_name (pnd));
// Print the example's menu
printf ("\nSelect the communication mode:\n");
@ -110,7 +112,7 @@ main (int argc, const char *argv[])
// Connect with the SAM
// FIXME: Its a private pn53x function
if (!pn53x_SAMConfiguration (pnd, mode, NULL)) {
if (pn53x_SAMConfiguration (pnd, mode, 0) < 0) {
nfc_perror (pnd, "pn53x_SAMConfiguration");
exit (EXIT_FAILURE);
}
@ -125,22 +127,25 @@ main (int argc, const char *argv[])
case PSM_WIRED_CARD:
{
nfc_target_t nt;
nfc_target nt;
// Set connected NFC device to initiator mode
nfc_initiator_init (pnd);
// Set opened NFC device to initiator mode
if (nfc_initiator_init (pnd) < 0) {
nfc_perror (pnd, "nfc_initiator_init");
exit (EXIT_FAILURE);
}
// Let the reader only try once to find a tag
if (!nfc_configure (pnd, NDO_INFINITE_SELECT, false)) {
nfc_perror (pnd, "nfc_configure");
if (nfc_device_set_property_bool (pnd, NP_INFINITE_SELECT, false) < 0) {
nfc_perror (pnd, "nfc_device_set_property_bool");
exit (EXIT_FAILURE);
}
// Read the SAM's info
const nfc_modulation_t nmSAM = {
const nfc_modulation nmSAM = {
.nmt = NMT_ISO14443A,
.nbr = NBR_106,
};
if (!nfc_initiator_select_passive_target (pnd, nmSAM, NULL, 0, &nt)) {
if (nfc_initiator_select_passive_target (pnd, nmSAM, NULL, 0, &nt) < 0) {
nfc_perror (pnd, "nfc_initiator_select_passive_target");
ERR ("%s", "Reading of SAM info failed.");
exit (EXIT_FAILURE);
@ -153,10 +158,9 @@ main (int argc, const char *argv[])
case PSM_DUAL_CARD:
{
byte_t abtRx[MAX_FRAME_LEN];
size_t szRx = sizeof(abtRx);
uint8_t abtRx[MAX_FRAME_LEN];
nfc_target_t nt = {
nfc_target nt = {
.nm = {
.nmt = NMT_ISO14443A,
.nbr = NBR_UNDEFINED,
@ -173,7 +177,7 @@ main (int argc, const char *argv[])
};
printf ("Now both, NFC device (configured as target) and SAM are readables from an external NFC initiator.\n");
printf ("Please note that NFC device (configured as target) stay in target mode until it receive RATS, ATR_REQ or proprietary command.\n");
if (!nfc_target_init (pnd, &nt, abtRx, &szRx)) {
if (nfc_target_init (pnd, &nt, abtRx, sizeof(abtRx), 0) < 0) {
nfc_perror(pnd, "nfc_target_init");
return EXIT_FAILURE;
}
@ -185,10 +189,11 @@ main (int argc, const char *argv[])
}
// Disconnect from the SAM
pn53x_SAMConfiguration (pnd, PSM_NORMAL, NULL);
pn53x_SAMConfiguration (pnd, PSM_NORMAL, 0);
// Disconnect from NFC device
nfc_disconnect (pnd);
// Close NFC device
nfc_close (pnd);
nfc_exit (NULL);
exit (EXIT_SUCCESS);
}

View file

@ -71,41 +71,46 @@
int main(int argc, const char* argv[])
{
nfc_device_t* pnd;
byte_t abtRx[MAX_FRAME_LEN];
byte_t abtTx[MAX_FRAME_LEN];
nfc_device *pnd;
uint8_t abtRx[MAX_FRAME_LEN];
uint8_t abtTx[MAX_FRAME_LEN];
size_t szRx = sizeof(abtRx);
size_t szTx;
extern FILE* stdin;
FILE* input = NULL;
if (argc >= 2) {
if((input=fopen(argv[1], "r"))==NULL) {
if((input = fopen(argv[1], "r"))==NULL) {
ERR ("%s", "Cannot open file.");
return EXIT_FAILURE;
}
}
nfc_init (NULL);
// Try to open the NFC reader
pnd = nfc_connect(NULL);
pnd = nfc_open(NULL, NULL);
if (pnd == NULL) {
ERR ("%s", "Unable to connect to NFC device.");
ERR ("%s", "Unable to open NFC device.");
return EXIT_FAILURE;
}
printf ("Connected to NFC reader: %s\n", pnd->acName);
nfc_initiator_init(pnd);
printf ("NFC reader: %s opened\n", nfc_device_get_name (pnd));
if (nfc_initiator_init (pnd) < 0) {
nfc_perror (pnd, "nfc_initiator_init");
exit (EXIT_FAILURE);
}
char * cmd;
char * prompt="> ";
char *cmd;
char *prompt = "> ";
while(1) {
int offset=0;
int offset = 0;
#if defined(HAVE_READLINE)
if (input==NULL) { // means we use stdin
cmd=readline(prompt);
if (input == NULL) { // means we use stdin
cmd = readline(prompt);
// NULL if ctrl-d
if (cmd==NULL) {
if (cmd == NULL) {
printf("Bye!\n");
break;
}
@ -113,7 +118,7 @@ int main(int argc, const char* argv[])
} else {
#endif //HAVE_READLINE
size_t n = 255;
char * ret = NULL;
char *ret = NULL;
cmd = malloc(n);
printf("%s", prompt);
fflush(0);
@ -132,18 +137,18 @@ int main(int argc, const char* argv[])
#if defined(HAVE_READLINE)
}
#endif //HAVE_READLINE
if (cmd[0]=='q') {
if (cmd[0] == 'q') {
printf("Bye!\n");
free(cmd);
break;
}
if (cmd[0]=='p') {
int s=0;
if (cmd[0] == 'p') {
int s = 0;
offset++;
while (isspace(cmd[offset])) {
offset++;
}
sscanf(cmd+offset, "%d", &s);
sscanf(cmd + offset, "%d", &s);
printf("Pause for %i msecs\n", s);
if (s>0) {
sleep(s * SUSP_TIME);
@ -152,14 +157,14 @@ int main(int argc, const char* argv[])
continue;
}
szTx = 0;
for(int i = 0; i<MAX_FRAME_LEN-10; i++) {
for(int i = 0; i < MAX_FRAME_LEN - 10; i++) {
int size;
byte_t byte;
uint8_t byte;
while (isspace(cmd[offset])) {
offset++;
}
size = sscanf(cmd+offset, "%2x", (unsigned int*)&byte);
if (size<1) {
if (size < 1 ) {
break;
}
abtTx[i] = byte;
@ -175,14 +180,16 @@ int main(int argc, const char* argv[])
continue;
}
printf("Tx: ");
print_hex((byte_t*)abtTx,szTx);
print_hex((uint8_t*)abtTx,szTx);
szRx = sizeof(abtRx);
if (!pn53x_transceive (pnd, abtTx, szTx, abtRx, &szRx, NULL)) {
int res = 0;
if ((res = pn53x_transceive (pnd, abtTx, szTx, abtRx, szRx, 0)) < 0) {
free(cmd);
nfc_perror (pnd, "Rx");
continue;
}
szRx = (size_t) res;
printf("Rx: ");
print_hex(abtRx, szRx);
@ -192,6 +199,7 @@ int main(int argc, const char* argv[])
if (input != NULL) {
fclose(input);
}
nfc_disconnect(pnd);
nfc_close(pnd);
nfc_exit (NULL);
return 1;
}