libnfc  1.7.0-rc7
nfc.h
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  * Additional contributors of this file:
11  *
12  * This program is free software: you can redistribute it and/or modify it
13  * under the terms of the GNU Lesser General Public License as published by the
14  * Free Software Foundation, either version 3 of the License, or (at your
15  * option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful, but WITHOUT
18  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
19  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
20  * more details.
21  *
22  * You should have received a copy of the GNU Lesser General Public License
23  * along with this program. If not, see <http://www.gnu.org/licenses/>
24  */
25 
33 #ifndef _LIBNFC_H_
34 # define _LIBNFC_H_
35 
36 # include <sys/time.h>
37 
38 # include <stdint.h>
39 # include <stdbool.h>
40 
41 # ifdef _WIN32
42 /* Windows platform */
43 # ifndef _WINDLL
44 /* CMake compilation */
45 # ifdef nfc_EXPORTS
46 # define NFC_EXPORT __declspec(dllexport)
47 # else
48 /* nfc_EXPORTS */
49 # define NFC_EXPORT __declspec(dllimport)
50 # endif
51 /* nfc_EXPORTS */
52 # else
53 /* _WINDLL */
54 /* Manual makefile */
55 # define NFC_EXPORT
56 # endif
57 /* _WINDLL */
58 # else
59 /* _WIN32 */
60 # define NFC_EXPORT
61 # endif
62 /* _WIN32 */
63 
64 # include <nfc/nfc-types.h>
65 
66 # ifndef __has_attribute
67 # define __has_attribute(x) 0
68 # endif
69 
70 # if __has_attribute(nonnull) || defined(__GNUC__)
71 # define __has_attribute_nonnull 1
72 # endif
73 
74 # if __has_attribute_nonnull
75 # define ATTRIBUTE_NONNULL( param ) __attribute__((nonnull (param)))
76 # else
77 # define ATTRIBUTE_NONNULL( param )
78 # endif
79 
80 # ifdef __cplusplus
81 extern "C" {
82 # endif // __cplusplus
83 
84  /* Library initialization/deinitialization */
85  NFC_EXPORT void nfc_init(nfc_context **context) ATTRIBUTE_NONNULL(1);
86  NFC_EXPORT void nfc_exit(nfc_context *context) ATTRIBUTE_NONNULL(1);
87  NFC_EXPORT int nfc_register_driver(const nfc_driver *driver);
88 
89  /* NFC Device/Hardware manipulation */
90  NFC_EXPORT nfc_device *nfc_open(nfc_context *context, const nfc_connstring connstring) ATTRIBUTE_NONNULL(1);
91  NFC_EXPORT void nfc_close(nfc_device *pnd);
92  NFC_EXPORT int nfc_abort_command(nfc_device *pnd);
93  NFC_EXPORT size_t nfc_list_devices(nfc_context *context, nfc_connstring connstrings[], size_t connstrings_len) ATTRIBUTE_NONNULL(1);
94  NFC_EXPORT int nfc_idle(nfc_device *pnd);
95 
96  /* NFC initiator: act as "reader" */
97  NFC_EXPORT int nfc_initiator_init(nfc_device *pnd);
98  NFC_EXPORT int nfc_initiator_init_secure_element(nfc_device *pnd);
99  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);
100  NFC_EXPORT int nfc_initiator_list_passive_targets(nfc_device *pnd, const nfc_modulation nm, nfc_target ant[], const size_t szTargets);
101  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);
102  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);
103  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);
104  NFC_EXPORT int nfc_initiator_deselect_target(nfc_device *pnd);
105  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);
106  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);
107  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);
108  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);
109  NFC_EXPORT int nfc_initiator_target_is_present(nfc_device *pnd, const nfc_target *pnt);
110 
111  /* NFC target: act as tag (i.e. MIFARE Classic) or NFC target device. */
112  NFC_EXPORT int nfc_target_init(nfc_device *pnd, nfc_target *pnt, uint8_t *pbtRx, const size_t szRx, int timeout);
113  NFC_EXPORT int nfc_target_send_bytes(nfc_device *pnd, const uint8_t *pbtTx, const size_t szTx, int timeout);
114  NFC_EXPORT int nfc_target_receive_bytes(nfc_device *pnd, uint8_t *pbtRx, const size_t szRx, int timeout);
115  NFC_EXPORT int nfc_target_send_bits(nfc_device *pnd, const uint8_t *pbtTx, const size_t szTxBits, const uint8_t *pbtTxPar);
116  NFC_EXPORT int nfc_target_receive_bits(nfc_device *pnd, uint8_t *pbtRx, const size_t szRx, uint8_t *pbtRxPar);
117 
118  /* Error reporting */
119  NFC_EXPORT const char *nfc_strerror(const nfc_device *pnd);
120  NFC_EXPORT int nfc_strerror_r(const nfc_device *pnd, char *buf, size_t buflen);
121  NFC_EXPORT void nfc_perror(const nfc_device *pnd, const char *s);
122  NFC_EXPORT int nfc_device_get_last_error(const nfc_device *pnd);
123 
124  /* Special data accessors */
125  NFC_EXPORT const char *nfc_device_get_name(nfc_device *pnd);
126  NFC_EXPORT const char *nfc_device_get_connstring(nfc_device *pnd);
127  NFC_EXPORT int nfc_device_get_supported_modulation(nfc_device *pnd, const nfc_mode mode, const nfc_modulation_type **const supported_mt);
128  NFC_EXPORT int nfc_device_get_supported_baud_rate(nfc_device *pnd, const nfc_modulation_type nmt, const nfc_baud_rate **const supported_br);
129 
130  /* Properties accessors */
131  NFC_EXPORT int nfc_device_set_property_int(nfc_device *pnd, const nfc_property property, const int value);
132  NFC_EXPORT int nfc_device_set_property_bool(nfc_device *pnd, const nfc_property property, const bool bEnable);
133 
134  /* Misc. functions */
135  NFC_EXPORT void iso14443a_crc(uint8_t *pbtData, size_t szLen, uint8_t *pbtCrc);
136  NFC_EXPORT void iso14443a_crc_append(uint8_t *pbtData, size_t szLen);
137  NFC_EXPORT uint8_t *iso14443a_locate_historical_bytes(uint8_t *pbtAts, size_t szAts, size_t *pszTk);
138 
139  NFC_EXPORT void nfc_free(void *p);
140  NFC_EXPORT const char *nfc_version(void);
141  NFC_EXPORT int nfc_device_get_information_about(nfc_device *pnd, char **buf);
142 
143  /* String converter functions */
144  NFC_EXPORT const char *str_nfc_modulation_type(const nfc_modulation_type nmt);
145  NFC_EXPORT const char *str_nfc_baud_rate(const nfc_baud_rate nbr);
146  NFC_EXPORT int str_nfc_target(char **buf, const nfc_target *pnt, bool verbose);
147 
148  /* Error codes */
153 #define NFC_SUCCESS 0
154 
158 #define NFC_EIO -1
159 
163 #define NFC_EINVARG -2
164 
168 #define NFC_EDEVNOTSUPP -3
169 
173 #define NFC_ENOTSUCHDEV -4
174 
178 #define NFC_EOVFLOW -5
179 
183 #define NFC_ETIMEOUT -6
184 
188 #define NFC_EOPABORTED -7
189 
193 #define NFC_ENOTIMPL -8
194 
198 #define NFC_ETGRELEASED -10
199 
203 #define NFC_ERFTRANS -20
204 
208 #define NFC_EMFCAUTHFAIL -30
209 
213 #define NFC_ESOFT -80
214 
218 #define NFC_ECHIP -90
219 
220 
221 # ifdef __cplusplus
222 }
223 # endif // __cplusplus
224 #endif // _LIBNFC_H_