Error conditions in utils & examples: fix leaks, unify style (see details)
* in main(): ** errx()/err()/return -> exit() ** return values -> EXIT_SUCCESS & EXIT_FAILURE * out of main: ** err()/errx()/exit() -> return ** change retval from size_t to int to allow returning errors ** don't use EXIT_SUCCESS / EXIT_FAILURE as retvals * add nfc_close() & nfc_exit() to exit() on errors * add missing fclose() on errors * add missing test if (pnd == NULL) * unify style if (pnd == / != NULL) * remove goto's * few related fixes * remove if(pnd!=NULL) test on nfc_close() calls
This commit is contained in:
parent
232930c3d5
commit
bece73faaf
21 changed files with 433 additions and 298 deletions
|
|
@ -50,7 +50,7 @@ main(int argc, const char *argv[])
|
|||
|
||||
if (pnd == NULL) {
|
||||
warnx("ERROR: %s", "Unable to open NFC device.");
|
||||
return EXIT_FAILURE;
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
// Set opened NFC device to initiator mode
|
||||
if (nfc_initiator_init(pnd) < 0) {
|
||||
|
|
@ -82,5 +82,5 @@ main(int argc, const char *argv[])
|
|||
nfc_close(pnd);
|
||||
// Release the context
|
||||
nfc_exit(context);
|
||||
return EXIT_SUCCESS;
|
||||
exit(EXIT_SUCCESS);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -42,7 +42,7 @@ main(int argc, const char *argv[])
|
|||
|
||||
if (pnd == NULL) {
|
||||
ERR("%s", "Unable to open NFC device.");
|
||||
return EXIT_FAILURE;
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
// Set opened NFC device to initiator mode
|
||||
if (nfc_initiator_init(pnd) < 0) {
|
||||
|
|
@ -74,5 +74,5 @@ main(int argc, const char *argv[])
|
|||
nfc_close(pnd);
|
||||
// Release the context
|
||||
nfc_exit(context);
|
||||
return EXIT_SUCCESS;
|
||||
exit(EXIT_SUCCESS);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -155,30 +155,39 @@ main(int argc, char *argv[])
|
|||
// Try to open the NFC reader
|
||||
pnd = nfc_open(context, NULL);
|
||||
|
||||
if (!pnd) {
|
||||
if (pnd == NULL) {
|
||||
printf("Error opening NFC reader\n");
|
||||
nfc_exit(context);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
// Initialise NFC device as "initiator"
|
||||
if (nfc_initiator_init(pnd) < 0) {
|
||||
nfc_perror(pnd, "nfc_initiator_init");
|
||||
nfc_close(pnd);
|
||||
nfc_exit(context);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
// Configure the CRC
|
||||
if (nfc_device_set_property_bool(pnd, NP_HANDLE_CRC, false) < 0) {
|
||||
nfc_perror(pnd, "nfc_device_set_property_bool");
|
||||
nfc_close(pnd);
|
||||
nfc_exit(context);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
// Use raw send/receive methods
|
||||
if (nfc_device_set_property_bool(pnd, NP_EASY_FRAMING, false) < 0) {
|
||||
nfc_perror(pnd, "nfc_device_set_property_bool");
|
||||
nfc_close(pnd);
|
||||
nfc_exit(context);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
// Disable 14443-4 autoswitching
|
||||
if (nfc_device_set_property_bool(pnd, NP_AUTO_ISO14443_4, false) < 0) {
|
||||
nfc_perror(pnd, "nfc_device_set_property_bool");
|
||||
nfc_close(pnd);
|
||||
nfc_exit(context);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
|
|
@ -189,7 +198,7 @@ main(int argc, char *argv[])
|
|||
printf("Error: No tag available\n");
|
||||
nfc_close(pnd);
|
||||
nfc_exit(context);
|
||||
return EXIT_FAILURE;
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
memcpy(abtAtqa, abtRx, 2);
|
||||
|
||||
|
|
@ -319,5 +328,5 @@ main(int argc, char *argv[])
|
|||
|
||||
nfc_close(pnd);
|
||||
nfc_exit(context);
|
||||
return EXIT_SUCCESS;
|
||||
exit(EXIT_SUCCESS);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -53,7 +53,7 @@ static nfc_device *pnd;
|
|||
static void stop_dep_communication(int sig)
|
||||
{
|
||||
(void) sig;
|
||||
if (pnd)
|
||||
if (pnd != NULL)
|
||||
nfc_abort_command(pnd);
|
||||
else
|
||||
exit(EXIT_FAILURE);
|
||||
|
|
@ -68,16 +68,17 @@ main(int argc, const char *argv[])
|
|||
|
||||
if (argc > 1) {
|
||||
printf("Usage: %s\n", argv[0]);
|
||||
return EXIT_FAILURE;
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
nfc_context *context;
|
||||
nfc_init(&context);
|
||||
|
||||
pnd = nfc_open(context, NULL);
|
||||
if (!pnd) {
|
||||
if (pnd == NULL) {
|
||||
printf("Unable to open NFC device.\n");
|
||||
return EXIT_FAILURE;
|
||||
nfc_exit(context);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
printf("NFC device: %s\n opened", nfc_device_get_name(pnd));
|
||||
|
||||
|
|
@ -85,12 +86,16 @@ main(int argc, const char *argv[])
|
|||
|
||||
if (nfc_initiator_init(pnd) < 0) {
|
||||
nfc_perror(pnd, "nfc_initiator_init");
|
||||
goto error;
|
||||
nfc_close(pnd);
|
||||
nfc_exit(context);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
if (nfc_initiator_select_dep_target(pnd, NDM_PASSIVE, NBR_212, NULL, &nt, 1000) < 0) {
|
||||
nfc_perror(pnd, "nfc_initiator_select_dep_target");
|
||||
goto error;
|
||||
nfc_close(pnd);
|
||||
nfc_exit(context);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
print_nfc_target(nt, false);
|
||||
|
||||
|
|
@ -98,7 +103,9 @@ main(int argc, const char *argv[])
|
|||
int res;
|
||||
if ((res = nfc_initiator_transceive_bytes(pnd, abtTx, sizeof(abtTx), abtRx, sizeof(abtRx), 0)) < 0) {
|
||||
nfc_perror(pnd, "nfc_initiator_transceive_bytes");
|
||||
goto error;
|
||||
nfc_close(pnd);
|
||||
nfc_exit(context);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
abtRx[res] = 0;
|
||||
|
|
@ -106,11 +113,12 @@ main(int argc, const char *argv[])
|
|||
|
||||
if (nfc_initiator_deselect_target(pnd) < 0) {
|
||||
nfc_perror(pnd, "nfc_initiator_deselect_target");
|
||||
goto error;
|
||||
nfc_close(pnd);
|
||||
nfc_exit(context);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
error:
|
||||
nfc_close(pnd);
|
||||
nfc_exit(context);
|
||||
return EXIT_SUCCESS;
|
||||
exit(EXIT_SUCCESS);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -52,7 +52,7 @@ static nfc_device *pnd;
|
|||
static void stop_dep_communication(int sig)
|
||||
{
|
||||
(void) sig;
|
||||
if (pnd)
|
||||
if (pnd != NULL)
|
||||
nfc_abort_command(pnd);
|
||||
else
|
||||
exit(EXIT_FAILURE);
|
||||
|
|
@ -65,6 +65,11 @@ main(int argc, const char *argv[])
|
|||
int szRx;
|
||||
uint8_t abtTx[] = "Hello Mars!";
|
||||
|
||||
if (argc > 1) {
|
||||
printf("Usage: %s\n", argv[0]);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
nfc_context *context;
|
||||
nfc_init(&context);
|
||||
#define MAX_DEVICE_COUNT 2
|
||||
|
|
@ -80,12 +85,8 @@ main(int argc, const char *argv[])
|
|||
pnd = nfc_open(context, connstrings[1]);
|
||||
} else {
|
||||
printf("No device found.\n");
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
if (argc > 1) {
|
||||
printf("Usage: %s\n", argv[0]);
|
||||
return EXIT_FAILURE;
|
||||
nfc_exit(context);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
nfc_target nt = {
|
||||
|
|
@ -109,9 +110,10 @@ main(int argc, const char *argv[])
|
|||
},
|
||||
};
|
||||
|
||||
if (!pnd) {
|
||||
if (pnd == NULL) {
|
||||
printf("Unable to open NFC device.\n");
|
||||
return EXIT_FAILURE;
|
||||
nfc_exit(context);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
printf("NFC device: %s opened\n", nfc_device_get_name(pnd));
|
||||
|
||||
|
|
@ -123,13 +125,17 @@ main(int argc, const char *argv[])
|
|||
printf("Waiting for initiator request...\n");
|
||||
if ((szRx = nfc_target_init(pnd, &nt, abtRx, sizeof(abtRx), 0)) < 0) {
|
||||
nfc_perror(pnd, "nfc_target_init");
|
||||
goto error;
|
||||
nfc_close(pnd);
|
||||
nfc_exit(context);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
printf("Initiator request received. Waiting for data...\n");
|
||||
if ((szRx = nfc_target_receive_bytes(pnd, abtRx, sizeof(abtRx), 0)) < 0) {
|
||||
nfc_perror(pnd, "nfc_target_receive_bytes");
|
||||
goto error;
|
||||
nfc_close(pnd);
|
||||
nfc_exit(context);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
abtRx[(size_t) szRx] = '\0';
|
||||
printf("Received: %s\n", abtRx);
|
||||
|
|
@ -137,12 +143,13 @@ main(int argc, const char *argv[])
|
|||
printf("Sending: %s\n", abtTx);
|
||||
if (nfc_target_send_bytes(pnd, abtTx, sizeof(abtTx), 0) < 0) {
|
||||
nfc_perror(pnd, "nfc_target_send_bytes");
|
||||
goto error;
|
||||
nfc_close(pnd);
|
||||
nfc_exit(context);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
printf("Data sent.\n");
|
||||
|
||||
error:
|
||||
nfc_close(pnd);
|
||||
nfc_exit(context);
|
||||
return EXIT_SUCCESS;
|
||||
exit(EXIT_SUCCESS);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -79,7 +79,7 @@ static void
|
|||
stop_emulation(int sig)
|
||||
{
|
||||
(void)sig;
|
||||
if (pnd) {
|
||||
if (pnd != NULL) {
|
||||
nfc_abort_command(pnd);
|
||||
} else {
|
||||
exit(EXIT_FAILURE);
|
||||
|
|
@ -191,6 +191,7 @@ main(int argc, char *argv[])
|
|||
|
||||
if (pnd == NULL) {
|
||||
ERR("Unable to open NFC device");
|
||||
nfc_exit(context);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
|
|
@ -198,18 +199,13 @@ main(int argc, char *argv[])
|
|||
printf("Emulating NDEF tag now, please touch it with a second NFC device\n");
|
||||
|
||||
if (nfc_emulate_target(pnd, &emulator, 0) < 0) {
|
||||
goto error;
|
||||
nfc_perror(pnd, argv[0]);
|
||||
nfc_close(pnd);
|
||||
nfc_exit(context);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
nfc_close(pnd);
|
||||
nfc_exit(context);
|
||||
|
||||
exit(EXIT_SUCCESS);
|
||||
|
||||
error:
|
||||
if (pnd) {
|
||||
nfc_perror(pnd, argv[0]);
|
||||
nfc_close(pnd);
|
||||
nfc_exit(context);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -66,9 +66,7 @@ intr_hdlr(int sig)
|
|||
{
|
||||
(void) sig;
|
||||
printf("\nQuitting...\n");
|
||||
if (pnd != NULL) {
|
||||
nfc_close(pnd);
|
||||
}
|
||||
nfc_close(pnd);
|
||||
nfc_exit(context);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
|
@ -184,15 +182,16 @@ main(int argc, char *argv[])
|
|||
|
||||
nfc_init(&context);
|
||||
|
||||
// Try to open the NFC reader
|
||||
pnd = nfc_open(context, NULL);
|
||||
|
||||
// Display libnfc version
|
||||
acLibnfcVersion = nfc_version();
|
||||
printf("%s uses libnfc %s\n", argv[0], acLibnfcVersion);
|
||||
|
||||
// Try to open the NFC reader
|
||||
pnd = nfc_open(context, NULL);
|
||||
|
||||
if (pnd == NULL) {
|
||||
ERR("Unable to open NFC device");
|
||||
nfc_exit(context);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
|
|
@ -267,6 +266,8 @@ main(int argc, char *argv[])
|
|||
printf("NFC device (configured as target) is now emulating the tag, please touch it with a second NFC device (initiator)\n");
|
||||
if (!nfc_target_emulate_tag(pnd, &nt)) {
|
||||
nfc_perror(pnd, "nfc_target_emulate_tag");
|
||||
nfc_close(pnd);
|
||||
nfc_exit(context);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -134,6 +134,7 @@ main(int argc, char *argv[])
|
|||
|
||||
if (pnd == NULL) {
|
||||
printf("Unable to open NFC device\n");
|
||||
nfc_exit(context);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
|
|
@ -162,13 +163,17 @@ main(int argc, char *argv[])
|
|||
if ((szRecvBits = nfc_target_init(pnd, &nt, abtRecv, sizeof(abtRecv), 0)) < 0) {
|
||||
nfc_perror(pnd, "nfc_target_init");
|
||||
ERR("Could not come out of auto-emulation, no command was received");
|
||||
goto error;
|
||||
nfc_close(pnd);
|
||||
nfc_exit(context);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
printf("[+] Received initiator command: ");
|
||||
print_hex_bits(abtRecv, (size_t) szRecvBits);
|
||||
printf("[+] Configuring communication\n");
|
||||
if ((nfc_device_set_property_bool(pnd, NP_HANDLE_CRC, false) < 0) || (nfc_device_set_property_bool(pnd, NP_HANDLE_PARITY, true) < 0)) {
|
||||
nfc_perror(pnd, "nfc_device_set_property_bool");
|
||||
nfc_close(pnd);
|
||||
nfc_exit(context);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
printf("[+] Done, the emulated tag is initialized with UID: %02X%02X%02X%02X\n\n", abtUidBcc[0], abtUidBcc[1],
|
||||
|
|
@ -211,7 +216,9 @@ main(int argc, char *argv[])
|
|||
// Send and print the command to the screen
|
||||
if (nfc_target_send_bits(pnd, pbtTx, szTxBits, NULL) < 0) {
|
||||
nfc_perror(pnd, "nfc_target_send_bits");
|
||||
goto error;
|
||||
nfc_close(pnd);
|
||||
nfc_exit(context);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
if (!quiet_output) {
|
||||
printf("T: ");
|
||||
|
|
@ -223,9 +230,4 @@ main(int argc, char *argv[])
|
|||
nfc_close(pnd);
|
||||
nfc_exit(context);
|
||||
exit(EXIT_SUCCESS);
|
||||
|
||||
error:
|
||||
nfc_close(pnd);
|
||||
nfc_exit(context);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -183,30 +183,39 @@ main(int argc, char *argv[])
|
|||
// Try to open the NFC reader
|
||||
pnd = nfc_open(context, NULL);
|
||||
|
||||
if (!pnd) {
|
||||
if (pnd == NULL) {
|
||||
printf("Error opening NFC reader\n");
|
||||
nfc_exit(context);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
// Initialise NFC device as "initiator"
|
||||
if (nfc_initiator_init(pnd) < 0) {
|
||||
nfc_perror(pnd, "nfc_initiator_init");
|
||||
nfc_close(pnd);
|
||||
nfc_exit(context);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
// Configure the CRC
|
||||
if (nfc_device_set_property_bool(pnd, NP_HANDLE_CRC, false) < 0) {
|
||||
nfc_perror(pnd, "nfc_device_set_property_bool");
|
||||
nfc_close(pnd);
|
||||
nfc_exit(context);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
// Use raw send/receive methods
|
||||
if (nfc_device_set_property_bool(pnd, NP_EASY_FRAMING, false) < 0) {
|
||||
nfc_perror(pnd, "nfc_device_set_property_bool");
|
||||
nfc_close(pnd);
|
||||
nfc_exit(context);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
// Disable 14443-4 autoswitching
|
||||
if (nfc_device_set_property_bool(pnd, NP_AUTO_ISO14443_4, false) < 0) {
|
||||
nfc_perror(pnd, "nfc_device_set_property_bool");
|
||||
nfc_close(pnd);
|
||||
nfc_exit(context);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
|
|
@ -217,7 +226,7 @@ main(int argc, char *argv[])
|
|||
printf("Error: No tag available\n");
|
||||
nfc_close(pnd);
|
||||
nfc_exit(context);
|
||||
return 1;
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
memcpy(abtAtqa, abtRx, 2);
|
||||
|
||||
|
|
@ -353,8 +362,7 @@ main(int argc, char *argv[])
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
nfc_close(pnd);
|
||||
nfc_exit(context);
|
||||
return EXIT_SUCCESS;
|
||||
exit(EXIT_SUCCESS);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -56,7 +56,7 @@ static nfc_device *pnd = NULL;
|
|||
static void stop_polling(int sig)
|
||||
{
|
||||
(void) sig;
|
||||
if (pnd)
|
||||
if (pnd != NULL)
|
||||
nfc_abort_command(pnd);
|
||||
else
|
||||
exit(EXIT_FAILURE);
|
||||
|
|
@ -110,11 +110,14 @@ main(int argc, const char *argv[])
|
|||
|
||||
if (pnd == NULL) {
|
||||
ERR("%s", "Unable to open NFC device.");
|
||||
nfc_exit(context);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
if (nfc_initiator_init(pnd) < 0) {
|
||||
nfc_perror(pnd, "nfc_initiator_init");
|
||||
nfc_close(pnd);
|
||||
nfc_exit(context);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -89,13 +89,13 @@ main(int argc, char *argv[])
|
|||
for (arg = 1; arg < argc; arg++) {
|
||||
if (0 == strcmp(argv[arg], "-h")) {
|
||||
print_usage(argv);
|
||||
return EXIT_SUCCESS;
|
||||
exit(EXIT_SUCCESS);
|
||||
} else if (0 == strcmp(argv[arg], "-q")) {
|
||||
quiet_output = true;
|
||||
} else {
|
||||
ERR("%s is not supported option.", argv[arg]);
|
||||
print_usage(argv);
|
||||
return EXIT_FAILURE;
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -116,14 +116,16 @@ main(int argc, char *argv[])
|
|||
|
||||
if (szFound < 2) {
|
||||
ERR("%zd device found but two opened devices are needed to relay NFC.", szFound);
|
||||
return EXIT_FAILURE;
|
||||
nfc_exit(context);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
// Try to open the NFC emulator device
|
||||
pndTag = nfc_open(context, connstrings[0]);
|
||||
if (pndTag == NULL) {
|
||||
printf("Error opening NFC emulator device\n");
|
||||
return EXIT_FAILURE;
|
||||
nfc_exit(context);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
printf("Hint: tag <---> initiator (relay) <---> target (relay) <---> original reader\n\n");
|
||||
|
|
@ -153,30 +155,44 @@ main(int argc, char *argv[])
|
|||
ERR("%s", "Initialization of NFC emulator failed");
|
||||
nfc_close(pndTag);
|
||||
nfc_exit(context);
|
||||
return EXIT_FAILURE;
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
printf("%s", "Configuring emulator settings...");
|
||||
if ((nfc_device_set_property_bool(pndTag, NP_HANDLE_CRC, false) < 0) ||
|
||||
(nfc_device_set_property_bool(pndTag, NP_HANDLE_PARITY, false) < 0) || (nfc_device_set_property_bool(pndTag, NP_ACCEPT_INVALID_FRAMES, true)) < 0) {
|
||||
nfc_perror(pndTag, "nfc_device_set_property_bool");
|
||||
nfc_close(pndTag);
|
||||
nfc_exit(context);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
printf("%s", "Done, emulated tag is initialized");
|
||||
|
||||
// Try to open the NFC reader
|
||||
pndReader = nfc_open(context, connstrings[1]);
|
||||
if (pndReader == NULL) {
|
||||
printf("Error opening NFC reader device\n");
|
||||
nfc_close(pndTag);
|
||||
nfc_exit(context);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
printf("NFC reader device: %s opened", nfc_device_get_name(pndReader));
|
||||
printf("%s", "Configuring NFC reader settings...");
|
||||
|
||||
if (nfc_initiator_init(pndReader) < 0) {
|
||||
nfc_perror(pndReader, "nfc_initiator_init");
|
||||
nfc_close(pndTag);
|
||||
nfc_close(pndReader);
|
||||
nfc_exit(context);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
if ((nfc_device_set_property_bool(pndReader, NP_HANDLE_CRC, false) < 0) ||
|
||||
(nfc_device_set_property_bool(pndReader, NP_HANDLE_PARITY, false) < 0) ||
|
||||
(nfc_device_set_property_bool(pndReader, NP_ACCEPT_INVALID_FRAMES, true)) < 0) {
|
||||
nfc_perror(pndReader, "nfc_device_set_property_bool");
|
||||
nfc_close(pndTag);
|
||||
nfc_close(pndReader);
|
||||
nfc_exit(context);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
printf("%s", "Done, relaying frames now!");
|
||||
|
|
@ -189,12 +205,18 @@ main(int argc, char *argv[])
|
|||
// Drop down field for a very short time (original tag will reboot)
|
||||
if (nfc_device_set_property_bool(pndReader, NP_ACTIVATE_FIELD, false) < 0) {
|
||||
nfc_perror(pndReader, "nfc_device_set_property_bool");
|
||||
nfc_close(pndTag);
|
||||
nfc_close(pndReader);
|
||||
nfc_exit(context);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
if (!quiet_output)
|
||||
printf("\n");
|
||||
if (nfc_device_set_property_bool(pndReader, NP_ACTIVATE_FIELD, true) < 0) {
|
||||
nfc_perror(pndReader, "nfc_device_set_property_bool");
|
||||
nfc_close(pndTag);
|
||||
nfc_close(pndReader);
|
||||
nfc_exit(context);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
}
|
||||
|
|
@ -209,6 +231,9 @@ main(int argc, char *argv[])
|
|||
// Redirect the answer back to the reader
|
||||
if (nfc_target_send_bits(pndTag, abtTagRx, szTagRxBits, abtTagRxPar) < 0) {
|
||||
nfc_perror(pndTag, "nfc_target_send_bits");
|
||||
nfc_close(pndTag);
|
||||
nfc_close(pndReader);
|
||||
nfc_exit(context);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
// Print the tag frame to the screen
|
||||
|
|
|
|||
|
|
@ -51,7 +51,7 @@ int
|
|||
main(int argc, const char *argv[])
|
||||
{
|
||||
size_t i;
|
||||
nfc_device *pnd;
|
||||
nfc_device *pnd = NULL;
|
||||
const char *acLibnfcVersion;
|
||||
bool result;
|
||||
int res = 0;
|
||||
|
|
@ -63,7 +63,8 @@ main(int argc, const char *argv[])
|
|||
const uint8_t pncmd_diagnose_ram_test[] = { Diagnose, 0x02 };
|
||||
|
||||
if (argc > 1) {
|
||||
errx(1, "usage: %s", argv[0]);
|
||||
printf("Usage: %s", argv[0]);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
nfc_context *context;
|
||||
|
|
@ -85,7 +86,8 @@ main(int argc, const char *argv[])
|
|||
|
||||
if (pnd == NULL) {
|
||||
ERR("%s", "Unable to open NFC device.");
|
||||
return EXIT_FAILURE;
|
||||
nfc_exit(context);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
printf("NFC device [%s] opened.\n", nfc_device_get_name(pnd));
|
||||
|
|
@ -119,4 +121,7 @@ main(int argc, const char *argv[])
|
|||
nfc_perror(pnd, "pn53x_transceive: cannot diagnose RAM");
|
||||
}
|
||||
}
|
||||
nfc_close(pnd);
|
||||
nfc_exit(context);
|
||||
exit(EXIT_SUCCESS);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -75,8 +75,6 @@ main(int argc, const char *argv[])
|
|||
(void) argc;
|
||||
(void) argv;
|
||||
|
||||
int ret = EXIT_FAILURE;
|
||||
|
||||
nfc_context *context;
|
||||
nfc_init(&context);
|
||||
|
||||
|
|
@ -90,7 +88,8 @@ main(int argc, const char *argv[])
|
|||
|
||||
if (pnd == NULL) {
|
||||
ERR("%s", "Unable to open NFC device.");
|
||||
return EXIT_FAILURE;
|
||||
nfc_exit(context);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
printf("NFC device: %s opened\n", nfc_device_get_name(pnd));
|
||||
|
|
@ -107,7 +106,9 @@ main(int argc, const char *argv[])
|
|||
printf("\n");
|
||||
if ((input < '1') || (input > '3')) {
|
||||
ERR("%s", "Invalid selection.");
|
||||
goto error;
|
||||
nfc_close(pnd);
|
||||
nfc_exit(context);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
/*
|
||||
|
|
@ -125,7 +126,9 @@ main(int argc, const char *argv[])
|
|||
// FIXME Its a private pn53x function
|
||||
if (pn532_SAMConfiguration(pnd, mode, 0) < 0) {
|
||||
nfc_perror(pnd, "pn53x_SAMConfiguration");
|
||||
goto error;
|
||||
nfc_close(pnd);
|
||||
nfc_exit(context);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
printf("Now the SAM is readable for 1 minute from an external reader.\n");
|
||||
wait_one_minute();
|
||||
|
|
@ -136,13 +139,17 @@ main(int argc, const char *argv[])
|
|||
// Set opened NFC device to initiator mode
|
||||
if (nfc_initiator_init_secure_element(pnd) < 0) {
|
||||
nfc_perror(pnd, "nfc_initiator_init_secure_element");
|
||||
goto error;
|
||||
nfc_close(pnd);
|
||||
nfc_exit(context);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
// Let the reader only try once to find a tag
|
||||
if (nfc_device_set_property_bool(pnd, NP_INFINITE_SELECT, false) < 0) {
|
||||
nfc_perror(pnd, "nfc_device_set_property_bool");
|
||||
goto error;
|
||||
nfc_close(pnd);
|
||||
nfc_exit(context);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
// Read the SAM's info
|
||||
const nfc_modulation nmSAM = {
|
||||
|
|
@ -154,16 +161,22 @@ main(int argc, const char *argv[])
|
|||
int res;
|
||||
if ((res = nfc_initiator_select_passive_target(pnd, nmSAM, NULL, 0, &nt)) < 0) {
|
||||
nfc_perror(pnd, "nfc_initiator_select_passive_target");
|
||||
goto error;
|
||||
nfc_close(pnd);
|
||||
nfc_exit(context);
|
||||
exit(EXIT_FAILURE);
|
||||
} else if (res == 0) {
|
||||
ERR("No SAM found.");
|
||||
goto error;
|
||||
nfc_close(pnd);
|
||||
nfc_exit(context);
|
||||
exit(EXIT_FAILURE);
|
||||
} else if (res == 1) {
|
||||
printf("The following ISO14443A tag (SAM) was found:\n");
|
||||
print_nfc_target(nt, true);
|
||||
} else {
|
||||
ERR("%s", "More than one ISO14442 tag found as SAM.");
|
||||
goto error;
|
||||
nfc_close(pnd);
|
||||
nfc_exit(context);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
|
@ -172,7 +185,9 @@ main(int argc, const char *argv[])
|
|||
// FIXME Its a private pn53x function
|
||||
if (pn532_SAMConfiguration(pnd, mode, 0) < 0) {
|
||||
nfc_perror(pnd, "pn53x_SAMConfiguration");
|
||||
goto error;
|
||||
nfc_close(pnd);
|
||||
nfc_exit(context);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
uint8_t abtRx[MAX_FRAME_LEN];
|
||||
|
||||
|
|
@ -195,7 +210,9 @@ main(int argc, const char *argv[])
|
|||
printf("Please note that NFC device (configured as target) stay in target mode until it receive RATS, ATR_REQ or proprietary command.\n");
|
||||
if (nfc_target_init(pnd, &nt, abtRx, sizeof(abtRx), 0) < 0) {
|
||||
nfc_perror(pnd, "nfc_target_init");
|
||||
return EXIT_FAILURE;
|
||||
nfc_close(pnd);
|
||||
nfc_exit(context);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
// wait_one_minute ();
|
||||
}
|
||||
|
|
@ -204,15 +221,12 @@ main(int argc, const char *argv[])
|
|||
// This should not happend... nothing to do.
|
||||
break;
|
||||
}
|
||||
ret = EXIT_SUCCESS;
|
||||
|
||||
error:
|
||||
// Disconnect from the SAM
|
||||
pn532_SAMConfiguration(pnd, PSM_NORMAL, -1);
|
||||
|
||||
// Close NFC device
|
||||
nfc_close(pnd);
|
||||
nfc_exit(context);
|
||||
|
||||
exit(ret);
|
||||
exit(EXIT_SUCCESS);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -79,7 +79,7 @@ int main(int argc, const char *argv[])
|
|||
if (argc >= 2) {
|
||||
if ((input = fopen(argv[1], "r")) == NULL) {
|
||||
ERR("%s", "Cannot open file.");
|
||||
return EXIT_FAILURE;
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -94,7 +94,8 @@ int main(int argc, const char *argv[])
|
|||
if (input != NULL) {
|
||||
fclose(input);
|
||||
}
|
||||
return EXIT_FAILURE;
|
||||
nfc_exit(context);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
printf("NFC reader: %s opened\n", nfc_device_get_name(pnd));
|
||||
|
|
@ -103,6 +104,8 @@ int main(int argc, const char *argv[])
|
|||
if (input != NULL) {
|
||||
fclose(input);
|
||||
}
|
||||
nfc_close(pnd);
|
||||
nfc_exit(context);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
|
|
@ -205,5 +208,5 @@ int main(int argc, const char *argv[])
|
|||
}
|
||||
nfc_close(pnd);
|
||||
nfc_exit(context);
|
||||
return EXIT_SUCCESS;
|
||||
exit(EXIT_SUCCESS);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue