nfc_initiator_transceive_bytes() now take a constant size for Rx buffer to have a cleaner API: no more in/out parameters

This commit is contained in:
Romuald Conty 2012-05-27 21:06:22 +00:00
parent f0e85c027a
commit 2c9275adde
13 changed files with 67 additions and 68 deletions

2
NEWS
View file

@ -10,6 +10,8 @@ API Changes:
str_nfc_baud_rate()
- New nfc_device_get_information_about() function to retreive some device's
information
- No more in/out function parameter: nfc_initiator_transceive_bytes() now
take a constant size for Rx buffer
New in 1.6.0-rc1:

View file

@ -104,14 +104,15 @@ transmit_bytes (const uint8_t *pbtTx, const size_t szTx)
printf ("Sent bits: ");
print_hex (pbtTx, szTx);
}
int res;
// Transmit the command bytes
if (nfc_initiator_transceive_bytes (pnd, pbtTx, szTx, abtRx, &szRx, 0) < 0)
if ((res = nfc_initiator_transceive_bytes (pnd, pbtTx, szTx, abtRx, sizeof(abtRx), 0)) < 0)
return false;
// Show received answer
if (!quiet_output) {
printf ("Received bits: ");
print_hex (abtRx, szRx);
print_hex (abtRx, res);
}
// Succesful transfer
return true;

View file

