diff --git a/src/anticol.c b/src/anticol.c index 3b7c3ba..7b4a406 100644 --- a/src/anticol.c +++ b/src/anticol.c @@ -50,7 +50,7 @@ bool transmit_bits(const byte_t* pbtTx, const uint32_t uiTxBits) printf("R: "); print_hex_bits(pbtTx,uiTxBits); // Transmit the bit frame command, we don't use the arbitrary parity feature - if (!nfc_reader_transceive_bits(pdi,pbtTx,uiTxBits,NULL,abtRx,&uiRxBits,NULL)) return false; + if (!nfc_initiator_transceive_bits(pdi,pbtTx,uiTxBits,NULL,abtRx,&uiRxBits,NULL)) return false; // Show received answer printf("T: "); print_hex_bits(abtRx,uiRxBits); @@ -66,7 +66,7 @@ bool transmit_bytes(const byte_t* pbtTx, const uint32_t uiTxLen) printf("R: "); print_hex(pbtTx,uiTxLen); // Transmit the command bytes - if (!nfc_reader_transceive_bytes(pdi,pbtTx,uiTxLen,abtRx,&uiRxLen)) return false; + if (!nfc_initiator_transceive_bytes(pdi,pbtTx,uiTxLen,abtRx,&uiRxLen)) return false; // Show received answer printf("T: "); print_hex(abtRx,uiRxLen); @@ -85,7 +85,7 @@ int main(int argc, const char* argv[]) printf("Error connecting NFC reader\n"); return 1; } - nfc_reader_init(pdi); + nfc_initiator_init(pdi); // Drop the field for a while nfc_configure(pdi,DCO_ACTIVATE_FIELD,false); diff --git a/src/dev_acr122.c b/src/dev_acr122.c index fa297a0..a45d704 100644 --- a/src/dev_acr122.c +++ b/src/dev_acr122.c @@ -24,12 +24,10 @@ along with this program. If not, see #include #include #include +#include -#ifndef __APPLE__ -#include -#else -#include -#include +#ifdef __APPLE__ + #include #endif #include "defines.h" @@ -60,7 +58,7 @@ static byte_t abtTxBuf[ACR122_WRAP_LEN+ACR122_COMMAND_LEN] = { 0xFF, 0x00, 0x00, static byte_t abtRxCmd[5] = { 0xFF,0xC0,0x00,0x00 }; static byte_t uiRxCmdLen = sizeof(abtRxCmd); static byte_t abtRxBuf[ACR122_RESPONSE_LEN]; -static size_t ulRxBufLen; +static uint32_t uiRxBufLen; static byte_t abtGetFw[5] = { 0xFF,0x00,0x48,0x00,0x00 }; static byte_t abtLed[9] = { 0xFF,0x00,0x40,0x05,0x04,0x00,0x00,0x00,0x00 }; @@ -191,7 +189,7 @@ bool dev_acr122_transceive(const dev_spec ds, const byte_t* pbtTx, const uint32_ // Prepare and transmit the send buffer memcpy(abtTxBuf+5,pbtTx,uiTxLen); - ulRxBufLen = sizeof(abtRxBuf); + uiRxBufLen = sizeof(abtRxBuf); #ifdef DEBUG printf("Tx: "); print_hex(abtTxBuf,uiTxLen+5); @@ -199,37 +197,37 @@ bool dev_acr122_transceive(const dev_spec ds, const byte_t* pbtTx, const uint32_ if (pdsa->ioCard.dwProtocol == SCARD_PROTOCOL_UNDEFINED) { - if (SCardControl(pdsa->hCard,IOCTL_CCID_ESCAPE_SCARD_CTL_CODE,abtTxBuf,uiTxLen+5,abtRxBuf,ulRxBufLen,(void*)&ulRxBufLen) != SCARD_S_SUCCESS) return false; + if (SCardControl(pdsa->hCard,IOCTL_CCID_ESCAPE_SCARD_CTL_CODE,abtTxBuf,uiTxLen+5,abtRxBuf,uiRxBufLen,(void*)&uiRxBufLen) != SCARD_S_SUCCESS) return false; } else { - if (SCardTransmit(pdsa->hCard,&(pdsa->ioCard),abtTxBuf,uiTxLen+5,NULL,abtRxBuf,(void*)&ulRxBufLen) != SCARD_S_SUCCESS) return false; + if (SCardTransmit(pdsa->hCard,&(pdsa->ioCard),abtTxBuf,uiTxLen+5,NULL,abtRxBuf,(void*)&uiRxBufLen) != SCARD_S_SUCCESS) return false; } if (pdsa->ioCard.dwProtocol == SCARD_PROTOCOL_T0) { // Make sure we received the byte-count we expected - if (ulRxBufLen != 2) return false; + if (uiRxBufLen != 2) return false; // Check if the operation was successful, so an answer is available if (*abtRxBuf == SCARD_OPERATION_ERROR) return false; // Retrieve the response bytes abtRxCmd[4] = abtRxBuf[1]; - ulRxBufLen = sizeof(abtRxBuf); - if (SCardTransmit(pdsa->hCard,&(pdsa->ioCard),abtRxCmd,uiRxCmdLen,NULL,abtRxBuf,(void*)&ulRxBufLen) != SCARD_S_SUCCESS) return false; + uiRxBufLen = sizeof(abtRxBuf); + if (SCardTransmit(pdsa->hCard,&(pdsa->ioCard),abtRxCmd,uiRxCmdLen,NULL,abtRxBuf,(void*)&uiRxBufLen) != SCARD_S_SUCCESS) return false; } #ifdef DEBUG printf("Rx: "); - print_hex(abtRxBuf,ulRxBufLen); + print_hex(abtRxBuf,uiRxBufLen); #endif // When the answer should be ignored, just return a succesful result if (pbtRx == NULL || puiRxLen == NULL) return true; // Make sure we have an emulated answer that fits the return buffer - if (ulRxBufLen < 4 || (ulRxBufLen-4) > *puiRxLen) return false; + if (uiRxBufLen < 4 || (uiRxBufLen-4) > *puiRxLen) return false; // Wipe out the 4 APDU emulation bytes: D5 4B .. .. .. 90 00 - *puiRxLen = ulRxBufLen-4; + *puiRxLen = uiRxBufLen-4; memcpy(pbtRx,abtRxBuf+2,*puiRxLen); // Transmission went successful @@ -242,7 +240,7 @@ char* dev_acr122_firmware(const dev_spec ds) dev_spec_acr122* pdsa = (dev_spec_acr122*)ds; static char abtFw[11]; - size_t ulFwLen = sizeof(abtFw); + uint32_t ulFwLen = sizeof(abtFw); memset(abtFw,0x00,ulFwLen); if (pdsa->ioCard.dwProtocol == SCARD_PROTOCOL_UNDEFINED) { @@ -265,7 +263,7 @@ bool dev_acr122_led_red(const dev_spec ds, bool bOn) { dev_spec_acr122* pdsa = (dev_spec_acr122*)ds; byte_t abtBuf[2]; - size_t ulBufLen = sizeof(abtBuf); + uint32_t ulBufLen = sizeof(abtBuf); if (pdsa->ioCard.dwProtocol == SCARD_PROTOCOL_UNDEFINED) { return (SCardControl(pdsa->hCard,IOCTL_CCID_ESCAPE_SCARD_CTL_CODE,abtLed,sizeof(abtLed),abtBuf,ulBufLen,(void*)&ulBufLen) == SCARD_S_SUCCESS); diff --git a/src/libnfc.c b/src/libnfc.c index 8aa1b40..cdb8524 100644 --- a/src/libnfc.c +++ b/src/libnfc.c @@ -370,7 +370,7 @@ bool nfc_configure(dev_info* pdi, const dev_config_option dco, const bool bEnabl return true; } -bool nfc_reader_init(const dev_info* pdi) +bool nfc_initiator_init(const dev_info* pdi) { // Make sure we are dealing with a active device if (!pdi->bActive) return false; @@ -384,7 +384,7 @@ bool nfc_reader_init(const dev_info* pdi) return true; } -bool nfc_reader_select(const dev_info* pdi, const init_modulation im, const byte_t* pbtInitData, const uint32_t uiInitDataLen, tag_info* pti) +bool nfc_initiator_select_tag(const dev_info* pdi, const init_modulation im, const byte_t* pbtInitData, const uint32_t uiInitDataLen, tag_info* pti) { // Make sure we are dealing with a active device if (!pdi->bActive) return false; @@ -480,12 +480,12 @@ bool nfc_reader_select(const dev_info* pdi, const init_modulation im, const byte return true; } -bool nfc_reader_deselect(const dev_info* pdi) +bool nfc_initiator_deselect_tag(const dev_info* pdi) { return (pdi->pdc->transceive(pdi->ds,pncmd_reader_deselect,3,NULL,NULL)); } -bool nfc_reader_transceive_bits(const dev_info* pdi, const byte_t* pbtTx, const uint32_t uiTxBits, const byte_t* pbtTxPar, byte_t* pbtRx, uint32_t* puiRxBits, byte_t* pbtRxPar) +bool nfc_initiator_transceive_bits(const dev_info* pdi, const byte_t* pbtTx, const uint32_t uiTxBits, const byte_t* pbtTxPar, byte_t* pbtRx, uint32_t* puiRxBits, byte_t* pbtRxPar) { uint32_t uiFrameBits = 0; uint32_t uiFrameBytes = 0; @@ -539,7 +539,7 @@ bool nfc_reader_transceive_bits(const dev_info* pdi, const byte_t* pbtTx, const return true; } -bool nfc_reader_transceive_bytes(const dev_info* pdi, const byte_t* pbtTx, const uint32_t uiTxLen, byte_t* pbtRx, uint32_t* puiRxLen) +bool nfc_initiator_transceive_bytes(const dev_info* pdi, const byte_t* pbtTx, const uint32_t uiTxLen, byte_t* pbtRx, uint32_t* puiRxLen) { // We can not just send bytes without parity if while the PN53X expects we handled them if (!pdi->bPar) return false; @@ -564,7 +564,7 @@ bool nfc_reader_transceive_bytes(const dev_info* pdi, const byte_t* pbtTx, const return true; } -bool nfc_reader_mifare_cmd(const dev_info* pdi, const mifare_cmd mc, const uint8_t ui8Block, mifare_param* pmp) +bool nfc_initiator_mifare_cmd(const dev_info* pdi, const mifare_cmd mc, const uint8_t ui8Block, mifare_param* pmp) { uint32_t uiParamLen; diff --git a/src/libnfc.h b/src/libnfc.h index b5d9822..eaa7ab9 100644 --- a/src/libnfc.h +++ b/src/libnfc.h @@ -32,12 +32,12 @@ dev_info* nfc_connect(void); void nfc_disconnect(dev_info* pdi); bool nfc_configure(dev_info* pdi, const dev_config_option dco, const bool bEnable); -bool nfc_reader_init(const dev_info* pdi); -bool nfc_reader_select(const dev_info* pdi, const init_modulation im, const byte_t* pbtInitData, const uint32_t uiInitDataLen, tag_info* pti); -bool nfc_reader_deselect(const dev_info* pdi); -bool nfc_reader_transceive_bits(const dev_info* pdi, const byte_t* pbtTx, const uint32_t uiTxBits, const byte_t* pbtTxPar, byte_t* pbtRx, uint32_t* puiRxBits, byte_t* pbtRxPar); -bool nfc_reader_transceive_bytes(const dev_info* pdi, const byte_t* pbtTx, const uint32_t uiTxLen, byte_t* pbtRx, uint32_t* puiRxLen); -bool nfc_reader_mifare_cmd(const dev_info* pdi, const mifare_cmd mc, const uint8_t ui8Block, mifare_param* pmp); +bool nfc_initiator_init(const dev_info* pdi); +bool nfc_initiator_select_tag(const dev_info* pdi, const init_modulation im, const byte_t* pbtInitData, const uint32_t uiInitDataLen, tag_info* pti); +bool nfc_initiator_deselect_tag(const dev_info* pdi); +bool nfc_initiator_transceive_bits(const dev_info* pdi, const byte_t* pbtTx, const uint32_t uiTxBits, const byte_t* pbtTxPar, byte_t* pbtRx, uint32_t* puiRxBits, byte_t* pbtRxPar); +bool nfc_initiator_transceive_bytes(const dev_info* pdi, const byte_t* pbtTx, const uint32_t uiTxLen, byte_t* pbtRx, uint32_t* puiRxLen); +bool nfc_initiator_mifare_cmd(const dev_info* pdi, const mifare_cmd mc, const uint8_t ui8Block, mifare_param* pmp); bool nfc_target_init(const dev_info* pdi, byte_t* pbtRx, uint32_t* puiRxBits); bool nfc_target_receive_bits(const dev_info* pdi, byte_t* pbtRx, uint32_t* puiRxBits, byte_t* pbtRxPar); diff --git a/src/list.c b/src/list.c index 649939b..cb3dc71 100644 --- a/src/list.c +++ b/src/list.c @@ -41,7 +41,7 @@ int main(int argc, const char* argv[]) printf("Error connecting NFC reader\n"); return 1; } - nfc_reader_init(pdi); + nfc_initiator_init(pdi); // Drop the field for a while nfc_configure(pdi,DCO_ACTIVATE_FIELD,false); @@ -59,7 +59,7 @@ int main(int argc, const char* argv[]) printf("\nConnected to NFC reader: %s\n\n",pdi->acName); // Poll for a ISO14443A (MIFARE) tag - if (nfc_reader_select(pdi,IM_ISO14443A_106,NULL,0,&ti)) + if (nfc_initiator_select_tag(pdi,IM_ISO14443A_106,NULL,0,&ti)) { printf("The following (NFC) ISO14443A tag was found:\n\n"); printf(" ATQA (SENS_RES): "); print_hex(ti.tia.abtAtqa,2); @@ -73,22 +73,29 @@ int main(int argc, const char* argv[]) } // Poll for a Felica tag - if (nfc_reader_select(pdi,IM_FELICA_212,abtFelica,5,&ti) || nfc_reader_select(pdi,IM_FELICA_424,abtFelica,5,&ti)) + if (nfc_initiator_select_tag(pdi,IM_FELICA_212,abtFelica,5,&ti) || nfc_initiator_select_tag(pdi,IM_FELICA_424,abtFelica,5,&ti)) { printf("The following (NFC) Felica tag was found:\n\n"); printf("%18s","ID (NFCID2): "); print_hex(ti.tif.abtId,8); printf("%18s","Parameter (PAD): "); print_hex(ti.tif.abtPad,8); } - // Poll for a ISO14443B tag - if (nfc_reader_select(pdi,IM_ISO14443B_106,NULL,0,&ti)) - { - // No test results yet - printf("iso14443b\n"); - } + // Poll for a ISO14443B tag + if (nfc_initiator_select_tag(pdi,IM_ISO14443B_106,"\x00",1,&ti)) + { + printf("The following (NFC) ISO14443-B tag was found:\n\n"); + printf(" ATQB: "); print_hex(ti.tib.abtAtqb,12); + printf(" ID: "); print_hex(ti.tib.abtId,4); + printf(" CID: %02x\n",ti.tib.btCid); + if (ti.tib.uiInfLen>0) + { + printf(" INF: "); print_hex(ti.tib.abtInf,ti.tib.uiInfLen); + } + printf("PARAMS: %02x %02x %02x %02x\n",ti.tib.btParam1,ti.tib.btParam2,ti.tib.btParam3,ti.tib.btParam4); + } // Poll for a Jewel tag - if (nfc_reader_select(pdi,IM_JEWEL_106,NULL,0,&ti)) + if (nfc_initiator_select_tag(pdi,IM_JEWEL_106,NULL,0,&ti)) { // No test results yet printf("jewel\n"); diff --git a/src/mftool.c b/src/mftool.c index fbeb4ce..7f41926 100644 --- a/src/mftool.c +++ b/src/mftool.c @@ -75,7 +75,7 @@ bool read_card() { printf("x"); // When a failure occured we need to redo the anti-collision - if (!nfc_reader_select(pdi,IM_ISO14443A_106,NULL,0,&ti)) + if (!nfc_initiator_select_tag(pdi,IM_ISO14443A_106,NULL,0,&ti)) { printf("!\nError: tag was removed\n"); return 1; @@ -104,14 +104,14 @@ bool read_card() } // Try to authenticate for the current sector - if (!nfc_reader_mifare_cmd(pdi,MC_AUTH_A,iBlock,&mp)) + if (!nfc_initiator_mifare_cmd(pdi,MC_AUTH_A,iBlock,&mp)) { printf("!\nError: authentication failed for block %02x\n",iBlock); return false; } // Try to read out the trailer - if (nfc_reader_mifare_cmd(pdi,MC_READ,iBlock,&mp)) + if (nfc_initiator_mifare_cmd(pdi,MC_READ,iBlock,&mp)) { // Copy the keys over from our key dump and store the retrieved access bits memcpy(mtDump.amb[iBlock].mbt.abtKeyA,mtKeys.amb[iBlock].mbt.abtKeyA,6); @@ -123,7 +123,7 @@ bool read_card() if (!bFailure) { // Try to read out the data block - if (nfc_reader_mifare_cmd(pdi,MC_READ,iBlock,&mp)) + if (nfc_initiator_mifare_cmd(pdi,MC_READ,iBlock,&mp)) { memcpy(mtDump.amb[iBlock].mbd.abtData,mp.mpd.abtData,16); } else { @@ -158,7 +158,7 @@ bool write_card() { printf("x"); // When a failure occured we need to redo the anti-collision - if (!nfc_reader_select(pdi,IM_ISO14443A_106,NULL,0,&ti)) + if (!nfc_initiator_select_tag(pdi,IM_ISO14443A_106,NULL,0,&ti)) { printf("!\nError: tag was removed\n"); return false; @@ -190,7 +190,7 @@ bool write_card() } // Try to authenticate for the current sector - if (!nfc_reader_mifare_cmd(pdi,mc,uiBlock,&mp)) + if (!nfc_initiator_mifare_cmd(pdi,mc,uiBlock,&mp)) { printf("!\nError: authentication failed for block %02x\n",uiBlock); return false; @@ -205,7 +205,7 @@ bool write_card() memcpy(mp.mpd.abtData+10,mtDump.amb[uiBlock].mbt.abtKeyB,6); // Try to write the trailer - nfc_reader_mifare_cmd(pdi,MC_WRITE,uiBlock,&mp); + nfc_initiator_mifare_cmd(pdi,MC_WRITE,uiBlock,&mp); } else { @@ -217,7 +217,7 @@ bool write_card() { // Try to write the data block memcpy(mp.mpd.abtData,mtDump.amb[uiBlock].mbd.abtData,16); - if (!nfc_reader_mifare_cmd(pdi,MC_WRITE,uiBlock,&mp)) bFailure = true; + if (!nfc_initiator_mifare_cmd(pdi,MC_WRITE,uiBlock,&mp)) bFailure = true; } } } @@ -297,7 +297,7 @@ int main(int argc, const char* argv[]) return 1; } - nfc_reader_init(pdi); + nfc_initiator_init(pdi); // Drop the field for a while nfc_configure(pdi,DCO_ACTIVATE_FIELD,false); @@ -313,7 +313,7 @@ int main(int argc, const char* argv[]) printf("Connected to NFC reader: %s\n",pdi->acName); // Try to find a MIFARE Classic tag - if (!nfc_reader_select(pdi,IM_ISO14443A_106,NULL,0,&ti)) + if (!nfc_initiator_select_tag(pdi,IM_ISO14443A_106,NULL,0,&ti)) { printf("Error: no tag was found\n"); nfc_disconnect(pdi); diff --git a/src/relay.c b/src/relay.c index aca9b8c..771bb6e 100644 --- a/src/relay.c +++ b/src/relay.c @@ -84,7 +84,7 @@ int main(int argc, const char* argv[]) print_hex_par(abtReaderRx,uiReaderRxBits,abtReaderRxPar); // Forward the frame to the original tag - if (nfc_reader_transceive_bits(pdiReader,abtReaderRx,uiReaderRxBits,abtReaderRxPar,abtTagRx,&uiTagRxBits,abtTagRxPar)) + if (nfc_initiator_transceive_bits(pdiReader,abtReaderRx,uiReaderRxBits,abtReaderRxPar,abtTagRx,&uiTagRxBits,abtTagRxPar)) { // Redirect the answer back to the reader nfc_target_send_bits(pdiTag,abtTagRx,uiTagRxBits,abtTagRxPar); diff --git a/src/rs232.c b/src/rs232.c index a8be31f..6ea30a7 100644 --- a/src/rs232.c +++ b/src/rs232.c @@ -158,7 +158,7 @@ serial_port rs232_open(const char* pcPortName) _strupr(acPortName); // Try to open the serial port - sp->hPort = CreateFileA(acPortName,GENERIC_READ|GENERIC_WRITE,NULL,NULL,OPEN_EXISTING,NULL,NULL); + sp->hPort = CreateFileA(acPortName,GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_EXISTING,0,NULL); if (sp->hPort == INVALID_HANDLE_VALUE) { rs232_close(sp); @@ -181,11 +181,11 @@ serial_port rs232_open(const char* pcPortName) return INVALID_SERIAL_PORT; } - sp->ct.ReadIntervalTimeout = 30; + sp->ct.ReadIntervalTimeout = 0; sp->ct.ReadTotalTimeoutMultiplier = 0; - sp->ct.ReadTotalTimeoutConstant = 0; + sp->ct.ReadTotalTimeoutConstant = 30; sp->ct.WriteTotalTimeoutMultiplier = 0; - sp->ct.WriteTotalTimeoutConstant = 0; + sp->ct.WriteTotalTimeoutConstant = 30; if(!SetCommTimeouts(sp->hPort,&sp->ct)) { @@ -205,19 +205,21 @@ void rs232_close(const serial_port sp) bool rs232_cts(const serial_port sp) { DWORD dwStatus; - if (GetCommModemStatus(((serial_port_windows*)sp)->hPort,&dwStatus) == NULL) return false; + if (!GetCommModemStatus(((serial_port_windows*)sp)->hPort,&dwStatus)) return false; return (dwStatus & MS_CTS_ON); } bool rs232_receive(const serial_port sp, byte_t* pbtRx, uint32_t* puiRxLen) { - return (ReadFile(((serial_port_windows*)sp)->hPort,pbtRx,*puiRxLen,(LPDWORD)puiRxLen,NULL) != NULL); + ReadFile(((serial_port_windows*)sp)->hPort,pbtRx,*puiRxLen,(LPDWORD)puiRxLen,NULL); + return (*puiRxLen != 0); } bool rs232_send(const serial_port sp, const byte_t* pbtTx, const uint32_t uiTxLen) { - DWORD dwTxLen; - return (WriteFile(((serial_port_windows*)sp)->hPort,pbtTx,uiTxLen,&dwTxLen,NULL) != NULL); + DWORD dwTxLen = 0; + return WriteFile(((serial_port_windows*)sp)->hPort,pbtTx,uiTxLen,&dwTxLen,NULL); + return (dwTxLen != 0); } #endif