From dafa6e42db9a825897cf421510db73bee694715f Mon Sep 17 00:00:00 2001 From: Romuald Conty Date: Mon, 4 Apr 2011 10:01:33 +0000 Subject: [PATCH] sync nfc-utils.h/c and mifare.c/h with libnfc's ones. --- src/mifare.c | 64 +++++++++++++++++++++++++++++++++++++++++-------- src/mifare.h | 42 ++++++++++++++++++++------------ src/nfc-utils.c | 3 +++ src/nfc-utils.h | 40 +++++++++++++++++++++++++++++++ 4 files changed, 124 insertions(+), 25 deletions(-) diff --git a/src/mifare.c b/src/mifare.c index f7e0082..6f94420 100644 --- a/src/mifare.c +++ b/src/mifare.c @@ -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 @@ -21,13 +51,10 @@ bool nfc_initiator_mifare_cmd (nfc_device_t * pnd, const mifare_cmd mc, const uint8_t ui8Block, mifare_param * pmp) { byte_t abtRx[265]; - size_t szRxLen; + size_t szRx = sizeof(abtRx); size_t szParamLen; byte_t abtCmd[265]; - - // Make sure we are dealing with a active device - if (!pnd->bActive) - return false; + bool bEasyFraming; abtCmd[0] = mc; // The MIFARE Classic command 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) memcpy (abtCmd + 2, (byte_t *) pmp, szParamLen); - // Fire the mifare command - if (!nfc_initiator_transceive_bytes (pnd, abtCmd, 2 + szParamLen, abtRx, &szRxLen)) { - if (pnd->iLastError != 0x14) - nfc_perror (pnd, "nfc_initiator_transceive_bytes"); + bEasyFraming = pnd->bEasyFraming; + if (!nfc_configure (pnd, NDO_EASY_FRAMING, true)) { + nfc_perror (pnd, "nfc_configure"); 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 if (mc == MC_READ) { - if (szRxLen == 16) { + if (szRx == 16) { memcpy (pmp->mpd.abtData, abtRx, 16); } else { return false; diff --git a/src/mifare.h b/src/mifare.h index 76019d0..df357df 100644 --- a/src/mifare.h +++ b/src/mifare.h @@ -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 - * under the terms of the GNU Lesser General Public License as published by the - * Free Software Foundation, either version 3 of the License, or (at your - * option) any later version. - * - * This program is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for - * more details. + * 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. * - * You should have received a copy of the GNU Lesser General Public License - * along with this program. If not, see - * + * 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 + * + */ + +/** * @file mifaretag.h * @brief provide samples structs and functions to manipulate MIFARE Classic and Ultralight tags using libnfc */ diff --git a/src/nfc-utils.c b/src/nfc-utils.c index 6cd13cc..9a0f0fb 100644 --- a/src/nfc-utils.c +++ b/src/nfc-utils.c @@ -532,6 +532,7 @@ print_nfc_iso14443a_info (const nfc_iso14443a_info_t nai, bool verbose) void print_nfc_felica_info (const nfc_felica_info_t nfi, bool verbose) { + (void) verbose; printf (" ID (NFCID2): "); print_hex (nfi.abtId, 8); printf (" Parameter (PAD): "); @@ -541,6 +542,7 @@ print_nfc_felica_info (const nfc_felica_info_t nfi, bool verbose) void print_nfc_jewel_info (const nfc_jewel_info_t nji, bool verbose) { + (void) verbose; printf (" ATQA (SENS_RES): "); print_hex (nji.btSensRes, 2); printf (" 4-LSB JEWELID: "); @@ -608,6 +610,7 @@ print_nfc_iso14443b_info (const nfc_iso14443b_info_t nbi, bool verbose) void print_nfc_dep_info (const nfc_dep_info_t ndi, bool verbose) { + (void) verbose; printf (" NFCID3: "); print_hex (ndi.abtNFCID3, 10); printf (" BS: %02x\n", ndi.btBS); diff --git a/src/nfc-utils.h b/src/nfc-utils.h index bfced61..018193c 100644 --- a/src/nfc-utils.h +++ b/src/nfc-utils.h @@ -38,6 +38,46 @@ # include # include +# include + +/** + * @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); void oddparity_byte_ts (const byte_t * pbtData, const size_t szLen, byte_t * pbtPar);