Skip to content

Commit

Permalink
Convert by value passing of nfc_target to pointer for str_nfc_target …
Browse files Browse the repository at this point in the history
…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.
  • Loading branch information
alexlian authored and doegox committed Mar 7, 2013
1 parent a262be5 commit c72846e
Show file tree
Hide file tree
Showing 16 changed files with 156 additions and 154 deletions.
2 changes: 1 addition & 1 deletion examples/nfc-dep-initiator.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion examples/nfc-dep-target.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion examples/nfc-emulate-tag.c
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
2 changes: 1 addition & 1 deletion examples/nfc-poll.c
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
Expand Down
4 changes: 2 additions & 2 deletions include/nfc/nfc.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions libnfc/chips/pn53x.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
2 changes: 1 addition & 1 deletion libnfc/chips/pn53x.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion libnfc/nfc-internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
8 changes: 4 additions & 4 deletions libnfc/nfc.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
}
Loading

0 comments on commit c72846e

Please sign in to comment.