2010-11-17 15:27:11 +01:00
|
|
|
/*-
|
|
|
|
* Public platform independent Near Field Communication (NFC) library examples
|
2012-05-29 02:33:22 +02:00
|
|
|
*
|
2012-10-21 16:09:16 +02:00
|
|
|
* Copyright (C) 2009 Roel Verdult
|
|
|
|
* Copyright (C) 2010, 2011 Romain Tartière
|
|
|
|
* Copyright (C) 2009, 2010, 2011, 2012 Romuald Conty
|
2012-05-29 02:33:22 +02:00
|
|
|
*
|
2010-11-17 15:27:11 +01:00
|
|
|
* 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,
|
2012-05-29 02:33:22 +02:00
|
|
|
* this list of conditions and the following disclaimer.
|
2010-11-17 15:27:11 +01:00
|
|
|
* 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.
|
2012-05-29 02:33:22 +02:00
|
|
|
*
|
2010-11-17 15:27:11 +01:00
|
|
|
* Note that this license only applies on the examples, NFC library itself is under LGPL
|
|
|
|
*
|
|
|
|
*/
|
2012-01-31 15:28:45 +01:00
|
|
|
/**
|
|
|
|
* @file nfc-utils.c
|
|
|
|
* @brief Provide some examples shared functions like print, parity calculation, options parsing.
|
|
|
|
*/
|
2010-04-09 23:57:03 +02:00
|
|
|
#include <nfc/nfc.h>
|
2010-10-19 14:50:52 +02:00
|
|
|
#include <err.h>
|
2010-04-09 23:57:03 +02:00
|
|
|
|
|
|
|
#include "nfc-utils.h"
|
|
|
|
|
2011-11-24 11:54:42 +01:00
|
|
|
uint8_t
|
2012-05-29 17:54:36 +02:00
|
|
|
oddparity(const uint8_t bt)
|
2010-09-06 12:02:19 +02:00
|
|
|
{
|
2012-05-02 12:02:56 +02:00
|
|
|
// cf http://graphics.stanford.edu/~seander/bithacks.html#ParityParallel
|
2012-05-29 17:54:36 +02:00
|
|
|
return (0x9669 >> ((bt ^(bt >> 4)) & 0xF)) & 1;
|
2010-09-06 12:02:19 +02:00
|
|
|
}
|
|
|
|
|
2010-09-07 19:51:03 +02:00
|
|
|
void
|
2012-05-29 17:54:36 +02:00
|
|
|
oddparity_bytes_ts(const uint8_t *pbtData, const size_t szLen, uint8_t *pbtPar)
|
2010-09-06 12:02:19 +02:00
|
|
|
{
|
2010-09-07 19:51:03 +02:00
|
|
|
size_t szByteNr;
|
2010-09-06 12:02:19 +02:00
|
|
|
// Calculate the parity bits for the command
|
2010-09-07 19:51:03 +02:00
|
|
|
for (szByteNr = 0; szByteNr < szLen; szByteNr++) {
|
2012-05-02 23:18:31 +02:00
|
|
|
pbtPar[szByteNr] = oddparity(pbtData[szByteNr]);
|
2010-09-06 12:02:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-09-07 19:51:03 +02:00
|
|
|
void
|
2012-05-29 17:54:36 +02:00
|
|
|
print_hex(const uint8_t *pbtData, const size_t szBytes)
|
2010-04-16 18:38:57 +02:00
|
|
|
{
|
2010-09-07 19:51:03 +02:00
|
|
|
size_t szPos;
|
2010-04-16 18:38:57 +02:00
|
|
|
|
2010-09-07 19:51:03 +02:00
|
|
|
for (szPos = 0; szPos < szBytes; szPos++) {
|
2012-05-29 17:54:36 +02:00
|
|
|
printf("%02x ", pbtData[szPos]);
|
2010-04-16 18:38:57 +02:00
|
|
|
}
|
2012-05-29 17:54:36 +02:00
|
|
|
printf("\n");
|
2010-04-16 18:38:57 +02:00
|
|
|
}
|
|
|
|
|
2010-09-07 19:51:03 +02:00
|
|
|
void
|
2012-05-29 17:54:36 +02:00
|
|
|
print_hex_bits(const uint8_t *pbtData, const size_t szBits)
|
2010-04-16 18:38:57 +02:00
|
|
|
{
|
|
|
|
uint8_t uRemainder;
|
2010-09-07 19:51:03 +02:00
|
|
|
size_t szPos;
|
|
|
|
size_t szBytes = szBits / 8;
|
2010-04-16 18:38:57 +02:00
|
|
|
|
2010-09-07 19:51:03 +02:00
|
|
|
for (szPos = 0; szPos < szBytes; szPos++) {
|
2012-05-29 17:54:36 +02:00
|
|
|
printf("%02x ", pbtData[szPos]);
|
2010-04-16 18:38:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
uRemainder = szBits % 8;
|
|
|
|
// Print the rest bits
|
2010-09-07 19:51:03 +02:00
|
|
|
if (uRemainder != 0) {
|
2010-04-16 18:38:57 +02:00
|
|
|
if (uRemainder < 5)
|
2012-05-29 17:54:36 +02:00
|
|
|
printf("%01x (%d bits)", pbtData[szBytes], uRemainder);
|
2010-04-16 18:38:57 +02:00
|
|
|
else
|
2012-05-29 17:54:36 +02:00
|
|
|
printf("%02x (%d bits)", pbtData[szBytes], uRemainder);
|
2010-04-16 18:38:57 +02:00
|
|
|
}
|
2012-05-29 17:54:36 +02:00
|
|
|
printf("\n");
|
2010-04-16 18:38:57 +02:00
|
|
|
}
|
|
|
|
|
2010-09-07 19:51:03 +02:00
|
|
|
void
|
2012-05-29 17:54:36 +02:00
|
|
|
print_hex_par(const uint8_t *pbtData, const size_t szBits, const uint8_t *pbtDataPar)
|
2010-04-16 18:38:57 +02:00
|
|
|
{
|
|
|
|
uint8_t uRemainder;
|
2010-09-07 19:51:03 +02:00
|
|
|
size_t szPos;
|
|
|
|
size_t szBytes = szBits / 8;
|
|
|
|
|
|
|
|
for (szPos = 0; szPos < szBytes; szPos++) {
|
2012-05-29 17:54:36 +02:00
|
|
|
printf("%02x", pbtData[szPos]);
|
2012-05-02 23:18:31 +02:00
|
|
|
if (oddparity(pbtData[szPos]) != pbtDataPar[szPos]) {
|
2012-05-29 17:54:36 +02:00
|
|
|
printf("! ");
|
2010-04-16 18:38:57 +02:00
|
|
|
} else {
|
2012-05-29 17:54:36 +02:00
|
|
|
printf(" ");
|
2010-04-16 18:38:57 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
uRemainder = szBits % 8;
|
|
|
|
// Print the rest bits, these cannot have parity bit
|
2010-09-07 19:51:03 +02:00
|
|
|
if (uRemainder != 0) {
|
2010-04-16 18:38:57 +02:00
|
|
|
if (uRemainder < 5)
|
2012-05-29 17:54:36 +02:00
|
|
|
printf("%01x (%d bits)", pbtData[szBytes], uRemainder);
|
2010-04-16 18:38:57 +02:00
|
|
|
else
|
2012-05-29 17:54:36 +02:00
|
|
|
printf("%02x (%d bits)", pbtData[szBytes], uRemainder);
|
2010-04-16 18:38:57 +02:00
|
|
|
}
|
2012-05-29 17:54:36 +02:00
|
|
|
printf("\n");
|
2010-04-16 18:38:57 +02:00
|
|
|
}
|
|
|
|
|
2010-10-13 19:43:23 +02:00
|
|
|
void
|
2012-05-29 17:54:36 +02:00
|
|
|
print_nfc_target(const nfc_target nt, bool verbose)
|
2010-10-13 19:43:23 +02:00
|
|
|
{
|
2012-09-17 15:47:54 +02:00
|
|
|
char *s;
|
|
|
|
str_nfc_target(&s, nt, verbose);
|
|
|
|
printf("%s", s);
|
2013-02-16 14:20:37 +01:00
|
|
|
nfc_free(s);
|
2010-10-13 23:40:54 +02:00
|
|
|
}
|