Skip to content

Commit

Permalink
nfc-barcode: add options -r & -n
Browse files Browse the repository at this point in the history
  • Loading branch information
doegox committed May 16, 2017
1 parent 9f4290b commit 9f1a685
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 37 deletions.
8 changes: 8 additions & 0 deletions examples/nfc-barcode.1
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
103 changes: 66 additions & 37 deletions examples/nfc-barcode.c
Original file line number Diff line number Diff line change
Expand Up @@ -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[])
Expand All @@ -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
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -235,57 +247,74 @@ 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");
nfc_close(pnd);
nfc_exit(context);
exit(EXIT_FAILURE);
}
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");
printf("No NFC Barcode found\n");
nfc_close(pnd);
nfc_exit(context);
exit(EXIT_FAILURE);
}
nfc_close(pnd);
nfc_exit(context);
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) {
print_hex_bits(pbtBarcode, res);
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);
}
continue;
}
exit(EXIT_FAILURE);
}
if (verbose)
print_hex_par(abtRx, res, abtRxPar);
res = bits2barcode(abtRx, res, abtRxPar, pbtBarcode, sizeof(pbtBarcode));

if (validate_crc(pbtBarcode, res)) {
if (verbose) {
printf("CRC correct\n");
if (res % 128 != 0) {
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;
}
} else {
printf("CRC error\n");
if (verbose) {
print_hex_bits(pbtBarcode, res);
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;
}
exit(EXIT_FAILURE);
}

if (verbose || ! decode) {
for (uint8_t i = 0; i < res / 8; i++) {
printf("%02x", pbtBarcode[i]);
if (verbose || ! decode) {
for (uint8_t i = 0; i < res / 8; i++) {
printf("%02x", pbtBarcode[i]);
}
printf("\n");
}
printf("\n");
}
if (decode) {
decode_barcode(pbtBarcode, res);
if (decode) {
decode_barcode(pbtBarcode, res);
}
break;
}
nfc_close(pnd);
nfc_exit(context);
exit(EXIT_SUCCESS);
}

0 comments on commit 9f1a685

Please sign in to comment.