Remove getopt dependency (to be able to compile under Windows). This fix Issue 12.
Some code cleanup.
This commit is contained in:
parent
36a4eb208f
commit
30c2fc4c93
5 changed files with 89 additions and 86 deletions
|
@ -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,27 +92,32 @@ 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);
|
||||||
|
|
|
@ -85,10 +85,8 @@ 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
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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
|
||||||
|
|
||||||
|
|
|
@ -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,47 +35,47 @@ 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':
|
|
||||||
default:
|
|
||||||
printf("\n\tusage:\n");
|
|
||||||
printf("\t\tnfc-emulate [OPTIONS] [UID]\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");
|
|
||||||
printf("\targs:\n");
|
|
||||||
printf("\t\t[UID]\tThe UID to emulate, specified as 8 HEX digits. Default is DEADBEAF.\n");
|
|
||||||
printf("\n");
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
// See if UID was specified as HEX string
|
|
||||||
if(argc > 1 && strlen(argv[optind]) == 8)
|
|
||||||
{
|
|
||||||
byte_t abtTmp[3] = { 0x00,0x00,0x00 };
|
byte_t abtTmp[3] = { 0x00,0x00,0x00 };
|
||||||
|
printf("[+] Using UID: %s\n",argv[arg]);
|
||||||
printf("[+] Using UID: %s\n",argv[optind]);
|
|
||||||
abtUidBcc[4]= 0x00;
|
abtUidBcc[4]= 0x00;
|
||||||
for(i= 0; i < 4; ++i)
|
for(i= 0; i < 4; ++i)
|
||||||
{
|
{
|
||||||
memcpy(abtTmp,argv[optind]+i*2,2);
|
memcpy(abtTmp,argv[arg]+i*2,2);
|
||||||
abtUidBcc[i]= (byte_t) strtol(abtTmp,NULL,16);
|
abtUidBcc[i]= (byte_t) strtol(abtTmp,NULL,16);
|
||||||
abtUidBcc[4] ^= abtUidBcc[i];
|
abtUidBcc[4] ^= abtUidBcc[i];
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
ERR("%s is not supported option.", argv[arg]);
|
||||||
|
print_usage();
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Try to open the NFC reader
|
// Try to open the NFC reader
|
||||||
|
|
|
@ -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;
|
||||||
}
|
}
|
||||||
|
|
35
src/relay.c
35
src/relay.c
|
@ -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,29 +34,33 @@ 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);
|
||||||
|
|
Loading…
Add table
Reference in a new issue