libnfc 1.8.0
nfc-internal.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 * 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
31
32#ifndef __NFC_INTERNAL_H__
33#define __NFC_INTERNAL_H__
34
35#include <stdbool.h>
36#include <err.h>
37#if !defined(_MSC_VER)
38# include <sys/time.h>
39#endif
40
41#include "nfc/nfc.h"
42
43#include "log.h"
44
49#define HAL( FUNCTION, ... ) __extension__ ({int res; \
50 pnd->last_error = 0; \
51 if (pnd->driver->FUNCTION) { \
52 res = pnd->driver->FUNCTION( __VA_ARGS__ ); \
53 } else { \
54 pnd->last_error = NFC_EDEVNOTSUPP; \
55 res = false; \
56 } \
57 res;})
58
59#ifndef MIN
60#define MIN(a,b) (((a) < (b)) ? (a) : (b))
61#endif
62#ifndef MAX
63#define MAX(a,b) (((a) > (b)) ? (a) : (b))
64#endif
65
66/*
67 * Buffer management macros.
68 *
69 * The following macros ease setting-up and using buffers:
70 * BUFFER_INIT (data, 5); // data -> [ xx, xx, xx, xx, xx ]
71 * BUFFER_SIZE (data); // size -> 0
72 * BUFFER_APPEND (data, 0x12); // data -> [ 12, xx, xx, xx, xx ]
73 * BUFFER_SIZE (data); // size -> 1
74 * uint16_t x = 0x3456; // We suppose we are little endian
75 * BUFFER_APPEND_BYTES (data, x, 2);
76 * // data -> [ 12, 56, 34, xx, xx ]
77 * BUFFER_SIZE (data); // size -> 3
78 * BUFFER_APPEND_LE (data, x, 2, sizeof (x));
79 * // data -> [ 12, 56, 34, 34, 56 ]
80 * BUFFER_SIZE (data); // size -> 5
81 */
82
83/*
84 * Initialise a buffer named buffer_name of size bytes.
85 */
86#define BUFFER_INIT(buffer_name, size) \
87 uint8_t buffer_name[size]; \
88 size_t __##buffer_name##_n = 0
89
90/*
91 * Create a wrapper for an existing buffer.
92 * BEWARE! It eats children!
93 */
94#define BUFFER_ALIAS(buffer_name, origin) \
95 uint8_t *buffer_name = (void *)origin; \
96 size_t __##buffer_name##_n = 0;
97
98#define BUFFER_SIZE(buffer_name) (__##buffer_name##_n)
99
100#define BUFFER_CLEAR(buffer_name) (__##buffer_name##_n = 0)
101/*
102 * Append one byte of data to the buffer buffer_name.
103 */
104#define BUFFER_APPEND(buffer_name, data) \
105 do { \
106 buffer_name[__##buffer_name##_n++] = data; \
107 } while (0)
108
109/*
110 * Append size bytes of data to the buffer buffer_name.
111 */
112#define BUFFER_APPEND_BYTES(buffer_name, data, size) \
113 do { \
114 size_t __n = 0; \
115 while (__n < size) { \
116 buffer_name[__##buffer_name##_n++] = ((uint8_t *)data)[__n++]; \
117 } \
118 } while (0)
119
120typedef enum {
121 NOT_INTRUSIVE,
122 INTRUSIVE,
123 NOT_AVAILABLE,
124} scan_type_enum;
125
126struct nfc_driver {
127 const char *name;
128 const scan_type_enum scan_type;
129 size_t (*scan)(const nfc_context *context, nfc_connstring connstrings[], const size_t connstrings_len);
130 struct nfc_device *(*open)(const nfc_context *context, const nfc_connstring connstring);
131 void (*close)(struct nfc_device *pnd);
132 const char *(*strerror)(const struct nfc_device *pnd);
133
134 int (*initiator_init)(struct nfc_device *pnd);
135 int (*initiator_init_secure_element)(struct nfc_device *pnd);
136 int (*initiator_select_passive_target)(struct nfc_device *pnd, const nfc_modulation nm, const uint8_t *pbtInitData, const size_t szInitData, nfc_target *pnt);
137 int (*initiator_poll_target)(struct nfc_device *pnd, const nfc_modulation *pnmModulations, const size_t szModulations, const uint8_t uiPollNr, const uint8_t btPeriod, nfc_target *pnt);
138 int (*initiator_select_dep_target)(struct nfc_device *pnd, const nfc_dep_mode ndm, const nfc_baud_rate nbr, const nfc_dep_info *pndiInitiator, nfc_target *pnt, const int timeout);
139 int (*initiator_deselect_target)(struct nfc_device *pnd);
140 int (*initiator_transceive_bytes)(struct nfc_device *pnd, const uint8_t *pbtTx, const size_t szTx, uint8_t *pbtRx, const size_t szRx, int timeout);
141 int (*initiator_transceive_bits)(struct nfc_device *pnd, const uint8_t *pbtTx, const size_t szTxBits, const uint8_t *pbtTxPar, uint8_t *pbtRx, uint8_t *pbtRxPar);
142 int (*initiator_transceive_bytes_timed)(struct nfc_device *pnd, const uint8_t *pbtTx, const size_t szTx, uint8_t *pbtRx, const size_t szRx, uint32_t *cycles);
143 int (*initiator_transceive_bits_timed)(struct nfc_device *pnd, const uint8_t *pbtTx, const size_t szTxBits, const uint8_t *pbtTxPar, uint8_t *pbtRx, uint8_t *pbtRxPar, uint32_t *cycles);
144 int (*initiator_target_is_present)(struct nfc_device *pnd, const nfc_target *pnt);
145
146 int (*target_init)(struct nfc_device *pnd, nfc_target *pnt, uint8_t *pbtRx, const size_t szRx, int timeout);
147 int (*target_send_bytes)(struct nfc_device *pnd, const uint8_t *pbtTx, const size_t szTx, int timeout);
148 int (*target_receive_bytes)(struct nfc_device *pnd, uint8_t *pbtRx, const size_t szRxLen, int timeout);
149 int (*target_send_bits)(struct nfc_device *pnd, const uint8_t *pbtTx, const size_t szTxBits, const uint8_t *pbtTxPar);
150 int (*target_receive_bits)(struct nfc_device *pnd, uint8_t *pbtRx, const size_t szRxLen, uint8_t *pbtRxPar);
151
152 int (*device_set_property_bool)(struct nfc_device *pnd, const nfc_property property, const bool bEnable);
153 int (*device_set_property_int)(struct nfc_device *pnd, const nfc_property property, const int value);
154 int (*get_supported_modulation)(struct nfc_device *pnd, const nfc_mode mode, const nfc_modulation_type **const supported_mt);
155 int (*get_supported_baud_rate)(struct nfc_device *pnd, const nfc_mode mode, const nfc_modulation_type nmt, const nfc_baud_rate **const supported_br);
156 int (*device_get_information_about)(struct nfc_device *pnd, char **buf);
157
158 int (*abort_command)(struct nfc_device *pnd);
159 int (*idle)(struct nfc_device *pnd);
160 int (*powerdown)(struct nfc_device *pnd);
161};
162
163# define DEVICE_NAME_LENGTH 256
164# define DEVICE_PORT_LENGTH 64
165
166#define MAX_USER_DEFINED_DEVICES 4
167
168struct nfc_user_defined_device {
169 char name[DEVICE_NAME_LENGTH];
170 nfc_connstring connstring;
171 bool optional;
172};
173
180 bool allow_autoscan;
181 bool allow_intrusive_scan;
182 uint32_t log_level;
183 struct nfc_user_defined_device user_defined_devices[MAX_USER_DEFINED_DEVICES];
184 unsigned int user_defined_device_count;
185};
186
187nfc_context *nfc_context_new(void);
188void nfc_context_free(nfc_context *context);
189
195 const nfc_context *context;
196 const struct nfc_driver *driver;
197 void *driver_data;
198 void *chip_data;
199
201 char name[DEVICE_NAME_LENGTH];
205 bool bCrc;
207 bool bPar;
219};
220
221nfc_device *nfc_device_new(const nfc_context *context, const nfc_connstring connstring);
222void nfc_device_free(nfc_device *dev);
223
224void string_as_boolean(const char *s, bool *value);
225
226void iso14443_cascade_uid(const uint8_t abtUID[], const size_t szUID, uint8_t *pbtCascadedUID, size_t *pszCascadedUID);
227
228void prepare_initiator_data(const nfc_modulation nm, uint8_t **ppbtInitiatorData, size_t *pszInitiatorData);
229
230int connstring_decode(const nfc_connstring connstring, const char *driver_name, const char *bus_name, char **pparam1, char **pparam2);
231
232#endif // __NFC_INTERNAL_H__
nfc_mode
NFC mode type enumeration.
Definition nfc-types.h:333
nfc_property
Definition nfc-types.h:68
char nfc_connstring[NFC_BUFSIZE_CONNSTRING]
Definition nfc-types.h:63
nfc_dep_mode
NFC D.E.P. (Data Exchange Protocol) active/passive mode.
Definition nfc-types.h:152
nfc_modulation_type
NFC modulation type enumeration.
Definition nfc-types.h:315
struct nfc_context nfc_context
Definition nfc-types.h:48
struct nfc_device nfc_device
Definition nfc-types.h:53
nfc_baud_rate
NFC baud rate enumeration.
Definition nfc-types.h:303
libnfc interface
NFC library context Struct which contains internal options, references, pointers, etc....
NFC device information.
char name[DEVICE_NAME_LENGTH]
bool bEasyFraming
nfc_connstring connstring
bool bInfiniteSelect
bool bAutoIso14443_4
uint8_t btSupportByte
NFC modulation structure.
Definition nfc-types.h:342