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