You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

nfc-utils.c 3.8KB

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