Codebase list libfreefare / 397222c
Removes --enable-serial-autoprobe option at compile and replace this feature by LIBNFC_INTRUSIVE_SCAN environment variable at run time. Romuald Conty 11 years ago
9 changed file(s) with 86 addition(s) and 61 deletion(s). Raw diff Collapse all Expand all
8383 # Handle --with-drivers option
8484 LIBNFC_ARG_WITH_DRIVERS
8585
86 # Serial autoprobing support (default:no)
87 AC_ARG_ENABLE([serial-autoprobe],AS_HELP_STRING([--enable-serial-autoprobe],[Allow serial ports to be probed (can seriously disturb connected serial devices)]),[enable_serial_autoprobe=$enableval],[enable_serial_autoprobe="no"])
88
89 AC_MSG_CHECKING(for serial autoprobe flag)
90 AC_MSG_RESULT($enable_serial_autoprobe)
91
92 if test x"$enable_serial_autoprobe" = "xyes"
93 then
94 CFLAGS="$CFLAGS -DSERIAL_AUTOPROBE_ENABLED"
95 fi
96
9786 # Documentation (default: no)
9887 AC_ARG_ENABLE([doc],AS_HELP_STRING([--enable-doc],[Enable documentation generation.]),[enable_doc=$enableval],[enable_doc="no"])
9988
22 *
33 * Copyright (C) 2009 Roel Verdult
44 * Copyright (C) 2010, 2011 Romain Tartière
5 * Copyright (C) 2010, 2011 Romuald Conty
5 * Copyright (C) 2010, 2011, 2012 Romuald Conty
66 *
77 * This program is free software: you can redistribute it and/or modify it
88 * under the terms of the GNU Lesser General Public License as published by the
3131 # include <stdbool.h>
3232 # include <stdio.h>
3333
34 typedef void *nfc_context;
34 /**
35 * NFC context
36 */
37 typedef struct nfc_context nfc_context;
3538
3639 /**
3740 * NFC device
324324 }
325325
326326 size_t device_found = 0;
327
328327 uint32_t uiBusIndex = 0;
329328 struct usb_bus *bus;
330329 for (bus = usb_get_busses(); bus; bus = bus->next) {
861860
862861 const struct nfc_driver acr122_usb_driver = {
863862 .name = ACR122_USB_DRIVER_NAME,
863 .scan_type = NOT_INTRUSIVE,
864864 .scan = acr122_usb_scan,
865865 .open = acr122_usb_open,
866866 .close = acr122_usb_close,
449449 static size_t
450450 acr122s_scan(nfc_connstring connstrings[], const size_t connstrings_len)
451451 {
452 /** @note: Due to UART bus we can't know if its really an ACR122S without
453 * sending some commands. But using this way to scan devices, we can
454 * have serious problem with other device on this bus */
455 #ifndef SERIAL_AUTOPROBE_ENABLED
456 (void) connstrings;
457 (void) connstrings_len;
458 log_put(LOG_CATEGORY, NFC_PRIORITY_INFO, "%s", "Serial auto-probing have been disabled at compile time. Skipping autoscan.");
459 return 0;
460 #else /* SERIAL_AUTOPROBE_ENABLED */
461452 size_t device_found = 0;
462453 serial_port sp;
463454 char **acPorts = uart_list_ports();
520511 }
521512 free(acPorts);
522513 return device_found;
523 #endif /* SERIAL_AUTOPROBE_ENABLED */
524514 }
525515
526516 static void
702692
703693 const struct nfc_driver acr122s_driver = {
704694 .name = ACR122S_DRIVER_NAME,
695 .scan_type = INTRUSIVE,
705696 .scan = acr122s_scan,
706697 .open = acr122s_open,
707698 .close = acr122s_close,
9393 static size_t
9494 arygon_scan(nfc_connstring connstrings[], const size_t connstrings_len)
9595 {
96 /** @note: Due to UART bus we can't know if its really an ARYGON without
97 * sending some commands. But using this way to scan devices, we can
98 * have serious problem with other device on this bus */
99 #ifndef SERIAL_AUTOPROBE_ENABLED
100 (void) connstrings;
101 (void) connstrings_len;
102 log_put(LOG_CATEGORY, NFC_PRIORITY_INFO, "%s", "Serial auto-probing have been disabled at compile time. Skipping autoscan.");
103 return 0;
104 #else /* SERIAL_AUTOPROBE_ENABLED */
10596 size_t device_found = 0;
10697 serial_port sp;
10798 char **acPorts = uart_list_ports();
158149 }
159150 free(acPorts);
160151 return device_found;
161 #endif /* SERIAL_AUTOPROBE_ENABLED */
162152 }
163153
164154 struct arygon_descriptor {
563553
564554 const struct nfc_driver arygon_driver = {
565555 .name = ARYGON_DRIVER_NAME,
556 .scan_type = INTRUSIVE,
566557 .scan = arygon_scan,
567558 .open = arygon_open,
568559 .close = arygon_close,
6666 static size_t
6767 pn532_uart_scan(nfc_connstring connstrings[], const size_t connstrings_len)
6868 {
69 /** @note: Due to UART bus we can't know if its really a pn532 without
70 * sending some PN53x commands. But using this way to scan devices, we can
71 * have serious problem with other device on this bus */
72 #ifndef SERIAL_AUTOPROBE_ENABLED
73 (void) connstrings;
74 (void) connstrings_len;
75 log_put(LOG_CATEGORY, NFC_PRIORITY_INFO, "%s", "Serial auto-probing have been disabled at compile time. Skipping autoscan.");
76 return 0;
77 #else /* SERIAL_AUTOPROBE_ENABLED */
7869 size_t device_found = 0;
7970 serial_port sp;
8071 char **acPorts = uart_list_ports();
135126 }
136127 free(acPorts);
137128 return device_found;
138 #endif /* SERIAL_AUTOPROBE_ENABLED */
139129 }
140130
141131 struct pn532_uart_descriptor {
504494
505495 const struct nfc_driver pn532_uart_driver = {
506496 .name = PN532_UART_DRIVER_NAME,
497 .scan_type = INTRUSIVE,
507498 .scan = pn532_uart_scan,
508499 .open = pn532_uart_open,
509500 .close = pn532_uart_close,
00 /*-
11 * Public platform independent Near Field Communication (NFC) library
22 *
3 * Copyright (C) 2011 Romuald Conty
3 * Copyright (C) 2011, 2012 Romuald Conty
44 *
55 * This program is free software: you can redistribute it and/or modify it
66 * under the terms of the GNU Lesser General Public License as published by the
2323
2424 #include <nfc/nfc.h>
2525 #include "nfc-internal.h"
26
27 #include <stdlib.h>
28 #include <string.h>
29
30 static bool
31 string_as_boolean(const char* s)
32 {
33 if ((s) && (
34 (strcmp(s, "yes") == 0) ||
35 (strcmp(s, "true") == 0) ||
36 (strcmp(s, "1") == 0))) {
37 return true;
38 }
39 return false;
40 }
41
42 nfc_context *
43 nfc_context_new(void)
44 {
45 nfc_context *res = malloc(sizeof(*res));
46
47 if (!res) {
48 err(EXIT_FAILURE, "nfc_context_new: malloc");
49 }
50
51 // Load "intrusive scan" option
52 // XXX: Load this option from configuration file too ?
53 char *envvar = getenv("LIBNFC_INTRUSIVE_SCAN");
54 res->allow_intrusive_scan = string_as_boolean(envvar);
55 log_put ("libnfc", NFC_PRIORITY_DEBUG, "allow_intrusive_scan is set to %s", (res->allow_intrusive_scan)?"true":"false");
56 return res;
57 }
58
59 void
60 nfc_context_free(nfc_context *context)
61 {
62 free(context);
63 }
2664
2765 void
2866 prepare_initiator_data(const nfc_modulation nm, uint8_t **ppbtInitiatorData, size_t *pszInitiatorData)
106106 } \
107107 } while (0)
108108
109 typedef enum {
110 NOT_INTRUSIVE,
111 INTRUSIVE,
112 NOT_AVAILABLE,
113 } scan_type_enum;
114
109115 struct nfc_driver {
110116 const char *name;
117 const scan_type_enum scan_type;
111118 size_t (*scan)(nfc_connstring connstrings[], const size_t connstrings_len);
112119 struct nfc_device *(*open)(const nfc_connstring connstring);
113120 void (*close)(struct nfc_device *pnd);
143150
144151 # define DEVICE_NAME_LENGTH 256
145152 # define DEVICE_PORT_LENGTH 64
153
154 /**
155 * @struct nfc_context
156 * @brief NFC library context
157 * Struct which contains internal options, references, pointers, etc. used by library
158 */
159 struct nfc_context {
160 bool allow_intrusive_scan;
161 };
162
163 nfc_context *nfc_context_new(void);
164 void nfc_context_free(nfc_context *context);
146165
147166 /**
148167 * @struct nfc_device
172191 int last_error;
173192 };
174193
175 nfc_device *nfc_device_new(const nfc_connstring connstring);
176 void nfc_device_free(nfc_device *dev);
177
178 void iso14443_cascade_uid(const uint8_t abtUID[], const size_t szUID, uint8_t *pbtCascadedUID, size_t *pszCascadedUID);
179
180 void prepare_initiator_data(const nfc_modulation nm, uint8_t **ppbtInitiatorData, size_t *pszInitiatorData);
194 nfc_device *nfc_device_new(const nfc_connstring connstring);
195 void nfc_device_free(nfc_device *dev);
196
197 void iso14443_cascade_uid(const uint8_t abtUID[], const size_t szUID, uint8_t *pbtCascadedUID, size_t *pszCascadedUID);
198
199 void prepare_initiator_data(const nfc_modulation nm, uint8_t **ppbtInitiatorData, size_t *pszInitiatorData);
181200
182201 #endif // __NFC_INTERNAL_H__
124124 void
125125 nfc_exit(nfc_context *context)
126126 {
127 (void) context;
127 if(context) nfc_context_free(context);
128128 log_fini();
129129 }
130130
250250 }
251251
252252 /** @ingroup dev
253 * @brief Probe for discoverable supported devices (ie. only available for some drivers)
253 * @brief Scan for discoverable supported devices (ie. only available for some drivers)
254254 * @return Returns the number of devices found.
255255 * @param context The context to operate on, or NULL for the default context.
256256 * @param connstrings array of \a nfc_connstring.
264264 const struct nfc_driver *ndr;
265265 const struct nfc_driver **pndr = nfc_drivers;
266266
267 (void) context;
267 if (!context) context = nfc_context_new(); // Should we support NULL context ?
268 // FIXME: Load device(s) from configuration file(s)
268269
269270 while ((ndr = *pndr)) {
270271 size_t _device_found = 0;
271 _device_found = ndr->scan(connstrings + (device_found), connstrings_len - (device_found));
272 log_put(LOG_CATEGORY, NFC_PRIORITY_TRACE, "%ld device(s) found using %s driver", (unsigned long) _device_found, ndr->name);
273 if (_device_found > 0) {
274 device_found += _device_found;
275 if (device_found == connstrings_len)
276 break;
277 }
272 if((ndr->scan_type == NOT_INTRUSIVE) || ((context->allow_intrusive_scan) && (ndr->scan_type == INTRUSIVE))) {
273 _device_found = ndr->scan(connstrings + (device_found), connstrings_len - (device_found));
274 log_put(LOG_CATEGORY, NFC_PRIORITY_TRACE, "%ld device(s) found using %s driver", (unsigned long) _device_found, ndr->name);
275 if (_device_found > 0) {
276 device_found += _device_found;
277 if (device_found == connstrings_len)
278 break;
279 }
280 } // scan_type is INTRUSIVE but not allowed or NOT_AVAILABLE
278281 pndr++;
279282 }
280283 log_fini();