From 30c2fc4c93d0e63c3a7a4fc1ee4dd0f0542f092e Mon Sep 17 00:00:00 2001 From: Romuald Conty Date: Mon, 7 Sep 2009 10:15:34 +0000 Subject: [PATCH] Remove getopt dependency (to be able to compile under Windows). This fix Issue 12. Some code cleanup. --- src/anticol.c | 37 ++++++++++++++------------ src/dev_acr122.c | 23 +++++++---------- src/emulate.c | 67 ++++++++++++++++++++++++------------------------ src/libnfc.c | 11 +++++--- src/relay.c | 37 ++++++++++++++------------ 5 files changed, 89 insertions(+), 86 deletions(-) diff --git a/src/anticol.c b/src/anticol.c index d8bb445..4d7fccf 100644 --- a/src/anticol.c +++ b/src/anticol.c @@ -23,8 +23,6 @@ along with this program. If not, see #include #include #include -#include - #include #include "libnfc.h" @@ -94,31 +92,36 @@ bool transmit_bytes(const byte_t* pbtTx, const uint32_t uiTxLen) return true; } +void print_usage(void) +{ + printf("Usage: nfc-anticol [OPTIONS]\n"); + printf("Options:\n"); + printf("\t-h\tHelp. Print this message.\n"); + printf("\t-q\tQuiet mode. Suppress output of READER and EMULATOR data (improves timing).\n"); +} + int main(int argc,char* argv[]) { - int i; + int arg; // Get commandline options - while ((i= getopt(argc, argv, "hq")) != -1) - switch (i) - { - case 'q': + for (arg=1;argacName,pcFirmware); + strcpy(pdi->acName,pcFirmware); pdi->ct = CT_PN532; pdi->ds = (dev_spec)pdsa; pdi->bActive = true; @@ -187,7 +182,7 @@ bool dev_acr122_transceive(const dev_spec ds, const byte_t* pbtTx, const uint32_ // Make sure the command does not overflow the send buffer if (uiTxLen > ACR122_COMMAND_LEN) return false; - + // Store the length of the command we are going to send abtTxBuf[4] = uiTxLen; @@ -195,7 +190,7 @@ bool dev_acr122_transceive(const dev_spec ds, const byte_t* pbtTx, const uint32_ memcpy(abtTxBuf+5,pbtTx,uiTxLen); ulRxBufLen = sizeof(abtRxBuf); #ifdef DEBUG - printf("Tx: "); + printf(" TX: "); print_hex(abtTxBuf,uiTxLen+5); #endif @@ -221,7 +216,7 @@ bool dev_acr122_transceive(const dev_spec ds, const byte_t* pbtTx, const uint32_ } #ifdef DEBUG - printf("Rx: "); + printf(" RX: "); print_hex(abtRxBuf,ulRxBufLen); #endif @@ -241,7 +236,7 @@ bool dev_acr122_transceive(const dev_spec ds, const byte_t* pbtTx, const uint32_ char* dev_acr122_firmware(const dev_spec ds) { uint32_t uiResult; - + dev_spec_acr122* pdsa = (dev_spec_acr122*)ds; static char abtFw[11]; size_t ulFwLen = sizeof(abtFw); diff --git a/src/emulate.c b/src/emulate.c index 900ee66..bce6613 100644 --- a/src/emulate.c +++ b/src/emulate.c @@ -23,7 +23,6 @@ along with this program. If not, see #include #include #include -#include #include "libnfc.h" @@ -36,52 +35,52 @@ byte_t abtAtqa [2] = { 0x04,0x00 }; byte_t abtUidBcc [5] = { 0xDE,0xAD,0xBE,0xAF,0x62 }; byte_t abtSak [9] = { 0x08,0xb6,0xdd }; +void print_usage(void) +{ + printf("Usage: nfc-emulate [OPTIONS] [UID]\n"); + printf("Options:\n"); + printf("\t-h\tHelp. Print this message.\n"); + printf("\t-q\tQuiet mode. Suppress output of READER and EMULATOR data (improves timing).\n"); + printf("\n"); + printf("\t[UID]\tUID to emulate, specified as 8 HEX digits (default is DEADBEAF).\n"); +} + int main(int argc, char *argv[]) -{ +{ byte_t* pbtTx = NULL; uint32_t uiTxBits; - int i; bool quiet_output = false; + int arg, i; + // Get commandline options - while ((i= getopt(argc, argv, "hq")) != -1) - switch (i) - { - case 'q': + for (arg=1;arg 1 && strlen(argv[optind]) == 8) - { - byte_t abtTmp[3] = { 0x00,0x00,0x00 }; - - printf("[+] Using UID: %s\n",argv[optind]); - abtUidBcc[4]= 0x00; - for(i= 0; i < 4; ++i) - { - memcpy(abtTmp,argv[optind]+i*2,2); - abtUidBcc[i]= (byte_t) strtol(abtTmp,NULL,16); - abtUidBcc[4] ^= abtUidBcc[i]; - } } // Try to open the NFC reader pdi = nfc_connect(NULL); - + if (pdi == INVALID_DEVICE_INFO) { printf("Error connecting NFC reader\n"); diff --git a/src/libnfc.c b/src/libnfc.c index 36ccb89..0addde6 100644 --- a/src/libnfc.c +++ b/src/libnfc.c @@ -281,6 +281,7 @@ dev_info* nfc_connect(nfc_device_desc_t* device_desc) // Test if the connection was successful if (pdi != INVALID_DEVICE_INFO) { + DBG("%s have been claimed.", pdi->acName); // Great we have claimed a device pdi->pdc = &(dev_callbacks_list[uiDev]); pdi->pdc->transceive(pdi->ds,pncmd_get_register,4,NULL,NULL); @@ -289,6 +290,7 @@ dev_info* nfc_connect(nfc_device_desc_t* device_desc) if (!pdi->pdc->transceive(pdi->ds,pncmd_get_firmware_version,2,abtFw,&uiFwLen)) { // Failed to get firmware revision??, whatever...let's disconnect and clean up and return err + ERR("Failed to get firmware revision for: %s", pdi->acName); pdi->pdc->disconnect(pdi); return INVALID_DEVICE_INFO; } @@ -312,9 +314,10 @@ dev_info* nfc_connect(nfc_device_desc_t* device_desc) if (!nfc_configure(pdi,DCO_ACTIVATE_CRYPTO1,false)) return INVALID_DEVICE_INFO; return pdi; - } + } else { + DBG("No device found using driver: %s", dev_callbacks_list[uiDev].acDriver); + } } - // To bad, no reader is ready to be claimed return INVALID_DEVICE_INFO; } @@ -327,8 +330,8 @@ void nfc_disconnect(dev_info* pdi) bool nfc_configure(dev_info* pdi, const dev_config_option dco, const bool bEnable) { - byte_t btValue; - + byte_t btValue; + // Make sure we are dealing with a active device if (!pdi->bActive) return false; diff --git a/src/relay.c b/src/relay.c index c32e3d8..7f7a658 100644 --- a/src/relay.c +++ b/src/relay.c @@ -22,7 +22,6 @@ along with this program. If not, see #include #include #include -#include #include "libnfc.h" @@ -35,30 +34,34 @@ static uint32_t uiTagRxBits; static dev_info* pdiReader; static dev_info* pdiTag; +void print_usage(void) +{ + printf("Usage: nfc-anticol [OPTIONS]\n"); + printf("Options:\n"); + printf("\t-h\tHelp. Print this message.\n"); + printf("\t-q\tQuiet mode. Suppress output of READER and EMULATOR data (improves timing).\n"); +} + int main(int argc,char* argv[]) -{ - int i; +{ + int arg; bool quiet_output = false; // Get commandline options - while ((i= getopt(argc, argv, "hq")) != -1) - switch (i) - { - case 'q': + for (arg=1;arg