sync nfc-utils.h/c and mifare.c/h with libnfc's ones.

This commit is contained in:
Romuald Conty 2011-04-04 10:01:33 +00:00
parent c973c3cd81
commit dafa6e42db
4 changed files with 124 additions and 25 deletions

View file

@ -1,3 +1,33 @@
/*-
* Public platform independent Near Field Communication (NFC) library examples
*
* Copyright (C) 2009, Roel Verdult
* Copyright (C) 2010, Romuald Conty, Romain Tartière
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* 1) Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2 )Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* Note that this license only applies on the examples, NFC library itself is under LGPL
*
*/
#include "mifare.h" #include "mifare.h"
#include <string.h> #include <string.h>
@ -21,13 +51,10 @@ bool
nfc_initiator_mifare_cmd (nfc_device_t * pnd, const mifare_cmd mc, const uint8_t ui8Block, mifare_param * pmp) nfc_initiator_mifare_cmd (nfc_device_t * pnd, const mifare_cmd mc, const uint8_t ui8Block, mifare_param * pmp)
{ {
byte_t abtRx[265]; byte_t abtRx[265];
size_t szRxLen; size_t szRx = sizeof(abtRx);
size_t szParamLen; size_t szParamLen;
byte_t abtCmd[265]; byte_t abtCmd[265];
bool bEasyFraming;
// Make sure we are dealing with a active device
if (!pnd->bActive)
return false;
abtCmd[0] = mc; // The MIFARE Classic command abtCmd[0] = mc; // The MIFARE Classic command
abtCmd[1] = ui8Block; // The block address (1K=0x00..0x39, 4K=0x00..0xff) abtCmd[1] = ui8Block; // The block address (1K=0x00..0x39, 4K=0x00..0xff)
@ -67,15 +94,32 @@ nfc_initiator_mifare_cmd (nfc_device_t * pnd, const mifare_cmd mc, const uint8_t
if (szParamLen) if (szParamLen)
memcpy (abtCmd + 2, (byte_t *) pmp, szParamLen); memcpy (abtCmd + 2, (byte_t *) pmp, szParamLen);
// Fire the mifare command bEasyFraming = pnd->bEasyFraming;
if (!nfc_initiator_transceive_bytes (pnd, abtCmd, 2 + szParamLen, abtRx, &szRxLen)) { if (!nfc_configure (pnd, NDO_EASY_FRAMING, true)) {
if (pnd->iLastError != 0x14) nfc_perror (pnd, "nfc_configure");
nfc_perror (pnd, "nfc_initiator_transceive_bytes");
return false; return false;
} }
// Fire the mifare command
if (!nfc_initiator_transceive_bytes (pnd, abtCmd, 2 + szParamLen, abtRx, &szRx)) {
if (pnd->iLastError == EINVRXFRAM) {
// "Invalid received frame" AKA EINVRXFRAM, usual means we are
// authenticated on a sector but the requested MIFARE cmd (read, write)
// is not permitted by current acces bytes;
// So there is nothing to do here.
} else {
nfc_perror (pnd, "nfc_initiator_transceive_bytes");
}
nfc_configure (pnd, NDO_EASY_FRAMING, bEasyFraming);
return false;
}
if (!nfc_configure (pnd, NDO_EASY_FRAMING, bEasyFraming)) {
nfc_perror (pnd, "nfc_configure");
return false;
}
// When we have executed a read command, copy the received bytes into the param // When we have executed a read command, copy the received bytes into the param
if (mc == MC_READ) { if (mc == MC_READ) {
if (szRxLen == 16) { if (szRx == 16) {
memcpy (pmp->mpd.abtData, abtRx, 16); memcpy (pmp->mpd.abtData, abtRx, 16);
} else { } else {
return false; return false;

View file

@ -1,22 +1,34 @@
/** /*-
* Public platform independent Near Field Communication (NFC) library * Public platform independent Near Field Communication (NFC) library examples
* *
* Copyright (C) 2009, Roel Verdult, 2010, Romuald Conty * Copyright (C) 2009, Roel Verdult
* Copyright (C) 2010, Romuald Conty, Romain Tartière
* *
* This program is free software: you can redistribute it and/or modify it * Redistribution and use in source and binary forms, with or without
* under the terms of the GNU Lesser General Public License as published by the * modification, are permitted provided that the following conditions are met:
* Free Software Foundation, either version 3 of the License, or (at your * 1) Redistributions of source code must retain the above copyright notice,
* option) any later version. * this list of conditions and the following disclaimer.
* * 2 )Redistributions in binary form must reproduce the above copyright
* This program is distributed in the hope that it will be useful, but WITHOUT * notice, this list of conditions and the following disclaimer in the
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * documentation and/or other materials provided with the distribution.
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
* *
* You should have received a copy of the GNU Lesser General Public License * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* along with this program. If not, see <http://www.gnu.org/licenses/> * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
* *
* Note that this license only applies on the examples, NFC library itself is under LGPL
*
*/
/**
* @file mifaretag.h * @file mifaretag.h
* @brief provide samples structs and functions to manipulate MIFARE Classic and Ultralight tags using libnfc * @brief provide samples structs and functions to manipulate MIFARE Classic and Ultralight tags using libnfc
*/ */

View file

@ -532,6 +532,7 @@ print_nfc_iso14443a_info (const nfc_iso14443a_info_t nai, bool verbose)
void void
print_nfc_felica_info (const nfc_felica_info_t nfi, bool verbose) print_nfc_felica_info (const nfc_felica_info_t nfi, bool verbose)
{ {
(void) verbose;
printf (" ID (NFCID2): "); printf (" ID (NFCID2): ");
print_hex (nfi.abtId, 8); print_hex (nfi.abtId, 8);
printf (" Parameter (PAD): "); printf (" Parameter (PAD): ");
@ -541,6 +542,7 @@ print_nfc_felica_info (const nfc_felica_info_t nfi, bool verbose)
void void
print_nfc_jewel_info (const nfc_jewel_info_t nji, bool verbose) print_nfc_jewel_info (const nfc_jewel_info_t nji, bool verbose)
{ {
(void) verbose;
printf (" ATQA (SENS_RES): "); printf (" ATQA (SENS_RES): ");
print_hex (nji.btSensRes, 2); print_hex (nji.btSensRes, 2);
printf (" 4-LSB JEWELID: "); printf (" 4-LSB JEWELID: ");
@ -608,6 +610,7 @@ print_nfc_iso14443b_info (const nfc_iso14443b_info_t nbi, bool verbose)
void void
print_nfc_dep_info (const nfc_dep_info_t ndi, bool verbose) print_nfc_dep_info (const nfc_dep_info_t ndi, bool verbose)
{ {
(void) verbose;
printf (" NFCID3: "); printf (" NFCID3: ");
print_hex (ndi.abtNFCID3, 10); print_hex (ndi.abtNFCID3, 10);
printf (" BS: %02x\n", ndi.btBS); printf (" BS: %02x\n", ndi.btBS);

View file

@ -38,6 +38,46 @@
# include <stdlib.h> # include <stdlib.h>
# include <string.h> # include <string.h>
# include <err.h>
/**
* @macro DBG
* @brief Print a message of standard output only in DEBUG mode
*/
#ifdef DEBUG
# define DBG(...) do { \
warnx ("DBG %s:%d", __FILE__, __LINE__); \
warnx (" " __VA_ARGS__ ); \
} while (0)
#else
# define DBG(...) {}
#endif
/**
* @macro WARN
* @brief Print a warn message
*/
#ifdef DEBUG
# define WARN(...) do { \
warnx ("WARNING %s:%d", __FILE__, __LINE__); \
warnx (" " __VA_ARGS__ ); \
} while (0)
#else
# define WARN(...) warnx ("WARNING: " __VA_ARGS__ )
#endif
/**
* @macro ERR
* @brief Print a error message
*/
#ifdef DEBUG
# define ERR(...) do { \
warnx ("ERROR %s:%d", __FILE__, __LINE__); \
warnx (" " __VA_ARGS__ ); \
} while (0)
#else
# define ERR(...) warnx ("ERROR: " __VA_ARGS__ )
#endif
byte_t oddparity (const byte_t bt); byte_t oddparity (const byte_t bt);
void oddparity_byte_ts (const byte_t * pbtData, const size_t szLen, byte_t * pbtPar); void oddparity_byte_ts (const byte_t * pbtData, const size_t szLen, byte_t * pbtPar);