From ae044799687dbe430e16873e1a272bd2d524e10c Mon Sep 17 00:00:00 2001 From: Romuald Conty Date: Tue, 17 Aug 2010 08:24:38 +0000 Subject: [PATCH] Move hardware polling function in pn53x.c/h. --- libnfc/chips/pn53x.c | 59 ++++++++++++++++++++++++++++++++++++++++++++ libnfc/chips/pn53x.h | 1 + libnfc/nfc.c | 55 +++-------------------------------------- 3 files changed, 64 insertions(+), 51 deletions(-) diff --git a/libnfc/chips/pn53x.c b/libnfc/chips/pn53x.c index 373adec..8ea1352 100644 --- a/libnfc/chips/pn53x.c +++ b/libnfc/chips/pn53x.c @@ -28,6 +28,7 @@ #include #include +#include #include "pn53x.h" #include "../mirror-subr.h" @@ -336,3 +337,61 @@ pn53x_InRelease(nfc_device_t* pnd, const uint8_t ui8Target) return(pn53x_transceive(pnd,abtCmd,sizeof(abtCmd),NULL,NULL)); } + +bool +pn53x_InAutoPoll(const 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) +{ + size_t szTxInAutoPoll, n, szRxLen; + byte_t abtRx[256]; + bool res; + byte_t *pbtTxInAutoPoll; + + if(pnd->nc == NC_PN531) { + // TODO This function is not supported by pn531 (set errno = ENOSUPP or similar) + return false; + } + + // InAutoPoll frame looks like this { 0xd4, 0x60, 0x0f, 0x01, 0x00 } => { direction, command, pollnr, period, types... } + szTxInAutoPoll = 4 + szTargetTypes; + pbtTxInAutoPoll = malloc( szTxInAutoPoll ); + pbtTxInAutoPoll[0] = 0xd4; + pbtTxInAutoPoll[1] = 0x60; + pbtTxInAutoPoll[2] = btPollNr; + pbtTxInAutoPoll[3] = btPeriod; + for(n=0; npdc->transceive(pnd->nds, pbtTxInAutoPoll, szTxInAutoPoll, abtRx, &szRxLen); + + if((szRxLen == 0)||(res == false)) { + return false; + } else { + *pszTargetFound = abtRx[0]; + if( *pszTargetFound ) { + uint8_t ln; + byte_t* pbt = abtRx + 1; + /* 1st target */ + // Target type + pntTargets[0].ntt = *(pbt++); + // AutoPollTargetData length + ln = *(pbt++); + pn53x_decode_target_data(pbt, ln, pnd->nc, pntTargets[0].ntt, &(pntTargets[0].nti)); + pbt += ln; + + if(abtRx[0] > 1) { + /* 2nd target */ + // Target type + pntTargets[1].ntt = *(pbt++); + // AutoPollTargetData length + ln = *(pbt++); + pn53x_decode_target_data(pbt, ln, pnd->nc, pntTargets[1].ntt, &(pntTargets[1].nti)); + } + } + } + return true; +} diff --git a/libnfc/chips/pn53x.h b/libnfc/chips/pn53x.h index fbcc784..88130a8 100644 --- a/libnfc/chips/pn53x.h +++ b/libnfc/chips/pn53x.h @@ -81,6 +81,7 @@ bool pn53x_decode_target_data(const byte_t* pbtRawData, size_t szDataLen, nfc_ch bool pn53x_InListPassiveTarget(const nfc_device_t* pnd, const nfc_modulation_t nmInitModulation, const byte_t szMaxTargets, const byte_t* pbtInitiatorData, const size_t szInitiatorDataLen, byte_t* pbtTargetsData, size_t* pszTargetsData); bool pn53x_InDeselect(const nfc_device_t* pnd, const uint8_t ui8Target); bool pn53x_InRelease(nfc_device_t* pnd, const uint8_t ui8Target); +bool pn53x_InAutoPoll(const 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); #endif // __NFC_CHIPS_PN53X_H__ diff --git a/libnfc/nfc.c b/libnfc/nfc.c index 5c662ea..f8558f1 100644 --- a/libnfc/nfc.c +++ b/libnfc/nfc.c @@ -438,7 +438,7 @@ nfc_initiator_select_passive_target(const nfc_device_t* pnd, // Make sure we are dealing with a active device if (!pnd->bActive) return false; - + // TODO Put this in a function switch(nmInitModulation) { case NM_ISO14443A_106: @@ -485,7 +485,8 @@ nfc_initiator_select_passive_target(const nfc_device_t* pnd, { case NM_ISO14443A_106: if(!pn53x_decode_target_data(abtTargetsData+1, szTargetsData-1, pnd->nc, NTT_GENERIC_PASSIVE_106, pnti)) return false; - + // TODO this should be handled by pn53x_decode_target_data() + // Strip CT (Cascade Tag) to retrieve and store the _real_ UID // (e.g. 0x8801020304050607 is in fact 0x01020304050607) if ((pnti->nai.szUidLen == 8) && (pnti->nai.abtUid[0] == 0x88)) { @@ -651,55 +652,7 @@ nfc_initiator_poll_targets(const nfc_device_t* pnd, const byte_t btPollNr, const byte_t btPeriod, nfc_target_t* pntTargets, size_t* pszTargetFound) { - size_t szTxInAutoPoll, n, szRxLen; - byte_t abtRx[256]; - bool res; - byte_t *pbtTxInAutoPoll; - if(pnd->nc == NC_PN531) { - // errno = ENOSUPP - return false; - } -// byte_t abtInAutoPoll[] = { 0xd4, 0x60, 0x0f, 0x01, 0x00 }; - szTxInAutoPoll = 4 + szTargetTypes; - pbtTxInAutoPoll = malloc( szTxInAutoPoll ); - pbtTxInAutoPoll[0] = 0xd4; - pbtTxInAutoPoll[1] = 0x60; - pbtTxInAutoPoll[2] = btPollNr; - pbtTxInAutoPoll[3] = btPeriod; - for(n=0; npdc->transceive(pnd->nds, pbtTxInAutoPoll, szTxInAutoPoll, abtRx, &szRxLen); - - if((szRxLen == 0)||(res == false)) { - DBG("pnd->pdc->tranceive() failed: szRxLen=%ld, res=%d", (unsigned long) szRxLen, res); - return false; - } else { - *pszTargetFound = abtRx[0]; - if( *pszTargetFound ) { - uint8_t ln; - byte_t* pbt = abtRx + 1; - /* 1st target */ - // Target type - pntTargets[0].ntt = *(pbt++); - // AutoPollTargetData length - ln = *(pbt++); - pn53x_decode_target_data(pbt, ln, pnd->nc, pntTargets[0].ntt, &(pntTargets[0].nti)); - pbt += ln; - - if(abtRx[0] > 1) { - /* 2nd target */ - // Target type - pntTargets[1].ntt = *(pbt++); - // AutoPollTargetData length - ln = *(pbt++); - pn53x_decode_target_data(pbt, ln, pnd->nc, pntTargets[1].ntt, &(pntTargets[1].nti)); - } - } - } - return true; + return pn53x_InAutoPoll(pnd, pnttTargetTypes, szTargetTypes, btPollNr, btPeriod, pntTargets, pszTargetFound); } /**