2010-01-15 11:18:11 +01:00
|
|
|
/*-
|
2010-11-17 15:27:11 +01:00
|
|
|
* Public platform independent Near Field Communication (NFC) library examples
|
2012-05-29 02:33:22 +02:00
|
|
|
*
|
2012-10-21 16:09:16 +02:00
|
|
|
* Copyright (C) 2009 Roel Verdult
|
|
|
|
* Copyright (C) 2010 Romuald Conty
|
2012-05-29 02:33:22 +02:00
|
|
|
*
|
2010-11-17 15:27:11 +01:00
|
|
|
* 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,
|
2012-05-29 02:33:22 +02:00
|
|
|
* this list of conditions and the following disclaimer.
|
2010-11-17 15:27:11 +01:00
|
|
|
* 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.
|
2012-05-29 02:33:22 +02:00
|
|
|
*
|
2010-11-17 15:27:11 +01:00
|
|
|
* Note that this license only applies on the examples, NFC library itself is under LGPL
|
2009-10-12 16:52:26 +02:00
|
|
|
*
|
2010-01-15 11:18:11 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2009-10-12 16:52:26 +02:00
|
|
|
* @file nfc-relay.c
|
2010-02-09 16:39:56 +01:00
|
|
|
* @brief Relay example using two devices.
|
2009-10-12 16:52:26 +02:00
|
|
|
*/
|
2009-04-29 14:47:41 +02:00
|
|
|
|
2010-01-15 11:18:11 +01:00
|
|
|
#ifdef HAVE_CONFIG_H
|
2010-09-07 19:51:03 +02:00
|
|
|
# include "config.h"
|
2010-02-09 16:39:56 +01:00
|
|
|
#endif /* HAVE_CONFIG_H */
|
2010-01-15 11:18:11 +01:00
|
|
|
|
2009-04-29 14:47:41 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2009-05-27 12:13:19 +02:00
|
|
|
#include <stdint.h>
|
2009-04-29 14:47:41 +02:00
|
|
|
#include <string.h>
|
2009-11-24 18:49:24 +01:00
|
|
|
#include <signal.h>
|
2009-05-27 12:13:19 +02:00
|
|
|
|
2009-12-01 15:23:00 +01:00
|
|
|
#include <nfc/nfc.h>
|
2009-11-02 12:34:58 +01:00
|
|
|
|
2011-09-30 13:33:31 +02:00
|
|
|
#include "utils/nfc-utils.h"
|
2009-04-29 14:47:41 +02:00
|
|
|
|
2009-11-10 10:47:59 +01:00
|
|
|
#define MAX_FRAME_LEN 264
|
2010-02-09 16:39:56 +01:00
|
|
|
#define MAX_DEVICE_COUNT 2
|
2009-11-10 10:47:59 +01:00
|
|
|
|
2011-11-24 11:54:42 +01:00
|
|
|
static uint8_t abtReaderRx[MAX_FRAME_LEN];
|
|
|
|
static uint8_t abtReaderRxPar[MAX_FRAME_LEN];
|
2012-01-09 12:26:57 +01:00
|
|
|
static int szReaderRxBits;
|
2011-11-24 11:54:42 +01:00
|
|
|
static uint8_t abtTagRx[MAX_FRAME_LEN];
|
|
|
|
static uint8_t abtTagRxPar[MAX_FRAME_LEN];
|
2012-01-04 16:30:42 +01:00
|
|
|
static int szTagRxBits;
|
2011-11-23 16:55:40 +01:00
|
|
|
static nfc_device *pndReader;
|
|
|
|
static nfc_device *pndTag;
|
2010-09-07 19:51:03 +02:00
|
|
|
static bool quitting = false;
|
2009-11-24 18:49:24 +01:00
|
|
|
|
2012-05-13 14:36:59 +02:00
|
|
|
static void
|
2012-05-29 17:54:36 +02:00
|
|
|
intr_hdlr(int sig)
|
2009-11-24 18:49:24 +01:00
|
|
|
{
|
2012-05-14 15:47:31 +02:00
|
|
|
(void) sig;
|
2012-05-29 17:54:36 +02:00
|
|
|
printf("\nQuitting...\n");
|
2010-09-07 19:51:03 +02:00
|
|
|
quitting = true;
|
2009-11-24 18:49:24 +01:00
|
|
|
return;
|
|
|
|
}
|
2009-04-29 14:47:41 +02:00
|
|
|
|
2012-05-13 14:36:59 +02:00
|
|
|
static void
|
2012-05-29 17:54:36 +02:00
|
|
|
print_usage(char *argv[])
|
2009-09-07 12:15:34 +02:00
|
|
|
{
|
2012-05-29 17:54:36 +02:00
|
|
|
printf("Usage: %s [OPTIONS]\n", argv[0]);
|
|
|
|
printf("Options:\n");
|
|
|
|
printf("\t-h\tHelp. Print this message.\n");
|
|
|
|
printf("\t-q\tQuiet mode. Suppress output of READER and EMULATOR data (improves timing).\n");
|
2009-09-07 12:15:34 +02:00
|
|
|
}
|
|
|
|
|
2010-09-07 19:51:03 +02:00
|
|
|
int
|
2012-05-29 17:54:36 +02:00
|
|
|
main(int argc, char *argv[])
|
2009-09-07 12:15:34 +02:00
|
|
|
{
|
2010-09-07 19:51:03 +02:00
|
|
|
int arg;
|
|
|
|
bool quiet_output = false;
|
2012-05-29 17:54:36 +02:00
|
|
|
const char *acLibnfcVersion = nfc_version();
|
2009-08-23 11:50:46 +02:00
|
|
|
|
|
|
|
// Get commandline options
|
2010-09-07 19:51:03 +02:00
|
|
|
for (arg = 1; arg < argc; arg++) {
|
2012-05-29 17:54:36 +02:00
|
|
|
if (0 == strcmp(argv[arg], "-h")) {
|
|
|
|
print_usage(argv);
|
2013-03-05 19:44:59 +01:00
|
|
|
exit(EXIT_SUCCESS);
|
2012-05-29 17:54:36 +02:00
|
|
|
} else if (0 == strcmp(argv[arg], "-q")) {
|
2009-08-26 12:57:38 +02:00
|
|
|
quiet_output = true;
|
2009-09-07 12:15:34 +02:00
|
|
|
} else {
|
2012-05-29 17:54:36 +02:00
|
|
|
ERR("%s is not supported option.", argv[arg]);
|
|
|
|
print_usage(argv);
|
2013-03-05 19:44:59 +01:00
|
|
|
exit(EXIT_FAILURE);
|
2009-08-23 11:50:46 +02:00
|
|
|
}
|
2009-09-07 12:15:34 +02:00
|
|
|
}
|
2009-08-23 11:50:46 +02:00
|
|
|
|
2010-02-09 16:39:56 +01:00
|
|
|
// Display libnfc version
|
2012-05-29 17:54:36 +02:00
|
|
|
printf("%s uses libnfc %s\n", argv[0], acLibnfcVersion);
|
2010-02-09 16:39:56 +01:00
|
|
|
|
2009-11-24 18:49:24 +01:00
|
|
|
#ifdef WIN32
|
2012-05-29 17:54:36 +02:00
|
|
|
signal(SIGINT, (void (__cdecl *)(int)) intr_hdlr);
|
2009-11-24 18:49:24 +01:00
|
|
|
#else
|
2012-05-29 17:54:36 +02:00
|
|
|
signal(SIGINT, intr_hdlr);
|
2009-11-24 18:49:24 +01:00
|
|
|
#endif
|
|
|
|
|
2012-12-04 19:29:57 +01:00
|
|
|
nfc_context *context;
|
|
|
|
nfc_init(&context);
|
2011-10-17 15:03:56 +02:00
|
|
|
nfc_connstring connstrings[MAX_DEVICE_COUNT];
|
2010-02-09 16:39:56 +01:00
|
|
|
// List available devices
|
2012-12-04 19:29:57 +01:00
|
|
|
size_t szFound = nfc_list_devices(context, connstrings, MAX_DEVICE_COUNT);
|
2010-02-09 16:39:56 +01:00
|
|
|
|
|
|
|
if (szFound < 2) {
|
2012-05-29 17:54:36 +02:00
|
|
|
ERR("%zd device found but two opened devices are needed to relay NFC.", szFound);
|
2013-03-05 19:44:59 +01:00
|
|
|
nfc_exit(context);
|
|
|
|
exit(EXIT_FAILURE);
|
2010-09-07 19:51:03 +02:00
|
|
|
}
|
2012-05-29 02:33:22 +02:00
|
|
|
|
2009-04-29 14:47:41 +02:00
|
|
|
// Try to open the NFC emulator device
|
2012-12-04 19:29:57 +01:00
|
|
|
pndTag = nfc_open(context, connstrings[0]);
|
2010-09-07 19:51:03 +02:00
|
|
|
if (pndTag == NULL) {
|
2012-05-29 17:54:36 +02:00
|
|
|
printf("Error opening NFC emulator device\n");
|
2013-03-05 19:44:59 +01:00
|
|
|
nfc_exit(context);
|
|
|
|
exit(EXIT_FAILURE);
|
2009-04-29 14:47:41 +02:00
|
|
|
}
|
2010-09-07 19:51:03 +02:00
|
|
|
|
2012-05-29 17:54:36 +02:00
|
|
|
printf("Hint: tag <---> initiator (relay) <---> target (relay) <---> original reader\n\n");
|
2010-09-07 19:51:03 +02:00
|
|
|
|
2012-05-29 17:54:36 +02:00
|
|
|
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");
|
2010-10-04 14:46:03 +02:00
|
|
|
|
2011-11-23 16:55:40 +01:00
|
|
|
nfc_target nt = {
|
2011-02-28 10:47:31 +01:00
|
|
|
.nm = {
|
|
|
|
.nmt = NMT_ISO14443A,
|
|
|
|
.nbr = NBR_UNDEFINED,
|
|
|
|
},
|
|
|
|
.nti = {
|
|
|
|
.nai = {
|
|
|
|
.abtAtqa = { 0x04, 0x00 },
|
2011-05-09 10:51:07 +02:00
|
|
|
.abtUid = { 0x08, 0xad, 0xbe, 0xef },
|
2011-02-28 10:47:31 +01:00
|
|
|
.btSak = 0x20,
|
|
|
|
.szUidLen = 4,
|
|
|
|
.szAtsLen = 0,
|
|
|
|
},
|
|
|
|
},
|
2010-10-04 14:46:03 +02:00
|
|
|
};
|
|
|
|
|
2012-05-29 17:54:36 +02:00
|
|
|
if ((szReaderRxBits = nfc_target_init(pndTag, &nt, abtReaderRx, sizeof(abtReaderRx), 0)) < 0) {
|
|
|
|
ERR("%s", "Initialization of NFC emulator failed");
|
|
|
|
nfc_close(pndTag);
|
2012-12-04 19:29:57 +01:00
|
|
|
nfc_exit(context);
|
2013-03-05 19:44:59 +01:00
|
|
|
exit(EXIT_FAILURE);
|
2009-11-24 18:49:24 +01:00
|
|
|
}
|
2012-05-29 17:54:36 +02:00
|
|
|
printf("%s", "Configuring emulator settings...");
|
|
|
|
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");
|
2013-03-05 19:44:59 +01:00
|
|
|
nfc_close(pndTag);
|
|
|
|
nfc_exit(context);
|
2012-05-29 17:54:36 +02:00
|
|
|
exit(EXIT_FAILURE);
|
2010-08-18 19:22:13 +02:00
|
|
|
}
|
2012-05-29 17:54:36 +02:00
|
|
|
printf("%s", "Done, emulated tag is initialized");
|
2009-04-29 14:47:41 +02:00
|
|
|
|
|
|
|
// Try to open the NFC reader
|
2012-12-04 19:29:57 +01:00
|
|
|
pndReader = nfc_open(context, connstrings[1]);
|
2013-03-05 19:44:59 +01:00
|
|
|
if (pndReader == NULL) {
|
|
|
|
printf("Error opening NFC reader device\n");
|
|
|
|
nfc_close(pndTag);
|
|
|
|
nfc_exit(context);
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
2010-09-07 19:51:03 +02:00
|
|
|
|
2012-05-29 17:54:36 +02:00
|
|
|
printf("NFC reader device: %s opened", nfc_device_get_name(pndReader));
|
|
|
|
printf("%s", "Configuring NFC reader settings...");
|
2012-05-29 02:33:22 +02:00
|
|
|
|
2012-05-29 17:54:36 +02:00
|
|
|
if (nfc_initiator_init(pndReader) < 0) {
|
|
|
|
nfc_perror(pndReader, "nfc_initiator_init");
|
2013-03-05 19:44:59 +01:00
|
|
|
nfc_close(pndTag);
|
|
|
|
nfc_close(pndReader);
|
|
|
|
nfc_exit(context);
|
2012-05-29 17:54:36 +02:00
|
|
|
exit(EXIT_FAILURE);
|
2012-01-05 11:33:50 +01:00
|
|
|
}
|
2012-05-29 17:54:36 +02:00
|
|
|
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");
|
2013-03-05 19:44:59 +01:00
|
|
|
nfc_close(pndTag);
|
|
|
|
nfc_close(pndReader);
|
|
|
|
nfc_exit(context);
|
2012-05-29 17:54:36 +02:00
|
|
|
exit(EXIT_FAILURE);
|
2010-08-18 19:22:13 +02:00
|
|
|
}
|
2012-05-29 17:54:36 +02:00
|
|
|
printf("%s", "Done, relaying frames now!");
|
2009-04-29 14:47:41 +02:00
|
|
|
|
2010-09-07 19:51:03 +02:00
|
|
|
while (!quitting) {
|
2009-04-29 14:47:41 +02:00
|
|
|
// Test if we received a frame from the reader
|
2012-05-29 17:54:36 +02:00
|
|
|
if ((szReaderRxBits = nfc_target_receive_bits(pndTag, abtReaderRx, sizeof(abtReaderRx), abtReaderRxPar)) > 0) {
|
2009-04-29 14:47:41 +02:00
|
|
|
// Drop down the field before sending a REQA command and start a new session
|
2010-09-07 19:51:03 +02:00
|
|
|
if (szReaderRxBits == 7 && abtReaderRx[0] == 0x26) {
|
2009-04-29 14:47:41 +02:00
|
|
|
// Drop down field for a very short time (original tag will reboot)
|
2012-05-29 17:54:36 +02:00
|
|
|
if (nfc_device_set_property_bool(pndReader, NP_ACTIVATE_FIELD, false) < 0) {
|
|
|
|
nfc_perror(pndReader, "nfc_device_set_property_bool");
|
2013-03-05 19:44:59 +01:00
|
|
|
nfc_close(pndTag);
|
|
|
|
nfc_close(pndReader);
|
|
|
|
nfc_exit(context);
|
2012-05-29 17:54:36 +02:00
|
|
|
exit(EXIT_FAILURE);
|
2010-08-18 19:22:13 +02:00
|
|
|
}
|
2010-09-07 19:51:03 +02:00
|
|
|
if (!quiet_output)
|
2012-05-29 17:54:36 +02:00
|
|
|
printf("\n");
|
|
|
|
if (nfc_device_set_property_bool(pndReader, NP_ACTIVATE_FIELD, true) < 0) {
|
|
|
|
nfc_perror(pndReader, "nfc_device_set_property_bool");
|
2013-03-05 19:44:59 +01:00
|
|
|
nfc_close(pndTag);
|
|
|
|
nfc_close(pndReader);
|
|
|
|
nfc_exit(context);
|
2012-05-29 17:54:36 +02:00
|
|
|
exit(EXIT_FAILURE);
|
2010-08-18 19:22:13 +02:00
|
|
|
}
|
2009-04-29 14:47:41 +02:00
|
|
|
}
|
|
|
|
// Print the reader frame to the screen
|
2010-09-07 19:51:03 +02:00
|
|
|
if (!quiet_output) {
|
2012-05-29 17:54:36 +02:00
|
|
|
printf("R: ");
|
|
|
|
print_hex_par(abtReaderRx, (size_t) szReaderRxBits, abtReaderRxPar);
|
2009-08-23 11:50:46 +02:00
|
|
|
}
|
2009-04-29 14:47:41 +02:00
|
|
|
// Forward the frame to the original tag
|
2012-01-04 16:30:42 +01:00
|
|
|
if ((szTagRxBits = nfc_initiator_transceive_bits
|
2012-11-24 13:00:23 +01:00
|
|
|
(pndReader, abtReaderRx, (size_t) szReaderRxBits, abtReaderRxPar, abtTagRx, sizeof(abtTagRx), abtTagRxPar)) > 0) {
|
2009-04-29 14:47:41 +02:00
|
|
|
// Redirect the answer back to the reader
|
2012-05-29 17:54:36 +02:00
|
|
|
if (nfc_target_send_bits(pndTag, abtTagRx, szTagRxBits, abtTagRxPar) < 0) {
|
|
|
|
nfc_perror(pndTag, "nfc_target_send_bits");
|
2013-03-05 19:44:59 +01:00
|
|
|
nfc_close(pndTag);
|
|
|
|
nfc_close(pndReader);
|
|
|
|
nfc_exit(context);
|
2012-05-29 17:54:36 +02:00
|
|
|
exit(EXIT_FAILURE);
|
2010-08-18 19:22:13 +02:00
|
|
|
}
|
2009-04-29 14:47:41 +02:00
|
|
|
// Print the tag frame to the screen
|
2010-09-07 19:51:03 +02:00
|
|
|
if (!quiet_output) {
|
2012-05-29 17:54:36 +02:00
|
|
|
printf("T: ");
|
|
|
|
print_hex_par(abtTagRx, szTagRxBits, abtTagRxPar);
|
2009-08-23 11:50:46 +02:00
|
|
|
}
|
2009-04-29 14:47:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-05-29 17:54:36 +02:00
|
|
|
nfc_close(pndTag);
|
|
|
|
nfc_close(pndReader);
|
2012-12-04 19:29:57 +01:00
|
|
|
nfc_exit(context);
|
2012-05-29 17:54:36 +02:00
|
|
|
exit(EXIT_SUCCESS);
|
2009-04-29 14:47:41 +02:00
|
|
|
}
|