nfc.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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. * This program is free software: you can redistribute it and/or modify it
  14. * under the terms of the GNU Lesser General Public License as published by the
  15. * Free Software Foundation, either version 3 of the License, or (at your
  16. * option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful, but WITHOUT
  19. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  20. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  21. * more details.
  22. *
  23. * You should have received a copy of the GNU Lesser General Public License
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>
  25. */
  26. /**
  27. * @file nfc.h
  28. * @brief libnfc interface
  29. *
  30. * Provide all usefull functions (API) to handle NFC devices.
  31. */
  32. #ifndef _LIBNFC_H_
  33. # define _LIBNFC_H_
  34. //# include <sys/time.h>
  35. # include <stdint.h>
  36. # include <stdbool.h>
  37. # ifdef _WIN32
  38. /* Windows platform */
  39. # ifndef _WINDLL
  40. /* CMake compilation */
  41. # ifdef nfc_EXPORTS
  42. # define NFC_EXPORT __declspec(dllexport)
  43. # else
  44. /* nfc_EXPORTS */
  45. # define NFC_EXPORT __declspec(dllimport)
  46. # endif
  47. /* nfc_EXPORTS */
  48. # else
  49. /* _WINDLL */
  50. /* Manual makefile */
  51. # define NFC_EXPORT
  52. # endif
  53. /* _WINDLL */
  54. # else
  55. /* _WIN32 */
  56. # define NFC_EXPORT
  57. # endif
  58. /* _WIN32 */
  59. # include <nfc/nfc-types.h>
  60. #include "pn532_uart.h"
  61. # ifndef __has_attribute
  62. # define __has_attribute(x) 0
  63. # endif
  64. # if __has_attribute(nonnull) || defined(__GNUC__)
  65. # define __has_attribute_nonnull 1
  66. # endif
  67. # if __has_attribute_nonnull
  68. # define ATTRIBUTE_NONNULL( param ) __attribute__((nonnull (param)))
  69. # else
  70. # define ATTRIBUTE_NONNULL( param )
  71. # endif
  72. # ifdef __cplusplus
  73. extern "C" {
  74. # endif // __cplusplus
  75. /* Library initialization/deinitialization */
  76. NFC_EXPORT void nfc_init() ATTRIBUTE_NONNULL(1);
  77. NFC_EXPORT void nfc_exit() ATTRIBUTE_NONNULL(1);
  78. /* NFC Device/Hardware manipulation */
  79. NFC_EXPORT nfc_device *nfc_open(const char* uart_name) ATTRIBUTE_NONNULL(1);
  80. NFC_EXPORT void nfc_close(nfc_device *pnd);
  81. NFC_EXPORT int nfc_abort_command(nfc_device *pnd);
  82. NFC_EXPORT size_t nfc_list_devices(const void* ports[], const size_t szPorts) ATTRIBUTE_NONNULL(1);
  83. NFC_EXPORT int nfc_idle(nfc_device *pnd);
  84. /* NFC initiator: act as "reader" */
  85. NFC_EXPORT int nfc_initiator_init(nfc_device *pnd);
  86. NFC_EXPORT int nfc_initiator_init_secure_element(nfc_device *pnd);
  87. NFC_EXPORT int nfc_initiator_select_passive_target(nfc_device *pnd, const nfc_modulation nm, const uint8_t *pbtInitData, const size_t szInitData, nfc_target *pnt);
  88. NFC_EXPORT int nfc_initiator_list_passive_targets(nfc_device *pnd, const nfc_modulation nm, nfc_target ant[], const size_t szTargets);
  89. NFC_EXPORT int nfc_initiator_poll_target(nfc_device *pnd, const nfc_modulation *pnmTargetTypes, const size_t szTargetTypes, const uint8_t uiPollNr, const uint8_t uiPeriod, nfc_target *pnt);
  90. NFC_EXPORT int nfc_initiator_select_dep_target(nfc_device *pnd, const nfc_dep_mode ndm, const nfc_baud_rate nbr, const nfc_dep_info *pndiInitiator, nfc_target *pnt, const int timeout);
  91. NFC_EXPORT int nfc_initiator_poll_dep_target(nfc_device *pnd, const nfc_dep_mode ndm, const nfc_baud_rate nbr, const nfc_dep_info *pndiInitiator, nfc_target *pnt, const int timeout);
  92. NFC_EXPORT int nfc_initiator_deselect_target(nfc_device *pnd);
  93. NFC_EXPORT int nfc_initiator_transceive_bytes(nfc_device *pnd, const uint8_t *pbtTx, const size_t szTx, uint8_t *pbtRx, const size_t szRx, int timeout);
  94. NFC_EXPORT int nfc_initiator_transceive_bits(nfc_device *pnd, const uint8_t *pbtTx, const size_t szTxBits, const uint8_t *pbtTxPar, uint8_t *pbtRx, const size_t szRx, uint8_t *pbtRxPar);
  95. NFC_EXPORT int nfc_initiator_transceive_bytes_timed(nfc_device *pnd, const uint8_t *pbtTx, const size_t szTx, uint8_t *pbtRx, const size_t szRx, uint32_t *cycles);
  96. NFC_EXPORT int nfc_initiator_transceive_bits_timed(nfc_device *pnd, const uint8_t *pbtTx, const size_t szTxBits, const uint8_t *pbtTxPar, uint8_t *pbtRx, const size_t szRx, uint8_t *pbtRxPar, uint32_t *cycles);
  97. NFC_EXPORT int nfc_initiator_target_is_present(nfc_device *pnd, const nfc_target *pnt);
  98. /* NFC target: act as tag (i.e. MIFARE Classic) or NFC target device. */
  99. NFC_EXPORT int nfc_target_init(nfc_device *pnd, nfc_target *pnt, uint8_t *pbtRx, const size_t szRx, int timeout);
  100. NFC_EXPORT int nfc_target_send_bytes(nfc_device *pnd, const uint8_t *pbtTx, const size_t szTx, int timeout);
  101. NFC_EXPORT int nfc_target_receive_bytes(nfc_device *pnd, uint8_t *pbtRx, const size_t szRx, int timeout);
  102. NFC_EXPORT int nfc_target_send_bits(nfc_device *pnd, const uint8_t *pbtTx, const size_t szTxBits, const uint8_t *pbtTxPar);
  103. NFC_EXPORT int nfc_target_receive_bits(nfc_device *pnd, uint8_t *pbtRx, const size_t szRx, uint8_t *pbtRxPar);
  104. /* Error reporting */
  105. NFC_EXPORT const char *nfc_strerror(const nfc_device *pnd);
  106. NFC_EXPORT int nfc_strerror_r(const nfc_device *pnd, char *buf, size_t buflen);
  107. NFC_EXPORT void nfc_perror(const nfc_device *pnd, const char *s);
  108. NFC_EXPORT int nfc_device_get_last_error(const nfc_device *pnd);
  109. /* Special data accessors */
  110. NFC_EXPORT const char *nfc_device_get_name(nfc_device *pnd);
  111. NFC_EXPORT const char *nfc_device_get_connstring(nfc_device *pnd);
  112. NFC_EXPORT int nfc_device_get_supported_modulation(nfc_device *pnd, const nfc_mode mode, const nfc_modulation_type **const supported_mt);
  113. NFC_EXPORT int nfc_device_get_supported_baud_rate(nfc_device *pnd, const nfc_modulation_type nmt, const nfc_baud_rate **const supported_br);
  114. /* Properties accessors */
  115. NFC_EXPORT int nfc_device_set_property_int(nfc_device *pnd, const nfc_property property, const int value);
  116. NFC_EXPORT int nfc_device_set_property_bool(nfc_device *pnd, const nfc_property property, const bool bEnable);
  117. /* Misc. functions */
  118. NFC_EXPORT void iso14443a_crc(uint8_t *pbtData, size_t szLen, uint8_t *pbtCrc);
  119. NFC_EXPORT void iso14443a_crc_append(uint8_t *pbtData, size_t szLen);
  120. NFC_EXPORT void iso14443b_crc(uint8_t *pbtData, size_t szLen, uint8_t *pbtCrc);
  121. NFC_EXPORT void iso14443b_crc_append(uint8_t *pbtData, size_t szLen);
  122. NFC_EXPORT uint8_t *iso14443a_locate_historical_bytes(uint8_t *pbtAts, size_t szAts, size_t *pszTk);
  123. NFC_EXPORT void nfc_free(void *p);
  124. NFC_EXPORT const char *nfc_version(void);
  125. NFC_EXPORT int nfc_device_get_information_about(nfc_device *pnd, char **buf);
  126. /* String converter functions */
  127. NFC_EXPORT const char *str_nfc_modulation_type(const nfc_modulation_type nmt);
  128. NFC_EXPORT const char *str_nfc_baud_rate(const nfc_baud_rate nbr);
  129. NFC_EXPORT int str_nfc_target(char **buf, const nfc_target *pnt, bool verbose);
  130. /* Error codes */
  131. /** @ingroup error
  132. * @hideinitializer
  133. * Success (no error)
  134. */
  135. #define NFC_SUCCESS 0
  136. /** @ingroup error
  137. * @hideinitializer
  138. * Input / output error, device may not be usable anymore without re-open it
  139. */
  140. #define NFC_EIO -1
  141. /** @ingroup error
  142. * @hideinitializer
  143. * Invalid argument(s)
  144. */
  145. #define NFC_EINVARG -2
  146. /** @ingroup error
  147. * @hideinitializer
  148. * Operation not supported by device
  149. */
  150. #define NFC_EDEVNOTSUPP -3
  151. /** @ingroup error
  152. * @hideinitializer
  153. * No such device
  154. */
  155. #define NFC_ENOTSUCHDEV -4
  156. /** @ingroup error
  157. * @hideinitializer
  158. * Buffer overflow
  159. */
  160. #define NFC_EOVFLOW -5
  161. /** @ingroup error
  162. * @hideinitializer
  163. * Operation timed out
  164. */
  165. #define NFC_ETIMEOUT -6
  166. /** @ingroup error
  167. * @hideinitializer
  168. * Operation aborted (by user)
  169. */
  170. #define NFC_EOPABORTED -7
  171. /** @ingroup error
  172. * @hideinitializer
  173. * Not (yet) implemented
  174. */
  175. #define NFC_ENOTIMPL -8
  176. /** @ingroup error
  177. * @hideinitializer
  178. * Target released
  179. */
  180. #define NFC_ETGRELEASED -10
  181. /** @ingroup error
  182. * @hideinitializer
  183. * Error while RF transmission
  184. */
  185. #define NFC_ERFTRANS -20
  186. /** @ingroup error
  187. * @hideinitializer
  188. * MIFARE Classic: authentication failed
  189. */
  190. #define NFC_EMFCAUTHFAIL -30
  191. /** @ingroup error
  192. * @hideinitializer
  193. * Software error (allocation, file/pipe creation, etc.)
  194. */
  195. #define NFC_ESOFT -80
  196. /** @ingroup error
  197. * @hideinitializer
  198. * Device's internal chip error
  199. */
  200. #define NFC_ECHIP -90
  201. # ifdef __cplusplus
  202. }
  203. # endif // __cplusplus
  204. #endif // _LIBNFC_H_