nfc-utils.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. */
  36. /**
  37. * @file nfc-utils.c
  38. * @brief Provide some examples shared functions like print, parity calculation, options parsing.
  39. */
  40. #include <nfc/nfc.h>
  41. // #include <err.h>
  42. #include "nfc-utils.h"
  43. uint8_t
  44. oddparity(const uint8_t bt)
  45. {
  46. // cf http://graphics.stanford.edu/~seander/bithacks.html#ParityParallel
  47. return (0x9669 >> ((bt ^ (bt >> 4)) & 0xF)) & 1;
  48. }
  49. void
  50. oddparity_bytes_ts(const uint8_t *pbtData, const size_t szLen, uint8_t *pbtPar)
  51. {
  52. size_t szByteNr;
  53. // Calculate the parity bits for the command
  54. for (szByteNr = 0; szByteNr < szLen; szByteNr++) {
  55. pbtPar[szByteNr] = oddparity(pbtData[szByteNr]);
  56. }
  57. }
  58. void
  59. print_hex(const uint8_t *pbtData, const size_t szBytes)
  60. {
  61. size_t szPos;
  62. for (szPos = 0; szPos < szBytes; szPos++) {
  63. printf("%02x ", pbtData[szPos]);
  64. }
  65. printf("\n");
  66. }
  67. void
  68. print_hex_bits(const uint8_t *pbtData, const size_t szBits)
  69. {
  70. uint8_t uRemainder;
  71. size_t szPos;
  72. size_t szBytes = szBits / 8;
  73. for (szPos = 0; szPos < szBytes; szPos++) {
  74. printf("%02x ", pbtData[szPos]);
  75. }
  76. uRemainder = szBits % 8;
  77. // Print the rest bits
  78. if (uRemainder != 0) {
  79. if (uRemainder < 5)
  80. printf("%01x (%d bits)", pbtData[szBytes], uRemainder);
  81. else
  82. printf("%02x (%d bits)", pbtData[szBytes], uRemainder);
  83. }
  84. printf("\n");
  85. }
  86. void
  87. print_hex_par(const uint8_t *pbtData, const size_t szBits, const uint8_t *pbtDataPar)
  88. {
  89. uint8_t uRemainder;
  90. size_t szPos;
  91. size_t szBytes = szBits / 8;
  92. for (szPos = 0; szPos < szBytes; szPos++) {
  93. printf("%02x", pbtData[szPos]);
  94. if (oddparity(pbtData[szPos]) != pbtDataPar[szPos]) {
  95. printf("! ");
  96. } else {
  97. printf(" ");
  98. }
  99. }
  100. uRemainder = szBits % 8;
  101. // Print the rest bits, these cannot have parity bit
  102. if (uRemainder != 0) {
  103. if (uRemainder < 5)
  104. printf("%01x (%d bits)", pbtData[szBytes], uRemainder);
  105. else
  106. printf("%02x (%d bits)", pbtData[szBytes], uRemainder);
  107. }
  108. printf("\n");
  109. }
  110. void
  111. print_nfc_target(const nfc_target *pnt, bool verbose)
  112. {
  113. char *s;
  114. str_nfc_target(&s, pnt, verbose);
  115. printf("%s", s);
  116. nfc_free(s);
  117. }