nfc-utils.c 3.82 KB
Newer Older
1
/*-
2
 * Free/Libre Near Field Communication (NFC) library
3
 *
4 5 6 7 8 9
 * Libnfc historical contributors:
 * Copyright (C) 2009      Roel Verdult
 * Copyright (C) 2009-2013 Romuald Conty
 * Copyright (C) 2010-2012 Romain Tartière
 * Copyright (C) 2010-2013 Philippe Teuwen
 * 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 14 15
 * 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,
16
 *  this list of conditions and the following disclaimer.
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
 *  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.
32
 *
33 34 35
 * Note that this license only applies on the examples, NFC library itself is under LGPL
 *
 */
36 37 38 39
/**
 * @file nfc-utils.c
 * @brief Provide some examples shared functions like print, parity calculation, options parsing.
 */
40
#include <nfc/nfc.h>
41
#include <err.h>
42 43 44

#include "nfc-utils.h"

45
uint8_t
46
oddparity(const uint8_t bt)
47
{
48
  // cf http://graphics.stanford.edu/~seander/bithacks.html#ParityParallel
Philippe Teuwen's avatar
Philippe Teuwen committed
49
  return (0x9669 >> ((bt ^ (bt >> 4)) & 0xF)) & 1;
50 51
}

52
void
53
oddparity_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
  }
}

62
void
63
print_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
}

73
void
74
print_hex_bits(const uint8_t *pbtData, const size_t szBits)
75 76
{
  uint8_t uRemainder;
77 78
  size_t  szPos;
  size_t  szBytes = szBits / 8;
79

80
  for (szPos = 0; szPos < szBytes; szPos++) {
81
    printf("%02x  ", pbtData[szPos]);
82 83 84 85
  }

  uRemainder = szBits % 8;
  // 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
}

95
void
96
print_hex_par(const uint8_t *pbtData, const size_t szBits, const uint8_t *pbtDataPar)
97 98
{
  uint8_t uRemainder;
99 100 101 102
  size_t  szPos;
  size_t  szBytes = szBits / 8;

  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 112
    }
  }

  uRemainder = szBits % 8;
  // 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
}

122
void
123
print_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
}