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

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");