Remove getopt dependency (to be able to compile under Windows). This fix Issue 12.

Some code cleanup.
This commit is contained in:
Romuald Conty 2009-09-07 10:15:34 +00:00
parent 36a4eb208f
commit 30c2fc4c93
5 changed files with 89 additions and 86 deletions

View file

@ -23,8 +23,6 @@ along with this program. If not, see <http://www.gnu.org/licenses/>
#include <stddef.h> #include <stddef.h>
#include <stdint.h> #include <stdint.h>
#include <stdbool.h> #include <stdbool.h>
#include <getopt.h>
#include <string.h> #include <string.h>
#include "libnfc.h" #include "libnfc.h"
@ -94,31 +92,36 @@ bool transmit_bytes(const byte_t* pbtTx, const uint32_t uiTxLen)
return true; 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 main(int argc,char* argv[])
{ {
int i; int arg;
// Get commandline options // Get commandline options
while ((i= getopt(argc, argv, "hq")) != -1) for (arg=1;arg<argc;arg++) {
switch (i) if (0 == strcmp(argv[arg], "-h")) {
{ print_usage();
case 'q': return 0;
} else if (0 == strcmp(argv[arg], "-q")) {
INFO("Quiet mode.");
quiet_output = true; quiet_output = true;
break; } else {
case 'h': ERR("%s is not supported option.", argv[arg]);
default: print_usage();
printf("\n\tusage:\n");
printf("\t\tnfc-anticol [OPTIONS]\n\n");
printf("\toptions:\n");
printf("\t\t-h\tHelp. Print this message.\n");
printf("\t\t-q\tQuiet mode. Suppress output of READER and EMULATOR data (improves timing).\n");
printf("\n");
return -1; return -1;
} }
}
// Try to open the NFC reader // Try to open the NFC reader
pdi = nfc_connect(NULL); pdi = nfc_connect(NULL);
if (!pdi) if (!pdi)
{ {
printf("Error connecting NFC reader\n"); printf("Error connecting NFC reader\n");

View file

@ -84,11 +84,9 @@ dev_info* dev_acr122_connect(const nfc_device_desc_t* device_desc)
// Retrieve the string array of all available pcsc readers // Retrieve the string array of all available pcsc readers
if (SCardListReaders(dsa.hCtx,NULL,acList,(void*)&ulListLen) != SCARD_S_SUCCESS) return INVALID_DEVICE_INFO; if (SCardListReaders(dsa.hCtx,NULL,acList,(void*)&ulListLen) != SCARD_S_SUCCESS) return INVALID_DEVICE_INFO;
#ifdef DEBUG DBG("PCSC reports following device(s):");
printf("Found the following PCSC device(s)\n"); DBG("- %s",acList);
printf("- %s\n",acList);
#endif
pacReaders[0] = acList; pacReaders[0] = acList;
uiReaderCount = 1; uiReaderCount = 1;
@ -109,10 +107,7 @@ dev_info* dev_acr122_connect(const nfc_device_desc_t* device_desc)
pacReaders[uiReaderCount] = acList+uiPos+1; pacReaders[uiReaderCount] = acList+uiPos+1;
uiReaderCount++; uiReaderCount++;
// Debug info DBG("- %s",acList+uiPos+1);
#ifdef DEBUG
printf("- %s\n",acList+uiPos+1);
#endif
} }
} }
@ -157,7 +152,7 @@ dev_info* dev_acr122_connect(const nfc_device_desc_t* device_desc)
// Done, we found the reader we are looking for // Done, we found the reader we are looking for
pdi = malloc(sizeof(dev_info)); pdi = malloc(sizeof(dev_info));
strcpy(pdi->acName,pcFirmware); strcpy(pdi->acName,pcFirmware);
pdi->ct = CT_PN532; pdi->ct = CT_PN532;
pdi->ds = (dev_spec)pdsa; pdi->ds = (dev_spec)pdsa;
pdi->bActive = true; 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 // Make sure the command does not overflow the send buffer
if (uiTxLen > ACR122_COMMAND_LEN) return false; if (uiTxLen > ACR122_COMMAND_LEN) return false;
// Store the length of the command we are going to send // Store the length of the command we are going to send
abtTxBuf[4] = uiTxLen; 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); memcpy(abtTxBuf+5,pbtTx,uiTxLen);
ulRxBufLen = sizeof(abtRxBuf); ulRxBufLen = sizeof(abtRxBuf);
#ifdef DEBUG #ifdef DEBUG
printf("Tx: "); printf(" TX: ");
print_hex(abtTxBuf,uiTxLen+5); print_hex(abtTxBuf,uiTxLen+5);
#endif #endif
@ -221,7 +216,7 @@ bool dev_acr122_transceive(const dev_spec ds, const byte_t* pbtTx, const uint32_
} }
#ifdef DEBUG #ifdef DEBUG
printf("Rx: "); printf(" RX: ");
print_hex(abtRxBuf,ulRxBufLen); print_hex(abtRxBuf,ulRxBufLen);
#endif #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) char* dev_acr122_firmware(const dev_spec ds)
{ {
uint32_t uiResult; uint32_t uiResult;
dev_spec_acr122* pdsa = (dev_spec_acr122*)ds; dev_spec_acr122* pdsa = (dev_spec_acr122*)ds;
static char abtFw[11]; static char abtFw[11];
size_t ulFwLen = sizeof(abtFw); size_t ulFwLen = sizeof(abtFw);

View file

@ -23,7 +23,6 @@ along with this program. If not, see <http://www.gnu.org/licenses/>
#include <stddef.h> #include <stddef.h>
#include <stdint.h> #include <stdint.h>
#include <string.h> #include <string.h>
#include <getopt.h>
#include "libnfc.h" #include "libnfc.h"
@ -36,52 +35,52 @@ byte_t abtAtqa [2] = { 0x04,0x00 };
byte_t abtUidBcc [5] = { 0xDE,0xAD,0xBE,0xAF,0x62 }; byte_t abtUidBcc [5] = { 0xDE,0xAD,0xBE,0xAF,0x62 };
byte_t abtSak [9] = { 0x08,0xb6,0xdd }; 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[]) int main(int argc, char *argv[])
{ {
byte_t* pbtTx = NULL; byte_t* pbtTx = NULL;
uint32_t uiTxBits; uint32_t uiTxBits;
int i;
bool quiet_output = false; bool quiet_output = false;
int arg, i;
// Get commandline options // Get commandline options
while ((i= getopt(argc, argv, "hq")) != -1) for (arg=1;arg<argc;arg++) {
switch (i) if (0 == strcmp(argv[arg], "-h")) {
{ print_usage();
case 'q': return 0;
} else if (0 == strcmp(argv[arg], "-q")) {
INFO("Quiet mode.");
quiet_output = true; quiet_output = true;
break; } else if((arg == argc-1) && (strlen(argv[arg]) == 8)) { // See if UID was specified as HEX string
case 'h': byte_t abtTmp[3] = { 0x00,0x00,0x00 };
default: printf("[+] Using UID: %s\n",argv[arg]);
printf("\n\tusage:\n"); abtUidBcc[4]= 0x00;
printf("\t\tnfc-emulate [OPTIONS] [UID]\n\n"); for(i= 0; i < 4; ++i)
printf("\toptions:\n"); {
printf("\t\t-h\tHelp. Print this message.\n"); memcpy(abtTmp,argv[arg]+i*2,2);
printf("\t\t-q\tQuiet mode. Suppress output of READER and EMULATOR data (improves timing).\n"); abtUidBcc[i]= (byte_t) strtol(abtTmp,NULL,16);
printf("\n"); abtUidBcc[4] ^= abtUidBcc[i];
printf("\targs:\n"); }
printf("\t\t[UID]\tThe UID to emulate, specified as 8 HEX digits. Default is DEADBEAF.\n"); } else {
printf("\n"); ERR("%s is not supported option.", argv[arg]);
print_usage();
return -1; return -1;
} }
// See if UID was specified as HEX string
if(argc > 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 // Try to open the NFC reader
pdi = nfc_connect(NULL); pdi = nfc_connect(NULL);
if (pdi == INVALID_DEVICE_INFO) if (pdi == INVALID_DEVICE_INFO)
{ {
printf("Error connecting NFC reader\n"); printf("Error connecting NFC reader\n");

View file

@ -281,6 +281,7 @@ dev_info* nfc_connect(nfc_device_desc_t* device_desc)
// Test if the connection was successful // Test if the connection was successful
if (pdi != INVALID_DEVICE_INFO) if (pdi != INVALID_DEVICE_INFO)
{ {
DBG("%s have been claimed.", pdi->acName);
// Great we have claimed a device // Great we have claimed a device
pdi->pdc = &(dev_callbacks_list[uiDev]); pdi->pdc = &(dev_callbacks_list[uiDev]);
pdi->pdc->transceive(pdi->ds,pncmd_get_register,4,NULL,NULL); 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)) 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 // 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); pdi->pdc->disconnect(pdi);
return INVALID_DEVICE_INFO; 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; if (!nfc_configure(pdi,DCO_ACTIVATE_CRYPTO1,false)) return INVALID_DEVICE_INFO;
return pdi; return pdi;
} } else {
DBG("No device found using driver: %s", dev_callbacks_list[uiDev].acDriver);
}
} }
// To bad, no reader is ready to be claimed // To bad, no reader is ready to be claimed
return INVALID_DEVICE_INFO; 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) 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 // Make sure we are dealing with a active device
if (!pdi->bActive) return false; if (!pdi->bActive) return false;

View file

@ -22,7 +22,6 @@ along with this program. If not, see <http://www.gnu.org/licenses/>
#include <stdlib.h> #include <stdlib.h>
#include <stdint.h> #include <stdint.h>
#include <string.h> #include <string.h>
#include <getopt.h>
#include "libnfc.h" #include "libnfc.h"
@ -35,30 +34,34 @@ static uint32_t uiTagRxBits;
static dev_info* pdiReader; static dev_info* pdiReader;
static dev_info* pdiTag; 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 main(int argc,char* argv[])
{ {
int i; int arg;
bool quiet_output = false; bool quiet_output = false;
// Get commandline options // Get commandline options
while ((i= getopt(argc, argv, "hq")) != -1) for (arg=1;arg<argc;arg++) {
switch (i) if (0 == strcmp(argv[arg], "-h")) {
{ print_usage();
case 'q': return 0;
} else if (0 == strcmp(argv[arg], "-q")) {
INFO("Quiet mode.");
quiet_output = true; quiet_output = true;
break; } else {
case 'h': ERR("%s is not supported option.", argv[arg]);
default: print_usage();
printf("\n\tusage:\n");
printf("\t\tnfc-relay [OPTIONS]\n\n");
printf("\toptions:\n");
printf("\t\t-h\tHelp. Print this message.\n");
printf("\t\t-q\tQuiet mode. Suppress output of READER and EMULATOR data (improves timing).\n");
printf("\n");
return -1; return -1;
} }
}
// Try to open the NFC emulator device // Try to open the NFC emulator device
pdiTag = nfc_connect(NULL); pdiTag = nfc_connect(NULL);
if (pdiTag == INVALID_DEVICE_INFO) if (pdiTag == INVALID_DEVICE_INFO)