master moved from 73c84c214dd0 to a7649da2b0d5
2 new revisions:
Revision: 2aa4b99b7bbb
Author: Philippe Teuwen <
yob...@gmail.com>
Date: Mon Feb 17 22:57:59 2014 UTC
Log: nfc-read-forum-tag3: full parsing of NDEF Attribute Block
http://code.google.com/p/libnfc/source/detail?r=2aa4b99b7bbb
Revision: a7649da2b0d5
Author: Philippe Teuwen <
yob...@gmail.com>
Date: Mon Feb 17 23:01:43 2014 UTC
Log: nfc-read-forum-tag3: add -q and document -o -
http://code.google.com/p/libnfc/source/detail?r=a7649da2b0d5
==============================================================================
Revision: 2aa4b99b7bbb
Author: Philippe Teuwen <
yob...@gmail.com>
Date: Mon Feb 17 22:57:59 2014 UTC
Log: nfc-read-forum-tag3: full parsing of NDEF Attribute Block
http://code.google.com/p/libnfc/source/detail?r=2aa4b99b7bbb
Modified:
/utils/nfc-read-forum-tag3.c
=======================================
--- /utils/nfc-read-forum-tag3.c Mon Feb 17 22:09:15 2014 UTC
+++ /utils/nfc-read-forum-tag3.c Mon Feb 17 22:57:59 2014 UTC
@@ -292,15 +292,50 @@
exit(EXIT_FAILURE);
}
+ fprintf(message_stream, "NDEF Attribute Block:\n");
const int ndef_major_version = (data[0] & 0xf0) >> 4;
const int ndef_minor_version = (data[0] & 0x0f);
- fprintf(message_stream, "NDEF Mapping version: %d.%d\n",
ndef_major_version, ndef_minor_version);
+ fprintf(message_stream, "* Mapping version: %d.%d\n",
ndef_major_version, ndef_minor_version);
- const int available_block_count = (data[3] << 8) + data[4];
- fprintf(message_stream, "NFC Forum Tag Type 3 capacity: %d bytes\n",
available_block_count * 16);
+ const int ndef_nbr = data[1];
+ fprintf(message_stream, "* Maximum nr of blocks to read by Check
Command: %3d block%s\n", ndef_nbr, ndef_nbr > 1 ? "s" : "");
+
+ const int ndef_nbw = data[2];
+ fprintf(message_stream, "* Maximum nr of blocks to write by Update
Command: %3d block%s\n", ndef_nbw, ndef_nbw > 1 ? "s" : "");
+
+ const int ndef_nmaxb = (data[3] << 8) + data[4];
+ fprintf(message_stream, "* Maximum nr of blocks available for NDEF
data: %3d block%s (%d bytes)\n", ndef_nmaxb, ndef_nmaxb > 1 ? "s" : "",
ndef_nmaxb * 16);
+
+ const int ndef_writeflag = data[9];
+ fprintf(message_stream, "* NDEF writing state: ");
+ switch (ndef_writeflag) {
+ case 0x00:
+ fprintf(message_stream, "finished (0x00)\n");
+ break;
+ case 0x0f:
+ fprintf(message_stream, "in progress (0x0F)\n");
+ break;
+ default:
+ fprintf(message_stream, "invalid (0x%02X)\n", ndef_writeflag);
+ break;
+ }
+
+ const int ndef_rwflag = data[10];
+ fprintf(message_stream, "* NDEF Access Attribute: ");
+ switch (ndef_rwflag) {
+ case 0x00:
+ fprintf(message_stream, "Read only (0x00)\n");
+ break;
+ case 0x01:
+ fprintf(message_stream, "Read/Write (0x01)\n");
+ break;
+ default:
+ fprintf(message_stream, "invalid (0x%02X)\n", ndef_rwflag);
+ break;
+ }
uint32_t ndef_data_len = (data[11] << 16) + (data[12] << 8) + data[13];
- fprintf(message_stream, "NDEF data length: %d bytes\n", ndef_data_len);
+ fprintf(message_stream, "* NDEF message length: %d bytes\n",
ndef_data_len);
uint16_t ndef_calculated_checksum = 0;
for (size_t n = 0; n < 14; n++)
@@ -308,15 +343,13 @@
const uint16_t ndef_checksum = (data[14] << 8) + data[15];
if (ndef_calculated_checksum != ndef_checksum) {
- fprintf(stderr, "NDEF CRC does not match with calculated one\n");
- fclose(ndef_stream);
- nfc_close(pnd);
- nfc_exit(context);
- exit(EXIT_FAILURE);
+ fprintf(message_stream, "* Checksum: fail (0x%04X != 0x%04X)\n",
ndef_calculated_checksum, ndef_checksum);
+ } else {
+ fprintf(message_stream, "* Checksum: ok (0x%04X)\n", ndef_checksum);
}
if (!ndef_data_len) {
- fprintf(stderr, "Empty NFC Forum Tag Type 3\n");
+ fprintf(stderr, "Error: empty NFC Forum Tag Type 3, nothing to
read!\n");
fclose(ndef_stream);
nfc_close(pnd);
nfc_exit(context);
@@ -345,6 +378,8 @@
nfc_close(pnd);
nfc_exit(context);
exit(EXIT_FAILURE);
+ } else {
+ fprintf(stderr, "%i bytes written to %s\n", ndef_data_len,
ndef_output);
}
fclose(ndef_stream);
==============================================================================
Revision: a7649da2b0d5
Author: Philippe Teuwen <
yob...@gmail.com>
Date: Mon Feb 17 23:01:43 2014 UTC
Log: nfc-read-forum-tag3: add -q and document -o -
http://code.google.com/p/libnfc/source/detail?r=a7649da2b0d5
Modified:
/utils/nfc-read-forum-tag3.1
/utils/nfc-read-forum-tag3.c
=======================================
--- /utils/nfc-read-forum-tag3.1 Tue Nov 22 15:23:18 2011 UTC
+++ /utils/nfc-read-forum-tag3.1 Mon Feb 17 23:01:43 2014 UTC
@@ -3,6 +3,9 @@
nfc-read-forum-tag3 \- Extract NDEF Message from a NFC Forum Tag Type 3
.SH SYNOPSIS
.B nfc-read-forum-tag3
+.RI [
+.RI \fR\fB\-q\fR
+.RI ]
.RI \fR\fB\-o\fR
.IR FILE
.SH DESCRIPTION
@@ -12,8 +15,15 @@
.
.SH OPTIONS
\fR\fB\-o\fR
-output extracted NDEF Message to
.IR FILE
+: output extracted NDEF Message to
+.IR FILE
+(use
+\fR\fB\-o \-\fR
+to output to stdout)
+
+\fR\fB\-q\fR
+: be quiet, don't display Attribute Block parsing info
.SH BUGS
Please report any bugs on the
=======================================
--- /utils/nfc-read-forum-tag3.c Mon Feb 17 22:57:59 2014 UTC
+++ /utils/nfc-read-forum-tag3.c Mon Feb 17 23:01:43 2014 UTC
@@ -72,9 +72,11 @@
static void
print_usage(char *progname)
{
- fprintf(stderr, "usage: %s -o FILE\n", progname);
+ fprintf(stderr, "usage: %s [-q] -o FILE\n", progname);
fprintf(stderr, "\nOptions:\n");
- fprintf(stderr, " -o Extract NDEF message if available in FILE\n");
+ fprintf(stderr, " -o FILE Extract NDEF message if available in
FILE\n");
+ fprintf(stderr, " -o - Extract NDEF message if available to
stdout\n");
+ fprintf(stderr, " -q Be quiet, don't display Attribute Block
parsing info\n");
}
static void stop_select(int sig)
@@ -169,13 +171,17 @@
(void)argv;
int ch;
+ bool quiet = false;
char *ndef_output = NULL;
- while ((ch = getopt(argc, argv, "ho:")) != -1) {
+ while ((ch = getopt(argc, argv, "hqo:")) != -1) {
switch (ch) {
case 'h':
print_usage(argv[0]);
exit(EXIT_SUCCESS);
break;
+ case 'q':
+ quiet = true;
+ break;
case 'o':
ndef_output = optarg;
break;
@@ -219,7 +225,9 @@
exit(EXIT_FAILURE);
}
- fprintf(message_stream, "NFC device: %s opened\n",
nfc_device_get_name(pnd));
+ if (!quiet) {
+ fprintf(message_stream, "NFC device: %s opened\n",
nfc_device_get_name(pnd));
+ }
nfc_modulation nm = {
.nmt = NMT_FELICA,
@@ -237,7 +245,10 @@
nfc_exit(context);
exit(EXIT_FAILURE);
}
- fprintf(message_stream, "Place your NFC Forum Tag Type 3 in the
field...\n");
+
+ if (!quiet) {
+ fprintf(message_stream, "Place your NFC Forum Tag Type 3 in the
field...\n");
+ }
// Polling payload (SENSF_REQ) must be present (see NFC Digital Protol)
const uint8_t *pbtSensfReq = (uint8_t *)"\x00\xff\xff\x01\x00";
@@ -292,60 +303,63 @@
exit(EXIT_FAILURE);
}
- fprintf(message_stream, "NDEF Attribute Block:\n");
const int ndef_major_version = (data[0] & 0xf0) >> 4;
const int ndef_minor_version = (data[0] & 0x0f);
- fprintf(message_stream, "* Mapping version: %d.%d\n",
ndef_major_version, ndef_minor_version);
-
const int ndef_nbr = data[1];
- fprintf(message_stream, "* Maximum nr of blocks to read by Check
Command: %3d block%s\n", ndef_nbr, ndef_nbr > 1 ? "s" : "");
-
const int ndef_nbw = data[2];
- fprintf(message_stream, "* Maximum nr of blocks to write by Update
Command: %3d block%s\n", ndef_nbw, ndef_nbw > 1 ? "s" : "");
-
const int ndef_nmaxb = (data[3] << 8) + data[4];
- fprintf(message_stream, "* Maximum nr of blocks available for NDEF
data: %3d block%s (%d bytes)\n", ndef_nmaxb, ndef_nmaxb > 1 ? "s" : "",
ndef_nmaxb * 16);
-
const int ndef_writeflag = data[9];
- fprintf(message_stream, "* NDEF writing state: ");
- switch (ndef_writeflag) {
- case 0x00:
- fprintf(message_stream, "finished (0x00)\n");
- break;
- case 0x0f:
- fprintf(message_stream, "in progress (0x0F)\n");
- break;
- default:
- fprintf(message_stream, "invalid (0x%02X)\n", ndef_writeflag);
- break;
- }
-
const int ndef_rwflag = data[10];
- fprintf(message_stream, "* NDEF Access Attribute: ");
- switch (ndef_rwflag) {
- case 0x00:
- fprintf(message_stream, "Read only (0x00)\n");
- break;
- case 0x01:
- fprintf(message_stream, "Read/Write (0x01)\n");
- break;
- default:
- fprintf(message_stream, "invalid (0x%02X)\n", ndef_rwflag);
- break;
- }
-
uint32_t ndef_data_len = (data[11] << 16) + (data[12] << 8) + data[13];
- fprintf(message_stream, "* NDEF message length: %d bytes\n",
ndef_data_len);
-
uint16_t ndef_calculated_checksum = 0;
for (size_t n = 0; n < 14; n++)
ndef_calculated_checksum += data[n];
+ const uint16_t ndef_checksum = (data[14] << 8) + data[15];
+
+ if (!quiet) {
+ fprintf(message_stream, "NDEF Attribute Block:\n");
+ fprintf(message_stream, "* Mapping version: %d.%d\n",
ndef_major_version, ndef_minor_version);
+ fprintf(message_stream, "* Maximum nr of blocks to read by Check
Command: %3d block%s\n", ndef_nbr, ndef_nbr > 1 ? "s" : "");
+ fprintf(message_stream, "* Maximum nr of blocks to write by Update
Command: %3d block%s\n", ndef_nbw, ndef_nbw > 1 ? "s" : "");
+ fprintf(message_stream, "* Maximum nr of blocks available for NDEF
data: %3d block%s (%d bytes)\n", ndef_nmaxb, ndef_nmaxb > 1 ? "s" : "",
ndef_nmaxb * 16);
+ fprintf(message_stream, "* NDEF writing state: ");
+ switch (ndef_writeflag) {
+ case 0x00:
+ fprintf(message_stream, "finished (0x00)\n");
+ break;
+ case 0x0f:
+ fprintf(message_stream, "in progress (0x0F)\n");
+ break;
+ default:
+ fprintf(message_stream, "invalid (0x%02X)\n", ndef_writeflag);
+ break;
+ }
+ fprintf(message_stream, "* NDEF Access Attribute: ");
+ switch (ndef_rwflag) {
+ case 0x00:
+ fprintf(message_stream, "Read only (0x00)\n");
+ break;
+ case 0x01:
+ fprintf(message_stream, "Read/Write (0x01)\n");
+ break;
+ default:
+ fprintf(message_stream, "invalid (0x%02X)\n", ndef_rwflag);
+ break;
+ }
+ fprintf(message_stream, "* NDEF message length: %d bytes\n",
ndef_data_len);
+ if (ndef_calculated_checksum != ndef_checksum) {
+ fprintf(message_stream, "* Checksum: fail (0x%04X != 0x%04X)\n",
ndef_calculated_checksum, ndef_checksum);
+ } else {
+ fprintf(message_stream, "* Checksum: ok (0x%04X)\n", ndef_checksum);
+ }
+ }
- const uint16_t ndef_checksum = (data[14] << 8) + data[15];
if (ndef_calculated_checksum != ndef_checksum) {
- fprintf(message_stream, "* Checksum: fail (0x%04X != 0x%04X)\n",
ndef_calculated_checksum, ndef_checksum);
- } else {
- fprintf(message_stream, "* Checksum: ok (0x%04X)\n", ndef_checksum);
+ fprintf(stderr, "Error: Checksum failed! Exiting now.\n");
+ fclose(ndef_stream);
+ nfc_close(pnd);
+ nfc_exit(context);
+ exit(EXIT_FAILURE);
}
if (!ndef_data_len) {
@@ -373,13 +387,15 @@
}
if (fwrite(data, 1, ndef_data_len, ndef_stream) != ndef_data_len) {
- fprintf(stderr, "Could not write to file.\n");
+ fprintf(stderr, "Error: could not write to file.\n");
fclose(ndef_stream);
nfc_close(pnd);
nfc_exit(context);
exit(EXIT_FAILURE);
} else {
- fprintf(stderr, "%i bytes written to %s\n", ndef_data_len,
ndef_output);
+ if (!quiet) {
+ fprintf(stderr, "%i bytes written to %s\n", ndef_data_len,
ndef_output);
+ }
}
fclose(ndef_stream);