diff --git a/examples/nfc-dep-target.c b/examples/nfc-dep-target.c index 9d7e73e..b2e8570 100644 --- a/examples/nfc-dep-target.c +++ b/examples/nfc-dep-target.c @@ -62,8 +62,7 @@ int main (int argc, const char *argv[]) { uint8_t abtRx[MAX_FRAME_LEN]; - int res = 0; - size_t szRx = sizeof(abtRx); + int szRx; size_t szDeviceFound; uint8_t abtTx[] = "Hello Mars!"; #define MAX_DEVICE_COUNT 2 @@ -120,18 +119,17 @@ main (int argc, const char *argv[]) print_nfc_target (nt, false); printf ("Waiting for initiator request...\n"); - if(nfc_target_init (pnd, &nt, abtRx, &szRx, 0) < 0) { + if ((szRx = nfc_target_init (pnd, &nt, abtRx, sizeof(abtRx), 0)) < 0) { nfc_perror(pnd, "nfc_target_init"); goto error; } printf("Initiator request received. Waiting for data...\n"); - if ((res = nfc_target_receive_bytes (pnd, abtRx, sizeof (abtRx), 0)) < 0) { + if ((szRx = nfc_target_receive_bytes (pnd, abtRx, sizeof (abtRx), 0)) < 0) { nfc_perror(pnd, "nfc_target_receive_bytes"); goto error; } - szRx = (size_t) res; - abtRx[szRx] = '\0'; + abtRx[(size_t) szRx] = '\0'; printf ("Received: %s\n", abtRx); printf ("Sending: %s\n", abtTx); diff --git a/examples/nfc-emulate-tag.c b/examples/nfc-emulate-tag.c index 8345370..e0dc77c 100644 --- a/examples/nfc-emulate-tag.c +++ b/examples/nfc-emulate-tag.c @@ -55,7 +55,7 @@ #define SAK_ISO14443_4_COMPLIANT 0x20 static uint8_t abtRx[MAX_FRAME_LEN]; -static size_t szRx = sizeof(abtRx); +static int szRx; static nfc_device *pnd; static bool quiet_output = false; static bool init_mfc_auth = false; @@ -137,17 +137,16 @@ bool nfc_target_emulate_tag(nfc_device *pnd, nfc_target *pnt) { size_t szTx; - int res = 0; uint8_t abtTx[MAX_FRAME_LEN]; bool loop = true; - if (nfc_target_init (pnd, pnt, abtRx, &szRx, 0) < 0) { + if ((szRx = nfc_target_init (pnd, pnt, abtRx, sizeof(abtRx), 0)) < 0) { nfc_perror (pnd, "nfc_target_init"); return false; } while ( loop ) { - loop = target_io( pnt, abtRx, szRx, abtTx, &szTx ); + loop = target_io( pnt, abtRx, (size_t) szRx, abtTx, &szTx ); if (szTx) { if (nfc_target_send_bytes(pnd, abtTx, szTx, 0) < 0) { nfc_perror (pnd, "nfc_target_send_bytes"); @@ -159,11 +158,10 @@ nfc_target_emulate_tag(nfc_device *pnd, nfc_target *pnt) nfc_device_set_property_bool (pnd, NP_HANDLE_CRC, false); init_mfc_auth = false; } - if ((res = nfc_target_receive_bytes(pnd, abtRx, sizeof (abtRx), 0)) < 0) { + if ((szRx = nfc_target_receive_bytes(pnd, abtRx, sizeof (abtRx), 0)) < 0) { nfc_perror (pnd, "nfc_target_receive_bytes"); return false; } - szRx = (size_t) res; } } return true; diff --git a/examples/nfc-emulate-uid.c b/examples/nfc-emulate-uid.c index 159ad4e..9baac45 100644 --- a/examples/nfc-emulate-uid.c +++ b/examples/nfc-emulate-uid.c @@ -57,7 +57,7 @@ #define MAX_FRAME_LEN 264 static uint8_t abtRecv[MAX_FRAME_LEN]; -static size_t szRecvBits; +static int szRecvBits; static nfc_device *pnd; // ISO14443A Anti-Collision response @@ -155,13 +155,13 @@ main (int argc, char *argv[]) }, }, }; - if (nfc_target_init (pnd, &nt, abtRecv, &szRecvBits, 0) < 0) { + if ((szRecvBits = nfc_target_init (pnd, &nt, abtRecv, sizeof (abtRecv), 0)) < 0) { nfc_perror (pnd, "nfc_target_init"); ERR ("Could not come out of auto-emulation, no command was received"); goto error; } printf ("[+] Received initiator command: "); - print_hex_bits (abtRecv, szRecvBits); + print_hex_bits (abtRecv, (size_t) szRecvBits); printf ("[+] Configuring communication\n"); if ((nfc_device_set_property_bool (pnd, NP_HANDLE_CRC, false) < 0) || (nfc_device_set_property_bool (pnd, NP_HANDLE_PARITY, true) < 0)) { nfc_perror (pnd, "nfc_device_set_property_bool"); @@ -200,7 +200,7 @@ main (int argc, char *argv[]) if (!quiet_output) { printf ("R: "); - print_hex_bits (abtRecv, szRecvBits); + print_hex_bits (abtRecv, (size_t) szRecvBits); } // Test if we know how to respond if (szTxBits) { diff --git a/examples/nfc-relay.c b/examples/nfc-relay.c index 6459259..5492ed8 100644 --- a/examples/nfc-relay.c +++ b/examples/nfc-relay.c @@ -52,7 +52,7 @@ static uint8_t abtReaderRx[MAX_FRAME_LEN]; static uint8_t abtReaderRxPar[MAX_FRAME_LEN]; -static size_t szReaderRxBits; +static int szReaderRxBits; static uint8_t abtTagRx[MAX_FRAME_LEN]; static uint8_t abtTagRxPar[MAX_FRAME_LEN]; static int szTagRxBits; @@ -146,7 +146,7 @@ main (int argc, char *argv[]) }, }; - if (nfc_target_init (pndTag, &nt, abtReaderRx, &szReaderRxBits, 0) < 0) { + if ((szReaderRxBits = nfc_target_init (pndTag, &nt, abtReaderRx, sizeof (abtReaderRx), 0)) < 0) { ERR ("%s", "Initialization of NFC emulator failed"); nfc_disconnect (pndTag); return EXIT_FAILURE; @@ -197,11 +197,11 @@ main (int argc, char *argv[]) // Print the reader frame to the screen if (!quiet_output) { printf ("R: "); - print_hex_par (abtReaderRx, szReaderRxBits, abtReaderRxPar); + print_hex_par (abtReaderRx, (size_t) szReaderRxBits, abtReaderRxPar); } // Forward the frame to the original tag if ((szTagRxBits = nfc_initiator_transceive_bits - (pndReader, abtReaderRx, szReaderRxBits, abtReaderRxPar, abtTagRx, abtTagRxPar)) > 0) { + (pndReader, abtReaderRx, (size_t) szReaderRxBits, abtReaderRxPar, abtTagRx, abtTagRxPar)) > 0) { // Redirect the answer back to the reader if (nfc_target_send_bits (pndTag, abtTagRx, szTagRxBits, abtTagRxPar) < 0) { nfc_perror (pndTag, "nfc_target_send_bits"); diff --git a/examples/pn53x-sam.c b/examples/pn53x-sam.c index 023a2dd..8757624 100644 --- a/examples/pn53x-sam.c +++ b/examples/pn53x-sam.c @@ -157,7 +157,6 @@ main (int argc, const char *argv[]) case PSM_DUAL_CARD: { uint8_t abtRx[MAX_FRAME_LEN]; - size_t szRx = sizeof(abtRx); nfc_target nt = { .nm = { @@ -176,7 +175,7 @@ main (int argc, const char *argv[]) }; printf ("Now both, NFC device (configured as target) and SAM are readables from an external NFC initiator.\n"); printf ("Please note that NFC device (configured as target) stay in target mode until it receive RATS, ATR_REQ or proprietary command.\n"); - if (nfc_target_init (pnd, &nt, abtRx, &szRx, 0) < 0) { + if (nfc_target_init (pnd, &nt, abtRx, sizeof(abtRx), 0) < 0) { nfc_perror(pnd, "nfc_target_init"); return EXIT_FAILURE; } diff --git a/include/nfc/nfc.h b/include/nfc/nfc.h index 8dd9ad7..de709e5 100644 --- a/include/nfc/nfc.h +++ b/include/nfc/nfc.h @@ -83,7 +83,7 @@ extern "C" { 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, uint8_t *pbtRxPar, uint32_t *cycles); /* 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, size_t *pszRx, int timeout); + NFC_EXPORT int nfc_target_init (nfc_device *pnd, nfc_target *pnt, uint8_t *pbtRx, const size_t szRx, int timeout); NFC_EXPORT int nfc_target_send_bytes (nfc_device *pnd, const uint8_t *pbtTx, const size_t szTx, int timeout); NFC_EXPORT int nfc_target_receive_bytes (nfc_device *pnd, uint8_t *pbtRx, const size_t szRx, int timeout); NFC_EXPORT int nfc_target_send_bits (nfc_device *pnd, const uint8_t *pbtTx, const size_t szTxBits, const uint8_t *pbtTxPar); diff --git a/libnfc/chips/pn53x.c b/libnfc/chips/pn53x.c index c3a0edc..d30a708 100644 --- a/libnfc/chips/pn53x.c +++ b/libnfc/chips/pn53x.c @@ -1592,7 +1592,7 @@ pn53x_initiator_deselect_target (struct nfc_device *pnd) #define SAK_ISO14443_4_COMPLIANT 0x20 #define SAK_ISO18092_COMPLIANT 0x40 int -pn53x_target_init (struct nfc_device *pnd, nfc_target *pnt, uint8_t *pbtRx, size_t *pszRx, int timeout) +pn53x_target_init (struct nfc_device *pnd, nfc_target *pnt, uint8_t *pbtRx, const size_t szRxLen, int timeout) { pn53x_reset_settings(pnd); @@ -1742,13 +1742,14 @@ pn53x_target_init (struct nfc_device *pnd, nfc_target *pnt, uint8_t *pbtRx, size } bool targetActivated = false; + size_t szRx; while (!targetActivated) { uint8_t btActivatedMode; - if((res = pn53x_TgInitAsTarget(pnd, ptm, pbtMifareParams, pbtTkt, szTkt, pbtFeliCaParams, pbtNFCID3t, pbtGBt, szGBt, pbtRx, *pszRx, &btActivatedMode, timeout)) < 0) { + if((res = pn53x_TgInitAsTarget(pnd, ptm, pbtMifareParams, pbtTkt, szTkt, pbtFeliCaParams, pbtNFCID3t, pbtGBt, szGBt, pbtRx, szRxLen, &btActivatedMode, timeout)) < 0) { return res; } - *pszRx = (size_t) res; + szRx = (size_t) res; nfc_modulation nm = { .nmt = NMT_DEP, // Silent compilation warnings .nbr = NBR_UNDEFINED @@ -1805,12 +1806,12 @@ pn53x_target_init (struct nfc_device *pnd, nfc_target *pnt, uint8_t *pbtRx, size if (ptm & PTM_ISO14443_4_PICC_ONLY) { // When PN532 is in PICC target mode, it automatically reply to RATS so // we don't need to forward this command - *pszRx = 0; + szRx = 0; } } } - return *pszRx; + return szRx; } int diff --git a/libnfc/chips/pn53x.h b/libnfc/chips/pn53x.h index cea741d..2115b3f 100644 --- a/libnfc/chips/pn53x.h +++ b/libnfc/chips/pn53x.h @@ -308,7 +308,7 @@ int pn53x_initiator_transceive_bytes_timed (struct nfc_device *pnd, const uin int pn53x_initiator_deselect_target (struct nfc_device *pnd); // NFC device as Target functions -int pn53x_target_init (struct nfc_device *pnd, nfc_target *pnt, uint8_t *pbtRx, size_t *pszRx, int timeout); +int pn53x_target_init (struct nfc_device *pnd, nfc_target *pnt, uint8_t *pbtRx, const size_t szRxLen, int timeout); int pn53x_target_receive_bits (struct nfc_device *pnd, uint8_t *pbtRx, const size_t szRxLen, uint8_t *pbtRxPar); int pn53x_target_receive_bytes (struct nfc_device *pnd, uint8_t *pbtRx, const size_t szRxLen, int timeout); int pn53x_target_send_bits (struct nfc_device *pnd, const uint8_t *pbtTx, const size_t szTxBits, const uint8_t *pbtTxPar); diff --git a/libnfc/nfc-emulation.c b/libnfc/nfc-emulation.c index b83d01e..d952fc5 100644 --- a/libnfc/nfc-emulation.c +++ b/libnfc/nfc-emulation.c @@ -31,23 +31,23 @@ int nfc_emulate_target (nfc_device *pnd, struct nfc_emulator *emulator) { uint8_t abtRx[ISO7816_SHORT_R_APDU_MAX_LEN]; - size_t szRx = sizeof(abtRx); + int szRx; uint8_t abtTx[ISO7816_SHORT_C_APDU_MAX_LEN]; int res = 0; - if (nfc_target_init (pnd, emulator->target, abtRx, &szRx, 0) < 0) { + if ((szRx = nfc_target_init (pnd, emulator->target, abtRx, sizeof(abtRx), 0)) < 0) { return -1; } while (res >= 0) { - res = emulator->state_machine->io (emulator, abtRx, szRx, abtTx, sizeof (abtTx)); + res = emulator->state_machine->io (emulator, abtRx, (size_t) szRx, abtTx, sizeof (abtTx)); if (res > 0) { if (nfc_target_send_bytes(pnd, abtTx, res, 0) < 0) { return -1; } } if (res >= 0) { - if ((res = nfc_target_receive_bytes(pnd, abtRx, szRx, 0)) < 0) { + if ((res = nfc_target_receive_bytes(pnd, abtRx, (size_t) szRx, 0)) < 0) { return -1; } } diff --git a/libnfc/nfc-internal.h b/libnfc/nfc-internal.h index f5fbddd..9f0693d 100644 --- a/libnfc/nfc-internal.h +++ b/libnfc/nfc-internal.h @@ -142,7 +142,7 @@ struct nfc_driver_t { int (*initiator_transceive_bytes_timed) (struct nfc_device *pnd, const uint8_t * pbtTx, const size_t szTx, uint8_t * pbtRx, 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 (*target_init) (struct nfc_device *pnd, nfc_target * pnt, uint8_t * pbtRx, size_t * pszRx, int timeout); + 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); int (*target_receive_bytes) (struct nfc_device *pnd, uint8_t * pbtRx, const size_t szRxLen, int timeout); int (*target_send_bits) (struct nfc_device *pnd, const uint8_t * pbtTx, const size_t szTxBits, const uint8_t * pbtTxPar); diff --git a/libnfc/nfc.c b/libnfc/nfc.c index 7ed26d1..a7dd02f 100644 --- a/libnfc/nfc.c +++ b/libnfc/nfc.c @@ -609,7 +609,7 @@ nfc_initiator_transceive_bits_timed (nfc_device *pnd, const uint8_t *pbtTx, cons * and/or NDM_UNDEFINED (ie. for DEP mode), these fields will be updated. * * @param[out] pbtRx Rx buffer pointer - * @param[out] pszRx received bytes count + * @param[out] szRx received bytes count * @param timeout in milliseconds * * This function initializes NFC device in \e target mode in order to emulate a @@ -629,7 +629,7 @@ nfc_initiator_transceive_bits_timed (nfc_device *pnd, const uint8_t *pbtTx, cons * receive functions can be used. */ int -nfc_target_init (nfc_device *pnd, nfc_target *pnt, uint8_t *pbtRx, size_t * pszRx, int timeout) +nfc_target_init (nfc_device *pnd, nfc_target *pnt, uint8_t *pbtRx, const size_t szRx, int timeout) { int res = 0; // Disallow invalid frame @@ -656,7 +656,7 @@ nfc_target_init (nfc_device *pnd, nfc_target *pnt, uint8_t *pbtRx, size_t * pszR if ((res = nfc_device_set_property_bool (pnd, NP_ACTIVATE_FIELD, false)) < 0) return res; - HAL (target_init, pnd, pnt, pbtRx, pszRx, timeout); + HAL (target_init, pnd, pnt, pbtRx, szRx, timeout); } /** diff --git a/test/test_dep_active.c b/test/test_dep_active.c index 3715639..b0e1b76 100644 --- a/test/test_dep_active.c +++ b/test/test_dep_active.c @@ -84,7 +84,7 @@ target_thread (void *arg) uint8_t abtRx[1024]; size_t szRx = sizeof (abtRx); - int res = nfc_target_init (device, &nt, abtRx, &szRx, 0); + int res = nfc_target_init (device, &nt, abtRx, sizeof (abtRx), 0); cut_assert_operator_int (res, >, 0, cut_message ("Can't initialize NFC device as target: %s", nfc_strerror (device))); if (res < 0) { thread_res = -1; return (void*) thread_res; } diff --git a/test/test_dep_passive.c b/test/test_dep_passive.c index 6bdd804..42d2b7f 100644 --- a/test/test_dep_passive.c +++ b/test/test_dep_passive.c @@ -82,7 +82,7 @@ target_thread (void *arg) uint8_t abtRx[1024]; size_t szRx = sizeof (abtRx); - int res = nfc_target_init (device, &nt, abtRx, &szRx, 0); + int res = nfc_target_init (device, &nt, abtRx, szRx, 0); cut_assert_operator_int (res, >, 0, cut_message ("Can't initialize NFC device as target: %s", nfc_strerror (device))); if (res < 0) { thread_res = -1; return (void*) thread_res; } diff --git a/utils/nfc-relay-picc.c b/utils/nfc-relay-picc.c index 4f0205a..ba89e41 100644 --- a/utils/nfc-relay-picc.c +++ b/utils/nfc-relay-picc.c @@ -355,7 +355,7 @@ main (int argc, char *argv[]) printf ("Connected to the NFC emulator device: %s\n", nfc_device_get_name (pndTarget)); szCapduLen = sizeof (abtCapdu); - if (nfc_target_init (pndTarget, &ntEmulatedTarget, abtCapdu, &szCapduLen, 0) < 0) { + if (nfc_target_init (pndTarget, &ntEmulatedTarget, abtCapdu, szCapduLen, 0) < 0) { ERR ("%s", "Initialization of NFC emulator failed"); if (!target_only_mode) { nfc_disconnect (pndInitiator);