libnfc 1.8.0
nfc-utils.c
Go to the documentation of this file.
1/*-
2 * Free/Libre Near Field Communication (NFC) library
3 *
4 * Libnfc historical contributors:
5 * Copyright (C) 2009 Roel Verdult
6 * Copyright (C) 2009-2013 Romuald Conty
7 * Copyright (C) 2010-2012 Romain Tartière
8 * Copyright (C) 2010-2013 Philippe Teuwen
9 * Copyright (C) 2012-2013 Ludovic Rousseau
10 * See AUTHORS file for a more comprehensive list of contributors.
11 * Additional contributors of this file:
12 *
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions are met:
15 * 1) Redistributions of source code must retain the above copyright notice,
16 * this list of conditions and the following disclaimer.
17 * 2 )Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
25 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 * POSSIBILITY OF SUCH DAMAGE.
32 *
33 * Note that this license only applies on the examples, NFC library itself is under LGPL
34 *
35 */
40#include <nfc/nfc.h>
41#include <err.h>
42
43#include "nfc-utils.h"
44
45uint8_t
46oddparity(const uint8_t bt)
47{
48 // cf http://graphics.stanford.edu/~seander/bithacks.html#ParityParallel
49 return (0x9669 >> ((bt ^ (bt >> 4)) & 0xF)) & 1;
50}
51
52void
53oddparity_bytes_ts(const uint8_t *pbtData, const size_t szLen, uint8_t *pbtPar)
54{
55 size_t szByteNr;
56 // Calculate the parity bits for the command
57 for (szByteNr = 0; szByteNr < szLen; szByteNr++) {
58 pbtPar[szByteNr] = oddparity(pbtData[szByteNr]);
59 }
60}
61
62void
63print_hex(const uint8_t *pbtData, const size_t szBytes)
64{
65 size_t szPos;
66
67 for (szPos = 0; szPos < szBytes; szPos++) {
68 printf("%02x ", pbtData[szPos]);
69 }
70 printf("\n");
71}
72
73void
74print_hex_bits(const uint8_t *pbtData, const size_t szBits)
75{
76 uint8_t uRemainder;
77 size_t szPos;
78 size_t szBytes = szBits / 8;
79
80 for (szPos = 0; szPos < szBytes; szPos++) {
81 printf("%02x ", pbtData[szPos]);
82 }
83
84 uRemainder = szBits % 8;
85 // Print the rest bits
86 if (uRemainder != 0) {
87 if (uRemainder < 5)
88 printf("%01x (%d bits)", pbtData[szBytes], uRemainder);
89 else
90 printf("%02x (%d bits)", pbtData[szBytes], uRemainder);
91 }
92 printf("\n");
93}
94
95void
96print_hex_par(const uint8_t *pbtData, const size_t szBits, const uint8_t *pbtDataPar)
97{
98 uint8_t uRemainder;
99 size_t szPos;
100 size_t szBytes = szBits / 8;
101
102 for (szPos = 0; szPos < szBytes; szPos++) {
103 printf("%02x", pbtData[szPos]);
104 if (oddparity(pbtData[szPos]) != pbtDataPar[szPos]) {
105 printf("! ");
106 } else {
107 printf(" ");
108 }
109 }
110
111 uRemainder = szBits % 8;
112 // Print the rest bits, these cannot have parity bit
113 if (uRemainder != 0) {
114 if (uRemainder < 5)
115 printf("%01x (%d bits)", pbtData[szBytes], uRemainder);
116 else
117 printf("%02x (%d bits)", pbtData[szBytes], uRemainder);
118 }
119 printf("\n");
120}
121
122void
123print_nfc_target(const nfc_target *pnt, bool verbose)
124{
125 char *s;
126 str_nfc_target(&s, pnt, verbose);
127 printf("%s", s);
128 nfc_free(s);
129}
void nfc_free(void *p)
Free buffer allocated by libnfc.
Definition: nfc.c:1334
int str_nfc_target(char **buf, const nfc_target *pnt, bool verbose)
Convert nfc_target content to string.
Definition: nfc.c:1421
Provide some examples shared functions like print, parity calculation, options parsing.
libnfc interface
NFC target structure.
Definition: nfc-types.h:351