From 08b06c1d03e362abb9ae096f372607309a2f2495 Mon Sep 17 00:00:00 2001 From: Romuald Conty Date: Fri, 8 Oct 2010 21:54:59 +0000 Subject: [PATCH] Emulated DEP targets can now be customized (NFCID3 and General Bytes). --- examples/nfc-dep-target.c | 23 +++++++++++++++-------- libnfc/chips/pn53x.c | 34 +++++++++++++++++++++++++++++----- libnfc/chips/pn53x.h | 6 ++++-- 3 files changed, 48 insertions(+), 15 deletions(-) diff --git a/examples/nfc-dep-target.c b/examples/nfc-dep-target.c index f3bbaa2..afab447 100644 --- a/examples/nfc-dep-target.c +++ b/examples/nfc-dep-target.c @@ -32,6 +32,8 @@ #include +#include "nfc-utils.h" + #define MAX_FRAME_LEN 264 int @@ -63,14 +65,17 @@ main (int argc, const char *argv[]) return EXIT_FAILURE; } - // 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 = { - .ntt = NTT_GENERIC_PASSIVE_106, - .nti.nai.abtAtqa = "\x04\x00", - .nti.nai.abtUid = "\xde\xad\xbe\xaf\x62", - .nti.nai.btSak = 0x20, - .nti.nai.szUidLen = 5, - .nti.nai.szAtsLen = 0, + const nfc_target_t nt = { + .ntt = NTT_DEP_PASSIVE_106, + .nti.ndi.abtNFCID3 = { 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xff, 0x00, 0x00 }, + .nti.ndi.szGB = 4, + .nti.ndi.abtGB = { 0x12, 0x34, 0x56, 0x78 }, + /* These bytes are not used by nfc_target_init: the chip will provide them automatically to the initiator */ + .nti.ndi.btDID = 0x00, + .nti.ndi.btBS = 0x00, + .nti.ndi.btBR = 0x00, + .nti.ndi.btTO = 0x00, + .nti.ndi.btPP = 0x01, }; if (!pnd) { @@ -79,6 +84,8 @@ main (int argc, const char *argv[]) } printf ("Connected to NFC device: %s\n", pnd->acName); + printf ("NFC device will now act as this D.E.P. target:\n"); + print_nfc_dep_info ( nt.nti.ndi ); printf ("Waiting for initiator request...\n"); if(!nfc_target_init (pnd, NTM_DEP, nt, abtRx, &szRx)) { nfc_perror(pnd, "nfc_target_init"); diff --git a/libnfc/chips/pn53x.c b/libnfc/chips/pn53x.c index 67b0817..d30f690 100644 --- a/libnfc/chips/pn53x.c +++ b/libnfc/chips/pn53x.c @@ -1040,6 +1040,10 @@ pn53x_target_init (nfc_device_t * pnd, const nfc_target_mode_t ntm, const nfc_ta byte_t abtMifareParams[6]; byte_t * pbtMifareParams = NULL; + const byte_t * pbtNFCID3t = NULL; + const byte_t * pbtGB = NULL; + size_t szGB = 0; + switch(nt.ntt) { case NTT_MIFARE: case NTT_GENERIC_PASSIVE_106: @@ -1058,9 +1062,16 @@ pn53x_target_init (nfc_device_t * pnd, const nfc_target_mode_t ntm, const nfc_ta pbtMifareParams = abtMifareParams; } break; + case NTT_DEP_PASSIVE_106: + case NTT_DEP_PASSIVE_212: + case NTT_DEP_PASSIVE_424: + pbtNFCID3t = nt.nti.ndi.abtNFCID3; + szGB = nt.nti.ndi.szGB; + if (szGB) pbtGB = nt.nti.ndi.abtGB; + break; } - if(!pn53x_TgInitAsTarget(pnd, ntm, pbtMifareParams, NULL, NULL, pbtRx, pszRxLen)) { + if(!pn53x_TgInitAsTarget(pnd, ntm, pbtMifareParams, NULL, pbtNFCID3t, pbtGB, szGB, pbtRx, pszRxLen)) { return false; } @@ -1075,12 +1086,15 @@ pn53x_target_init (nfc_device_t * pnd, const nfc_target_mode_t ntm, const nfc_ta bool pn53x_TgInitAsTarget (nfc_device_t * pnd, nfc_target_mode_t ntm, - const byte_t * pbtMifareParams, const byte_t * pbtFeliCaParams, const byte_t * pbtNFCID3t, + const byte_t * pbtMifareParams, + const byte_t * pbtFeliCaParams, + const byte_t * pbtNFCID3t, const byte_t * pbtGB, const size_t szGB, byte_t * pbtRx, size_t * pszRxLen) { byte_t abtRx[MAX_FRAME_LEN]; size_t szRxLen; - byte_t abtCmd[sizeof (pncmd_target_init)]; + byte_t abtCmd[sizeof (pncmd_target_init) + 48]; // 47 bytes max. for General Bytes and 1 for GB lenght + size_t szOptionalBytes = 0; memcpy (abtCmd, pncmd_target_init, sizeof (pncmd_target_init)); @@ -1099,11 +1113,21 @@ pn53x_TgInitAsTarget (nfc_device_t * pnd, nfc_target_mode_t ntm, if (pbtNFCID3t) { memcpy(abtCmd+27, pbtNFCID3t, 10); } - // TODO Handle General bytes and Tk (Historical bytes) length + if (szGB) { + if( pnd->nc == NC_PN531 ) { + memcpy (abtCmd+37, pbtGB, szGB); + szOptionalBytes = szGB; + } else { + abtCmd[37] = (byte_t)(szGB); + memcpy (abtCmd+38, pbtGB, szGB); + szOptionalBytes = szGB + 1; + } + } + // TODO Handle Tk (Historical bytes) length (only available on PN532, PN533) // Request the initialization as a target szRxLen = MAX_FRAME_LEN; - if (!pn53x_transceive (pnd, abtCmd, sizeof (pncmd_target_init), abtRx, &szRxLen)) + if (!pn53x_transceive (pnd, abtCmd, sizeof (pncmd_target_init) + szOptionalBytes, abtRx, &szRxLen)) return false; // Note: the first byte is skip: diff --git a/libnfc/chips/pn53x.h b/libnfc/chips/pn53x.h index 0e2c9a1..8d87a0b 100644 --- a/libnfc/chips/pn53x.h +++ b/libnfc/chips/pn53x.h @@ -144,8 +144,10 @@ bool pn53x_InRelease (nfc_device_t * pnd, const uint8_t ui8Target); bool pn53x_InAutoPoll (nfc_device_t * pnd, const nfc_target_type_t * pnttTargetTypes, const size_t szTargetTypes, const byte_t btPollNr, const byte_t btPeriod, nfc_target_t * pntTargets, size_t * pszTargetFound); -bool pn53x_TgInitAsTarget (nfc_device_t * pnd, nfc_target_mode_t ntm, - const byte_t * pbtMifareParams, const byte_t * pbtFeliCaParams, const byte_t * pbtNFCID3t, +bool pn53x_TgInitAsTarget (nfc_device_t * pnd, nfc_target_mode_t ntm, + const byte_t * pbtMifareParams, + const byte_t * pbtFeliCaParams, + const byte_t * pbtNFCID3t, const byte_t * pbtGB, const size_t szGB, byte_t * pbtRx, size_t * pszRxLen);