nfc-barcode: add options -r & -n

This commit is contained in:
Philippe Teuwen 2017-05-16 14:35:58 +02:00
parent 9f4290b61b
commit 9f1a68530a
2 changed files with 78 additions and 41 deletions

View file

@ -14,6 +14,14 @@ Decode content, if possible.
.B -v
Verbose.
.B -r
Keep RF field on (for slow devices such as ASK LoGO).
.B -n N
Try up to
.B N
times (default=1, max 255).
.SH BUGS
Please report any bugs on the
.B libnfc

View file

@ -56,6 +56,8 @@ static nfc_device *pnd;
bool verbose = false;
bool decode = false;
bool rfoff = true;
uint8_t n = 1;
static void
print_usage(char *argv[])
@ -65,6 +67,8 @@ print_usage(char *argv[])
printf("\t-h\tHelp. Print this message.\n");
printf("\t-q\tVerbose mode.\n");
printf("\t-d\tDecode content.\n");
printf("\t-r\tKeep RF field on (for slow devices such as ASK LoGO).\n");
printf("\t-n\tTry up to n times (default=1, max 255).\n");
}
static int
@ -202,6 +206,14 @@ main(int argc, char *argv[])
verbose = true;
} else if (0 == strcmp(argv[arg], "-d")) {
decode = true;
} else if (0 == strcmp(argv[arg], "-r")) {
rfoff = false;
} else if (0 == strcmp(argv[arg], "-n")) {
arg++;
int tmpn = atoi(argv[arg]);
if ((tmpn > 0) && (tmpn < 256)) {
n = tmpn;
}
} else {
ERR("%s is not supported option.", argv[arg]);
print_usage(argv);
@ -235,7 +247,7 @@ main(int argc, char *argv[])
printf("NFC reader: %s opened\n\n", nfc_device_get_name(pnd));
if ((nfc_device_set_property_bool(pnd, NP_ACTIVATE_FIELD, false) < 0) ||
if ((rfoff && (nfc_device_set_property_bool(pnd, NP_ACTIVATE_FIELD, false) < 0)) ||
(nfc_device_set_property_bool(pnd, NP_HANDLE_CRC, false) < 0) ||
(nfc_device_set_property_bool(pnd, NP_HANDLE_PARITY, false) < 0)) {
nfc_perror(pnd, "nfc_device_set_property_bool");
@ -243,40 +255,53 @@ main(int argc, char *argv[])
nfc_exit(context);
exit(EXIT_FAILURE);
}
for (; n > 0; n--) {
int res;
if ((res = nfc_initiator_transceive_bits(pnd, NULL, 0, NULL, abtRx, sizeof(abtRx), abtRxPar)) < 0) {
if (verbose)
nfc_perror(pnd, "nfc_initiator_transceive_bits");
if (n == 1) {
printf("No NFC Barcode found\n");
nfc_close(pnd);
nfc_exit(context);
exit(EXIT_FAILURE);
}
nfc_close(pnd);
nfc_exit(context);
continue;
}
if (verbose)
print_hex_par(abtRx, res, abtRxPar);
res = bits2barcode(abtRx, res, abtRxPar, pbtBarcode, sizeof(pbtBarcode));
if (res % 128 != 0) {
printf("Error, NFC Barcode seems incomplete, received %u bits\n", res);
if (verbose) {
printf("Error, NFC Barcode seems incomplete, received %u bits\n", res);
print_hex_bits(pbtBarcode, res);
}
if (n == 1) {
printf("Error, NFC Barcode seems incomplete, received %u bits\n", res);
nfc_close(pnd);
nfc_exit(context);
exit(EXIT_FAILURE);
}
continue;
}
if (validate_crc(pbtBarcode, res)) {
if (verbose) {
printf("CRC correct\n");
}
} else {
if (n == 1) {
printf("CRC error\n");
if (verbose) {
print_hex_bits(pbtBarcode, res);
}
nfc_close(pnd);
nfc_exit(context);
exit(EXIT_FAILURE);
}
continue;
}
if (verbose || ! decode) {
for (uint8_t i = 0; i < res / 8; i++) {
@ -287,5 +312,9 @@ main(int argc, char *argv[])
if (decode) {
decode_barcode(pbtBarcode, res);
}
break;
}
nfc_close(pnd);
nfc_exit(context);
exit(EXIT_SUCCESS);
}