2009-04-29 14:47:41 +02:00
|
|
|
/*
|
|
|
|
|
|
|
|
Public platform independent Near Field Communication (NFC) library
|
|
|
|
Copyright (C) 2009, Roel Verdult
|
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
2009-06-26 11:05:25 +02:00
|
|
|
it under the terms of the GNU Lesser General Public License as published by
|
2009-04-29 14:47:41 +02:00
|
|
|
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.
|
|
|
|
|
2009-06-26 11:05:25 +02:00
|
|
|
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/>
|
2009-04-29 14:47:41 +02:00
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2009-05-27 14:18:21 +02:00
|
|
|
#include <stddef.h>
|
2009-05-27 12:13:19 +02:00
|
|
|
#include <stdint.h>
|
2009-05-27 16:05:07 +02:00
|
|
|
#include <stdbool.h>
|
2009-04-29 14:47:41 +02:00
|
|
|
#include <string.h>
|
2009-05-27 12:13:19 +02:00
|
|
|
|
2009-04-29 14:47:41 +02:00
|
|
|
#include "libnfc.h"
|
|
|
|
|
2009-09-08 12:25:59 +02:00
|
|
|
#include "messages.h"
|
|
|
|
|
2009-04-29 14:47:41 +02:00
|
|
|
#define SAK_FLAG_ATS_SUPPORTED 0x20
|
|
|
|
|
2009-07-16 14:09:06 +02:00
|
|
|
static byte_t abtRx[MAX_FRAME_LEN];
|
2009-05-27 12:13:19 +02:00
|
|
|
static uint32_t uiRxBits;
|
|
|
|
static uint32_t uiRxLen;
|
2009-07-16 14:09:06 +02:00
|
|
|
static byte_t abtUid[10];
|
2009-05-27 12:13:19 +02:00
|
|
|
static uint32_t uiUidLen = 4;
|
2009-04-29 14:47:41 +02:00
|
|
|
static dev_info* pdi;
|
|
|
|
|
2009-08-26 12:57:38 +02:00
|
|
|
bool quiet_output = false;
|
2009-08-23 11:50:46 +02:00
|
|
|
|
2009-04-29 14:47:41 +02:00
|
|
|
// ISO14443A Anti-Collision Commands
|
2009-07-16 14:09:06 +02:00
|
|
|
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,0xbc,0xa5 };
|
|
|
|
byte_t abtHalt [4] = { 0x50,0x00,0x57,0xcd };
|
2009-04-29 14:47:41 +02:00
|
|
|
|
2009-07-16 14:09:06 +02:00
|
|
|
bool transmit_bits(const byte_t* pbtTx, const uint32_t uiTxBits)
|
2009-04-29 14:47:41 +02:00
|
|
|
{
|
|
|
|
// Show transmitted command
|
2009-08-26 12:57:38 +02:00
|
|
|
if(!quiet_output)
|
2009-08-23 11:50:46 +02:00
|
|
|
{
|
|
|
|
printf("R: ");
|
|
|
|
print_hex_bits(pbtTx,uiTxBits);
|
|
|
|
}
|
2009-04-29 14:47:41 +02:00
|
|
|
|
|
|
|
// Transmit the bit frame command, we don't use the arbitrary parity feature
|
2009-07-23 12:24:04 +02:00
|
|
|
if (!nfc_initiator_transceive_bits(pdi,pbtTx,uiTxBits,NULL,abtRx,&uiRxBits,NULL)) return false;
|
2009-04-29 14:47:41 +02:00
|
|
|
|
|
|
|
// Show received answer
|
2009-08-26 12:57:38 +02:00
|
|
|
if(!quiet_output)
|
2009-08-23 11:50:46 +02:00
|
|
|
{
|
|
|
|
printf("T: ");
|
|
|
|
print_hex_bits(abtRx,uiRxBits);
|
|
|
|
}
|
2009-04-29 14:47:41 +02:00
|
|
|
|
|
|
|
// Succesful transfer
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-07-16 14:09:06 +02:00
|
|
|
bool transmit_bytes(const byte_t* pbtTx, const uint32_t uiTxLen)
|
2009-04-29 14:47:41 +02:00
|
|
|
{
|
|
|
|
// Show transmitted command
|
2009-08-26 12:57:38 +02:00
|
|
|
if(!quiet_output)
|
2009-08-23 11:50:46 +02:00
|
|
|
{
|
|
|
|
printf("R: ");
|
|
|
|
print_hex(pbtTx,uiTxLen);
|
|
|
|
}
|
2009-04-29 14:47:41 +02:00
|
|
|
|
|
|
|
// Transmit the command bytes
|
2009-07-23 12:24:04 +02:00
|
|
|
if (!nfc_initiator_transceive_bytes(pdi,pbtTx,uiTxLen,abtRx,&uiRxLen)) return false;
|
2009-04-29 14:47:41 +02:00
|
|
|
|
|
|
|
// Show received answer
|
2009-08-26 12:57:38 +02:00
|
|
|
if(!quiet_output)
|
2009-08-23 11:50:46 +02:00
|
|
|
{
|
|
|
|
printf("T: ");
|
|
|
|
print_hex(abtRx,uiRxLen);
|
|
|
|
}
|
2009-04-29 14:47:41 +02:00
|
|
|
|
|
|
|
// Succesful transfer
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-09-07 12:15:34 +02:00
|
|
|
void print_usage(void)
|
|
|
|
{
|
|
|
|
printf("Usage: nfc-anticol [OPTIONS]\n");
|
|
|
|
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-04-29 14:47:41 +02:00
|
|
|
{
|
2009-09-07 12:15:34 +02:00
|
|
|
int arg;
|
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")) {
|
|
|
|
print_usage();
|
|
|
|
return 0;
|
|
|
|
} else if (0 == strcmp(argv[arg], "-q")) {
|
|
|
|
INFO("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]);
|
|
|
|
print_usage();
|
2009-08-23 11:50:46 +02:00
|
|
|
return -1;
|
|
|
|
}
|
2009-09-07 12:15:34 +02:00
|
|
|
}
|
2009-08-23 11:50:46 +02:00
|
|
|
|
2009-04-29 14:47:41 +02:00
|
|
|
// Try to open the NFC reader
|
2009-09-04 15:24:34 +02:00
|
|
|
pdi = nfc_connect(NULL);
|
2009-09-07 12:15:34 +02:00
|
|
|
|
2009-04-29 14:47:41 +02:00
|
|
|
if (!pdi)
|
|
|
|
{
|
|
|
|
printf("Error connecting NFC reader\n");
|
|
|
|
return 1;
|
|
|
|
}
|
2009-07-23 12:24:04 +02:00
|
|
|
nfc_initiator_init(pdi);
|
2009-04-29 14:47:41 +02:00
|
|
|
|
|
|
|
// Drop the field for a while
|
|
|
|
nfc_configure(pdi,DCO_ACTIVATE_FIELD,false);
|
|
|
|
|
|
|
|
// Configure the CRC and Parity settings
|
|
|
|
nfc_configure(pdi,DCO_HANDLE_CRC,false);
|
|
|
|
nfc_configure(pdi,DCO_HANDLE_PARITY,true);
|
|
|
|
|
|
|
|
// Enable field so more power consuming cards can power themselves up
|
|
|
|
nfc_configure(pdi,DCO_ACTIVATE_FIELD,true);
|
|
|
|
|
|
|
|
printf("\nConnected to NFC reader: %s\n\n",pdi->acName);
|
|
|
|
|
|
|
|
// Send the 7 bits request command specified in ISO 14443A (0x26)
|
|
|
|
if (!transmit_bits(abtReqa,7))
|
|
|
|
{
|
|
|
|
printf("Error: No tag available\n");
|
|
|
|
nfc_disconnect(pdi);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Anti-collision
|
|
|
|
transmit_bytes(abtSelectAll,2);
|
|
|
|
|
|
|
|
// Save the UID
|
|
|
|
memcpy(abtUid,abtRx,4);
|
|
|
|
memcpy(abtSelectTag+2,abtRx,5);
|
|
|
|
append_iso14443a_crc(abtSelectTag,7);
|
|
|
|
transmit_bytes(abtSelectTag,9);
|
|
|
|
|
|
|
|
// Test if we are dealing with a 4 bytes uid
|
|
|
|
if (abtUid[0]!= 0x88)
|
|
|
|
{
|
|
|
|
uiUidLen = 4;
|
|
|
|
} else {
|
|
|
|
// We have to do the anti-collision for cascade level 2
|
|
|
|
abtSelectAll[0] = 0x95;
|
|
|
|
abtSelectTag[0] = 0x95;
|
|
|
|
|
|
|
|
// Anti-collision
|
|
|
|
transmit_bytes(abtSelectAll,2);
|
|
|
|
|
|
|
|
// Save the UID
|
|
|
|
memcpy(abtUid+4,abtRx,4);
|
|
|
|
memcpy(abtSelectTag+2,abtRx,5);
|
|
|
|
append_iso14443a_crc(abtSelectTag,7);
|
|
|
|
transmit_bytes(abtSelectTag,9);
|
|
|
|
uiUidLen = 7;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Request ATS, this only applies to tags that support ISO 14443A-4
|
|
|
|
if (abtRx[0] & SAK_FLAG_ATS_SUPPORTED) transmit_bytes(abtRats,4);
|
|
|
|
|
|
|
|
// Done, halt the tag now
|
|
|
|
transmit_bytes(abtHalt,4);
|
|
|
|
|
|
|
|
printf("\nFound tag with UID: ");
|
|
|
|
if (uiUidLen == 4)
|
|
|
|
{
|
|
|
|
printf("%08x\n",swap_endian32(abtUid));
|
|
|
|
} else {
|
|
|
|
printf("%014llx\n",swap_endian64(abtUid)&0x00ffffffffffffffull);
|
|
|
|
}
|
|
|
|
|
|
|
|
nfc_disconnect(pdi);
|
|
|
|
return 0;
|
|
|
|
}
|