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