Emulated DEP targets can now be customized (NFCID3 and General Bytes).

This commit is contained in:
Romuald Conty 2010-10-08 21:54:59 +00:00
parent b72ce3decd
commit 08b06c1d03
3 changed files with 48 additions and 15 deletions

View file

@ -32,6 +32,8 @@
#include <nfc/nfc.h> #include <nfc/nfc.h>
#include "nfc-utils.h"
#define MAX_FRAME_LEN 264 #define MAX_FRAME_LEN 264
int int
@ -63,14 +65,17 @@ main (int argc, const char *argv[])
return EXIT_FAILURE; 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. const nfc_target_t nt = {
nfc_target_t nt = { .ntt = NTT_DEP_PASSIVE_106,
.ntt = NTT_GENERIC_PASSIVE_106, .nti.ndi.abtNFCID3 = { 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xff, 0x00, 0x00 },
.nti.nai.abtAtqa = "\x04\x00", .nti.ndi.szGB = 4,
.nti.nai.abtUid = "\xde\xad\xbe\xaf\x62", .nti.ndi.abtGB = { 0x12, 0x34, 0x56, 0x78 },
.nti.nai.btSak = 0x20, /* These bytes are not used by nfc_target_init: the chip will provide them automatically to the initiator */
.nti.nai.szUidLen = 5, .nti.ndi.btDID = 0x00,
.nti.nai.szAtsLen = 0, .nti.ndi.btBS = 0x00,
.nti.ndi.btBR = 0x00,
.nti.ndi.btTO = 0x00,
.nti.ndi.btPP = 0x01,
}; };
if (!pnd) { if (!pnd) {
@ -79,6 +84,8 @@ main (int argc, const char *argv[])
} }
printf ("Connected to NFC device: %s\n", pnd->acName); 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"); printf ("Waiting for initiator request...\n");
if(!nfc_target_init (pnd, NTM_DEP, nt, abtRx, &szRx)) { if(!nfc_target_init (pnd, NTM_DEP, nt, abtRx, &szRx)) {
nfc_perror(pnd, "nfc_target_init"); nfc_perror(pnd, "nfc_target_init");

View file

@ -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 abtMifareParams[6];
byte_t * pbtMifareParams = NULL; byte_t * pbtMifareParams = NULL;
const byte_t * pbtNFCID3t = NULL;
const byte_t * pbtGB = NULL;
size_t szGB = 0;
switch(nt.ntt) { switch(nt.ntt) {
case NTT_MIFARE: case NTT_MIFARE:
case NTT_GENERIC_PASSIVE_106: 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; pbtMifareParams = abtMifareParams;
} }
break; 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; return false;
} }
@ -1075,12 +1086,15 @@ pn53x_target_init (nfc_device_t * pnd, const nfc_target_mode_t ntm, const nfc_ta
bool bool
pn53x_TgInitAsTarget (nfc_device_t * pnd, nfc_target_mode_t ntm, 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 * pbtRx, size_t * pszRxLen)
{ {
byte_t abtRx[MAX_FRAME_LEN]; byte_t abtRx[MAX_FRAME_LEN];
size_t szRxLen; 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)); 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) { if (pbtNFCID3t) {
memcpy(abtCmd+27, pbtNFCID3t, 10); 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 // Request the initialization as a target
szRxLen = MAX_FRAME_LEN; 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; return false;
// Note: the first byte is skip: // Note: the first byte is skip:

View file

@ -145,7 +145,9 @@ bool pn53x_InAutoPoll (nfc_device_t * pnd, const nfc_target_type_t * pnttTarg
const byte_t btPollNr, const byte_t btPeriod, nfc_target_t * pntTargets, const byte_t btPollNr, const byte_t btPeriod, nfc_target_t * pntTargets,
size_t * pszTargetFound); size_t * pszTargetFound);
bool pn53x_TgInitAsTarget (nfc_device_t * pnd, nfc_target_mode_t ntm, 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 * pbtRx, size_t * pszRxLen);