From c72846e3c62e320ab2d8b98ac1cb8f23739c776b Mon Sep 17 00:00:00 2001 From: Alex Lian Date: Sun, 3 Mar 2013 23:40:07 -0500 Subject: [PATCH] Convert by value passing of nfc_target to pointer for str_nfc_target and nfc_initiator_target_is_present This becomes more consistent with all other pass by pointer of most structures. Additionally, this should lessen stack memory usage, as building strings with str_nfc_target would push the target (283 bytes) plus then a copy of the info objects (up to 275) onto the stack as it dives into the sprintf functions. Lastly, this makes my attempt at a .NET wrapper easier, as I can make passing by pointer work, but passing by value seems to bomb on the interop right now. --- examples/nfc-dep-initiator.c | 2 +- examples/nfc-dep-target.c | 2 +- examples/nfc-emulate-tag.c | 2 +- examples/nfc-poll.c | 2 +- include/nfc/nfc.h | 4 +- libnfc/chips/pn53x.c | 4 +- libnfc/chips/pn53x.h | 2 +- libnfc/nfc-internal.h | 2 +- libnfc/nfc.c | 8 +- libnfc/target-subr.c | 236 ++++++++++++++++++----------------- libnfc/target-subr.h | 18 +-- utils/nfc-list.c | 16 +-- utils/nfc-mfclassic.c | 2 +- utils/nfc-relay-picc.c | 4 +- utils/nfc-utils.c | 4 +- utils/nfc-utils.h | 2 +- 16 files changed, 156 insertions(+), 154 deletions(-) diff --git a/examples/nfc-dep-initiator.c b/examples/nfc-dep-initiator.c index c095a4b..36033a8 100644 --- a/examples/nfc-dep-initiator.c +++ b/examples/nfc-dep-initiator.c @@ -103,7 +103,7 @@ main(int argc, const char *argv[]) nfc_exit(context); exit(EXIT_FAILURE); } - print_nfc_target(nt, false); + print_nfc_target(&nt, false); printf("Sending: %s\n", abtTx); int res; diff --git a/examples/nfc-dep-target.c b/examples/nfc-dep-target.c index 1aef22d..fe1bd38 100644 --- a/examples/nfc-dep-target.c +++ b/examples/nfc-dep-target.c @@ -126,7 +126,7 @@ main(int argc, const char *argv[]) signal(SIGINT, stop_dep_communication); printf("NFC device will now act as: "); - print_nfc_target(nt, false); + print_nfc_target(&nt, false); printf("Waiting for initiator request...\n"); if ((szRx = nfc_target_init(pnd, &nt, abtRx, sizeof(abtRx), 0)) < 0) { diff --git a/examples/nfc-emulate-tag.c b/examples/nfc-emulate-tag.c index 42c9a10..94368c8 100644 --- a/examples/nfc-emulate-tag.c +++ b/examples/nfc-emulate-tag.c @@ -266,7 +266,7 @@ main(int argc, char *argv[]) */ printf("%s will emulate this ISO14443-A tag:\n", argv[0]); - print_nfc_target(nt, true); + print_nfc_target(&nt, true); // Switch off NP_EASY_FRAMING if target is not ISO14443-4 nfc_device_set_property_bool(pnd, NP_EASY_FRAMING, (nt.nti.nai.btSak & SAK_ISO14443_4_COMPLIANT)); diff --git a/examples/nfc-poll.c b/examples/nfc-poll.c index b5f29ee..40cd87d 100644 --- a/examples/nfc-poll.c +++ b/examples/nfc-poll.c @@ -137,7 +137,7 @@ main(int argc, const char *argv[]) } if (res > 0) { - print_nfc_target(nt, verbose); + print_nfc_target(&nt, verbose); } else { printf("No target found.\n"); } diff --git a/include/nfc/nfc.h b/include/nfc/nfc.h index 74596b5..6862db0 100644 --- a/include/nfc/nfc.h +++ b/include/nfc/nfc.h @@ -101,7 +101,7 @@ extern "C" { NFC_EXPORT int nfc_initiator_transceive_bits(nfc_device *pnd, const uint8_t *pbtTx, const size_t szTxBits, const uint8_t *pbtTxPar, uint8_t *pbtRx, const size_t szRx, uint8_t *pbtRxPar); NFC_EXPORT int nfc_initiator_transceive_bytes_timed(nfc_device *pnd, const uint8_t *pbtTx, const size_t szTx, uint8_t *pbtRx, const size_t szRx, uint32_t *cycles); NFC_EXPORT int nfc_initiator_transceive_bits_timed(nfc_device *pnd, const uint8_t *pbtTx, const size_t szTxBits, const uint8_t *pbtTxPar, uint8_t *pbtRx, const size_t szRx, uint8_t *pbtRxPar, uint32_t *cycles); - NFC_EXPORT int nfc_initiator_target_is_present(nfc_device *pnd, const nfc_target nt); + NFC_EXPORT int nfc_initiator_target_is_present(nfc_device *pnd, const nfc_target *pnt); /* NFC target: act as tag (i.e. MIFARE Classic) or NFC target device. */ NFC_EXPORT int nfc_target_init(nfc_device *pnd, nfc_target *pnt, uint8_t *pbtRx, const size_t szRx, int timeout); @@ -138,7 +138,7 @@ extern "C" { /* String converter functions */ NFC_EXPORT const char *str_nfc_modulation_type(const nfc_modulation_type nmt); NFC_EXPORT const char *str_nfc_baud_rate(const nfc_baud_rate nbr); - NFC_EXPORT int str_nfc_target(char **buf, const nfc_target nt, bool verbose); + NFC_EXPORT int str_nfc_target(char **buf, const nfc_target *pnt, bool verbose); /* Error codes */ /** @ingroup error diff --git a/libnfc/chips/pn53x.c b/libnfc/chips/pn53x.c index 9b5946c..96c4bda 100644 --- a/libnfc/chips/pn53x.c +++ b/libnfc/chips/pn53x.c @@ -1668,10 +1668,10 @@ pn53x_initiator_deselect_target(struct nfc_device *pnd) } int -pn53x_initiator_target_is_present(struct nfc_device *pnd, const nfc_target nt) +pn53x_initiator_target_is_present(struct nfc_device *pnd, const nfc_target *pnt) { // Check if the argument target nt is equals to current saved target - if (!pn53x_current_target_is(pnd, &nt)) { + if (!pn53x_current_target_is(pnd, pnt)) { return NFC_ETGRELEASED; } diff --git a/libnfc/chips/pn53x.h b/libnfc/chips/pn53x.h index 8acf2ae..be67ef8 100644 --- a/libnfc/chips/pn53x.h +++ b/libnfc/chips/pn53x.h @@ -337,7 +337,7 @@ int pn53x_initiator_transceive_bits_timed(struct nfc_device *pnd, const uint8 int pn53x_initiator_transceive_bytes_timed(struct nfc_device *pnd, const uint8_t *pbtTx, const size_t szTx, uint8_t *pbtRx, const size_t szRx, uint32_t *cycles); int pn53x_initiator_deselect_target(struct nfc_device *pnd); -int pn53x_initiator_target_is_present(struct nfc_device *pnd, const nfc_target nt); +int pn53x_initiator_target_is_present(struct nfc_device *pnd, const nfc_target *pnt); // NFC device as Target functions int pn53x_target_init(struct nfc_device *pnd, nfc_target *pnt, uint8_t *pbtRx, const size_t szRxLen, int timeout); diff --git a/libnfc/nfc-internal.h b/libnfc/nfc-internal.h index 30e4947..1a5eec4 100644 --- a/libnfc/nfc-internal.h +++ b/libnfc/nfc-internal.h @@ -131,7 +131,7 @@ struct nfc_driver { int (*initiator_transceive_bits)(struct nfc_device *pnd, const uint8_t *pbtTx, const size_t szTxBits, const uint8_t *pbtTxPar, uint8_t *pbtRx, uint8_t *pbtRxPar); int (*initiator_transceive_bytes_timed)(struct nfc_device *pnd, const uint8_t *pbtTx, const size_t szTx, uint8_t *pbtRx, const size_t szRx, uint32_t *cycles); int (*initiator_transceive_bits_timed)(struct nfc_device *pnd, const uint8_t *pbtTx, const size_t szTxBits, const uint8_t *pbtTxPar, uint8_t *pbtRx, uint8_t *pbtRxPar, uint32_t *cycles); - int (*initiator_target_is_present)(struct nfc_device *pnd, const nfc_target nt); + int (*initiator_target_is_present)(struct nfc_device *pnd, const nfc_target *pnt); int (*target_init)(struct nfc_device *pnd, nfc_target *pnt, uint8_t *pbtRx, const size_t szRx, int timeout); int (*target_send_bytes)(struct nfc_device *pnd, const uint8_t *pbtTx, const size_t szTx, int timeout); diff --git a/libnfc/nfc.c b/libnfc/nfc.c index 53f68be..473af3f 100644 --- a/libnfc/nfc.c +++ b/libnfc/nfc.c @@ -821,9 +821,9 @@ nfc_initiator_transceive_bytes_timed(nfc_device *pnd, * @warning To run the test, one or more commands will be sent to target */ int -nfc_initiator_target_is_present(nfc_device *pnd, const nfc_target nt) +nfc_initiator_target_is_present(nfc_device *pnd, const nfc_target *pnt) { - HAL(initiator_target_is_present, pnd, nt); + HAL(initiator_target_is_present, pnd, pnt); } /** @ingroup initiator @@ -1287,12 +1287,12 @@ str_nfc_modulation_type(const nfc_modulation_type nmt) * @warning *buf must be freed using nfc_free() */ int -str_nfc_target(char **buf, const nfc_target nt, bool verbose) +str_nfc_target(char **buf, const nfc_target *pnt, bool verbose) { *buf = malloc(4096); if (! *buf) return NFC_ESOFT; (*buf)[0] = '\0'; - snprint_nfc_target(*buf, 4096, nt, verbose); + snprint_nfc_target(*buf, 4096, pnt, verbose); return strlen(*buf); } diff --git a/libnfc/target-subr.c b/libnfc/target-subr.c index cdc9028..b46d6bc 100644 --- a/libnfc/target-subr.c +++ b/libnfc/target-subr.c @@ -133,14 +133,14 @@ snprint_hex(char *dst, size_t size, const uint8_t *pbtData, const size_t szBytes #define SAK_ISO18092_COMPLIANT 0x40 void -snprint_nfc_iso14443a_info(char *dst, size_t size, const nfc_iso14443a_info nai, bool verbose) +snprint_nfc_iso14443a_info(char *dst, size_t size, const nfc_iso14443a_info *pnai, bool verbose) { int off = 0; off += snprintf(dst + off, size - off, " ATQA (SENS_RES): "); - off += snprint_hex(dst + off, size - off, nai.abtAtqa, 2); + off += snprint_hex(dst + off, size - off, pnai->abtAtqa, 2); if (verbose) { off += snprintf(dst + off, size - off, "* UID size: "); - switch ((nai.abtAtqa[1] & 0xc0) >> 6) { + switch ((pnai->abtAtqa[1] & 0xc0) >> 6) { case 0: off += snprintf(dst + off, size - off, "single\n"); break; @@ -155,7 +155,7 @@ snprint_nfc_iso14443a_info(char *dst, size_t size, const nfc_iso14443a_info nai, break; } off += snprintf(dst + off, size - off, "* bit frame anticollision "); - switch (nai.abtAtqa[1] & 0x1f) { + switch (pnai->abtAtqa[1] & 0x1f) { case 0x01: case 0x02: case 0x04: @@ -168,42 +168,42 @@ snprint_nfc_iso14443a_info(char *dst, size_t size, const nfc_iso14443a_info nai, break; } } - off += snprintf(dst + off, size - off, " UID (NFCID%c): ", (nai.abtUid[0] == 0x08 ? '3' : '1')); - off += snprint_hex(dst + off, size - off, nai.abtUid, nai.szUidLen); + off += snprintf(dst + off, size - off, " UID (NFCID%c): ", (pnai->abtUid[0] == 0x08 ? '3' : '1')); + off += snprint_hex(dst + off, size - off, pnai->abtUid, pnai->szUidLen); if (verbose) { - if (nai.abtUid[0] == 0x08) { + if (pnai->abtUid[0] == 0x08) { off += snprintf(dst + off, size - off, "* Random UID\n"); } } off += snprintf(dst + off, size - off, " SAK (SEL_RES): "); - off += snprint_hex(dst + off, size - off, &nai.btSak, 1); + off += snprint_hex(dst + off, size - off, &pnai->btSak, 1); if (verbose) { - if (nai.btSak & SAK_UID_NOT_COMPLETE) { + if (pnai->btSak & SAK_UID_NOT_COMPLETE) { off += snprintf(dst + off, size - off, "* Warning! Cascade bit set: UID not complete\n"); } - if (nai.btSak & SAK_ISO14443_4_COMPLIANT) { + if (pnai->btSak & SAK_ISO14443_4_COMPLIANT) { off += snprintf(dst + off, size - off, "* Compliant with ISO/IEC 14443-4\n"); } else { off += snprintf(dst + off, size - off, "* Not compliant with ISO/IEC 14443-4\n"); } - if (nai.btSak & SAK_ISO18092_COMPLIANT) { + if (pnai->btSak & SAK_ISO18092_COMPLIANT) { off += snprintf(dst + off, size - off, "* Compliant with ISO/IEC 18092\n"); } else { off += snprintf(dst + off, size - off, "* Not compliant with ISO/IEC 18092\n"); } } - if (nai.szAtsLen) { + if (pnai->szAtsLen) { off += snprintf(dst + off, size - off, " ATS: "); - off += snprint_hex(dst + off, size - off, nai.abtAts, nai.szAtsLen); + off += snprint_hex(dst + off, size - off, pnai->abtAts, pnai->szAtsLen); } - if (nai.szAtsLen && verbose) { + if (pnai->szAtsLen && verbose) { // Decode ATS according to ISO/IEC 14443-4 (5.2 Answer to select) const int iMaxFrameSizes[] = { 16, 24, 32, 40, 48, 64, 96, 128, 256 }; - off += snprintf(dst + off, size - off, "* Max Frame Size accepted by PICC: %d bytes\n", iMaxFrameSizes[nai.abtAts[0] & 0x0F]); + off += snprintf(dst + off, size - off, "* Max Frame Size accepted by PICC: %d bytes\n", iMaxFrameSizes[pnai->abtAts[0] & 0x0F]); size_t offset = 1; - if (nai.abtAts[0] & 0x10) { // TA(1) present - uint8_t TA = nai.abtAts[offset]; + if (pnai->abtAts[0] & 0x10) { // TA(1) present + uint8_t TA = pnai->abtAts[offset]; offset++; off += snprintf(dst + off, size - off, "* Bit Rate Capability:\n"); if (TA == 0) { @@ -234,8 +234,8 @@ snprint_nfc_iso14443a_info(char *dst, size_t size, const nfc_iso14443a_info nai, off += snprintf(dst + off, size - off, " * ERROR unknown value\n"); } } - if (nai.abtAts[0] & 0x20) { // TB(1) present - uint8_t TB = nai.abtAts[offset]; + if (pnai->abtAts[0] & 0x20) { // TB(1) present + uint8_t TB = pnai->abtAts[offset]; offset++; off += snprintf(dst + off, size - off, "* Frame Waiting Time: %.4g ms\n", 256.0 * 16.0 * (1 << ((TB & 0xf0) >> 4)) / 13560.0); if ((TB & 0x0f) == 0) { @@ -244,8 +244,8 @@ snprint_nfc_iso14443a_info(char *dst, size_t size, const nfc_iso14443a_info nai, off += snprintf(dst + off, size - off, "* Start-up Frame Guard Time: %.4g ms\n", 256.0 * 16.0 * (1 << (TB & 0x0f)) / 13560.0); } } - if (nai.abtAts[0] & 0x40) { // TC(1) present - uint8_t TC = nai.abtAts[offset]; + if (pnai->abtAts[0] & 0x40) { // TC(1) present + uint8_t TC = pnai->abtAts[offset]; offset++; if (TC & 0x1) { off += snprintf(dst + off, size - off, "* Node ADdress supported\n"); @@ -258,23 +258,23 @@ snprint_nfc_iso14443a_info(char *dst, size_t size, const nfc_iso14443a_info nai, off += snprintf(dst + off, size - off, "* Card IDentifier not supported\n"); } } - if (nai.szAtsLen > offset) { + if (pnai->szAtsLen > offset) { off += snprintf(dst + off, size - off, "* Historical bytes Tk: "); - off += snprint_hex(dst + off, size - off, nai.abtAts + offset, (nai.szAtsLen - offset)); - uint8_t CIB = nai.abtAts[offset]; + off += snprint_hex(dst + off, size - off, pnai->abtAts + offset, (pnai->szAtsLen - offset)); + uint8_t CIB = pnai->abtAts[offset]; offset++; if (CIB != 0x00 && CIB != 0x10 && (CIB & 0xf0) != 0x80) { off += snprintf(dst + off, size - off, " * Proprietary format\n"); if (CIB == 0xc1) { off += snprintf(dst + off, size - off, " * Tag byte: Mifare or virtual cards of various types\n"); - uint8_t L = nai.abtAts[offset]; + uint8_t L = pnai->abtAts[offset]; offset++; - if (L != (nai.szAtsLen - offset)) { + if (L != (pnai->szAtsLen - offset)) { off += snprintf(dst + off, size - off, " * Warning: Type Identification Coding length (%i)", L); - off += snprintf(dst + off, size - off, " not matching Tk length (%zi)\n", (nai.szAtsLen - offset)); + off += snprintf(dst + off, size - off, " not matching Tk length (%zi)\n", (pnai->szAtsLen - offset)); } - if ((nai.szAtsLen - offset - 2) > 0) { // Omit 2 CRC bytes - uint8_t CTC = nai.abtAts[offset]; + if ((pnai->szAtsLen - offset - 2) > 0) { // Omit 2 CRC bytes + uint8_t CTC = pnai->abtAts[offset]; offset++; off += snprintf(dst + off, size - off, " * Chip Type: "); switch (CTC & 0xf0) { @@ -316,8 +316,8 @@ snprint_nfc_iso14443a_info(char *dst, size_t size, const nfc_iso14443a_info nai, break; } } - if ((nai.szAtsLen - offset) > 0) { // Omit 2 CRC bytes - uint8_t CVC = nai.abtAts[offset]; + if ((pnai->szAtsLen - offset) > 0) { // Omit 2 CRC bytes + uint8_t CVC = pnai->abtAts[offset]; offset++; off += snprintf(dst + off, size - off, " * Chip Status: "); switch (CVC & 0xf0) { @@ -350,8 +350,8 @@ snprint_nfc_iso14443a_info(char *dst, size_t size, const nfc_iso14443a_info nai, break; } } - if ((nai.szAtsLen - offset) > 0) { // Omit 2 CRC bytes - uint8_t VCS = nai.abtAts[offset]; + if ((pnai->szAtsLen - offset) > 0) { // Omit 2 CRC bytes + uint8_t VCS = pnai->abtAts[offset]; offset++; off += snprintf(dst + off, size - off, " * Specifics (Virtual Card Selection):\n"); if ((VCS & 0x09) == 0x00) { @@ -379,10 +379,10 @@ snprint_nfc_iso14443a_info(char *dst, size_t size, const nfc_iso14443a_info nai, off += snprintf(dst + off, size - off, " See ISO/IEC 7816-4 8.1.1.3 for more info\n"); } if (CIB == 0x10) { - off += snprintf(dst + off, size - off, " * DIR data reference: %02x\n", nai.abtAts[offset]); + off += snprintf(dst + off, size - off, " * DIR data reference: %02x\n", pnai->abtAts[offset]); } if (CIB == 0x80) { - if (nai.szAtsLen == offset) { + if (pnai->szAtsLen == offset) { off += snprintf(dst + off, size - off, " * No COMPACT-TLV objects found, no status found\n"); } else { off += snprintf(dst + off, size - off, " * Tk after 0x80 consist of optional consecutive COMPACT-TLV data objects;\n"); @@ -400,9 +400,9 @@ snprint_nfc_iso14443a_info(char *dst, size_t size, const nfc_iso14443a_info nai, uint8_t i, j; bool found_possible_match = false; - atqa = (((uint16_t)nai.abtAtqa[0] & 0xff) << 8); - atqa += (((uint16_t)nai.abtAtqa[1] & 0xff)); - sak = ((uint8_t)nai.btSak & 0xff); + atqa = (((uint16_t)pnai->abtAtqa[0] & 0xff) << 8); + atqa += (((uint16_t)pnai->abtAtqa[1] & 0xff)); + sak = ((uint8_t)pnai->btSak & 0xff); for (i = 0; i < sizeof(const_ca) / sizeof(const_ca[0]); i++) { if ((atqa & const_ca[i].mask) == const_ca[i].atqa) { @@ -420,9 +420,9 @@ snprint_nfc_iso14443a_info(char *dst, size_t size, const nfc_iso14443a_info nai, // but seen in the field: off += snprintf(dst + off, size - off, "Other possible matches based on ATQA & SAK values:\n"); uint32_t atqasak = 0; - atqasak += (((uint32_t)nai.abtAtqa[0] & 0xff) << 16); - atqasak += (((uint32_t)nai.abtAtqa[1] & 0xff) << 8); - atqasak += ((uint32_t)nai.btSak & 0xff); + atqasak += (((uint32_t)pnai->abtAtqa[0] & 0xff) << 16); + atqasak += (((uint32_t)pnai->abtAtqa[1] & 0xff) << 8); + atqasak += ((uint32_t)pnai->btSak & 0xff); switch (atqasak) { case 0x000488: off += snprintf(dst + off, size - off, "* Mifare Classic 1K Infineon\n"); @@ -469,96 +469,96 @@ snprint_nfc_iso14443a_info(char *dst, size_t size, const nfc_iso14443a_info nai, } void -snprint_nfc_felica_info(char *dst, size_t size, const nfc_felica_info nfi, bool verbose) +snprint_nfc_felica_info(char *dst, size_t size, const nfc_felica_info *pnfi, bool verbose) { (void) verbose; int off = 0; off += snprintf(dst + off, size - off, " ID (NFCID2): "); - off += snprint_hex(dst + off, size - off, nfi.abtId, 8); + off += snprint_hex(dst + off, size - off, pnfi->abtId, 8); off += snprintf(dst + off, size - off, " Parameter (PAD): "); - off += snprint_hex(dst + off, size - off, nfi.abtPad, 8); + off += snprint_hex(dst + off, size - off, pnfi->abtPad, 8); off += snprintf(dst + off, size - off, " System Code (SC): "); - snprint_hex(dst + off, size - off, nfi.abtSysCode, 2); + snprint_hex(dst + off, size - off, pnfi->abtSysCode, 2); } void -snprint_nfc_jewel_info(char *dst, size_t size, const nfc_jewel_info nji, bool verbose) +snprint_nfc_jewel_info(char *dst, size_t size, const nfc_jewel_info *pnji, bool verbose) { (void) verbose; int off = 0; off += snprintf(dst + off, size - off, " ATQA (SENS_RES): "); - off += snprint_hex(dst + off, size - off, nji.btSensRes, 2); + off += snprint_hex(dst + off, size - off, pnji->btSensRes, 2); off += snprintf(dst + off, size - off, " 4-LSB JEWELID: "); - snprint_hex(dst + off, size - off, nji.btId, 4); + snprint_hex(dst + off, size - off, pnji->btId, 4); } #define PI_ISO14443_4_SUPPORTED 0x01 #define PI_NAD_SUPPORTED 0x01 #define PI_CID_SUPPORTED 0x02 void -snprint_nfc_iso14443b_info(char *dst, size_t size, const nfc_iso14443b_info nbi, bool verbose) +snprint_nfc_iso14443b_info(char *dst, size_t size, const nfc_iso14443b_info *pnbi, bool verbose) { int off = 0; off += snprintf(dst + off, size - off, " PUPI: "); - off += snprint_hex(dst + off, size - off, nbi.abtPupi, 4); + off += snprint_hex(dst + off, size - off, pnbi->abtPupi, 4); off += snprintf(dst + off, size - off, " Application Data: "); - off += snprint_hex(dst + off, size - off, nbi.abtApplicationData, 4); + off += snprint_hex(dst + off, size - off, pnbi->abtApplicationData, 4); off += snprintf(dst + off, size - off, " Protocol Info: "); - off += snprint_hex(dst + off, size - off, nbi.abtProtocolInfo, 3); + off += snprint_hex(dst + off, size - off, pnbi->abtProtocolInfo, 3); if (verbose) { off += snprintf(dst + off, size - off, "* Bit Rate Capability:\n"); - if (nbi.abtProtocolInfo[0] == 0) { + if (pnbi->abtProtocolInfo[0] == 0) { off += snprintf(dst + off, size - off, " * PICC supports only 106 kbits/s in both directions\n"); } - if (nbi.abtProtocolInfo[0] & 1 << 7) { + if (pnbi->abtProtocolInfo[0] & 1 << 7) { off += snprintf(dst + off, size - off, " * Same bitrate in both directions mandatory\n"); } - if (nbi.abtProtocolInfo[0] & 1 << 4) { + if (pnbi->abtProtocolInfo[0] & 1 << 4) { off += snprintf(dst + off, size - off, " * PICC to PCD, 1etu=64/fc, bitrate 212 kbits/s supported\n"); } - if (nbi.abtProtocolInfo[0] & 1 << 5) { + if (pnbi->abtProtocolInfo[0] & 1 << 5) { off += snprintf(dst + off, size - off, " * PICC to PCD, 1etu=32/fc, bitrate 424 kbits/s supported\n"); } - if (nbi.abtProtocolInfo[0] & 1 << 6) { + if (pnbi->abtProtocolInfo[0] & 1 << 6) { off += snprintf(dst + off, size - off, " * PICC to PCD, 1etu=16/fc, bitrate 847 kbits/s supported\n"); } - if (nbi.abtProtocolInfo[0] & 1 << 0) { + if (pnbi->abtProtocolInfo[0] & 1 << 0) { off += snprintf(dst + off, size - off, " * PCD to PICC, 1etu=64/fc, bitrate 212 kbits/s supported\n"); } - if (nbi.abtProtocolInfo[0] & 1 << 1) { + if (pnbi->abtProtocolInfo[0] & 1 << 1) { off += snprintf(dst + off, size - off, " * PCD to PICC, 1etu=32/fc, bitrate 424 kbits/s supported\n"); } - if (nbi.abtProtocolInfo[0] & 1 << 2) { + if (pnbi->abtProtocolInfo[0] & 1 << 2) { off += snprintf(dst + off, size - off, " * PCD to PICC, 1etu=16/fc, bitrate 847 kbits/s supported\n"); } - if (nbi.abtProtocolInfo[0] & 1 << 3) { + if (pnbi->abtProtocolInfo[0] & 1 << 3) { off += snprintf(dst + off, size - off, " * ERROR unknown value\n"); } - if ((nbi.abtProtocolInfo[1] & 0xf0) <= 0x80) { + if ((pnbi->abtProtocolInfo[1] & 0xf0) <= 0x80) { const int iMaxFrameSizes[] = { 16, 24, 32, 40, 48, 64, 96, 128, 256 }; - off += snprintf(dst + off, size - off, "* Maximum frame sizes: %d bytes\n", iMaxFrameSizes[((nbi.abtProtocolInfo[1] & 0xf0) >> 4)]); + off += snprintf(dst + off, size - off, "* Maximum frame sizes: %d bytes\n", iMaxFrameSizes[((pnbi->abtProtocolInfo[1] & 0xf0) >> 4)]); } - if ((nbi.abtProtocolInfo[1] & 0x0f) == PI_ISO14443_4_SUPPORTED) { + if ((pnbi->abtProtocolInfo[1] & 0x0f) == PI_ISO14443_4_SUPPORTED) { off += snprintf(dst + off, size - off, "* Protocol types supported: ISO/IEC 14443-4\n"); } - off += snprintf(dst + off, size - off, "* Frame Waiting Time: %.4g ms\n", 256.0 * 16.0 * (1 << ((nbi.abtProtocolInfo[2] & 0xf0) >> 4)) / 13560.0); - if ((nbi.abtProtocolInfo[2] & (PI_NAD_SUPPORTED | PI_CID_SUPPORTED)) != 0) { + off += snprintf(dst + off, size - off, "* Frame Waiting Time: %.4g ms\n", 256.0 * 16.0 * (1 << ((pnbi->abtProtocolInfo[2] & 0xf0) >> 4)) / 13560.0); + if ((pnbi->abtProtocolInfo[2] & (PI_NAD_SUPPORTED | PI_CID_SUPPORTED)) != 0) { off += snprintf(dst + off, size - off, "* Frame options supported: "); - if ((nbi.abtProtocolInfo[2] & PI_NAD_SUPPORTED) != 0) off += snprintf(dst + off, size - off, "NAD "); - if ((nbi.abtProtocolInfo[2] & PI_CID_SUPPORTED) != 0) off += snprintf(dst + off, size - off, "CID "); + if ((pnbi->abtProtocolInfo[2] & PI_NAD_SUPPORTED) != 0) off += snprintf(dst + off, size - off, "NAD "); + if ((pnbi->abtProtocolInfo[2] & PI_CID_SUPPORTED) != 0) off += snprintf(dst + off, size - off, "CID "); snprintf(dst + off, size - off, "\n"); } } } void -snprint_nfc_iso14443bi_info(char *dst, size_t size, const nfc_iso14443bi_info nii, bool verbose) +snprint_nfc_iso14443bi_info(char *dst, size_t size, const nfc_iso14443bi_info *pnii, bool verbose) { int off = 0; off += snprintf(dst + off, size - off, " DIV: "); - off += snprint_hex(dst + off, size - off, nii.abtDIV, 4); + off += snprint_hex(dst + off, size - off, pnii->abtDIV, 4); if (verbose) { - int version = (nii.btVerLog & 0x1e) >> 1; + int version = (pnii->btVerLog & 0x1e) >> 1; off += snprintf(dst + off, size - off, " Software Version: "); if (version == 15) { off += snprintf(dst + off, size - off, "Undefined\n"); @@ -566,86 +566,88 @@ snprint_nfc_iso14443bi_info(char *dst, size_t size, const nfc_iso14443bi_info ni off += snprintf(dst + off, size - off, "%i\n", version); } - if ((nii.btVerLog & 0x80) && (nii.btConfig & 0x80)) { + if ((pnii->btVerLog & 0x80) && (pnii->btConfig & 0x80)) { off += snprintf(dst + off, size - off, " Wait Enable: yes"); } } - if ((nii.btVerLog & 0x80) && (nii.btConfig & 0x40)) { + if ((pnii->btVerLog & 0x80) && (pnii->btConfig & 0x40)) { off += snprintf(dst + off, size - off, " ATS: "); - snprint_hex(dst + off, size - off, nii.abtAtr, nii.szAtrLen); + snprint_hex(dst + off, size - off, pnii->abtAtr, pnii->szAtrLen); } } void -snprint_nfc_iso14443b2sr_info(char *dst, size_t size, const nfc_iso14443b2sr_info nsi, bool verbose) +snprint_nfc_iso14443b2sr_info(char *dst, size_t size, const nfc_iso14443b2sr_info *pnsi, bool verbose) { (void) verbose; int off = 0; off += snprintf(dst + off, size - off, " UID: "); - snprint_hex(dst + off, size - off, nsi.abtUID, 8); + snprint_hex(dst + off, size - off, pnsi->abtUID, 8); } void -snprint_nfc_iso14443b2ct_info(char *dst, size_t size, const nfc_iso14443b2ct_info nci, bool verbose) +snprint_nfc_iso14443b2ct_info(char *dst, size_t size, const nfc_iso14443b2ct_info *pnci, bool verbose) { (void) verbose; int off = 0; uint32_t uid; - uid = (nci.abtUID[3] << 24) + (nci.abtUID[2] << 16) + (nci.abtUID[1] << 8) + nci.abtUID[0]; + uid = (pnci->abtUID[3] << 24) + (pnci->abtUID[2] << 16) + (pnci->abtUID[1] << 8) + pnci->abtUID[0]; off += snprintf(dst + off, size - off, " UID: "); - off += snprint_hex(dst + off, size - off, nci.abtUID, sizeof(nci.abtUID)); + off += snprint_hex(dst + off, size - off, pnci->abtUID, sizeof(pnci->abtUID)); off += snprintf(dst + off, size - off, " UID (decimal): %010u\n", uid); - off += snprintf(dst + off, size - off, " Product Code: %02X\n", nci.btProdCode); - snprintf(dst + off, size - off, " Fab Code: %02X\n", nci.btFabCode); + off += snprintf(dst + off, size - off, " Product Code: %02X\n", pnci->btProdCode); + snprintf(dst + off, size - off, " Fab Code: %02X\n", pnci->btFabCode); } void -snprint_nfc_dep_info(char *dst, size_t size, const nfc_dep_info ndi, bool verbose) +snprint_nfc_dep_info(char *dst, size_t size, const nfc_dep_info *pndi, bool verbose) { (void) verbose; int off = 0; off += snprintf(dst + off, size - off, " NFCID3: "); - off += snprint_hex(dst + off, size - off, ndi.abtNFCID3, 10); - off += snprintf(dst + off, size - off, " BS: %02x\n", ndi.btBS); - off += snprintf(dst + off, size - off, " BR: %02x\n", ndi.btBR); - off += snprintf(dst + off, size - off, " TO: %02x\n", ndi.btTO); - off += snprintf(dst + off, size - off, " PP: %02x\n", ndi.btPP); - if (ndi.szGB) { + off += snprint_hex(dst + off, size - off, pndi->abtNFCID3, 10); + off += snprintf(dst + off, size - off, " BS: %02x\n", pndi->btBS); + off += snprintf(dst + off, size - off, " BR: %02x\n", pndi->btBR); + off += snprintf(dst + off, size - off, " TO: %02x\n", pndi->btTO); + off += snprintf(dst + off, size - off, " PP: %02x\n", pndi->btPP); + if (pndi->szGB) { off += snprintf(dst + off, size - off, "General Bytes: "); - snprint_hex(dst + off, size - off, ndi.abtGB, ndi.szGB); + snprint_hex(dst + off, size - off, pndi->abtGB, pndi->szGB); } } void -snprint_nfc_target(char *dst, size_t size, const nfc_target nt, bool verbose) +snprint_nfc_target(char *dst, size_t size, const nfc_target *pnt, bool verbose) { - int off = 0; - off += snprintf(dst + off, size - off, "%s (%s%s) target:\n", str_nfc_modulation_type(nt.nm.nmt), str_nfc_baud_rate(nt.nm.nbr), (nt.nm.nmt != NMT_DEP) ? "" : (nt.nti.ndi.ndm == NDM_ACTIVE) ? "active mode" : "passive mode"); - switch (nt.nm.nmt) { - case NMT_ISO14443A: - snprint_nfc_iso14443a_info(dst + off, size - off, nt.nti.nai, verbose); - break; - case NMT_JEWEL: - snprint_nfc_jewel_info(dst + off, size - off, nt.nti.nji, verbose); - break; - case NMT_FELICA: - snprint_nfc_felica_info(dst + off, size - off, nt.nti.nfi, verbose); - break; - case NMT_ISO14443B: - snprint_nfc_iso14443b_info(dst + off, size - off, nt.nti.nbi, verbose); - break; - case NMT_ISO14443BI: - snprint_nfc_iso14443bi_info(dst + off, size - off, nt.nti.nii, verbose); - break; - case NMT_ISO14443B2SR: - snprint_nfc_iso14443b2sr_info(dst + off, size - off, nt.nti.nsi, verbose); - break; - case NMT_ISO14443B2CT: - snprint_nfc_iso14443b2ct_info(dst + off, size - off, nt.nti.nci, verbose); - break; - case NMT_DEP: - snprint_nfc_dep_info(dst + off, size - off, nt.nti.ndi, verbose); - break; + if (NULL != pnt) { + int off = 0; + off += snprintf(dst + off, size - off, "%s (%s%s) target:\n", str_nfc_modulation_type(pnt->nm.nmt), str_nfc_baud_rate(pnt->nm.nbr), (pnt->nm.nmt != NMT_DEP) ? "" : (pnt->nti.ndi.ndm == NDM_ACTIVE) ? "active mode" : "passive mode"); + switch (pnt->nm.nmt) { + case NMT_ISO14443A: + snprint_nfc_iso14443a_info(dst + off, size - off, &pnt->nti.nai, verbose); + break; + case NMT_JEWEL: + snprint_nfc_jewel_info(dst + off, size - off, &pnt->nti.nji, verbose); + break; + case NMT_FELICA: + snprint_nfc_felica_info(dst + off, size - off, &pnt->nti.nfi, verbose); + break; + case NMT_ISO14443B: + snprint_nfc_iso14443b_info(dst + off, size - off, &pnt->nti.nbi, verbose); + break; + case NMT_ISO14443BI: + snprint_nfc_iso14443bi_info(dst + off, size - off, &pnt->nti.nii, verbose); + break; + case NMT_ISO14443B2SR: + snprint_nfc_iso14443b2sr_info(dst + off, size - off, &pnt->nti.nsi, verbose); + break; + case NMT_ISO14443B2CT: + snprint_nfc_iso14443b2ct_info(dst + off, size - off, &pnt->nti.nci, verbose); + break; + case NMT_DEP: + snprint_nfc_dep_info(dst + off, size - off, &pnt->nti.ndi, verbose); + break; + } } } diff --git a/libnfc/target-subr.h b/libnfc/target-subr.h index 89710b6..a1b9d60 100644 --- a/libnfc/target-subr.h +++ b/libnfc/target-subr.h @@ -28,14 +28,14 @@ #define _TARGET_SUBR_H_ int snprint_hex(char *dst, size_t size, const uint8_t *pbtData, const size_t szLen); -void snprint_nfc_iso14443a_info(char *dst, size_t size, const nfc_iso14443a_info nai, bool verbose); -void snprint_nfc_iso14443b_info(char *dst, size_t size, const nfc_iso14443b_info nbi, bool verbose); -void snprint_nfc_iso14443bi_info(char *dst, size_t size, const nfc_iso14443bi_info nii, bool verbose); -void snprint_nfc_iso14443b2sr_info(char *dst, size_t size, const nfc_iso14443b2sr_info nsi, bool verbose); -void snprint_nfc_iso14443b2ct_info(char *dst, size_t size, const nfc_iso14443b2ct_info nci, bool verbose); -void snprint_nfc_felica_info(char *dst, size_t size, const nfc_felica_info nfi, bool verbose); -void snprint_nfc_jewel_info(char *dst, size_t size, const nfc_jewel_info nji, bool verbose); -void snprint_nfc_dep_info(char *dst, size_t size, const nfc_dep_info ndi, bool verbose); -void snprint_nfc_target(char *dst, size_t size, const nfc_target nt, bool verbose); +void snprint_nfc_iso14443a_info(char *dst, size_t size, const nfc_iso14443a_info *pnai, bool verbose); +void snprint_nfc_iso14443b_info(char *dst, size_t size, const nfc_iso14443b_info *pnbi, bool verbose); +void snprint_nfc_iso14443bi_info(char *dst, size_t size, const nfc_iso14443bi_info *pnii, bool verbose); +void snprint_nfc_iso14443b2sr_info(char *dst, size_t size, const nfc_iso14443b2sr_info *pnsi, bool verbose); +void snprint_nfc_iso14443b2ct_info(char *dst, size_t size, const nfc_iso14443b2ct_info *pnci, bool verbose); +void snprint_nfc_felica_info(char *dst, size_t size, const nfc_felica_info *pnfi, bool verbose); +void snprint_nfc_jewel_info(char *dst, size_t size, const nfc_jewel_info *pnji, bool verbose); +void snprint_nfc_dep_info(char *dst, size_t size, const nfc_dep_info *pndi, bool verbose); +void snprint_nfc_target(char *dst, size_t size, const nfc_target *pnt, bool verbose); #endif diff --git a/utils/nfc-list.c b/utils/nfc-list.c index 47d485c..251ede2 100644 --- a/utils/nfc-list.c +++ b/utils/nfc-list.c @@ -143,7 +143,7 @@ main(int argc, const char *argv[]) printf("%d ISO14443A passive target(s) found%s\n", res, (res == 0) ? ".\n" : ":"); } for (n = 0; n < res; n++) { - print_nfc_target(ant[n], verbose); + print_nfc_target(&ant[n], verbose); printf("\n"); } } @@ -157,7 +157,7 @@ main(int argc, const char *argv[]) printf("%d Felica (212 kbps) passive target(s) found%s\n", res, (res == 0) ? ".\n" : ":"); } for (n = 0; n < res; n++) { - print_nfc_target(ant[n], verbose); + print_nfc_target(&ant[n], verbose); printf("\n"); } } @@ -169,7 +169,7 @@ main(int argc, const char *argv[]) printf("%d Felica (424 kbps) passive target(s) found%s\n", res, (res == 0) ? ".\n" : ":"); } for (n = 0; n < res; n++) { - print_nfc_target(ant[n], verbose); + print_nfc_target(&ant[n], verbose); printf("\n"); } } @@ -183,7 +183,7 @@ main(int argc, const char *argv[]) printf("%d ISO14443B passive target(s) found%s\n", res, (res == 0) ? ".\n" : ":"); } for (n = 0; n < res; n++) { - print_nfc_target(ant[n], verbose); + print_nfc_target(&ant[n], verbose); printf("\n"); } } @@ -197,7 +197,7 @@ main(int argc, const char *argv[]) printf("%d ISO14443B' passive target(s) found%s\n", res, (res == 0) ? ".\n" : ":"); } for (n = 0; n < res; n++) { - print_nfc_target(ant[n], verbose); + print_nfc_target(&ant[n], verbose); printf("\n"); } } @@ -211,7 +211,7 @@ main(int argc, const char *argv[]) printf("%d ISO14443B-2 ST SRx passive target(s) found%s\n", res, (res == 0) ? ".\n" : ":"); } for (n = 0; n < res; n++) { - print_nfc_target(ant[n], verbose); + print_nfc_target(&ant[n], verbose); printf("\n"); } } @@ -225,7 +225,7 @@ main(int argc, const char *argv[]) printf("%d ISO14443B-2 ASK CTx passive target(s) found%s\n", res, (res == 0) ? ".\n" : ":"); } for (n = 0; n < res; n++) { - print_nfc_target(ant[n], verbose); + print_nfc_target(&ant[n], verbose); printf("\n"); } } @@ -239,7 +239,7 @@ main(int argc, const char *argv[]) printf("%d Jewel passive target(s) found%s\n", res, (res == 0) ? ".\n" : ":"); } for (n = 0; n < res; n++) { - print_nfc_target(ant[n], verbose); + print_nfc_target(&ant[n], verbose); printf("\n"); } } diff --git a/utils/nfc-mfclassic.c b/utils/nfc-mfclassic.c index 938aef7..964bfeb 100644 --- a/utils/nfc-mfclassic.c +++ b/utils/nfc-mfclassic.c @@ -558,7 +558,7 @@ main(int argc, const char *argv[]) } } printf("Found MIFARE Classic card:\n"); - print_nfc_target(nt, false); + print_nfc_target(&nt, false); // Guessing size if ((nt.nti.nai.abtAtqa[1] & 0x02) == 0x02) diff --git a/utils/nfc-relay-picc.c b/utils/nfc-relay-picc.c index 18ec0fa..ed180ca 100644 --- a/utils/nfc-relay-picc.c +++ b/utils/nfc-relay-picc.c @@ -271,7 +271,7 @@ main(int argc, char *argv[]) } printf("Found tag:\n"); - print_nfc_target(ntRealTarget, false); + print_nfc_target(&ntRealTarget, false); if (initiator_only_mode) { if (print_hex_fd4(ntRealTarget.nti.nai.abtUid, ntRealTarget.nti.nai.szUidLen, "UID") < 0) { fprintf(stderr, "Error while printing UID to FD4\n"); @@ -372,7 +372,7 @@ main(int argc, char *argv[]) memcpy(&(ntEmulatedTarget.nti.nai.abtAts[4]), pbtTkt, szTk); printf("We will emulate:\n"); - print_nfc_target(ntEmulatedTarget, false); + print_nfc_target(&ntEmulatedTarget, false); // Try to open the NFC emulator device if (swap_devices) { diff --git a/utils/nfc-utils.c b/utils/nfc-utils.c index a56b0f7..4d714bf 100644 --- a/utils/nfc-utils.c +++ b/utils/nfc-utils.c @@ -115,10 +115,10 @@ print_hex_par(const uint8_t *pbtData, const size_t szBits, const uint8_t *pbtDat } void -print_nfc_target(const nfc_target nt, bool verbose) +print_nfc_target(const nfc_target *pnt, bool verbose) { char *s; - str_nfc_target(&s, nt, verbose); + str_nfc_target(&s, pnt, verbose); printf("%s", s); nfc_free(s); } diff --git a/utils/nfc-utils.h b/utils/nfc-utils.h index c92fbc2..6203d0f 100644 --- a/utils/nfc-utils.h +++ b/utils/nfc-utils.h @@ -94,6 +94,6 @@ void print_hex(const uint8_t *pbtData, const size_t szLen); void print_hex_bits(const uint8_t *pbtData, const size_t szBits); void print_hex_par(const uint8_t *pbtData, const size_t szBits, const uint8_t *pbtDataPar); -void print_nfc_target(const nfc_target nt, bool verbose); +void print_nfc_target(const nfc_target *pnt, bool verbose); #endif