libnfc  1.7.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  *
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 
73 #ifdef HAVE_CONFIG_H
74 # include "config.h"
75 #endif // HAVE_CONFIG_H
76 
77 #include <fcntl.h>
78 #include <stdio.h>
79 #include <stdlib.h>
80 #include <stddef.h>
81 #include <string.h>
82 
83 #include <nfc/nfc.h>
84 
85 #include "nfc-internal.h"
86 #include "target-subr.h"
87 #include "drivers.h"
88 
89 #if defined (DRIVER_ACR122_PCSC_ENABLED)
90 # include "drivers/acr122_pcsc.h"
91 #endif /* DRIVER_ACR122_PCSC_ENABLED */
92 
93 #if defined (DRIVER_ACR122_USB_ENABLED)
94 # include "drivers/acr122_usb.h"
95 #endif /* DRIVER_ACR122_USB_ENABLED */
96 
97 #if defined (DRIVER_ACR122S_ENABLED)
98 # include "drivers/acr122s.h"
99 #endif /* DRIVER_ACR122S_ENABLED */
100 
101 #if defined (DRIVER_PN53X_USB_ENABLED)
102 # include "drivers/pn53x_usb.h"
103 #endif /* DRIVER_PN53X_USB_ENABLED */
104 
105 #if defined (DRIVER_ARYGON_ENABLED)
106 # include "drivers/arygon.h"
107 #endif /* DRIVER_ARYGON_ENABLED */
108 
109 #if defined (DRIVER_PN532_UART_ENABLED)
110 # include "drivers/pn532_uart.h"
111 #endif /* DRIVER_PN532_UART_ENABLED */
112 
113 #if defined (DRIVER_PN532_SPI_ENABLED)
114 # include "drivers/pn532_spi.h"
115 #endif /* DRIVER_PN532_SPI_ENABLED */
116 
117 #if defined (DRIVER_PN532_I2C_ENABLED)
118 # include "drivers/pn532_i2c.h"
119 #endif /* DRIVER_PN532_I2C_ENABLED */
120 
121 
122 #define LOG_CATEGORY "libnfc.general"
123 #define LOG_GROUP NFC_LOG_GROUP_GENERAL
124 
125 struct nfc_driver_list {
126  const struct nfc_driver_list *next;
127  const struct nfc_driver *driver;
128 };
129 
130 const struct nfc_driver_list *nfc_drivers = NULL;
131 
132 static void
133 nfc_drivers_init(void)
134 {
135 #if defined (DRIVER_PN53X_USB_ENABLED)
136  nfc_register_driver(&pn53x_usb_driver);
137 #endif /* DRIVER_PN53X_USB_ENABLED */
138 #if defined (DRIVER_ACR122_PCSC_ENABLED)
139  nfc_register_driver(&acr122_pcsc_driver);
140 #endif /* DRIVER_ACR122_PCSC_ENABLED */
141 #if defined (DRIVER_ACR122_USB_ENABLED)
142  nfc_register_driver(&acr122_usb_driver);
143 #endif /* DRIVER_ACR122_USB_ENABLED */
144 #if defined (DRIVER_ACR122S_ENABLED)
145  nfc_register_driver(&acr122s_driver);
146 #endif /* DRIVER_ACR122S_ENABLED */
147 #if defined (DRIVER_PN532_UART_ENABLED)
148  nfc_register_driver(&pn532_uart_driver);
149 #endif /* DRIVER_PN532_UART_ENABLED */
150 #if defined (DRIVER_PN532_SPI_ENABLED)
151  nfc_register_driver(&pn532_spi_driver);
152 #endif /* DRIVER_PN532_SPI_ENABLED */
153 #if defined (DRIVER_PN532_I2C_ENABLED)
154  nfc_register_driver(&pn532_i2c_driver);
155 #endif /* DRIVER_PN532_I2C_ENABLED */
156 #if defined (DRIVER_ARYGON_ENABLED)
157  nfc_register_driver(&arygon_driver);
158 #endif /* DRIVER_ARYGON_ENABLED */
159 }
160 
161 
169 int
170 nfc_register_driver(const struct nfc_driver *ndr)
171 {
172  if (!ndr)
173  return NFC_EINVARG;
174 
175  struct nfc_driver_list *pndl = (struct nfc_driver_list *)malloc(sizeof(struct nfc_driver_list));
176  if (!pndl)
177  return NFC_ESOFT;
178 
179  pndl->driver = ndr;
180  pndl->next = nfc_drivers;
181  nfc_drivers = pndl;
182 
183  return NFC_SUCCESS;
184 }
185 
191 void
193 {
194  *context = nfc_context_new();
195  if (!*context) {
196  perror("malloc");
197  return;
198  }
199  if (!nfc_drivers)
200  nfc_drivers_init();
201 }
202 
208 void
210 {
211  while (nfc_drivers) {
212  struct nfc_driver_list *pndl = (struct nfc_driver_list *) nfc_drivers;
213  nfc_drivers = pndl->next;
214  free(pndl);
215  }
216 
217  nfc_context_free(context);
218 }
219 
237 nfc_device *
238 nfc_open(nfc_context *context, const nfc_connstring connstring)
239 {
240  nfc_device *pnd = NULL;
241 
242  nfc_connstring ncs;
243  if (connstring == NULL) {
244  if (!nfc_list_devices(context, &ncs, 1)) {
245  return NULL;
246  }
247  } else {
248  strncpy(ncs, connstring, sizeof(nfc_connstring));
249  ncs[sizeof(nfc_connstring) - 1] = '\0';
250  }
251 
252  // Search through the device list for an available device
253  const struct nfc_driver_list *pndl = nfc_drivers;
254  while (pndl) {
255  const struct nfc_driver *ndr = pndl->driver;
256 
257  // Specific device is requested: using device description
258  if (0 != strncmp(ndr->name, ncs, strlen(ndr->name))) {
259  // Check if connstring driver is usb -> accept any driver *_usb
260  if ((0 != strncmp("usb", ncs, strlen("usb"))) || 0 != strncmp("_usb", ndr->name + (strlen(ndr->name) - 4), 4)) {
261  pndl = pndl->next;
262  continue;
263  }
264  }
265 
266  pnd = ndr->open(context, ncs);
267  // Test if the opening was successful
268  if (pnd == NULL) {
269  if (0 == strncmp("usb", ncs, strlen("usb"))) {
270  // We've to test the other usb drivers before giving up
271  pndl = pndl->next;
272  continue;
273  }
274  log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG, "Unable to open \"%s\".", ncs);
275  return NULL;
276  }
277  for (uint32_t i = 0; i > context->user_defined_device_count; i++) {
278  if (strcmp(ncs, context->user_defined_devices[i].connstring) == 0) {
279  // This is a device sets by user, we use the device name given by user
280  strcpy(pnd->name, context->user_defined_devices[i].name);
281  break;
282  }
283  }
284  log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG, "\"%s\" (%s) has been claimed.", pnd->name, pnd->connstring);
285  return pnd;
286  }
287 
288  // Too bad, no driver can decode connstring
289  log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG, "No driver available to handle \"%s\".", ncs);
290  return NULL;
291 }
292 
299 void
301 {
302  if (pnd) {
303  // Close, clean up and release the device
304  pnd->driver->close(pnd);
305  }
306 }
307 
316 size_t
317 nfc_list_devices(nfc_context *context, nfc_connstring connstrings[], const size_t connstrings_len)
318 {
319  size_t device_found = 0;
320 
321 #ifdef CONFFILES
322  // Load manually configured devices (from config file and env variables)
323  // TODO From env var...
324  for (uint32_t i = 0; i < context->user_defined_device_count; i++) {
325  if (context->user_defined_devices[i].optional) {
326  // let's make sure the device exists
327  nfc_device *pnd = NULL;
328 
329 #ifdef ENVVARS
330  char *env_log_level = getenv("LIBNFC_LOG_LEVEL");
331  char *old_env_log_level = NULL;
332  // do it silently
333  if (env_log_level) {
334  if ((old_env_log_level = malloc(strlen(env_log_level) + 1)) == NULL) {
335  log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "%s", "Unable to malloc()");
336  return 0;
337  }
338  strcpy(old_env_log_level, env_log_level);
339  }
340  setenv("LIBNFC_LOG_LEVEL", "0", 1);
341 #endif // ENVVARS
342 
343  pnd = nfc_open(context, context->user_defined_devices[i].connstring);
344 
345 #ifdef ENVVARS
346  if (old_env_log_level) {
347  setenv("LIBNFC_LOG_LEVEL", old_env_log_level, 1);
348  free(old_env_log_level);
349  } else {
350  unsetenv("LIBNFC_LOG_LEVEL");
351  }
352 #endif // ENVVARS
353 
354  if (pnd) {
355  nfc_close(pnd);
356  log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG, "User device %s found", context->user_defined_devices[i].name);
357  strcpy((char *)(connstrings + device_found), context->user_defined_devices[i].connstring);
358  device_found ++;
359  if (device_found == connstrings_len)
360  break;
361  }
362  } else {
363  // manual choice is not marked as optional so let's take it blindly
364  strcpy((char *)(connstrings + device_found), context->user_defined_devices[i].connstring);
365  device_found++;
366  if (device_found >= connstrings_len)
367  return device_found;
368  }
369  }
370 #endif // CONFFILES
371 
372  // Device auto-detection
373  if (context->allow_autoscan) {
374  const struct nfc_driver_list *pndl = nfc_drivers;
375  while (pndl) {
376  const struct nfc_driver *ndr = pndl->driver;
377  if ((ndr->scan_type == NOT_INTRUSIVE) || ((context->allow_intrusive_scan) && (ndr->scan_type == INTRUSIVE))) {
378  size_t _device_found = ndr->scan(context, connstrings + (device_found), connstrings_len - (device_found));
379  log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG, "%ld device(s) found using %s driver", (unsigned long) _device_found, ndr->name);
380  if (_device_found > 0) {
381  device_found += _device_found;
382  if (device_found == connstrings_len)
383  break;
384  }
385  } // scan_type is INTRUSIVE but not allowed or NOT_AVAILABLE
386  pndl = pndl->next;
387  }
388  } else if (context->user_defined_device_count == 0) {
389  log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_INFO, "Warning: %s" , "user must specify device(s) manually when autoscan is disabled");
390  }
391 
392  return device_found;
393 }
394 
406 int
407 nfc_device_set_property_int(nfc_device *pnd, const nfc_property property, const int value)
408 {
409  HAL(device_set_property_int, pnd, property, value);
410 }
411 
412 
425 int
426 nfc_device_set_property_bool(nfc_device *pnd, const nfc_property property, const bool bEnable)
427 {
428  HAL(device_set_property_bool, pnd, property, bEnable);
429 }
430 
451 int
453 {
454  int res = 0;
455  // Drop the field for a while
456  if ((res = nfc_device_set_property_bool(pnd, NP_ACTIVATE_FIELD, false)) < 0)
457  return res;
458  // Enable field so more power consuming cards can power themselves up
459  if ((res = nfc_device_set_property_bool(pnd, NP_ACTIVATE_FIELD, true)) < 0)
460  return res;
461  // Let the device try forever to find a target/tag
462  if ((res = nfc_device_set_property_bool(pnd, NP_INFINITE_SELECT, true)) < 0)
463  return res;
464  // Activate auto ISO14443-4 switching by default
465  if ((res = nfc_device_set_property_bool(pnd, NP_AUTO_ISO14443_4, true)) < 0)
466  return res;
467  // Force 14443-A mode
468  if ((res = nfc_device_set_property_bool(pnd, NP_FORCE_ISO14443_A, true)) < 0)
469  return res;
470  // Force speed at 106kbps
471  if ((res = nfc_device_set_property_bool(pnd, NP_FORCE_SPEED_106, true)) < 0)
472  return res;
473  // Disallow invalid frame
474  if ((res = nfc_device_set_property_bool(pnd, NP_ACCEPT_INVALID_FRAMES, false)) < 0)
475  return res;
476  // Disallow multiple frames
477  if ((res = nfc_device_set_property_bool(pnd, NP_ACCEPT_MULTIPLE_FRAMES, false)) < 0)
478  return res;
479  HAL(initiator_init, pnd);
480 }
481 
491 int
493 {
494  HAL(initiator_init_secure_element, pnd);
495 }
496 
517 int
519  const nfc_modulation nm,
520  const uint8_t *pbtInitData, const size_t szInitData,
521  nfc_target *pnt)
522 {
523  uint8_t abtInit[MAX(12, szInitData)];
524  size_t szInit;
525 
526  switch (nm.nmt) {
527  case NMT_ISO14443A:
528  iso14443_cascade_uid(pbtInitData, szInitData, abtInit, &szInit);
529  break;
530 
531  case NMT_JEWEL:
532  case NMT_ISO14443B:
533  case NMT_ISO14443BI:
534  case NMT_ISO14443B2SR:
535  case NMT_ISO14443B2CT:
536  case NMT_FELICA:
537  case NMT_DEP:
538  memcpy(abtInit, pbtInitData, szInitData);
539  szInit = szInitData;
540  break;
541  }
542 
543  HAL(initiator_select_passive_target, pnd, nm, abtInit, szInit, pnt);
544 }
545 
562 int
564  const nfc_modulation nm,
565  nfc_target ant[], const size_t szTargets)
566 {
567  nfc_target nt;
568  size_t szTargetFound = 0;
569  uint8_t *pbtInitData = NULL;
570  size_t szInitDataLen = 0;
571  int res = 0;
572 
573  pnd->last_error = 0;
574 
575  // Let the reader only try once to find a tag
576  if ((res = nfc_device_set_property_bool(pnd, NP_INFINITE_SELECT, false)) < 0) {
577  return res;
578  }
579 
580  prepare_initiator_data(nm, &pbtInitData, &szInitDataLen);
581 
582  while (nfc_initiator_select_passive_target(pnd, nm, pbtInitData, szInitDataLen, &nt) > 0) {
583  size_t i;
584  bool seen = false;
585  // Check if we've already seen this tag
586  for (i = 0; i < szTargetFound; i++) {
587  if (memcmp(&(ant[i]), &nt, sizeof(nfc_target)) == 0) {
588  seen = true;
589  }
590  }
591  if (seen) {
592  break;
593  }
594  memcpy(&(ant[szTargetFound]), &nt, sizeof(nfc_target));
595  szTargetFound++;
596  if (szTargets == szTargetFound) {
597  break;
598  }
599  nfc_initiator_deselect_target(pnd);
600  // deselect has no effect on FeliCa and Jewel cards so we'll stop after one...
601  // ISO/IEC 14443 B' cards are polled at 100% probability so it's not possible to detect correctly two cards at the same time
602  if ((nm.nmt == NMT_FELICA) || (nm.nmt == NMT_JEWEL) || (nm.nmt == NMT_ISO14443BI) || (nm.nmt == NMT_ISO14443B2SR) || (nm.nmt == NMT_ISO14443B2CT)) {
603  break;
604  }
605  }
606  return szTargetFound;
607 }
608 
622 int
624  const nfc_modulation *pnmModulations, const size_t szModulations,
625  const uint8_t uiPollNr, const uint8_t uiPeriod,
626  nfc_target *pnt)
627 {
628  HAL(initiator_poll_target, pnd, pnmModulations, szModulations, uiPollNr, uiPeriod, pnt);
629 }
630 
631 
652 int
654  const nfc_dep_mode ndm, const nfc_baud_rate nbr,
655  const nfc_dep_info *pndiInitiator, nfc_target *pnt, const int timeout)
656 {
657  HAL(initiator_select_dep_target, pnd, ndm, nbr, pndiInitiator, pnt, timeout);
658 }
659 
677 int
679  const nfc_dep_mode ndm, const nfc_baud_rate nbr,
680  const nfc_dep_info *pndiInitiator,
681  nfc_target *pnt,
682  const int timeout)
683 {
684  const int period = 300;
685  int remaining_time = timeout;
686  int res;
687  if ((res = nfc_device_set_property_bool(pnd, NP_INFINITE_SELECT, true)) < 0)
688  return res;
689  while (remaining_time > 0) {
690  if ((res = nfc_initiator_select_dep_target(pnd, ndm, nbr, pndiInitiator, pnt, period)) < 0) {
691  if (res != NFC_ETIMEOUT)
692  return res;
693  }
694  if (res == 1)
695  return res;
696  remaining_time -= period;
697  }
698  return 0;
699 }
700 
713 int
714 nfc_initiator_deselect_target(nfc_device *pnd)
715 {
716  HAL(initiator_deselect_target, pnd);
717 }
718 
747 int
748 nfc_initiator_transceive_bytes(nfc_device *pnd, const uint8_t *pbtTx, const size_t szTx, uint8_t *pbtRx,
749  const size_t szRx, int timeout)
750 {
751  HAL(initiator_transceive_bytes, pnd, pbtTx, szTx, pbtRx, szRx, timeout)
752 }
753 
790 int
792  const uint8_t *pbtTx, const size_t szTxBits, const uint8_t *pbtTxPar,
793  uint8_t *pbtRx, const size_t szRx,
794  uint8_t *pbtRxPar)
795 {
796  (void)szRx;
797  HAL(initiator_transceive_bits, pnd, pbtTx, szTxBits, pbtTxPar, pbtRx, pbtRxPar);
798 }
799 
826 int
828  const uint8_t *pbtTx, const size_t szTx,
829  uint8_t *pbtRx, const size_t szRx,
830  uint32_t *cycles)
831 {
832  HAL(initiator_transceive_bytes_timed, pnd, pbtTx, szTx, pbtRx, szRx, cycles);
833 }
834 
843 int
845 {
846  HAL(initiator_target_is_present, pnd, pnt);
847 }
848 
870 int
872  const uint8_t *pbtTx, const size_t szTxBits, const uint8_t *pbtTxPar,
873  uint8_t *pbtRx, const size_t szRx,
874  uint8_t *pbtRxPar,
875  uint32_t *cycles)
876 {
877  (void)szRx;
878  HAL(initiator_transceive_bits_timed, pnd, pbtTx, szTxBits, pbtTxPar, pbtRx, pbtRxPar, cycles);
879 }
880 
914 int
915 nfc_target_init(nfc_device *pnd, nfc_target *pnt, uint8_t *pbtRx, const size_t szRx, int timeout)
916 {
917  int res = 0;
918  // Disallow invalid frame
919  if ((res = nfc_device_set_property_bool(pnd, NP_ACCEPT_INVALID_FRAMES, false)) < 0)
920  return res;
921  // Disallow multiple frames
922  if ((res = nfc_device_set_property_bool(pnd, NP_ACCEPT_MULTIPLE_FRAMES, false)) < 0)
923  return res;
924  // Make sure we reset the CRC and parity to chip handling.
925  if ((res = nfc_device_set_property_bool(pnd, NP_HANDLE_CRC, true)) < 0)
926  return res;
927  if ((res = nfc_device_set_property_bool(pnd, NP_HANDLE_PARITY, true)) < 0)
928  return res;
929  // Activate auto ISO14443-4 switching by default
930  if ((res = nfc_device_set_property_bool(pnd, NP_AUTO_ISO14443_4, true)) < 0)
931  return res;
932  // Activate "easy framing" feature by default
933  if ((res = nfc_device_set_property_bool(pnd, NP_EASY_FRAMING, true)) < 0)
934  return res;
935  // Deactivate the CRYPTO1 cipher, it may could cause problems when still active
936  if ((res = nfc_device_set_property_bool(pnd, NP_ACTIVATE_CRYPTO1, false)) < 0)
937  return res;
938  // Drop explicitely the field
939  if ((res = nfc_device_set_property_bool(pnd, NP_ACTIVATE_FIELD, false)) < 0)
940  return res;
941 
942  HAL(target_init, pnd, pnt, pbtRx, szRx, timeout);
943 }
944 
955 int
957 {
958  HAL(idle, pnd);
959 }
960 
972 int
974 {
975  HAL(abort_command, pnd);
976 }
977 
993 int
994 nfc_target_send_bytes(nfc_device *pnd, const uint8_t *pbtTx, const size_t szTx, int timeout)
995 {
996  HAL(target_send_bytes, pnd, pbtTx, szTx, timeout);
997 }
998 
1013 int
1014 nfc_target_receive_bytes(nfc_device *pnd, uint8_t *pbtRx, const size_t szRx, int timeout)
1015 {
1016  HAL(target_receive_bytes, pnd, pbtRx, szRx, timeout);
1017 }
1018 
1030 int
1031 nfc_target_send_bits(nfc_device *pnd, const uint8_t *pbtTx, const size_t szTxBits, const uint8_t *pbtTxPar)
1032 {
1033  HAL(target_send_bits, pnd, pbtTx, szTxBits, pbtTxPar);
1034 }
1035 
1052 int
1053 nfc_target_receive_bits(nfc_device *pnd, uint8_t *pbtRx, const size_t szRx, uint8_t *pbtRxPar)
1054 {
1055  HAL(target_receive_bits, pnd, pbtRx, szRx, pbtRxPar);
1056 }
1057 
1058 static struct sErrorMessage {
1059  int iErrorCode;
1060  const char *pcErrorMsg;
1061 } sErrorMessages[] = {
1062  /* Chip-level errors (internal errors, RF errors, etc.) */
1063  { NFC_SUCCESS, "Success" },
1064  { NFC_EIO, "Input / Output Error" },
1065  { NFC_EINVARG, "Invalid argument(s)" },
1066  { NFC_EDEVNOTSUPP, "Not Supported by Device" },
1067  { NFC_ENOTSUCHDEV, "No Such Device" },
1068  { NFC_EOVFLOW, "Buffer Overflow" },
1069  { NFC_ETIMEOUT, "Timeout" },
1070  { NFC_EOPABORTED, "Operation Aborted" },
1071  { NFC_ENOTIMPL, "Not (yet) Implemented" },
1072  { NFC_ETGRELEASED, "Target Released" },
1073  { NFC_EMFCAUTHFAIL, "Mifare Authentication Failed" },
1074  { NFC_ERFTRANS, "RF Transmission Error" },
1075  { NFC_ECHIP, "Device's Internal Chip Error" },
1076 };
1077 
1084 const char *
1086 {
1087  const char *pcRes = "Unknown error";
1088  size_t i;
1089  for (i = 0; i < (sizeof(sErrorMessages) / sizeof(struct sErrorMessage)); i++) {
1090  if (sErrorMessages[i].iErrorCode == pnd->last_error) {
1091  pcRes = sErrorMessages[i].pcErrorMsg;
1092  break;
1093  }
1094  }
1095 
1096  return pcRes;
1097 }
1098 
1107 int
1108 nfc_strerror_r(const nfc_device *pnd, char *pcStrErrBuf, size_t szBufLen)
1109 {
1110  return (snprintf(pcStrErrBuf, szBufLen, "%s", nfc_strerror(pnd)) < 0) ? -1 : 0;
1111 }
1112 
1119 void
1120 nfc_perror(const nfc_device *pnd, const char *pcString)
1121 {
1122  fprintf(stderr, "%s: %s\n", pcString, nfc_strerror(pnd));
1123 }
1124 
1131 int
1133 {
1134  return pnd->last_error;
1135 }
1136 
1137 /* Special data accessors */
1138 
1145 const char *
1147 {
1148  return pnd->name;
1149 }
1150 
1157 const char *
1159 {
1160  return pnd->connstring;
1161 }
1162 
1171 int
1173 {
1174  HAL(get_supported_modulation, pnd, mode, supported_mt);
1175 }
1176 
1185 int
1187 {
1188  HAL(get_supported_baud_rate, pnd, nmt, supported_br);
1189 }
1190 
1191 /* Misc. functions */
1192 
1199 const char *
1201 {
1202 #ifdef GIT_REVISION
1203  return GIT_REVISION;
1204 #else
1205  return PACKAGE_VERSION;
1206 #endif // GIT_REVISION
1207 }
1208 
1214 void
1215 nfc_free(void *p)
1216 {
1217  free(p);
1218 }
1219 
1228 int
1230 {
1231  HAL(device_get_information_about, pnd, buf);
1232 }
1233 
1239 const char *
1241 {
1242  switch (nbr) {
1243  case NBR_UNDEFINED:
1244  return "undefined baud rate";
1245  break;
1246  case NBR_106:
1247  return "106 kbps";
1248  break;
1249  case NBR_212:
1250  return "212 kbps";
1251  break;
1252  case NBR_424:
1253  return "424 kbps";
1254  break;
1255  case NBR_847:
1256  return "847 kbps";
1257  break;
1258  }
1259  // Should never go there..
1260  return "";
1261 }
1262 
1268 const char *
1270 {
1271  switch (nmt) {
1272  case NMT_ISO14443A:
1273  return "ISO/IEC 14443A";
1274  break;
1275  case NMT_ISO14443B:
1276  return "ISO/IEC 14443-4B";
1277  break;
1278  case NMT_ISO14443BI:
1279  return "ISO/IEC 14443-4B'";
1280  break;
1281  case NMT_ISO14443B2CT:
1282  return "ISO/IEC 14443-2B ASK CTx";
1283  break;
1284  case NMT_ISO14443B2SR:
1285  return "ISO/IEC 14443-2B ST SRx";
1286  break;
1287  case NMT_FELICA:
1288  return "FeliCa";
1289  break;
1290  case NMT_JEWEL:
1291  return "Innovision Jewel";
1292  break;
1293  case NMT_DEP:
1294  return "D.E.P.";
1295  break;
1296  }
1297  // Should never go there..
1298  return "";
1299 }
1300 
1309 int
1310 str_nfc_target(char **buf, const nfc_target *pnt, bool verbose)
1311 {
1312  *buf = malloc(4096);
1313  if (! *buf)
1314  return NFC_ESOFT;
1315  (*buf)[0] = '\0';
1316  snprint_nfc_target(*buf, 4096, pnt, verbose);
1317  return strlen(*buf);
1318 }
void nfc_init(nfc_context **context)
Initialize libnfc. This function must be called before calling any other libnfc function.
Definition: nfc.c:192
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:563
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:748
NFC target information in D.E.P. (Data Exchange Protocol) see ISO/IEC 18092 (NFCIP-1) ...
Definition: nfc-types.h:161
#define NFC_EINVARG
Definition: nfc.h:166
Internal defines and macros.
int nfc_initiator_target_is_present(nfc_device *pnd, const nfc_target *pnt)
Check target presence.
Definition: nfc.c:844
int nfc_device_set_property_bool(nfc_device *pnd, const nfc_property property, const bool bEnable)
Set a device&#39;s boolean-property value.
Definition: nfc.c:426
#define NFC_ETGRELEASED
Definition: nfc.h:201
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:915
const char * nfc_version(void)
Returns the library version.
Definition: nfc.c:1200
#define NFC_EDEVNOTSUPP
Definition: nfc.h:171
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:871
#define HAL(FUNCTION,...)
Execute corresponding driver function if exists.
Definition: nfc-internal.h:47
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:1108
int nfc_initiator_init(nfc_device *pnd)
Initialize NFC device as initiator (reader)
Definition: nfc.c:452
libnfc interface
nfc_connstring connstring
Definition: nfc-internal.h:199
#define NFC_ENOTSUCHDEV
Definition: nfc.h:176
int nfc_device_get_information_about(nfc_device *pnd, char **buf)
Print information about NFC device.
Definition: nfc.c:1229
int nfc_target_receive_bits(nfc_device *pnd, uint8_t *pbtRx, const size_t szRx, uint8_t *pbtRxPar)
Receive bit-frames.
Definition: nfc.c:1053
const char * str_nfc_modulation_type(const nfc_modulation_type nmt)
Convert nfc_modulation_type value to string.
Definition: nfc.c:1269
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:678
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:623
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:994
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:653
nfc_modulation_type
NFC modulation type enumeration.
Definition: nfc-types.h:295
void nfc_perror(const nfc_device *pnd, const char *pcString)
Display the last error occured on a nfc_device.
Definition: nfc.c:1120
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:317
void nfc_exit(nfc_context *context)
Deinitialize libnfc. Should be called after closing all open devices and before your application term...
Definition: nfc.c:209
#define NFC_ETIMEOUT
Definition: nfc.h:186
#define NFC_EIO
Definition: nfc.h:161
nfc_device * nfc_open(nfc_context *context, const nfc_connstring connstring)
Open a NFC device.
Definition: nfc.c:238
int nfc_initiator_init_secure_element(nfc_device *pnd)
Initialize NFC device as initiator with its secure element initiator (reader)
Definition: nfc.c:492
nfc_property
Definition: nfc-types.h:67
#define NFC_EOVFLOW
Definition: nfc.h:181
int nfc_device_set_property_int(nfc_device *pnd, const nfc_property property, const int value)
Set a device&#39;s integer-property value.
Definition: nfc.c:407
#define NFC_SUCCESS
Definition: nfc.h:156
const char * nfc_device_get_connstring(nfc_device *pnd)
Returns the device connection string.
Definition: nfc.c:1158
struct nfc_driver nfc_driver
Definition: nfc-types.h:57
nfc_dep_mode
NFC D.E.P. (Data Exchange Protocol) active/passive mode.
Definition: nfc-types.h:151
NFC device information.
Definition: nfc-internal.h:190
char name[DEVICE_NAME_LENGTH]
Definition: nfc-internal.h:197
nfc_baud_rate
NFC baud rate enumeration.
Definition: nfc-types.h:283
#define NFC_EMFCAUTHFAIL
Definition: nfc.h:211
const char * nfc_strerror(const nfc_device *pnd)
Return the last error string.
Definition: nfc.c:1085
int nfc_device_get_last_error(const nfc_device *pnd)
Returns last error occured on a nfc_device.
Definition: nfc.c:1132
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.
Definition: nfc.c:1186
NFC modulation structure.
Definition: nfc-types.h:319
char nfc_connstring[NFC_BUFSIZE_CONNSTRING]
Definition: nfc-types.h:62
const char * nfc_device_get_name(nfc_device *pnd)
Returns the device name.
Definition: nfc.c:1146
int nfc_abort_command(nfc_device *pnd)
Abort current running command.
Definition: nfc.c:973
NFC target structure.
Definition: nfc-types.h:328
#define NFC_EOPABORTED
Definition: nfc.h:191
int nfc_idle(nfc_device *pnd)
Turn NFC device in idle mode.
Definition: nfc.c:956
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:518
#define NFC_ERFTRANS
Definition: nfc.h:206
int str_nfc_target(char **buf, const nfc_target *pnt, bool verbose)
Convert nfc_modulation_type value to string.
Definition: nfc.c:1310
const char * str_nfc_baud_rate(const nfc_baud_rate nbr)
Convert nfc_baud_rate value to string.
Definition: nfc.c:1240
void nfc_free(void *p)
Free buffer allocated by libnfc.
Definition: nfc.c:1215
#define NFC_ESOFT
Definition: nfc.h:216
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:1014
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:1031
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:1172
int nfc_register_driver(const struct nfc_driver *ndr)
Register an NFC device driver with libnfc. This function registers a driver with libnfc, the caller is responsible of managing the lifetime of the driver and make sure that any resources associated with the driver are available after registration.
Definition: nfc.c:170
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:791
#define NFC_ECHIP
Definition: nfc.h:221
#define NFC_ENOTIMPL
Definition: nfc.h:196
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:827
void nfc_close(nfc_device *pnd)
Close from a NFC device.
Definition: nfc.c:300
NFC library context Struct which contains internal options, references, pointers, etc...
Definition: nfc-internal.h:175
nfc_mode
NFC mode type enumeration.
Definition: nfc-types.h:310