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
83 | 83 | # Handle --with-drivers option |
84 | 84 | LIBNFC_ARG_WITH_DRIVERS |
85 | 85 | |
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 | ||
97 | 86 | # Documentation (default: no) |
98 | 87 | AC_ARG_ENABLE([doc],AS_HELP_STRING([--enable-doc],[Enable documentation generation.]),[enable_doc=$enableval],[enable_doc="no"]) |
99 | 88 |
2 | 2 | * |
3 | 3 | * Copyright (C) 2009 Roel Verdult |
4 | 4 | * Copyright (C) 2010, 2011 Romain Tartière |
5 | * Copyright (C) 2010, 2011 Romuald Conty | |
5 | * Copyright (C) 2010, 2011, 2012 Romuald Conty | |
6 | 6 | * |
7 | 7 | * This program is free software: you can redistribute it and/or modify it |
8 | 8 | * under the terms of the GNU Lesser General Public License as published by the |
31 | 31 | # include <stdbool.h> |
32 | 32 | # include <stdio.h> |
33 | 33 | |
34 | typedef void *nfc_context; | |
34 | /** | |
35 | * NFC context | |
36 | */ | |
37 | typedef struct nfc_context nfc_context; | |
35 | 38 | |
36 | 39 | /** |
37 | 40 | * NFC device |
324 | 324 | } |
325 | 325 | |
326 | 326 | size_t device_found = 0; |
327 | ||
328 | 327 | uint32_t uiBusIndex = 0; |
329 | 328 | struct usb_bus *bus; |
330 | 329 | for (bus = usb_get_busses(); bus; bus = bus->next) { |
861 | 860 | |
862 | 861 | const struct nfc_driver acr122_usb_driver = { |
863 | 862 | .name = ACR122_USB_DRIVER_NAME, |
863 | .scan_type = NOT_INTRUSIVE, | |
864 | 864 | .scan = acr122_usb_scan, |
865 | 865 | .open = acr122_usb_open, |
866 | 866 | .close = acr122_usb_close, |
449 | 449 | static size_t |
450 | 450 | acr122s_scan(nfc_connstring connstrings[], const size_t connstrings_len) |
451 | 451 | { |
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 */ | |
461 | 452 | size_t device_found = 0; |
462 | 453 | serial_port sp; |
463 | 454 | char **acPorts = uart_list_ports(); |
520 | 511 | } |
521 | 512 | free(acPorts); |
522 | 513 | return device_found; |
523 | #endif /* SERIAL_AUTOPROBE_ENABLED */ | |
524 | 514 | } |
525 | 515 | |
526 | 516 | static void |
702 | 692 | |
703 | 693 | const struct nfc_driver acr122s_driver = { |
704 | 694 | .name = ACR122S_DRIVER_NAME, |
695 | .scan_type = INTRUSIVE, | |
705 | 696 | .scan = acr122s_scan, |
706 | 697 | .open = acr122s_open, |
707 | 698 | .close = acr122s_close, |
93 | 93 | static size_t |
94 | 94 | arygon_scan(nfc_connstring connstrings[], const size_t connstrings_len) |
95 | 95 | { |
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 */ | |
105 | 96 | size_t device_found = 0; |
106 | 97 | serial_port sp; |
107 | 98 | char **acPorts = uart_list_ports(); |
158 | 149 | } |
159 | 150 | free(acPorts); |
160 | 151 | return device_found; |
161 | #endif /* SERIAL_AUTOPROBE_ENABLED */ | |
162 | 152 | } |
163 | 153 | |
164 | 154 | struct arygon_descriptor { |
563 | 553 | |
564 | 554 | const struct nfc_driver arygon_driver = { |
565 | 555 | .name = ARYGON_DRIVER_NAME, |
556 | .scan_type = INTRUSIVE, | |
566 | 557 | .scan = arygon_scan, |
567 | 558 | .open = arygon_open, |
568 | 559 | .close = arygon_close, |
66 | 66 | static size_t |
67 | 67 | pn532_uart_scan(nfc_connstring connstrings[], const size_t connstrings_len) |
68 | 68 | { |
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 */ | |
78 | 69 | size_t device_found = 0; |
79 | 70 | serial_port sp; |
80 | 71 | char **acPorts = uart_list_ports(); |
135 | 126 | } |
136 | 127 | free(acPorts); |
137 | 128 | return device_found; |
138 | #endif /* SERIAL_AUTOPROBE_ENABLED */ | |
139 | 129 | } |
140 | 130 | |
141 | 131 | struct pn532_uart_descriptor { |
504 | 494 | |
505 | 495 | const struct nfc_driver pn532_uart_driver = { |
506 | 496 | .name = PN532_UART_DRIVER_NAME, |
497 | .scan_type = INTRUSIVE, | |
507 | 498 | .scan = pn532_uart_scan, |
508 | 499 | .open = pn532_uart_open, |
509 | 500 | .close = pn532_uart_close, |
0 | 0 | /*- |
1 | 1 | * Public platform independent Near Field Communication (NFC) library |
2 | 2 | * |
3 | * Copyright (C) 2011 Romuald Conty | |
3 | * Copyright (C) 2011, 2012 Romuald Conty | |
4 | 4 | * |
5 | 5 | * This program is free software: you can redistribute it and/or modify it |
6 | 6 | * under the terms of the GNU Lesser General Public License as published by the |
23 | 23 | |
24 | 24 | #include <nfc/nfc.h> |
25 | 25 | #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 | } | |
26 | 64 | |
27 | 65 | void |
28 | 66 | prepare_initiator_data(const nfc_modulation nm, uint8_t **ppbtInitiatorData, size_t *pszInitiatorData) |
106 | 106 | } \ |
107 | 107 | } while (0) |
108 | 108 | |
109 | typedef enum { | |
110 | NOT_INTRUSIVE, | |
111 | INTRUSIVE, | |
112 | NOT_AVAILABLE, | |
113 | } scan_type_enum; | |
114 | ||
109 | 115 | struct nfc_driver { |
110 | 116 | const char *name; |
117 | const scan_type_enum scan_type; | |
111 | 118 | size_t (*scan)(nfc_connstring connstrings[], const size_t connstrings_len); |
112 | 119 | struct nfc_device *(*open)(const nfc_connstring connstring); |
113 | 120 | void (*close)(struct nfc_device *pnd); |
143 | 150 | |
144 | 151 | # define DEVICE_NAME_LENGTH 256 |
145 | 152 | # 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); | |
146 | 165 | |
147 | 166 | /** |
148 | 167 | * @struct nfc_device |
172 | 191 | int last_error; |
173 | 192 | }; |
174 | 193 | |
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); | |
181 | 200 | |
182 | 201 | #endif // __NFC_INTERNAL_H__ |
124 | 124 | void |
125 | 125 | nfc_exit(nfc_context *context) |
126 | 126 | { |
127 | (void) context; | |
127 | if(context) nfc_context_free(context); | |
128 | 128 | log_fini(); |
129 | 129 | } |
130 | 130 | |
250 | 250 | } |
251 | 251 | |
252 | 252 | /** @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) | |
254 | 254 | * @return Returns the number of devices found. |
255 | 255 | * @param context The context to operate on, or NULL for the default context. |
256 | 256 | * @param connstrings array of \a nfc_connstring. |
264 | 264 | const struct nfc_driver *ndr; |
265 | 265 | const struct nfc_driver **pndr = nfc_drivers; |
266 | 266 | |
267 | (void) context; | |
267 | if (!context) context = nfc_context_new(); // Should we support NULL context ? | |
268 | // FIXME: Load device(s) from configuration file(s) | |
268 | 269 | |
269 | 270 | while ((ndr = *pndr)) { |
270 | 271 | 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 | |
278 | 281 | pndr++; |
279 | 282 | } |
280 | 283 | log_fini(); |