2010-01-15 11:18:11 +01:00
|
|
|
/*-
|
2009-10-12 16:52:26 +02:00
|
|
|
* Public platform independent Near Field Communication (NFC) library
|
|
|
|
*
|
2010-02-09 16:39:56 +01:00
|
|
|
* Copyright (C) 2009, 2010, Roel Verdult, Romuald Conty
|
2009-10-12 16:52:26 +02:00
|
|
|
*
|
|
|
|
* 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/>
|
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
|
|
|
|
#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
|
|
|
|
2009-12-01 15:23:00 +01:00
|
|
|
#include <nfc/nfc-messages.h>
|
2010-04-16 18:38:57 +02:00
|
|
|
#include "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
|
|
|
|
2009-07-16 14:09:06 +02:00
|
|
|
static byte_t abtReaderRx[MAX_FRAME_LEN];
|
|
|
|
static byte_t abtReaderRxPar[MAX_FRAME_LEN];
|
2009-10-02 11:52:02 +02:00
|
|
|
static size_t szReaderRxBits;
|
2009-07-16 14:09:06 +02:00
|
|
|
static byte_t abtTagRx[MAX_FRAME_LEN];
|
|
|
|
static byte_t abtTagRxPar[MAX_FRAME_LEN];
|
2009-10-02 11:52:02 +02:00
|
|
|
static size_t szTagRxBits;
|
2009-11-09 12:23:33 +01:00
|
|
|
static nfc_device_t* pndReader;
|
|
|
|
static nfc_device_t* pndTag;
|
2009-11-24 18:49:24 +01:00
|
|
|
static bool quitting=false;
|
|
|
|
|
|
|
|
void intr_hdlr(void)
|
|
|
|
{
|
|
|
|
printf("\nQuitting...\n");
|
|
|
|
quitting=true;
|
|
|
|
return;
|
|
|
|
}
|
2009-04-29 14:47:41 +02:00
|
|
|
|
2009-10-05 17:20:42 +02:00
|
|
|
void print_usage(char* argv[])
|
2009-09-07 12:15:34 +02:00
|
|
|
{
|
2009-10-05 17:01:08 +02:00
|
|
|
printf("Usage: %s [OPTIONS]\n", argv[0]);
|
2009-09-07 12:15:34 +02:00
|
|
|
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-08-23 11:50:46 +02:00
|
|
|
int main(int argc,char* argv[])
|
2009-09-07 12:15:34 +02:00
|
|
|
{
|
|
|
|
int arg;
|
2009-08-26 12:57:38 +02:00
|
|
|
bool quiet_output = false;
|
2010-02-09 16:39:56 +01:00
|
|
|
size_t szFound;
|
|
|
|
nfc_device_desc_t *pnddDevices;
|
2010-03-22 21:34:02 +01:00
|
|
|
const char* acLibnfcVersion = nfc_version();
|
2009-08-23 11:50:46 +02:00
|
|
|
|
|
|
|
// Get commandline options
|
2009-09-07 12:15:34 +02:00
|
|
|
for (arg=1;arg<argc;arg++) {
|
|
|
|
if (0 == strcmp(argv[arg], "-h")) {
|
2009-10-05 17:20:42 +02:00
|
|
|
print_usage(argv);
|
2010-02-09 16:39:56 +01:00
|
|
|
return EXIT_SUCCESS;
|
2009-09-07 12:15:34 +02:00
|
|
|
} else if (0 == strcmp(argv[arg], "-q")) {
|
2009-12-01 12:39:35 +01:00
|
|
|
INFO("%s", "Quiet mode.");
|
2009-08-26 12:57:38 +02:00
|
|
|
quiet_output = true;
|
2009-09-07 12:15:34 +02:00
|
|
|
} else {
|
|
|
|
ERR("%s is not supported option.", argv[arg]);
|
2009-10-05 17:20:42 +02:00
|
|
|
print_usage(argv);
|
2010-02-09 16:39:56 +01:00
|
|
|
return 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
|
|
|
|
printf("%s use libnfc %s\n", argv[0], acLibnfcVersion);
|
|
|
|
|
2009-11-24 18:49:24 +01:00
|
|
|
#ifdef WIN32
|
|
|
|
signal(SIGINT, (void (__cdecl*)(int)) intr_hdlr);
|
|
|
|
#else
|
|
|
|
signal(SIGINT, (void (*)()) intr_hdlr);
|
|
|
|
#endif
|
|
|
|
|
2010-02-09 16:39:56 +01:00
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
// List available devices
|
|
|
|
nfc_list_devices (pnddDevices, MAX_DEVICE_COUNT, &szFound);
|
|
|
|
|
|
|
|
if (szFound < 2) {
|
|
|
|
ERR("%zd device found but two connected devices are needed to relay NFC.", szFound);
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
2009-04-29 14:47:41 +02:00
|
|
|
// Try to open the NFC emulator device
|
2010-02-09 16:39:56 +01:00
|
|
|
pndTag = nfc_connect(&(pnddDevices[0]));
|
2009-11-10 10:47:59 +01:00
|
|
|
if (pndTag == NULL)
|
2009-04-29 14:47:41 +02:00
|
|
|
{
|
|
|
|
printf("Error connecting NFC emulator device\n");
|
2010-02-09 16:39:56 +01:00
|
|
|
return EXIT_FAILURE;
|
2009-04-29 14:47:41 +02:00
|
|
|
}
|
2010-02-09 16:39:56 +01:00
|
|
|
|
|
|
|
printf("Hint: tag <---> emulator (relay) <---> reader (relay) <---> original reader\n\n");
|
|
|
|
|
|
|
|
printf("Connected to the NFC emulator device: %s\n", pndTag->acName);
|
2009-08-26 12:57:38 +02:00
|
|
|
printf("[+] Try to break out the auto-emulation, this requires a second reader!\n");
|
2009-04-29 14:47:41 +02:00
|
|
|
printf("[+] To do this, please send any command after the anti-collision\n");
|
2009-08-26 12:57:38 +02:00
|
|
|
printf("[+] For example, send a RATS command or use the \"nfc-anticol\" tool\n");
|
2009-11-24 18:49:24 +01:00
|
|
|
if (!nfc_target_init(pndTag,abtReaderRx,&szReaderRxBits))
|
|
|
|
{
|
2010-02-09 16:39:56 +01:00
|
|
|
ERR("%s", "Initialization of NFC emulator failed");
|
2009-11-24 18:49:24 +01:00
|
|
|
nfc_disconnect(pndTag);
|
2010-02-09 16:39:56 +01:00
|
|
|
return EXIT_FAILURE;
|
2009-11-24 18:49:24 +01:00
|
|
|
}
|
2010-02-09 16:39:56 +01:00
|
|
|
printf("%s", "Configuring emulator settings...");
|
2009-11-18 12:11:06 +01:00
|
|
|
nfc_configure(pndTag,NDO_HANDLE_CRC,false);
|
|
|
|
nfc_configure(pndTag,NDO_HANDLE_PARITY,false);
|
|
|
|
nfc_configure(pndTag,NDO_ACCEPT_INVALID_FRAMES,true);
|
2010-02-09 16:39:56 +01:00
|
|
|
printf("%s", "Done, emulated tag is initialized");
|
2009-04-29 14:47:41 +02:00
|
|
|
|
|
|
|
// Try to open the NFC reader
|
2010-02-09 16:39:56 +01:00
|
|
|
pndReader = nfc_connect(&(pnddDevices[1]));
|
|
|
|
|
|
|
|
printf("Connected to the NFC reader device: %s", pndReader->acName);
|
|
|
|
printf("%s", "Configuring NFC reader settings...");
|
2009-11-24 18:49:24 +01:00
|
|
|
nfc_initiator_init(pndReader);
|
2009-11-18 12:11:06 +01:00
|
|
|
nfc_configure(pndReader,NDO_HANDLE_CRC,false);
|
|
|
|
nfc_configure(pndReader,NDO_HANDLE_PARITY,false);
|
|
|
|
nfc_configure(pndReader,NDO_ACCEPT_INVALID_FRAMES,true);
|
2010-02-09 16:39:56 +01:00
|
|
|
printf("%s", "Done, relaying frames now!");
|
2009-04-29 14:47:41 +02:00
|
|
|
|
2009-11-24 18:49:24 +01:00
|
|
|
while(!quitting)
|
2009-04-29 14:47:41 +02:00
|
|
|
{
|
|
|
|
// Test if we received a frame from the reader
|
2009-11-09 12:23:33 +01:00
|
|
|
if (nfc_target_receive_bits(pndTag,abtReaderRx,&szReaderRxBits,abtReaderRxPar))
|
2009-04-29 14:47:41 +02:00
|
|
|
{
|
|
|
|
// Drop down the field before sending a REQA command and start a new session
|
2009-10-02 11:52:02 +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)
|
2009-11-18 12:11:06 +01:00
|
|
|
nfc_configure(pndReader,NDO_ACTIVATE_FIELD,false);
|
2009-08-26 12:57:38 +02:00
|
|
|
if(!quiet_output)
|
2009-08-23 11:50:46 +02:00
|
|
|
printf("\n");
|
2009-11-18 12:11:06 +01:00
|
|
|
nfc_configure(pndReader,NDO_ACTIVATE_FIELD,true);
|
2009-04-29 14:47:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Print the reader frame to the screen
|
2009-08-26 12:57:38 +02:00
|
|
|
if(!quiet_output)
|
2009-08-23 11:50:46 +02:00
|
|
|
{
|
|
|
|
printf("R: ");
|
2009-10-02 11:52:02 +02:00
|
|
|
print_hex_par(abtReaderRx,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
|
2009-11-09 12:23:33 +01:00
|
|
|
if (nfc_initiator_transceive_bits(pndReader,abtReaderRx,szReaderRxBits,abtReaderRxPar,abtTagRx,&szTagRxBits,abtTagRxPar))
|
2009-04-29 14:47:41 +02:00
|
|
|
{
|
|
|
|
// Redirect the answer back to the reader
|
2009-11-09 12:23:33 +01:00
|
|
|
nfc_target_send_bits(pndTag,abtTagRx,szTagRxBits,abtTagRxPar);
|
2009-09-08 10:14:36 +02:00
|
|
|
|
2009-04-29 14:47:41 +02:00
|
|
|
// Print the tag frame to the screen
|
2009-08-26 12:57:38 +02:00
|
|
|
if(!quiet_output)
|
2009-08-23 11:50:46 +02:00
|
|
|
{
|
|
|
|
printf("T: ");
|
2009-10-02 11:52:02 +02:00
|
|
|
print_hex_par(abtTagRx,szTagRxBits,abtTagRxPar);
|
2009-08-23 11:50:46 +02:00
|
|
|
}
|
2009-04-29 14:47:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-11-09 12:23:33 +01:00
|
|
|
nfc_disconnect(pndTag);
|
|
|
|
nfc_disconnect(pndReader);
|
2010-02-09 16:39:56 +01:00
|
|
|
exit(EXIT_SUCCESS);
|
2009-04-29 14:47:41 +02:00
|
|
|
}
|