2011-06-30 16:59:47 +02: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) 2011 Romuald Conty
|
2012-05-29 02:33:22 +02:00
|
|
|
*
|
2011-06-30 16:59:47 +02: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.
|
2011-06-30 16:59:47 +02: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
|
|
|
*
|
2011-06-30 16:59:47 +02:00
|
|
|
* Note that this license only applies on the examples, NFC library itself is under LGPL
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @file nfc-emulate-forum-tag2.c
|
2012-09-19 12:25:33 +02:00
|
|
|
* @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.
|
2012-05-29 02:33:22 +02:00
|
|
|
*
|
2012-09-19 12:25:33 +02:00
|
|
|
* This example has been developed using PN533 USB hardware as target and
|
|
|
|
* Google Nexus S phone as initiator.
|
2011-06-30 16:59:47 +02:00
|
|
|
*
|
2012-09-19 12:25:33 +02:00
|
|
|
* This is know to NOT work with Nokia 6212 Classic and could fail with
|
|
|
|
* several NFC Forum compliant devices due to the following reasons:
|
|
|
|
* - The emulated target has only a 4-byte UID while most devices assume a Tag
|
|
|
|
* Type 2 has always a 7-byte UID (as a real Mifare Ultralight tag);
|
|
|
|
* - The chip is emulating an ISO/IEC 14443-3 tag, without any hardware helper.
|
|
|
|
* If the initiator has too strict timeouts for software-based emulation
|
|
|
|
* (which is usually the case), this example will fail. This is not a bug
|
|
|
|
* and we can't do anything using this hardware (PN531/PN533).
|
2011-06-30 16:59:47 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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
|
2012-05-29 02:33:22 +02:00
|
|
|
*
|
|
|
|
* ISO/IEC 14443-3
|
2011-06-30 16:59:47 +02:00
|
|
|
* 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>
|
|
|
|
|
2011-11-24 11:27:02 +01:00
|
|
|
#include "utils/nfc-utils.h"
|
2011-06-30 16:59:47 +02:00
|
|
|
|
2011-11-23 16:55:40 +01:00
|
|
|
static nfc_device *pnd;
|
2011-06-30 16:59:47 +02:00
|
|
|
|
2012-05-13 14:50:30 +02:00
|
|
|
static void
|
2012-05-29 17:54:36 +02:00
|
|
|
stop_emulation(int sig)
|
2011-06-30 16:59:47 +02:00
|
|
|
{
|
|
|
|
(void)sig;
|
|
|
|
if (pnd) {
|
2012-05-29 17:52:51 +02:00
|
|
|
nfc_abort_command(pnd);
|
2011-06-30 16:59:47 +02:00
|
|
|
} else {
|
2012-05-29 17:54:36 +02:00
|
|
|
exit(EXIT_FAILURE);
|
2011-06-30 16:59:47 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static uint8_t __nfcforum_tag2_memory_area[] = {
|
|
|
|
0x00, 0x00, 0x00, 0x00, // Block 0
|
2012-05-29 02:33:22 +02:00
|
|
|
0x00, 0x00, 0x00, 0x00,
|
2011-06-30 16:59:47 +02:00
|
|
|
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
|
2012-05-13 14:50:30 +02:00
|
|
|
static int
|
2012-05-29 17:54:36 +02:00
|
|
|
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)
|
2011-06-30 16:59:47 +02:00
|
|
|
{
|
|
|
|
int res = 0;
|
|
|
|
|
|
|
|
uint8_t *nfcforum_tag2_memory_area = (uint8_t *)(emulator->user_data);
|
|
|
|
|
2012-05-29 17:54:36 +02:00
|
|
|
printf(" In: ");
|
|
|
|
print_hex(data_in, data_in_len);
|
2011-06-30 16:59:47 +02:00
|
|
|
|
|
|
|
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:
|
2012-05-29 17:54:36 +02:00
|
|
|
printf("HALT sent\n");
|
2011-06-30 16:59:47 +02:00
|
|
|
res = -ECONNABORTED;
|
|
|
|
break;
|
|
|
|
default:
|
2012-05-29 17:54:36 +02:00
|
|
|
printf("Unknown command: 0x%02x\n", data_in[0]);
|
2011-06-30 16:59:47 +02:00
|
|
|
res = -ENOTSUP;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (res < 0) {
|
2012-05-29 17:54:36 +02:00
|
|
|
ERR("%s (%d)", strerror(-res), -res);
|
2011-06-30 16:59:47 +02:00
|
|
|
} else {
|
2012-05-29 17:54:36 +02:00
|
|
|
printf(" Out: ");
|
|
|
|
print_hex(data_out, res);
|
2011-06-30 16:59:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
(void)argc;
|
|
|
|
(void)argv;
|
|
|
|
|
2011-11-23 16:55:40 +01:00
|
|
|
nfc_target nt = {
|
2011-06-30 16:59:47 +02:00
|
|
|
.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 = {
|
2012-01-20 10:17:38 +01:00
|
|
|
.io = nfcforum_tag2_io
|
2011-06-30 16:59:47 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
struct nfc_emulator emulator = {
|
2012-01-20 10:17:38 +01:00
|
|
|
.target = &nt,
|
2011-06-30 16:59:47 +02:00
|
|
|
.state_machine = &state_machine,
|
|
|
|
.user_data = __nfcforum_tag2_memory_area,
|
|
|
|
};
|
|
|
|
|
2012-05-29 17:54:36 +02:00
|
|
|
signal(SIGINT, stop_emulation);
|
2012-12-04 19:29:57 +01:00
|
|
|
|
|
|
|
nfc_context *context;
|
|
|
|
nfc_init(&context);
|
|
|
|
pnd = nfc_open(context, NULL);
|
2011-06-30 16:59:47 +02:00
|
|
|
|
|
|
|
if (pnd == NULL) {
|
2012-01-17 16:21:56 +01:00
|
|
|
ERR("Unable to open NFC device");
|
2012-05-29 17:54:36 +02:00
|
|
|
exit(EXIT_FAILURE);
|
2011-06-30 16:59:47 +02:00
|
|
|
}
|
|
|
|
|
2012-05-29 17:54:36 +02:00
|
|
|
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");
|
2011-06-30 16:59:47 +02:00
|
|
|
|
2012-05-29 17:54:36 +02:00
|
|
|
if (nfc_emulate_target(pnd, &emulator) < 0) {
|
2011-06-30 16:59:47 +02:00
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
2012-01-17 15:52:39 +01:00
|
|
|
nfc_close(pnd);
|
2012-12-04 19:29:57 +01:00
|
|
|
nfc_exit(context);
|
2011-06-30 16:59:47 +02:00
|
|
|
|
2012-05-29 17:54:36 +02:00
|
|
|
exit(EXIT_SUCCESS);
|
2011-06-30 16:59:47 +02:00
|
|
|
|
|
|
|
error:
|
|
|
|
if (pnd) {
|
2012-05-29 17:54:36 +02:00
|
|
|
nfc_perror(pnd, argv[0]);
|
|
|
|
nfc_close(pnd);
|
2012-12-04 19:29:57 +01:00
|
|
|
nfc_exit(context);
|
2011-06-30 16:59:47 +02:00
|
|
|
}
|
|
|
|
}
|