libnfc  1.8.0
nfc.c
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  * Copyright (C) 2020 Adam Laurie
13  *
14  * This program is free software: you can redistribute it and/or modify it
15  * under the terms of the GNU Lesser General Public License as published by the
16  * Free Software Foundation, either version 3 of the License, or (at your
17  * option) any later version.
18  *
19  * This program is distributed in the hope that it will be useful, but WITHOUT
20  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
22  * more details.
23  *
24  * You should have received a copy of the GNU Lesser General Public License
25  * along with this program. If not, see <http://www.gnu.org/licenses/>
26  */
27 
74 #ifdef HAVE_CONFIG_H
75 # include "config.h"
76 #endif // HAVE_CONFIG_H
77 
78 #include <fcntl.h>
79 #include <stdio.h>
80 #include <stdlib.h>
81 #include <stddef.h>
82 #include <string.h>
83 #include <assert.h>
84 
85 #include <nfc/nfc.h>
86 
87 #include "nfc-internal.h"
88 #include "target-subr.h"
89 #include "drivers.h"
90 
91 #if defined (DRIVER_PCSC_ENABLED)
92 # include "drivers/pcsc.h"
93 #endif /* DRIVER_PCSC_ENABLED */
94 
95 #if defined (DRIVER_ACR122_PCSC_ENABLED)
96 # include "drivers/acr122_pcsc.h"
97 #endif /* DRIVER_ACR122_PCSC_ENABLED */
98 
99 #if defined (DRIVER_ACR122_USB_ENABLED)
100 # include "drivers/acr122_usb.h"
101 #endif /* DRIVER_ACR122_USB_ENABLED */
102 
103 #if defined (DRIVER_ACR122S_ENABLED)
104 # include "drivers/acr122s.h"
105 #endif /* DRIVER_ACR122S_ENABLED */
106 
107 #if defined (DRIVER_PN53X_USB_ENABLED)
108 # include "drivers/pn53x_usb.h"
109 #endif /* DRIVER_PN53X_USB_ENABLED */
110 
111 #if defined (DRIVER_ARYGON_ENABLED)
112 # include "drivers/arygon.h"
113 #endif /* DRIVER_ARYGON_ENABLED */
114 
115 #if defined (DRIVER_PN532_UART_ENABLED)
116 # include "drivers/pn532_uart.h"
117 #endif /* DRIVER_PN532_UART_ENABLED */
118 
119 #if defined (DRIVER_PN532_SPI_ENABLED)
120 # include "drivers/pn532_spi.h"
121 #endif /* DRIVER_PN532_SPI_ENABLED */
122 
123 #if defined (DRIVER_PN532_I2C_ENABLED)
124 # include "drivers/pn532_i2c.h"
125 #endif /* DRIVER_PN532_I2C_ENABLED */
126 
127 #if defined (DRIVER_PN71XX_ENABLED)
128 # include "drivers/pn71xx.h"
129 #endif /* DRIVER_PN71XX_ENABLED */
130 
131 
132 #define LOG_CATEGORY "libnfc.general"
133 #define LOG_GROUP NFC_LOG_GROUP_GENERAL
134 
135 struct nfc_driver_list {
136  const struct nfc_driver_list *next;
137  const struct nfc_driver *driver;
138 };
139 
140 const struct nfc_driver_list *nfc_drivers = NULL;
141 
142 // descritions for debugging
143 const char *nfc_property_name[] = {
144  "NP_TIMEOUT_COMMAND",
145  "NP_TIMEOUT_ATR",
146  "NP_TIMEOUT_COM",
147  "NP_HANDLE_CRC",
148  "NP_HANDLE_PARITY",
149  "NP_ACTIVATE_FIELD",
150  "NP_ACTIVATE_CRYPTO1",
151  "NP_INFINITE_SELECT",
152  "NP_ACCEPT_INVALID_FRAMES",
153  "NP_ACCEPT_MULTIPLE_FRAMES",
154  "NP_AUTO_ISO14443_4",
155  "NP_EASY_FRAMING",
156  "NP_FORCE_ISO14443_A",
157  "NP_FORCE_ISO14443_B",
158  "NP_FORCE_SPEED_106"
159 };
160 
161 static void
162 nfc_drivers_init(void)
163 {
164 #if defined (DRIVER_PN53X_USB_ENABLED)
165  nfc_register_driver(&pn53x_usb_driver);
166 #endif /* DRIVER_PN53X_USB_ENABLED */
167 #if defined (DRIVER_PCSC_ENABLED)
168  nfc_register_driver(&pcsc_driver);
169 #endif /* DRIVER_ACR122_PCSC_ENABLED */
170 #if defined (DRIVER_ACR122_PCSC_ENABLED)
171  nfc_register_driver(&acr122_pcsc_driver);
172 #endif /* DRIVER_ACR122_PCSC_ENABLED */
173 #if defined (DRIVER_ACR122_USB_ENABLED)
174  nfc_register_driver(&acr122_usb_driver);
175 #endif /* DRIVER_ACR122_USB_ENABLED */
176 #if defined (DRIVER_ACR122S_ENABLED)
177  nfc_register_driver(&acr122s_driver);
178 #endif /* DRIVER_ACR122S_ENABLED */
179 #if defined (DRIVER_PN532_UART_ENABLED)
180  nfc_register_driver(&pn532_uart_driver);
181 #endif /* DRIVER_PN532_UART_ENABLED */
182 #if defined (DRIVER_PN532_SPI_ENABLED)
183  nfc_register_driver(&pn532_spi_driver);
184 #endif /* DRIVER_PN532_SPI_ENABLED */
185 #if defined (DRIVER_PN532_I2C_ENABLED)
186  nfc_register_driver(&pn532_i2c_driver);
187 #endif /* DRIVER_PN532_I2C_ENABLED */
188 #if defined (DRIVER_ARYGON_ENABLED)
189  nfc_register_driver(&arygon_driver);
190 #endif /* DRIVER_ARYGON_ENABLED */
191 #if defined (DRIVER_PN71XX_ENABLED)
192  nfc_register_driver(&pn71xx_driver);
193 #endif /* DRIVER_PN71XX_ENABLED */
194 }
195 
196 static int
198 
206 int
207 nfc_register_driver(const struct nfc_driver *ndr)
208 {
209  if (!ndr) {
210  log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG, "nfc_register_driver returning NFC_EINVARG");
211  return NFC_EINVARG;
212  }
213 
214  struct nfc_driver_list *pndl = (struct nfc_driver_list *)malloc(sizeof(struct nfc_driver_list));
215  if (!pndl)
216  return NFC_ESOFT;
217 
218  pndl->driver = ndr;
219  pndl->next = nfc_drivers;
220  nfc_drivers = pndl;
221 
222  return NFC_SUCCESS;
223 }
224 
230 void
232 {
233  *context = nfc_context_new();
234  if (!*context) {
235  perror("malloc");
236  return;
237  }
238  if (!nfc_drivers)
239  nfc_drivers_init();
240 }
241 
247 void
249 {
250  while (nfc_drivers) {
251  struct nfc_driver_list *pndl = (struct nfc_driver_list *) nfc_drivers;
252  nfc_drivers = pndl->next;
253  free(pndl);
254  }
255 
256  nfc_context_free(context);
257 }
258 
276 nfc_device *
277 nfc_open(nfc_context *context, const nfc_connstring connstring)
278 {
279  nfc_device *pnd = NULL;
280 
281  nfc_connstring ncs;
282  if (connstring == NULL) {
283  if (!nfc_list_devices(context, &ncs, 1)) {
284  return NULL;
285  }
286  } else {
287  strncpy(ncs, connstring, sizeof(nfc_connstring));
288  ncs[sizeof(nfc_connstring) - 1] = '\0';
289  }
290 
291  // Search through the device list for an available device
292  const struct nfc_driver_list *pndl = nfc_drivers;
293  while (pndl) {
294  const struct nfc_driver *ndr = pndl->driver;
295 
296  // Specific device is requested: using device description
297  if (0 != strncmp(ndr->name, ncs, strlen(ndr->name))) {
298  // Check if connstring driver is usb -> accept any driver *_usb
299  if ((0 != strncmp("usb", ncs, strlen("usb"))) || 0 != strncmp("_usb", ndr->name + (strlen(ndr->name) - 4), 4)) {
300  pndl = pndl->next;
301  continue;
302  }
303  }
304 
305  pnd = ndr->open(context, ncs);
306  // Test if the opening was successful
307  if (pnd == NULL) {
308  if (0 == strncmp("usb", ncs, strlen("usb"))) {
309  // We've to test the other usb drivers before giving up
310  pndl = pndl->next;
311  continue;
312  }
313  log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG, "Unable to open \"%s\".", ncs);
314  return NULL;
315  }
316  for (uint32_t i = 0; i < context->user_defined_device_count; i++) {
317  if (strcmp(ncs, context->user_defined_devices[i].connstring) == 0) {
318  // This is a device sets by user, we use the device name given by user
319  strcpy(pnd->name, context->user_defined_devices[i].name);
320  break;
321  }
322  }
323  log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG, "\"%s\" (%s) has been claimed.", pnd->name, pnd->connstring);
324  return pnd;
325  }
326 
327  // Too bad, no driver can decode connstring
328  log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG, "No driver available to handle \"%s\".", ncs);
329  return NULL;
330 }
331 
338 void
340 {
341  if (pnd) {
342  // Close, clean up and release the device
343  pnd->driver->close(pnd);
344  }
345 }
346 
355 size_t
356 nfc_list_devices(nfc_context *context, nfc_connstring connstrings[], const size_t connstrings_len)
357 {
358  size_t device_found = 0;
359 
360 #ifdef CONFFILES
361  // Load manually configured devices (from config file and env variables)
362  // TODO From env var...
363  for (uint32_t i = 0; i < context->user_defined_device_count; i++) {
364  if (context->user_defined_devices[i].optional) {
365  // let's make sure the device exists
366  nfc_device *pnd = NULL;
367 
368 #ifdef ENVVARS
369  char *env_log_level = getenv("LIBNFC_LOG_LEVEL");
370  char *old_env_log_level = NULL;
371  // do it silently
372  if (env_log_level) {
373  if ((old_env_log_level = malloc(strlen(env_log_level) + 1)) == NULL) {
374  log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "%s", "Unable to malloc()");
375  return 0;
376  }
377  strcpy(old_env_log_level, env_log_level);
378  }
379  setenv("LIBNFC_LOG_LEVEL", "0", 1);
380 #endif // ENVVARS
381 
382  pnd = nfc_open(context, context->user_defined_devices[i].connstring);
383 
384 #ifdef ENVVARS
385  if (old_env_log_level) {
386  setenv("LIBNFC_LOG_LEVEL", old_env_log_level, 1);
387  free(old_env_log_level);
388  } else {
389  unsetenv("LIBNFC_LOG_LEVEL");
390  }
391 #endif // ENVVARS
392 
393  if (pnd) {
394  nfc_close(pnd);
395  log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG, "User device %s found", context->user_defined_devices[i].name);
396  strcpy((char *)(connstrings + device_found), context->user_defined_devices[i].connstring);
397  device_found ++;
398  if (device_found == connstrings_len)
399  break;
400  }
401  } else {
402  // manual choice is not marked as optional so let's take it blindly
403  strcpy((char *)(connstrings + device_found), context->user_defined_devices[i].connstring);
404  device_found++;
405  if (device_found >= connstrings_len)
406  return device_found;
407  }
408  }
409 #endif // CONFFILES
410 
411  // Device auto-detection
412  if (context->allow_autoscan) {
413  const struct nfc_driver_list *pndl = nfc_drivers;
414  while (pndl) {
415  const struct nfc_driver *ndr = pndl->driver;
416  if ((ndr->scan_type == NOT_INTRUSIVE) || ((context->allow_intrusive_scan) && (ndr->scan_type == INTRUSIVE))) {
417  size_t _device_found = ndr->scan(context, connstrings + (device_found), connstrings_len - (device_found));
418  log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG, "%ld device(s) found using %s driver", (unsigned long) _device_found, ndr->name);
419  if (_device_found > 0) {
420  device_found += _device_found;
421  if (device_found == connstrings_len)
422  break;
423  }
424  } // scan_type is INTRUSIVE but not allowed or NOT_AVAILABLE
425  pndl = pndl->next;
426  }
427  } else if (context->user_defined_device_count == 0) {
428  log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_INFO, "Warning: %s", "user must specify device(s) manually when autoscan is disabled");
429  }
430 
431  return device_found;
432 }
433 
445 int
446 nfc_device_set_property_int(nfc_device *pnd, const nfc_property property, const int value)
447 {
448  log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG, "set_property_int %s %s", nfc_property_name[property], value ? "True" : "False");
449  HAL(device_set_property_int, pnd, property, value);
450 }
451 
452 
465 int
466 nfc_device_set_property_bool(nfc_device *pnd, const nfc_property property, const bool bEnable)
467 {
468  log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG, "set_property_bool %s %s", nfc_property_name[property], bEnable ? "True" : "False");
469  HAL(device_set_property_bool, pnd, property, bEnable);
470 }
471 
492 int
494 {
495  int res = 0;
496  // Drop the field for a while
497  if ((res = nfc_device_set_property_bool(pnd, NP_ACTIVATE_FIELD, false)) < 0)
498  return res;
499  // Enable field so more power consuming cards can power themselves up
500  if ((res = nfc_device_set_property_bool(pnd, NP_ACTIVATE_FIELD, true)) < 0)
501  return res;
502  // Let the device try forever to find a target/tag
503  if ((res = nfc_device_set_property_bool(pnd, NP_INFINITE_SELECT, true)) < 0)
504  return res;
505  // Activate auto ISO14443-4 switching by default
506  if ((res = nfc_device_set_property_bool(pnd, NP_AUTO_ISO14443_4, true)) < 0)
507  return res;
508  // Force 14443-A mode
509  if ((res = nfc_device_set_property_bool(pnd, NP_FORCE_ISO14443_A, true)) < 0)
510  return res;
511  // Force speed at 106kbps
512  if ((res = nfc_device_set_property_bool(pnd, NP_FORCE_SPEED_106, true)) < 0)
513  return res;
514  // Disallow invalid frame
515  if ((res = nfc_device_set_property_bool(pnd, NP_ACCEPT_INVALID_FRAMES, false)) < 0)
516  return res;
517  // Disallow multiple frames
518  if ((res = nfc_device_set_property_bool(pnd, NP_ACCEPT_MULTIPLE_FRAMES, false)) < 0)
519  return res;
520  HAL(initiator_init, pnd);
521 }
522 
532 int
534 {
535  HAL(initiator_init_secure_element, pnd);
536 }
537 
561 int
563  const nfc_modulation nm,
564  const uint8_t *pbtInitData, const size_t szInitData,
565  nfc_target *pnt)
566 {
567  uint8_t *abtInit = NULL;
568  uint8_t abtTmpInit[MAX(12, szInitData)];
569  size_t szInit = 0;
570  int res;
571  if ((res = nfc_device_validate_modulation(pnd, N_INITIATOR, &nm)) != NFC_SUCCESS)
572  return res;
573  if (szInitData == 0) {
574  // Provide default values, if any
575  prepare_initiator_data(nm, &abtInit, &szInit);
576  } else if (nm.nmt == NMT_ISO14443A) {
577  abtInit = abtTmpInit;
578  iso14443_cascade_uid(pbtInitData, szInitData, abtInit, &szInit);
579  } else {
580  abtInit = abtTmpInit;
581  memcpy(abtInit, pbtInitData, szInitData);
582  szInit = szInitData;
583  }
584 
585  HAL(initiator_select_passive_target, pnd, nm, abtInit, szInit, pnt);
586 }
587 
604 int
606  const nfc_modulation nm,
607  nfc_target ant[], const size_t szTargets)
608 {
609  nfc_target nt;
610  size_t szTargetFound = 0;
611  uint8_t *pbtInitData = NULL;
612  size_t szInitDataLen = 0;
613  int res = 0;
614 
615  pnd->last_error = 0;
616 
617  // Let the reader only try once to find a tag
618  bool bInfiniteSelect = pnd->bInfiniteSelect;
619  if ((res = nfc_device_set_property_bool(pnd, NP_INFINITE_SELECT, false)) < 0) {
620  return res;
621  }
622 
623  prepare_initiator_data(nm, &pbtInitData, &szInitDataLen);
624 
625  while (nfc_initiator_select_passive_target(pnd, nm, pbtInitData, szInitDataLen, &nt) > 0) {
626  size_t i;
627  bool seen = false;
628  // Check if we've already seen this tag
629  for (i = 0; i < szTargetFound; i++) {
630  if (memcmp(&(ant[i]), &nt, sizeof(nfc_target)) == 0) {
631  seen = true;
632  }
633  }
634  if (seen) {
635  break;
636  }
637  memcpy(&(ant[szTargetFound]), &nt, sizeof(nfc_target));
638  szTargetFound++;
639  if (szTargets == szTargetFound) {
640  break;
641  }
642  nfc_initiator_deselect_target(pnd);
643  // deselect has no effect on FeliCa, Jewel and Thinfilm cards so we'll stop after one...
644  // ISO/IEC 14443 B' cards are polled at 100% probability so it's not possible to detect correctly two cards at the same time
645  if ((nm.nmt == NMT_FELICA) || (nm.nmt == NMT_JEWEL) || (nm.nmt == NMT_BARCODE) ||
646  (nm.nmt == NMT_ISO14443BI) || (nm.nmt == NMT_ISO14443B2SR) || (nm.nmt == NMT_ISO14443B2CT)) {
647  break;
648  }
649  }
650  if (bInfiniteSelect) {
651  if ((res = nfc_device_set_property_bool(pnd, NP_INFINITE_SELECT, true)) < 0) {
652  return res;
653  }
654  }
655  return szTargetFound;
656 }
657 
671 int
673  const nfc_modulation *pnmModulations, const size_t szModulations,
674  const uint8_t uiPollNr, const uint8_t uiPeriod,
675  nfc_target *pnt)
676 {
677  HAL(initiator_poll_target, pnd, pnmModulations, szModulations, uiPollNr, uiPeriod, pnt);
678 }
679 
680 
701 int
703  const nfc_dep_mode ndm, const nfc_baud_rate nbr,
704  const nfc_dep_info *pndiInitiator, nfc_target *pnt, const int timeout)
705 {
706  HAL(initiator_select_dep_target, pnd, ndm, nbr, pndiInitiator, pnt, timeout);
707 }
708 
726 int
728  const nfc_dep_mode ndm, const nfc_baud_rate nbr,
729  const nfc_dep_info *pndiInitiator,
730  nfc_target *pnt,
731  const int timeout)
732 {
733  const int period = 300;
734  int remaining_time = timeout;
735  int res;
736  int result = 0;
737  bool bInfiniteSelect = pnd->bInfiniteSelect;
738  if ((res = nfc_device_set_property_bool(pnd, NP_INFINITE_SELECT, true)) < 0)
739  return res;
740  while (remaining_time > 0) {
741  if ((res = nfc_initiator_select_dep_target(pnd, ndm, nbr, pndiInitiator, pnt, period)) < 0) {
742  if (res != NFC_ETIMEOUT) {
743  result = res;
744  goto end;
745  }
746  }
747  if (res == 1) {
748  result = res;
749  goto end;
750  }
751  remaining_time -= period;
752  }
753 end:
754  if (! bInfiniteSelect) {
755  if ((res = nfc_device_set_property_bool(pnd, NP_INFINITE_SELECT, false)) < 0) {
756  return res;
757  }
758  }
759  return result;
760 }
761 
774 int
775 nfc_initiator_deselect_target(nfc_device *pnd)
776 {
777  HAL(initiator_deselect_target, pnd);
778 }
779 
808 int
809 nfc_initiator_transceive_bytes(nfc_device *pnd, const uint8_t *pbtTx, const size_t szTx, uint8_t *pbtRx,
810  const size_t szRx, int timeout)
811 {
812  HAL(initiator_transceive_bytes, pnd, pbtTx, szTx, pbtRx, szRx, timeout)
813 }
814 
851 int
853  const uint8_t *pbtTx, const size_t szTxBits, const uint8_t *pbtTxPar,
854  uint8_t *pbtRx, const size_t szRx,
855  uint8_t *pbtRxPar)
856 {
857  (void)szRx;
858  HAL(initiator_transceive_bits, pnd, pbtTx, szTxBits, pbtTxPar, pbtRx, pbtRxPar);
859 }
860 
887 int
889  const uint8_t *pbtTx, const size_t szTx,
890  uint8_t *pbtRx, const size_t szRx,
891  uint32_t *cycles)
892 {
893  HAL(initiator_transceive_bytes_timed, pnd, pbtTx, szTx, pbtRx, szRx, cycles);
894 }
895 
906 int
908 {
909  HAL(initiator_target_is_present, pnd, pnt);
910 }
911 
933 int
935  const uint8_t *pbtTx, const size_t szTxBits, const uint8_t *pbtTxPar,
936  uint8_t *pbtRx, const size_t szRx,
937  uint8_t *pbtRxPar,
938  uint32_t *cycles)
939 {
940  (void)szRx;
941  HAL(initiator_transceive_bits_timed, pnd, pbtTx, szTxBits, pbtTxPar, pbtRx, pbtRxPar, cycles);
942 }
943 
977 int
978 nfc_target_init(nfc_device *pnd, nfc_target *pnt, uint8_t *pbtRx, const size_t szRx, int timeout)
979 {
980  int res = 0;
981  // Disallow invalid frame
982  if ((res = nfc_device_set_property_bool(pnd, NP_ACCEPT_INVALID_FRAMES, false)) < 0)
983  return res;
984  // Disallow multiple frames
985  if ((res = nfc_device_set_property_bool(pnd, NP_ACCEPT_MULTIPLE_FRAMES, false)) < 0)
986  return res;
987  // Make sure we reset the CRC and parity to chip handling.
988  if ((res = nfc_device_set_property_bool(pnd, NP_HANDLE_CRC, true)) < 0)
989  return res;
990  if ((res = nfc_device_set_property_bool(pnd, NP_HANDLE_PARITY, true)) < 0)
991  return res;
992  // Activate auto ISO14443-4 switching by default
993  if ((res = nfc_device_set_property_bool(pnd, NP_AUTO_ISO14443_4, true)) < 0)
994  return res;
995  // Activate "easy framing" feature by default
996  if ((res = nfc_device_set_property_bool(pnd, NP_EASY_FRAMING, true)) < 0)
997  return res;
998  // Deactivate the CRYPTO1 cipher, it may could cause problems when still active
999  if ((res = nfc_device_set_property_bool(pnd, NP_ACTIVATE_CRYPTO1, false)) < 0)
1000  return res;
1001  // Drop explicitely the field
1002  if ((res = nfc_device_set_property_bool(pnd, NP_ACTIVATE_FIELD, false)) < 0)
1003  return res;
1004 
1005  HAL(target_init, pnd, pnt, pbtRx, szRx, timeout);
1006 }
1007 
1018 int
1020 {
1021  HAL(idle, pnd);
1022 }
1023 
1035 int
1037 {
1038  HAL(abort_command, pnd);
1039 }
1040 
1056 int
1057 nfc_target_send_bytes(nfc_device *pnd, const uint8_t *pbtTx, const size_t szTx, int timeout)
1058 {
1059  HAL(target_send_bytes, pnd, pbtTx, szTx, timeout);
1060 }
1061 
1076 int
1077 nfc_target_receive_bytes(nfc_device *pnd, uint8_t *pbtRx, const size_t szRx, int timeout)
1078 {
1079  HAL(target_receive_bytes, pnd, pbtRx, szRx, timeout);
1080 }
1081 
1093 int
1094 nfc_target_send_bits(nfc_device *pnd, const uint8_t *pbtTx, const size_t szTxBits, const uint8_t *pbtTxPar)
1095 {
1096  HAL(target_send_bits, pnd, pbtTx, szTxBits, pbtTxPar);
1097 }
1098 
1115 int
1116 nfc_target_receive_bits(nfc_device *pnd, uint8_t *pbtRx, const size_t szRx, uint8_t *pbtRxPar)
1117 {
1118  HAL(target_receive_bits, pnd, pbtRx, szRx, pbtRxPar);
1119 }
1120 
1121 static struct sErrorMessage {
1122  int iErrorCode;
1123  const char *pcErrorMsg;
1124 } sErrorMessages[] = {
1125  /* Chip-level errors (internal errors, RF errors, etc.) */
1126  { NFC_SUCCESS, "Success" },
1127  { NFC_EIO, "Input / Output Error" },
1128  { NFC_EINVARG, "Invalid argument(s)" },
1129  { NFC_EDEVNOTSUPP, "Not Supported by Device" },
1130  { NFC_ENOTSUCHDEV, "No Such Device" },
1131  { NFC_EOVFLOW, "Buffer Overflow" },
1132  { NFC_ETIMEOUT, "Timeout" },
1133  { NFC_EOPABORTED, "Operation Aborted" },
1134  { NFC_ENOTIMPL, "Not (yet) Implemented" },
1135  { NFC_ETGRELEASED, "Target Released" },
1136  { NFC_EMFCAUTHFAIL, "Mifare Authentication Failed" },
1137  { NFC_ERFTRANS, "RF Transmission Error" },
1138  { NFC_ECHIP, "Device's Internal Chip Error" },
1139 };
1140 
1147 const char *
1149 {
1150  const char *pcRes = "Unknown error";
1151  size_t i;
1152  for (i = 0; i < (sizeof(sErrorMessages) / sizeof(struct sErrorMessage)); i++) {
1153  if (sErrorMessages[i].iErrorCode == pnd->last_error) {
1154  pcRes = sErrorMessages[i].pcErrorMsg;
1155  break;
1156  }
1157  }
1158 
1159  return pcRes;
1160 }
1161 
1170 int
1171 nfc_strerror_r(const nfc_device *pnd, char *pcStrErrBuf, size_t szBufLen)
1172 {
1173  return (snprintf(pcStrErrBuf, szBufLen, "%s", nfc_strerror(pnd)) < 0) ? -1 : 0;
1174 }
1175 
1182 void
1183 nfc_perror(const nfc_device *pnd, const char *pcString)
1184 {
1185  fprintf(stderr, "%s: %s\n", pcString, nfc_strerror(pnd));
1186 }
1187 
1194 int
1196 {
1197  return pnd->last_error;
1198 }
1199 
1200 /* Special data accessors */
1201 
1208 const char *
1210 {
1211  return pnd->name;
1212 }
1213 
1220 const char *
1222 {
1223  return pnd->connstring;
1224 }
1225 
1234 int
1236 {
1237  HAL(get_supported_modulation, pnd, mode, supported_mt);
1238 }
1239 
1248 int
1250 {
1251  HAL(get_supported_baud_rate, pnd, N_INITIATOR, nmt, supported_br);
1252 }
1253 
1262 int
1264 {
1265  HAL(get_supported_baud_rate, pnd, N_TARGET, nmt, supported_br);
1266 }
1267 
1276 static int
1278 {
1279  int res;
1280  const nfc_modulation_type *nmt = NULL;
1281  if ((res = nfc_device_get_supported_modulation(pnd, mode, &nmt)) < 0) {
1282  return res;
1283  }
1284  assert(nmt != NULL);
1285  for (int i = 0; nmt[i]; i++) {
1286  if (nmt[i] == nm->nmt) {
1287  const nfc_baud_rate *nbr = NULL;
1288  if (mode == N_INITIATOR) {
1289  if ((res = nfc_device_get_supported_baud_rate(pnd, nmt[i], &nbr)) < 0) {
1290  return res;
1291  }
1292  } else {
1293  if ((res = nfc_device_get_supported_baud_rate_target_mode(pnd, nmt[i], &nbr)) < 0) {
1294  return res;
1295  }
1296  }
1297  assert(nbr != NULL);
1298  for (int j = 0; nbr[j]; j++) {
1299  if (nbr[j] == nm->nbr)
1300  return NFC_SUCCESS;
1301  }
1302  log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG, "nfc_device_validate_modulation returning NFC_EINVARG");
1303  return NFC_EINVARG;
1304  }
1305  }
1306  log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG, "nfc_device_validate_modulation returning NFC_EINVARG");
1307  return NFC_EINVARG;
1308 }
1309 
1310 /* Misc. functions */
1311 
1318 const char *
1320 {
1321 #ifdef GIT_REVISION
1322  return GIT_REVISION;
1323 #else
1324  return PACKAGE_VERSION;
1325 #endif // GIT_REVISION
1326 }
1327 
1333 void
1334 nfc_free(void *p)
1335 {
1336  free(p);
1337 }
1338 
1347 int
1349 {
1350  HAL(device_get_information_about, pnd, buf);
1351 }
1352 
1358 const char *
1360 {
1361  switch (nbr) {
1362  case NBR_UNDEFINED:
1363  return "undefined baud rate";
1364  case NBR_106:
1365  return "106 kbps";
1366  case NBR_212:
1367  return "212 kbps";
1368  case NBR_424:
1369  return "424 kbps";
1370  case NBR_847:
1371  return "847 kbps";
1372  }
1373 
1374  return "???";
1375 }
1376 
1382 const char *
1384 {
1385  switch (nmt) {
1386  case NMT_ISO14443A:
1387  return "ISO/IEC 14443A";
1388  case NMT_ISO14443B:
1389  return "ISO/IEC 14443-4B";
1390  case NMT_ISO14443BI:
1391  return "ISO/IEC 14443-4B'";
1392  case NMT_ISO14443BICLASS:
1393  return "ISO/IEC 14443-2B-3B iClass (Picopass)";
1394  case NMT_ISO14443B2CT:
1395  return "ISO/IEC 14443-2B ASK CTx";
1396  case NMT_ISO14443B2SR:
1397  return "ISO/IEC 14443-2B ST SRx";
1398  case NMT_FELICA:
1399  return "FeliCa";
1400  case NMT_JEWEL:
1401  return "Innovision Jewel";
1402  case NMT_BARCODE:
1403  return "Thinfilm NFC Barcode";
1404  case NMT_DEP:
1405  return "D.E.P.";
1406  }
1407 
1408  return "???";
1409 }
1410 
1420 int
1421 str_nfc_target(char **buf, const nfc_target *pnt, bool verbose)
1422 {
1423  *buf = malloc(4096);
1424  if (! *buf)
1425  return NFC_ESOFT;
1426  (*buf)[0] = '\0';
1427  snprint_nfc_target(*buf, 4096, pnt, verbose);
1428  return strlen(*buf);
1429 }
int nfc_device_get_supported_baud_rate_target_mode(nfc_device *pnd, const nfc_modulation_type nmt, const nfc_baud_rate **const supported_br)
Get supported baud rates for target mode.
Definition: nfc.c:1263
int nfc_device_get_supported_modulation(nfc_device *pnd, const nfc_mode mode, const nfc_modulation_type **const supported_mt)
Get supported modulations.
Definition: nfc.c:1235
int nfc_device_get_supported_baud_rate(nfc_device *pnd, const nfc_modulation_type nmt, const nfc_baud_rate **const supported_br)
Get supported baud rates (initiator mode).
Definition: nfc.c:1249
const char * nfc_device_get_connstring(nfc_device *pnd)
Returns the device connection string.
Definition: nfc.c:1221
const char * nfc_device_get_name(nfc_device *pnd)
Returns the device name.
Definition: nfc.c:1209
static int nfc_device_validate_modulation(nfc_device *pnd, const nfc_mode mode, const nfc_modulation *nm)
Validate combination of modulation and baud rate on the currently used device.
Definition: nfc.c:1277
void nfc_close(nfc_device *pnd)
Close from a NFC device.
Definition: nfc.c:339
int nfc_idle(nfc_device *pnd)
Turn NFC device in idle mode.
Definition: nfc.c:1019
nfc_device * nfc_open(nfc_context *context, const nfc_connstring connstring)
Open a NFC device.
Definition: nfc.c:277
int nfc_abort_command(nfc_device *pnd)
Abort current running command.
Definition: nfc.c:1036
size_t nfc_list_devices(nfc_context *context, nfc_connstring connstrings[], const size_t connstrings_len)
Scan for discoverable supported devices (ie. only available for some drivers)
Definition: nfc.c:356
void nfc_perror(const nfc_device *pnd, const char *pcString)
Display the last error occured on a nfc_device.
Definition: nfc.c:1183
#define NFC_ETIMEOUT
Definition: nfc.h:185
#define NFC_ENOTIMPL
Definition: nfc.h:195
#define NFC_ECHIP
Definition: nfc.h:220
#define NFC_EOVFLOW
Definition: nfc.h:180
#define NFC_EINVARG
Definition: nfc.h:165
int nfc_strerror_r(const nfc_device *pnd, char *pcStrErrBuf, size_t szBufLen)
Renders the last error in pcStrErrBuf for a maximum size of szBufLen chars.
Definition: nfc.c:1171
#define NFC_ETGRELEASED
Definition: nfc.h:200
#define NFC_ESOFT
Definition: nfc.h:215
#define NFC_ENOTSUCHDEV
Definition: nfc.h:175
#define NFC_ERFTRANS
Definition: nfc.h:205
#define NFC_EMFCAUTHFAIL
Definition: nfc.h:210
#define NFC_EIO
Definition: nfc.h:160
#define NFC_EOPABORTED
Definition: nfc.h:190
const char * nfc_strerror(const nfc_device *pnd)
Return the last error string.
Definition: nfc.c:1148
int nfc_device_get_last_error(const nfc_device *pnd)
Returns last error occured on a nfc_device.
Definition: nfc.c:1195
#define NFC_EDEVNOTSUPP
Definition: nfc.h:170
#define NFC_SUCCESS
Definition: nfc.h:155
int nfc_initiator_transceive_bytes(nfc_device *pnd, const uint8_t *pbtTx, const size_t szTx, uint8_t *pbtRx, const size_t szRx, int timeout)
Send data to target then retrieve data from target.
Definition: nfc.c:809
int nfc_initiator_transceive_bits(nfc_device *pnd, const uint8_t *pbtTx, const size_t szTxBits, const uint8_t *pbtTxPar, uint8_t *pbtRx, const size_t szRx, uint8_t *pbtRxPar)
Transceive raw bit-frames to a target.
Definition: nfc.c:852
int nfc_initiator_transceive_bits_timed(nfc_device *pnd, const uint8_t *pbtTx, const size_t szTxBits, const uint8_t *pbtTxPar, uint8_t *pbtRx, const size_t szRx, uint8_t *pbtRxPar, uint32_t *cycles)
Transceive raw bit-frames to a target.
Definition: nfc.c:934
int nfc_initiator_init(nfc_device *pnd)
Initialize NFC device as initiator (reader)
Definition: nfc.c:493
int nfc_initiator_init_secure_element(nfc_device *pnd)
Initialize NFC device as initiator with its secure element as target (reader)
Definition: nfc.c:533
int nfc_initiator_poll_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)
Poll a target and request active or passive mode for D.E.P. (Data Exchange Protocol)
Definition: nfc.c:727
int nfc_initiator_transceive_bytes_timed(nfc_device *pnd, const uint8_t *pbtTx, const size_t szTx, uint8_t *pbtRx, const size_t szRx, uint32_t *cycles)
Send data to target then retrieve data from target.
Definition: nfc.c:888
int nfc_initiator_target_is_present(nfc_device *pnd, const nfc_target *pnt)
Check target presence.
Definition: nfc.c:907
int nfc_initiator_list_passive_targets(nfc_device *pnd, const nfc_modulation nm, nfc_target ant[], const size_t szTargets)
List passive or emulated tags.
Definition: nfc.c:605
int nfc_initiator_select_dep_target(nfc_device *pnd, const nfc_dep_mode ndm, const nfc_baud_rate nbr, const nfc_dep_info *pndiInitiator, nfc_target *pnt, const int timeout)
Select a target and request active or passive mode for D.E.P. (Data Exchange Protocol)
Definition: nfc.c:702
int nfc_initiator_select_passive_target(nfc_device *pnd, const nfc_modulation nm, const uint8_t *pbtInitData, const size_t szInitData, nfc_target *pnt)
Select a passive or emulated tag.
Definition: nfc.c:562
int nfc_initiator_poll_target(nfc_device *pnd, const nfc_modulation *pnmModulations, const size_t szModulations, const uint8_t uiPollNr, const uint8_t uiPeriod, nfc_target *pnt)
Polling for NFC targets.
Definition: nfc.c:672
int nfc_register_driver(const struct nfc_driver *ndr)
Register an NFC device driver with libnfc. This function registers a driver with libnfc,...
Definition: nfc.c:207
void nfc_exit(nfc_context *context)
Deinitialize libnfc. Should be called after closing all open devices and before your application term...
Definition: nfc.c:248
void nfc_init(nfc_context **context)
Initialize libnfc. This function must be called before calling any other libnfc function.
Definition: nfc.c:231
const char * nfc_version(void)
Returns the library version.
Definition: nfc.c:1319
void nfc_free(void *p)
Free buffer allocated by libnfc.
Definition: nfc.c:1334
int nfc_device_get_information_about(nfc_device *pnd, char **buf)
Print information about NFC device.
Definition: nfc.c:1348
int nfc_device_set_property_int(nfc_device *pnd, const nfc_property property, const int value)
Set a device's integer-property value.
Definition: nfc.c:446
int nfc_device_set_property_bool(nfc_device *pnd, const nfc_property property, const bool bEnable)
Set a device's boolean-property value.
Definition: nfc.c:466
int str_nfc_target(char **buf, const nfc_target *pnt, bool verbose)
Convert nfc_target content to string.
Definition: nfc.c:1421
const char * str_nfc_baud_rate(const nfc_baud_rate nbr)
Convert nfc_baud_rate value to string.
Definition: nfc.c:1359
const char * str_nfc_modulation_type(const nfc_modulation_type nmt)
Convert nfc_modulation_type value to string.
Definition: nfc.c:1383
int nfc_target_send_bytes(nfc_device *pnd, const uint8_t *pbtTx, const size_t szTx, int timeout)
Send bytes and APDU frames.
Definition: nfc.c:1057
int nfc_target_send_bits(nfc_device *pnd, const uint8_t *pbtTx, const size_t szTxBits, const uint8_t *pbtTxPar)
Send raw bit-frames.
Definition: nfc.c:1094
int nfc_target_init(nfc_device *pnd, nfc_target *pnt, uint8_t *pbtRx, const size_t szRx, int timeout)
Initialize NFC device as an emulated tag.
Definition: nfc.c:978
int nfc_target_receive_bytes(nfc_device *pnd, uint8_t *pbtRx, const size_t szRx, int timeout)
Receive bytes and APDU frames.
Definition: nfc.c:1077
int nfc_target_receive_bits(nfc_device *pnd, uint8_t *pbtRx, const size_t szRx, uint8_t *pbtRxPar)
Receive bit-frames.
Definition: nfc.c:1116
Internal defines and macros.
#define HAL(FUNCTION,...)
Execute corresponding driver function if exists.
Definition: nfc-internal.h:47
nfc_mode
NFC mode type enumeration.
Definition: nfc-types.h:333
struct nfc_driver nfc_driver
Definition: nfc-types.h:58
nfc_property
Definition: nfc-types.h:68
@ NP_INFINITE_SELECT
Definition: nfc-types.h:115
@ NP_HANDLE_CRC
Definition: nfc-types.h:94
@ NP_FORCE_ISO14443_A
Definition: nfc-types.h:138
@ NP_ACCEPT_INVALID_FRAMES
Definition: nfc-types.h:119
@ NP_ACTIVATE_FIELD
Definition: nfc-types.h:105
@ NP_AUTO_ISO14443_4
Definition: nfc-types.h:134
@ NP_EASY_FRAMING
Definition: nfc-types.h:136
@ NP_ACTIVATE_CRYPTO1
Definition: nfc-types.h:109
@ NP_ACCEPT_MULTIPLE_FRAMES
Definition: nfc-types.h:126
@ NP_HANDLE_PARITY
Definition: nfc-types.h:102
@ NP_FORCE_SPEED_106
Definition: nfc-types.h:142
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
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....
Definition: nfc-internal.h:175
NFC target information in D.E.P. (Data Exchange Protocol) see ISO/IEC 18092 (NFCIP-1)
Definition: nfc-types.h:162
NFC device information.
Definition: nfc-internal.h:190
char name[DEVICE_NAME_LENGTH]
Definition: nfc-internal.h:197
nfc_connstring connstring
Definition: nfc-internal.h:199
bool bInfiniteSelect
Definition: nfc-internal.h:207
NFC modulation structure.
Definition: nfc-types.h:342
NFC target structure.
Definition: nfc-types.h:351