Commit c72846e3 authored by Alex Lian's avatar Alex Lian Committed by Philippe Teuwen

Convert by value passing of nfc_target to pointer for str_nfc_target and...

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.
parent a262be56
......@@ -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;
......
......@@ -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) {
......
......@@ -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));
......
......@@ -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");
}
......
......@@ -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
......
......@@ -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;
}
......
......@@ -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);
......
......@@ -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);
......
......@@ -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);
}
This diff is collapsed.
......@@ -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
......@@ -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");
}
}
......
......@@ -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)
......
......@@ -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) {
......
......@@ -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);
}
......@@ -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
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment