From 7c76e1bf3201aa31c2d52dbaae8c0802982109f1 Mon Sep 17 00:00:00 2001 From: Romuald Conty Date: Thu, 14 Oct 2010 16:27:50 +0000 Subject: [PATCH] nfc_target_init() now update nfc_target_t baud rate --- examples/nfc-dep-target.c | 6 +++--- examples/nfc-emulate-forum-tag4.c | 4 ++-- examples/nfc-emulate-tag.c | 18 ++++++++--------- examples/nfc-emulate-uid.c | 2 +- examples/nfc-relay-picc.c | 2 +- examples/nfc-relay.c | 2 +- examples/pn53x-sam.c | 2 +- include/nfc/nfc.h | 2 +- libnfc/chips/pn53x.c | 33 +++++++++++++++++-------------- libnfc/chips/pn53x.h | 2 +- libnfc/nfc.c | 9 +++++++-- 11 files changed, 45 insertions(+), 37 deletions(-) diff --git a/examples/nfc-dep-target.c b/examples/nfc-dep-target.c index 4cd064b..d1dd016 100644 --- a/examples/nfc-dep-target.c +++ b/examples/nfc-dep-target.c @@ -65,9 +65,9 @@ main (int argc, const char *argv[]) return EXIT_FAILURE; } - const nfc_target_t nt = { + nfc_target_t nt = { .nm.nmt = NMT_DEP, - .nm.nbr = NBR_UNDEFINED, // Not used by nfc_target_init + .nm.nbr = NBR_UNDEFINED, // Will be updated by nfc_target_init .nti.ndi.abtNFCID3 = { 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xff, 0x00, 0x00 }, .nti.ndi.szGB = 4, .nti.ndi.abtGB = { 0x12, 0x34, 0x56, 0x78 }, @@ -89,7 +89,7 @@ main (int argc, const char *argv[]) print_nfc_target (nt); printf ("Waiting for initiator request...\n"); - if(!nfc_target_init (pnd, NTM_DEP_ONLY, nt, abtRx, &szRx)) { + if(!nfc_target_init (pnd, NTM_DEP_ONLY, &nt, abtRx, &szRx)) { nfc_perror(pnd, "nfc_target_init"); return EXIT_FAILURE; } diff --git a/examples/nfc-emulate-forum-tag4.c b/examples/nfc-emulate-forum-tag4.c index 9d04597..0da47a5 100644 --- a/examples/nfc-emulate-forum-tag4.c +++ b/examples/nfc-emulate-forum-tag4.c @@ -113,7 +113,7 @@ main (int argc, char *argv[]) nfc_target_t nt = { .nm.nmt = NMT_ISO14443A, - .nm.nbr = NBR_UNDEFINED, + .nm.nbr = NBR_UNDEFINED, // Will be updated by nfc_target_init() .nti.nai.abtAtqa = { 0x00, 0x04 }, .nti.nai.abtUid = { 0x08, 0x00, 0xb0, 0x0b }, .nti.nai.btSak = 0x20, @@ -121,7 +121,7 @@ main (int argc, char *argv[]) .nti.nai.szAtsLen = 0, }; - if (!nfc_target_init (pnd, NTM_ISO14443_4_PICC_ONLY, nt, abtRx, &szRx)) { + if (!nfc_target_init (pnd, NTM_ISO14443_4_PICC_ONLY, &nt, abtRx, &szRx)) { nfc_perror (pnd, "nfc_target_init"); ERR("Could not come out of auto-emulation, no command was received"); return EXIT_FAILURE; diff --git a/examples/nfc-emulate-tag.c b/examples/nfc-emulate-tag.c index 13ff243..650d8bd 100644 --- a/examples/nfc-emulate-tag.c +++ b/examples/nfc-emulate-tag.c @@ -61,7 +61,7 @@ intr_hdlr (void) } bool -target_io( const nfc_target_t nt, const byte_t * pbtInput, const size_t szInput, byte_t * pbtOutput, size_t *pszOutput ) +target_io( nfc_target_t * pnt, const byte_t * pbtInput, const size_t szInput, byte_t * pbtOutput, size_t *pszOutput ) { bool loop = true; *pszOutput = 0; @@ -84,10 +84,10 @@ target_io( const nfc_target_t nt, const byte_t * pbtInput, const size_t szInput, break; case 0xe0: // RATS // Send ATS - *pszOutput = nt.nti.nai.szAtsLen + 1; - pbtOutput[0] = nt.nti.nai.szAtsLen + 1; // ISO14443-4 says that ATS contains ATS_Lenght as first byte - if(nt.nti.nai.szAtsLen) { - memcpy(pbtOutput+1, nt.nti.nai.abtAts, nt.nti.nai.szAtsLen); + *pszOutput = pnt->nti.nai.szAtsLen + 1; + pbtOutput[0] = pnt->nti.nai.szAtsLen + 1; // ISO14443-4 says that ATS contains ATS_Lenght as first byte + if(pnt->nti.nai.szAtsLen) { + memcpy(pbtOutput+1, pnt->nti.nai.abtAts, pnt->nti.nai.szAtsLen); } break; case 0xc2: // S-block DESELECT @@ -112,19 +112,19 @@ target_io( const nfc_target_t nt, const byte_t * pbtInput, const size_t szInput, } bool -nfc_target_emulate_tag(nfc_device_t* pnd, const nfc_target_t nt) +nfc_target_emulate_tag(nfc_device_t* pnd, nfc_target_t * pnt) { size_t szTx; byte_t abtTx[MAX_FRAME_LEN]; bool loop = true; - if (!nfc_target_init (pnd, NTM_PASSIVE_ONLY, nt, abtRx, &szRx)) { + if (!nfc_target_init (pnd, NTM_PASSIVE_ONLY, pnt, abtRx, &szRx)) { nfc_perror (pnd, "nfc_target_init"); return false; } while ( loop ) { - loop = target_io( nt, abtRx, szRx, abtTx, &szTx ); + loop = target_io( pnt, abtRx, szRx, abtTx, &szTx ); if (szTx) { if (!nfc_target_send_bytes(pnd, abtTx, szTx)) { nfc_perror (pnd, "nfc_target_send_bytes"); @@ -209,7 +209,7 @@ main (int argc, char *argv[]) print_nfc_iso14443a_info( nt.nti.nai ); printf ("NFC device (configured as target) is now emulating the tag, please touch it with a second NFC device (initiator)\n"); - if (!nfc_target_emulate_tag (pnd, nt)) { + if (!nfc_target_emulate_tag (pnd, &nt)) { nfc_perror (pnd, "nfc_target_emulate_tag"); return EXIT_FAILURE; } diff --git a/examples/nfc-emulate-uid.c b/examples/nfc-emulate-uid.c index 93ebce9..a7728a0 100644 --- a/examples/nfc-emulate-uid.c +++ b/examples/nfc-emulate-uid.c @@ -140,7 +140,7 @@ main (int argc, char *argv[]) .nti.nai.szUidLen = 4, .nti.nai.szAtsLen = 0, }; - if (!nfc_target_init (pnd, NTM_PASSIVE_ONLY, nt, abtRecv, &szRecvBits)) { + if (!nfc_target_init (pnd, NTM_PASSIVE_ONLY, &nt, abtRecv, &szRecvBits)) { ERR ("Could not come out of auto-emulation, no command was received"); exit(EXIT_FAILURE); } diff --git a/examples/nfc-relay-picc.c b/examples/nfc-relay-picc.c index 51c1159..e3cafe8 100644 --- a/examples/nfc-relay-picc.c +++ b/examples/nfc-relay-picc.c @@ -343,7 +343,7 @@ main (int argc, char *argv[]) printf ("Connected to the NFC emulator device: %s\n", pndTarget->acName); - if (!nfc_target_init (pndTarget, NTM_ISO14443_4_PICC_ONLY, ntEmulatedTarget, abtCapdu, &szCapduLen)) { + if (!nfc_target_init (pndTarget, NTM_ISO14443_4_PICC_ONLY, &ntEmulatedTarget, abtCapdu, &szCapduLen)) { ERR ("%s", "Initialization of NFC emulator failed"); if (!target_only_mode) { nfc_disconnect (pndInitiator); diff --git a/examples/nfc-relay.c b/examples/nfc-relay.c index 4550633..b12bb9e 100644 --- a/examples/nfc-relay.c +++ b/examples/nfc-relay.c @@ -136,7 +136,7 @@ main (int argc, char *argv[]) .nti.nai.szAtsLen = 0, }; - if (!nfc_target_init (pndTag, NTM_PASSIVE_ONLY, nt, abtReaderRx, &szReaderRxBits)) { + if (!nfc_target_init (pndTag, NTM_PASSIVE_ONLY, &nt, abtReaderRx, &szReaderRxBits)) { ERR ("%s", "Initialization of NFC emulator failed"); nfc_disconnect (pndTag); return EXIT_FAILURE; diff --git a/examples/pn53x-sam.c b/examples/pn53x-sam.c index b019547..c4e25b4 100644 --- a/examples/pn53x-sam.c +++ b/examples/pn53x-sam.c @@ -212,7 +212,7 @@ main (int argc, const char *argv[]) }; printf ("Now both, NFC device (configured as target) and SAM are readables from an external NFC initiator.\n"); printf ("Please note that NFC device (configured as target) stay in target mode until it receive RATS, ATR_REQ or proprietary command.\n"); - if (!nfc_target_init (pnd, NTM_NORMAL, nt, abtRx, &szRx)) { + if (!nfc_target_init (pnd, NTM_NORMAL, &nt, abtRx, &szRx)) { nfc_perror(pnd, "nfc_target_init"); return EXIT_FAILURE; } diff --git a/include/nfc/nfc.h b/include/nfc/nfc.h index dca1640..e0bd455 100644 --- a/include/nfc/nfc.h +++ b/include/nfc/nfc.h @@ -89,7 +89,7 @@ extern "C" { byte_t * pbtRxPar); /* NFC target: act as tag (i.e. MIFARE Classic) or NFC target device. */ - NFC_EXPORT bool nfc_target_init (nfc_device_t * pnd, const nfc_target_mode_t ntm, const nfc_target_t nt, byte_t * pbtRx, size_t * pszRx); + NFC_EXPORT bool nfc_target_init (nfc_device_t * pnd, const nfc_target_mode_t ntm, nfc_target_t * pnt, byte_t * pbtRx, size_t * pszRx); NFC_EXPORT bool nfc_target_send_bytes (nfc_device_t * pnd, const byte_t * pbtTx, const size_t szTx); NFC_EXPORT bool nfc_target_receive_bytes (nfc_device_t * pnd, byte_t * pbtRx, size_t * pszRx); NFC_EXPORT bool nfc_target_send_bits (nfc_device_t * pnd, const byte_t * pbtTx, const size_t szTxBits, diff --git a/libnfc/chips/pn53x.c b/libnfc/chips/pn53x.c index 560970f..a781761 100644 --- a/libnfc/chips/pn53x.c +++ b/libnfc/chips/pn53x.c @@ -1108,7 +1108,7 @@ pn53x_initiator_transceive_bytes (nfc_device_t * pnd, const byte_t * pbtTx, cons } bool -pn53x_target_init (nfc_device_t * pnd, const nfc_target_mode_t ntm, const nfc_target_t nt, byte_t * pbtRx, size_t * pszRx) +pn53x_target_init (nfc_device_t * pnd, const nfc_target_mode_t ntm, nfc_target_t * pnt, byte_t * pbtRx, size_t * pszRx) { // Save the current configuration settings bool bCrc = pnd->bCrc; @@ -1165,18 +1165,18 @@ pn53x_target_init (nfc_device_t * pnd, const nfc_target_mode_t ntm, const nfc_ta const byte_t * pbtGBt = NULL; size_t szGBt = 0; - switch(nt.nm.nmt) { + switch(pnt->nm.nmt) { case NMT_ISO14443A: { // Set ATQA (SENS_RES) - abtMifareParams[0] = nt.nti.nai.abtAtqa[1]; - abtMifareParams[1] = nt.nti.nai.abtAtqa[0]; + abtMifareParams[0] = pnt->nti.nai.abtAtqa[1]; + abtMifareParams[1] = pnt->nti.nai.abtAtqa[0]; // Set UID // Note: in this mode we can only emulate a single size (4 bytes) UID where the first is hard-wired by PN53x as 0x08 - abtMifareParams[2] = nt.nti.nai.abtUid[1]; - abtMifareParams[3] = nt.nti.nai.abtUid[2]; - abtMifareParams[4] = nt.nti.nai.abtUid[3]; + abtMifareParams[2] = pnt->nti.nai.abtUid[1]; + abtMifareParams[3] = pnt->nti.nai.abtUid[2]; + abtMifareParams[4] = pnt->nti.nai.abtUid[3]; // Set SAK (SEL_RES) - abtMifareParams[5] = nt.nti.nai.btSak; + abtMifareParams[5] = pnt->nti.nai.btSak; pbtMifareParams = abtMifareParams; } @@ -1184,20 +1184,20 @@ pn53x_target_init (nfc_device_t * pnd, const nfc_target_mode_t ntm, const nfc_ta case NMT_FELICA: // Set NFCID2t - memcpy(abtFeliCaParams, nt.nti.nfi.abtId, 8); + memcpy(abtFeliCaParams, pnt->nti.nfi.abtId, 8); // Set PAD - memcpy(abtFeliCaParams+8, nt.nti.nfi.abtPad, 8); + memcpy(abtFeliCaParams+8, pnt->nti.nfi.abtPad, 8); // Set SystemCode - memcpy(abtFeliCaParams+16, nt.nti.nfi.abtSysCode, 2); + memcpy(abtFeliCaParams+16, pnt->nti.nfi.abtSysCode, 2); pbtFeliCaParams = abtFeliCaParams; break; case NMT_DEP: // Set NFCID3 - pbtNFCID3t = nt.nti.ndi.abtNFCID3; + pbtNFCID3t = pnt->nti.ndi.abtNFCID3; // Set General Bytes, if relevant - szGBt = nt.nti.ndi.szGB; - if (szGBt) pbtGBt = nt.nti.ndi.abtGB; + szGBt = pnt->nti.ndi.szGB; + if (szGBt) pbtGBt = pnt->nti.ndi.abtGB; break; case NMT_ISO14443B: case NMT_JEWEL: @@ -1241,7 +1241,10 @@ target_activation: } // XXX When using DEP, shouldn't we update target modulation ? - if(nm.nmt != nt.nm.nmt) goto target_activation; + if(nm.nmt != pnt->nm.nmt) { + goto target_activation; + } + pnt->nm.nbr = nm.nbr; // Restore the CRC & parity setting to the original value (if needed) if (!bCrc) diff --git a/libnfc/chips/pn53x.h b/libnfc/chips/pn53x.h index f7fb998..b575e66 100644 --- a/libnfc/chips/pn53x.h +++ b/libnfc/chips/pn53x.h @@ -193,7 +193,7 @@ bool pn53x_initiator_transceive_bits (nfc_device_t * pnd, const byte_t * pbtT bool pn53x_initiator_transceive_bytes (nfc_device_t * pnd, const byte_t * pbtTx, const size_t szTx, byte_t * pbtRx, size_t * pszRx); // NFC device as Target functions -bool pn53x_target_init (nfc_device_t * pnd, const nfc_target_mode_t ntm, const nfc_target_t nt, byte_t * pbtRx, size_t * pszRx); +bool pn53x_target_init (nfc_device_t * pnd, const nfc_target_mode_t ntm, nfc_target_t * pnt, byte_t * pbtRx, size_t * pszRx); bool pn53x_target_receive_bits (nfc_device_t * pnd, byte_t * pbtRx, size_t * pszRxBits, byte_t * pbtRxPar); bool pn53x_target_receive_bytes (nfc_device_t * pnd, byte_t * pbtRx, size_t * pszRx); bool pn53x_target_send_bits (nfc_device_t * pnd, const byte_t * pbtTx, const size_t szTxBits, diff --git a/libnfc/nfc.c b/libnfc/nfc.c index f312e32..7d23cf5 100644 --- a/libnfc/nfc.c +++ b/libnfc/nfc.c @@ -538,6 +538,11 @@ nfc_initiator_transceive_bits (nfc_device_t * pnd, const byte_t * pbtTx, const s * * @param pnd \a nfc_device_t struct pointer that represent currently used device * @param ntm target mode restriction that you want to emulate (eg. NTM_PASSIVE_ONLY) + * @param pnt pointer to \a nfc_target_t struct that represents the wanted emulated target + * + * @note \a pnt can be updated by this function: if you set NBR_UNDEFINED + * and/or NDM_UNDEFINED (ie. for DEP mode), these fields will be updated. + * * @param[out] pbtRx Rx buffer pointer * @param[out] pszRx received bytes count * @@ -550,11 +555,11 @@ nfc_initiator_transceive_bits (nfc_device_t * pnd, const byte_t * pbtTx, const s * receive functions can be used. */ bool -nfc_target_init (nfc_device_t * pnd, const nfc_target_mode_t ntm, const nfc_target_t nt, byte_t * pbtRx, size_t * pszRx) +nfc_target_init (nfc_device_t * pnd, const nfc_target_mode_t ntm, nfc_target_t * pnt, byte_t * pbtRx, size_t * pszRx) { pnd->iLastError = 0; - return pn53x_target_init (pnd, ntm, nt, pbtRx, pszRx); + return pn53x_target_init (pnd, ntm, pnt, pbtRx, pszRx); } /**