@ -64,7 +64,6 @@ main (int argc, const char *argv[])
{
nfc_target nt;
uint8_t abtRx[MAX_FRAME_LEN];
size_t szRx = sizeof(abtRx);
uint8_t abtTx[] = "Hello World!";
if (argc > 1) {
@ -95,12 +94,13 @@ main (int argc, const char *argv[])
print_nfc_target (nt, false);
printf ("Sending: %s\n", abtTx);
if (nfc_initiator_transceive_bytes (pnd, abtTx, sizeof(abtTx), abtRx, &szRx, 0) < 0) {
int res;
if ((res = nfc_initiator_transceive_bytes (pnd, abtTx, sizeof(abtTx), abtRx, sizeof(abtRx), 0)) < 0) {
nfc_perror(pnd, "nfc_initiator_transceive_bytes");
goto error;
}
abtRx[szRx] = 0;
abtRx[res] = 0;
printf ("Received: %s\n", abtRx);
if (nfc_initiator_deselect_target (pnd) < 0) {

View file

@ -58,7 +58,6 @@
static uint8_t abtRx[MAX_FRAME_LEN];
static int szRxBits;
static size_t szRx = sizeof(abtRx);
static uint8_t abtRawUid[12];
static uint8_t abtAtqa[2];
static uint8_t abtSak;
@ -117,14 +116,15 @@ transmit_bytes (const uint8_t *pbtTx, const size_t szTx)
printf ("Sent bits: ");
print_hex (pbtTx, szTx);
}
int res;
// Transmit the command bytes
if (nfc_initiator_transceive_bytes (pnd, pbtTx, szTx, abtRx, &szRx, 0) < 0)
if ((res = nfc_initiator_transceive_bytes (pnd, pbtTx, szTx, abtRx, sizeof(abtRx), 0)) < 0)
return false;
// Show received answer
if (!quiet_output) {
printf ("Received bits: ");
print_hex (abtRx, szRx);
print_hex (abtRx, res);
}
// Succesful transfer
return true;

View file

@ -82,7 +82,7 @@ extern "C" {
NFC_EXPORT int nfc_initiator_select_dep_target (nfc_device *pnd, const nfc_dep_mode ndm, const nfc_baud_rate nbr, const nfc_dep_info *pndiInitiator, nfc_target *pnt, const int timeout);
NFC_EXPORT int nfc_initiator_poll_dep_target (nfc_device *pnd, const nfc_dep_mode ndm, const nfc_baud_rate nbr, const nfc_dep_info *pndiInitiator, nfc_target *pnt, const int timeout);
NFC_EXPORT int nfc_initiator_deselect_target (nfc_device *pnd);
NFC_EXPORT int nfc_initiator_transceive_bytes (nfc_device *pnd, const uint8_t *pbtTx, const size_t szTx, uint8_t *pbtRx, size_t *pszRx, int timeout);
NFC_EXPORT int nfc_initiator_transceive_bytes (nfc_device *pnd, const uint8_t *pbtTx, const size_t szTx, uint8_t *pbtRx, const size_t szRx, int timeout);
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, 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, 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, uint8_t *pbtRxPar, uint32_t *cycles);

View file

@ -1003,7 +1003,7 @@ pn53x_initiator_select_passive_target_ext (struct nfc_device *pnd,
int timeout)
{
uint8_t abtTargetsData[PN53x_EXTENDED_FRAME__DATA_MAX_LEN];
size_t szTargetsData = sizeof(abtTargetsData);
size_t szTargetsData;
int res = 0;
if (nm.nmt == NMT_ISO14443BI || nm.nmt == NMT_ISO14443B2SR || nm.nmt == NMT_ISO14443B2CT) {
@ -1027,37 +1027,35 @@ pn53x_initiator_select_passive_target_ext (struct nfc_device *pnd,
// Some work to do before getting the UID...
uint8_t abtInitiate[]="\x06\x00";
size_t szInitiateLen = 2;
uint8_t abtSelect[]="\x0e\x00";
size_t szSelectLen = 2;
uint8_t abtSelect[] = { 0x0e, 0x00 };
uint8_t abtRx[1];
size_t szRxLen = 1;
// Getting random Chip_ID
if ((res = pn53x_initiator_transceive_bytes (pnd, abtInitiate, szInitiateLen, abtRx, &szRxLen, timeout)) < 0) {
if ((res = pn53x_initiator_transceive_bytes (pnd, abtInitiate, szInitiateLen, abtRx, sizeof(abtRx), timeout)) < 0) {
return res;
}
abtSelect[1] = abtRx[0];
if ((res = pn53x_initiator_transceive_bytes (pnd, abtSelect, szSelectLen, abtRx, &szRxLen, timeout)) < 0) {
if ((res = pn53x_initiator_transceive_bytes (pnd, abtSelect, sizeof(abtSelect), abtRx, sizeof(abtRx), timeout)) < 0) {
return res;
}
szTargetsData = (size_t)res;
}
else if (nm.nmt == NMT_ISO14443B2CT) {
// Some work to do before getting the UID...
uint8_t abtReqt[]="\x10";
size_t szReqtLen = 1;
const uint8_t abtReqt[]= { 0x10 };
// Getting product code / fab code & store it in output buffer after the serial nr we'll obtain later
if ((res = pn53x_initiator_transceive_bytes (pnd, abtReqt, szReqtLen, abtTargetsData+2, &szTargetsData, timeout)) < 0) {
if ((res = pn53x_initiator_transceive_bytes (pnd, abtReqt, sizeof(abtReqt), abtTargetsData+2, sizeof(abtTargetsData)-2, timeout)) < 0) {
return res;
}
szTargetsData = (size_t)res;
}
if ((res = pn53x_initiator_transceive_bytes (pnd, pbtInitData, szInitData, abtTargetsData, &szTargetsData, timeout)) < 0) {
if ((res = pn53x_initiator_transceive_bytes (pnd, pbtInitData, szInitData, abtTargetsData, sizeof(abtTargetsData), timeout)) < 0) {
return res;
}
if (nm.nmt == NMT_ISO14443B2CT) {
if (szTargetsData != 2)
return NFC_ECHIP;
uint8_t abtRead[]="\xC4"; // Reading UID_MSB (Read address 4)
size_t szReadLen = 1;
if ((res = pn53x_initiator_transceive_bytes (pnd, abtRead, szReadLen, abtTargetsData+4, &szTargetsData, timeout) < 0)) {
return NFC_ECHIP; // FIXME: It should not return a NFC_ECHIP here!
uint8_t abtRead[]= { 0xC4 }; // Reading UID_MSB (Read address 4)
if ((res = pn53x_initiator_transceive_bytes (pnd, abtRead, sizeof(abtRead), abtTargetsData+4, sizeof(abtTargetsData)-4, timeout) < 0)) {
return res;
}
szTargetsData = 6; // u16 UID_LSB, u8 prod code, u8 fab code, u16 UID_MSB
@ -1072,12 +1070,12 @@ pn53x_initiator_select_passive_target_ext (struct nfc_device *pnd,
if (nm.nmt == NMT_ISO14443BI) {
// Select tag
uint8_t abtAttrib[6];
size_t szAttribLen = sizeof(abtAttrib);
memcpy(abtAttrib, abtTargetsData, szAttribLen);
memcpy(abtAttrib, abtTargetsData, sizeof(abtAttrib));
abtAttrib[1] = 0x0f; // ATTRIB
if ((res = pn53x_initiator_transceive_bytes (pnd, abtAttrib, szAttribLen, NULL, NULL, timeout)) < 0) {
if ((res = pn53x_initiator_transceive_bytes (pnd, abtAttrib, sizeof(abtAttrib), NULL, 0, timeout)) < 0) {
return res;
}
szTargetsData = (size_t)res;
}
return abtTargetsData[0];
} // else:
@ -1284,7 +1282,7 @@ pn53x_initiator_transceive_bits (struct nfc_device *pnd, const uint8_t *pbtTx, c
int
pn53x_initiator_transceive_bytes (struct nfc_device *pnd, const uint8_t *pbtTx, const size_t szTx, uint8_t *pbtRx,
size_t *pszRx, int timeout)
const size_t szRx, int timeout)
{
size_t szExtraTxLen;
uint8_t abtCmd[PN53x_EXTENDED_FRAME__DATA_MAX_LEN];
@ -1317,22 +1315,20 @@ pn53x_initiator_transceive_bytes (struct nfc_device *pnd, const uint8_t *pbtTx,
// Send the frame to the PN53X chip and get the answer
// We have to give the amount of bytes + (the two command bytes 0xD4, 0x42)
uint8_t abtRx[PN53x_EXTENDED_FRAME__DATA_MAX_LEN];
size_t szRx = sizeof(abtRx);
if ((res = pn53x_transceive (pnd, abtCmd, szTx + szExtraTxLen, abtRx, szRx, timeout)) < 0) {
if ((res = pn53x_transceive (pnd, abtCmd, szTx + szExtraTxLen, abtRx, sizeof(abtRx), timeout)) < 0) {
pnd->last_error = res;
return pnd->last_error;
}
szRx = (size_t) res;
const size_t szRxLen = (size_t)res - 1;
if (pbtRx != NULL) {
// Save the received byte count
*pszRx = szRx - 1;
if (szRxLen > szRx) {
return NFC_EOVFLOW;
}
// Copy the received bytes
memcpy (pbtRx, abtRx + 1, *pszRx);
memcpy (pbtRx, abtRx + 1, szRxLen);
}
// Everything went successful, we return received bytes count
return (szRx - 1);
return szRxLen;
}
static void __pn53x_init_timer(struct nfc_device *pnd, const uint32_t max_cycles)

View file

@ -328,7 +328,7 @@ int pn53x_initiator_select_dep_target (struct nfc_device *pnd,
int pn53x_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 pn53x_initiator_transceive_bytes (struct nfc_device *pnd, const uint8_t *pbtTx, const size_t szTx,
uint8_t *pbtRx, size_t *pszRx, int timeout);
uint8_t *pbtRx, const size_t szRx, int timeout);
int pn53x_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 pn53x_initiator_transceive_bytes_timed (struct nfc_device *pnd, const uint8_t *pbtTx, const size_t szTx,

View file

@ -145,7 +145,7 @@ struct nfc_driver {
int (*initiator_poll_target) (struct nfc_device *pnd, const nfc_modulation * pnmModulations, const size_t szModulations, const uint8_t uiPollNr, const uint8_t btPeriod, nfc_target * pnt);
int (*initiator_select_dep_target) (struct nfc_device *pnd, const nfc_dep_mode ndm, const nfc_baud_rate nbr, const nfc_dep_info * pndiInitiator, nfc_target * pnt, const int timeout);
int (*initiator_deselect_target) (struct nfc_device *pnd);
int (*initiator_transceive_bytes) (struct nfc_device *pnd, const uint8_t * pbtTx, const size_t szTx, uint8_t * pbtRx, size_t * pszRx, int timeout);
int (*initiator_transceive_bytes) (struct nfc_device *pnd, const uint8_t * pbtTx, const size_t szTx, uint8_t * pbtRx, const size_t szRx, int timeout);
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, 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);

View file

@ -597,7 +597,7 @@ nfc_initiator_deselect_target (nfc_device *pnd)
* @param pbtTx contains a byte array of the frame that needs to be transmitted.
* @param szTx contains the length in bytes.
* @param[out] pbtRx response from the tags
* @param pszRx size of \a pbtRx
* @param szRx size of \a pbtRx (Will return NFC_EOVFLOW if RX exceeds this size)
* @param timeout in milliseconds
*
* The NFC device (configured as initiator) will transmit the supplied bytes (\a pbtTx) to the target.
@ -617,9 +617,9 @@ nfc_initiator_deselect_target (nfc_device *pnd)
*/
int
nfc_initiator_transceive_bytes (nfc_device *pnd, const uint8_t *pbtTx, const size_t szTx, uint8_t *pbtRx,
size_t *pszRx, int timeout)
const size_t szRx, int timeout)
{
HAL (initiator_transceive_bytes, pnd, pbtTx, szTx, pbtRx, pszRx, timeout)
HAL (initiator_transceive_bytes, pnd, pbtTx, szTx, pbtRx, szRx, timeout)
}
/** @ingroup initiator

View file

@ -54,7 +54,6 @@ bool
nfc_initiator_mifare_cmd (nfc_device *pnd, const mifare_cmd mc, const uint8_t ui8Block, mifare_param *pmp)
{
uint8_t abtRx[265];
size_t szRx = sizeof(abtRx);
size_t szParamLen;
uint8_t abtCmd[265];
//bool bEasyFraming;
@ -105,7 +104,7 @@ nfc_initiator_mifare_cmd (nfc_device *pnd, const mifare_cmd mc, const uint8_t ui
}
// Fire the mifare command
int res;
if ((res = nfc_initiator_transceive_bytes (pnd, abtCmd, 2 + szParamLen, abtRx, &szRx, -1)) < 0) {
if ((res = nfc_initiator_transceive_bytes (pnd, abtCmd, 2 + szParamLen, abtRx, sizeof(abtRx), -1)) < 0) {
if (res == NFC_ERFTRANS) {
// "Invalid received frame", usual means we are
// authenticated on a sector but the requested MIFARE cmd (read, write)
@ -126,7 +125,7 @@ nfc_initiator_mifare_cmd (nfc_device *pnd, const mifare_cmd mc, const uint8_t ui
// When we have executed a read command, copy the received bytes into the param
if (mc == MC_READ) {
if (szRx == 16) {
if (res == 16) {
memcpy (pmp->mpd.abtData, abtRx, 16);
} else {
return false;

View file

@ -83,7 +83,6 @@ static size_t num_keys = sizeof (keys) / 6;
static uint8_t abtRx[MAX_FRAME_LEN];
static int szRxBits;
static size_t szRx = sizeof(abtRx);
uint8_t abtHalt[4] = { 0x50, 0x00, 0x00, 0x00 };
@ -116,12 +115,13 @@ transmit_bytes (const uint8_t *pbtTx, const size_t szTx)
printf ("Sent bits: ");
print_hex (pbtTx, szTx);
// Transmit the command bytes
if (nfc_initiator_transceive_bytes (pnd, pbtTx, szTx, abtRx, &szRx, 0) < 0)
int res;
if ((res = nfc_initiator_transceive_bytes (pnd, pbtTx, szTx, abtRx, sizeof(abtRx), 0)) < 0)
return false;
// Show received answer
printf ("Received bits: ");
print_hex (abtRx, szRx);
print_hex (abtRx, res);
// Succesful transfer
return true;
}

View file

@ -111,40 +111,39 @@ nfc_forum_tag_type3_check (nfc_device *dev, const nfc_target nt, const uint16_t
size_t frame_len = sizeof(frame);
build_felica_frame (nt.nti.nfi, CHECK, payload, payload_len, frame, &frame_len);
uint8_t res[1024];
size_t res_len;
if (nfc_initiator_transceive_bytes (dev, frame, frame_len, res, &res_len, 0) < 0) {
return -1;
uint8_t rx[1024];
int res;
if ((res = nfc_initiator_transceive_bytes (dev, frame, frame_len, rx, sizeof(rx), 0)) < 0) {
return res;
}
const size_t res_overhead = 1 + 1 + 8 + 2; // 1+1+8+2: LEN + CMD + NFCID2 + STATUS
if (res_len < res_overhead) {
const int res_overhead = 1 + 1 + 8 + 2; // 1+1+8+2: LEN + CMD + NFCID2 + STATUS
if (res < res_overhead) {
// Not enough data
return -1;
}
uint8_t felica_res_len = res[0];
if (res_len != felica_res_len) {
uint8_t felica_res_len = rx[0];
if (res != felica_res_len) {
// Error while receiving felica frame
return -1;
}
if ((CHECK + 1) != res[1]) {
if ((CHECK + 1) != rx[1]) {
// Command return does not match
return -1;
}
if (0 != memcmp (&res[2], nt.nti.nfi.abtId, 8)) {
if (0 != memcmp (&rx[2], nt.nti.nfi.abtId, 8)) {
// NFCID2 does not match
return -1;
}
const uint8_t status_flag1 = res[10];
const uint8_t status_flag2 = res[11];
const uint8_t status_flag1 = rx[10];
const uint8_t status_flag2 = rx[11];
if ((status_flag1) || (status_flag2)) {
// Felica card's error
fprintf (stderr, "Status bytes: %02x, %02x\n", status_flag1, status_flag2);
return -1;
}
// const uint8_t res_block_count = res[12];
*data_len = res_len - res_overhead + 1; // +1 => block count is stored on 1 byte
memcpy (data, &res[res_overhead+1], *data_len);
*data_len = res - res_overhead + 1; // +1 => block count is stored on 1 byte
memcpy (data, &rx[res_overhead+1], *data_len);
return *data_len;
}

View file

@ -365,9 +365,8 @@ main (int argc, char *argv[])
}
printf ("NFC emulator device: %s opened\n", nfc_device_get_name (pndTarget));
szCapduLen = sizeof (abtCapdu);
if (nfc_target_init (pndTarget, &ntEmulatedTarget, abtCapdu, szCapduLen, 0) < 0) {
int res;
if ((res = nfc_target_init (pndTarget, &ntEmulatedTarget, abtCapdu, sizeof(abtCapdu), 0)) < 0) {
ERR ("%s", "Initialization of NFC emulator failed");
if (!target_only_mode) {
nfc_close (pndInitiator);
@ -379,7 +378,6 @@ main (int argc, char *argv[])
printf ("%s\n", "Done, relaying frames now!");
}
while (!quitting) {
bool ret;
int res = 0;
@ -419,8 +417,12 @@ main (int argc, char *argv[])
if (!target_only_mode) {
// Forward the frame to the original tag
ret = (nfc_initiator_transceive_bytes
(pndInitiator, abtCapdu, szCapduLen, abtRapdu, &szRapduLen, 0) < 0) ? 0 : 1;
if ((res = nfc_initiator_transceive_bytes (pndInitiator, abtCapdu, szCapduLen, abtRapdu, sizeof(abtRapdu), -1) < 0)) {
ret = false;
} else {
szCapduLen = (size_t) res;
ret = true;
}
} else {
if (scan_hex_fd3(abtRapdu, &szRapduLen, "R-APDU") != EXIT_SUCCESS) {
fprintf (stderr, "Error while scanning R-APDU from FD3\n");