libnfc  1.7.1
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 
520 int
522  const nfc_modulation nm,
523  const uint8_t *pbtInitData, const size_t szInitData,
524  nfc_target *pnt)
525 {
526  uint8_t *abtInit = NULL;
527  uint8_t abtTmpInit[MAX(12, szInitData)];
528  size_t szInit = 0;
529  if (szInitData == 0) {
530  // Provide default values, if any
531  prepare_initiator_data(nm, &abtInit, &szInit);
532  } else if (nm.nmt == NMT_ISO14443A) {
533  abtInit = abtTmpInit;
534  iso14443_cascade_uid(pbtInitData, szInitData, abtInit, &szInit);
535  } else {
536  abtInit = abtTmpInit;
537  memcpy(abtInit, pbtInitData, szInitData);
538  szInit = szInitData;
539  }
540 
541  HAL(initiator_select_passive_target, pnd, nm, abtInit, szInit, pnt);
542 }
543 
560 int
562  const nfc_modulation nm,
563  nfc_target ant[], const size_t szTargets)
564 {
565  nfc_target nt;
566  size_t szTargetFound = 0;
567  uint8_t *pbtInitData = NULL;
568  size_t szInitDataLen = 0;
569  int res = 0;
570 
571  pnd->last_error = 0;
572 
573  // Let the reader only try once to find a tag
574  bool bInfiniteSelect = pnd->bInfiniteSelect;
575  if ((res = nfc_device_set_property_bool(pnd, NP_INFINITE_SELECT, false)) < 0) {
576  return res;
577  }
578 
579  prepare_initiator_data(nm, &pbtInitData, &szInitDataLen);
580 
581  while (nfc_initiator_select_passive_target(pnd, nm, pbtInitData, szInitDataLen, &nt) > 0) {
582  size_t i;
583  bool seen = false;
584  // Check if we've already seen this tag
585  for (i = 0; i < szTargetFound; i++) {
586  if (memcmp(&(ant[i]), &nt, sizeof(nfc_target)) == 0) {
587  seen = true;
588  }
589  }
590  if (seen) {
591  break;
592  }
593  memcpy(&(ant[szTargetFound]), &nt, sizeof(nfc_target));
594  szTargetFound++;
595  if (szTargets == szTargetFound) {
596  break;
597  }
598  nfc_initiator_deselect_target(pnd);
599  // deselect has no effect on FeliCa and Jewel cards so we'll stop after one...
600  // ISO/IEC 14443 B' cards are polled at 100% probability so it's not possible to detect correctly two cards at the same time
601  if ((nm.nmt == NMT_FELICA) || (nm.nmt == NMT_JEWEL) || (nm.nmt == NMT_ISO14443BI) || (nm.nmt == NMT_ISO14443B2SR) || (nm.nmt == NMT_ISO14443B2CT)) {
602  break;
603  }
604  }
605  if (bInfiniteSelect) {
606  if ((res = nfc_device_set_property_bool(pnd, NP_INFINITE_SELECT, true)) < 0) {
607  return res;
608  }
609  }
610  return szTargetFound;
611 }
612 
626 int
628  const nfc_modulation *pnmModulations, const size_t szModulations,
629  const uint8_t uiPollNr, const uint8_t uiPeriod,
630  nfc_target *pnt)
631 {
632  HAL(initiator_poll_target, pnd, pnmModulations, szModulations, uiPollNr, uiPeriod, pnt);
633 }
634 
635 
656 int
658  const nfc_dep_mode ndm, const nfc_baud_rate nbr,
659  const nfc_dep_info *pndiInitiator, nfc_target *pnt, const int timeout)
660 {
661  HAL(initiator_select_dep_target, pnd, ndm, nbr, pndiInitiator, pnt, timeout);
662 }
663 
681 int
683  const nfc_dep_mode ndm, const nfc_baud_rate nbr,
684  const nfc_dep_info *pndiInitiator,
685  nfc_target *pnt,
686  const int timeout)
687 {
688  const int period = 300;
689  int remaining_time = timeout;
690  int res;
691  int result = 0;
692  bool bInfiniteSelect = pnd->bInfiniteSelect;
693  if ((res = nfc_device_set_property_bool(pnd, NP_INFINITE_SELECT, true)) < 0)
694  return res;
695  while (remaining_time > 0) {
696  if ((res = nfc_initiator_select_dep_target(pnd, ndm, nbr, pndiInitiator, pnt, period)) < 0) {
697  if (res != NFC_ETIMEOUT) {
698  result = res;
699  goto end;
700  }
701  }
702  if (res == 1) {
703  result = res;
704  goto end;
705  }
706  remaining_time -= period;
707  }
708 end:
709  if (! bInfiniteSelect) {
710  if ((res = nfc_device_set_property_bool(pnd, NP_INFINITE_SELECT, false)) < 0) {
711  return res;
712  }
713  }
714  return result;
715 }
716 
729 int
730 nfc_initiator_deselect_target(nfc_device *pnd)
731 {
732  HAL(initiator_deselect_target, pnd);
733 }
734 
763 int
764 nfc_initiator_transceive_bytes(nfc_device *pnd, const uint8_t *pbtTx, const size_t szTx, uint8_t *pbtRx,
765  const size_t szRx, int timeout)
766 {
767  HAL(initiator_transceive_bytes, pnd, pbtTx, szTx, pbtRx, szRx, timeout)
768 }
769 
806 int
808  const uint8_t *pbtTx, const size_t szTxBits, const uint8_t *pbtTxPar,
809  uint8_t *pbtRx, const size_t szRx,
810  uint8_t *pbtRxPar)
811 {
812  (void)szRx;
813  HAL(initiator_transceive_bits, pnd, pbtTx, szTxBits, pbtTxPar, pbtRx, pbtRxPar);
814 }
815 
842 int
844  const uint8_t *pbtTx, const size_t szTx,
845  uint8_t *pbtRx, const size_t szRx,
846  uint32_t *cycles)
847 {
848  HAL(initiator_transceive_bytes_timed, pnd, pbtTx, szTx, pbtRx, szRx, cycles);
849 }
850 
861 int
863 {
864  HAL(initiator_target_is_present, pnd, pnt);
865 }
866 
888 int
890  const uint8_t *pbtTx, const size_t szTxBits, const uint8_t *pbtTxPar,
891  uint8_t *pbtRx, const size_t szRx,
892  uint8_t *pbtRxPar,
893  uint32_t *cycles)
894 {
895  (void)szRx;
896  HAL(initiator_transceive_bits_timed, pnd, pbtTx, szTxBits, pbtTxPar, pbtRx, pbtRxPar, cycles);
897 }
898 
932 int
933 nfc_target_init(nfc_device *pnd, nfc_target *pnt, uint8_t *pbtRx, const size_t szRx, int timeout)
934 {
935  int res = 0;
936  // Disallow invalid frame
937  if ((res = nfc_device_set_property_bool(pnd, NP_ACCEPT_INVALID_FRAMES, false)) < 0)
938  return res;
939  // Disallow multiple frames
940  if ((res = nfc_device_set_property_bool(pnd, NP_ACCEPT_MULTIPLE_FRAMES, false)) < 0)
941  return res;
942  // Make sure we reset the CRC and parity to chip handling.
943  if ((res = nfc_device_set_property_bool(pnd, NP_HANDLE_CRC, true)) < 0)
944  return res;
945  if ((res = nfc_device_set_property_bool(pnd, NP_HANDLE_PARITY, true)) < 0)
946  return res;
947  // Activate auto ISO14443-4 switching by default
948  if ((res = nfc_device_set_property_bool(pnd, NP_AUTO_ISO14443_4, true)) < 0)
949  return res;
950  // Activate "easy framing" feature by default
951  if ((res = nfc_device_set_property_bool(pnd, NP_EASY_FRAMING, true)) < 0)
952  return res;
953  // Deactivate the CRYPTO1 cipher, it may could cause problems when still active
954  if ((res = nfc_device_set_property_bool(pnd, NP_ACTIVATE_CRYPTO1, false)) < 0)
955  return res;
956  // Drop explicitely the field
957  if ((res = nfc_device_set_property_bool(pnd, NP_ACTIVATE_FIELD, false)) < 0)
958  return res;
959 
960  HAL(target_init, pnd, pnt, pbtRx, szRx, timeout);
961 }
962 
973 int
975 {
976  HAL(idle, pnd);
977 }
978 
990 int
992 {
993  HAL(abort_command, pnd);
994 }
995 
1011 int
1012 nfc_target_send_bytes(nfc_device *pnd, const uint8_t *pbtTx, const size_t szTx, int timeout)
1013 {
1014  HAL(target_send_bytes, pnd, pbtTx, szTx, timeout);
1015 }
1016 
1031 int
1032 nfc_target_receive_bytes(nfc_device *pnd, uint8_t *pbtRx, const size_t szRx, int timeout)
1033 {
1034  HAL(target_receive_bytes, pnd, pbtRx, szRx, timeout);
1035 }
1036 
1048 int
1049 nfc_target_send_bits(nfc_device *pnd, const uint8_t *pbtTx, const size_t szTxBits, const uint8_t *pbtTxPar)
1050 {
1051  HAL(target_send_bits, pnd, pbtTx, szTxBits, pbtTxPar);
1052 }
1053 
1070 int
1071 nfc_target_receive_bits(nfc_device *pnd, uint8_t *pbtRx, const size_t szRx, uint8_t *pbtRxPar)
1072 {
1073  HAL(target_receive_bits, pnd, pbtRx, szRx, pbtRxPar);
1074 }
1075 
1076 static struct sErrorMessage {
1077  int iErrorCode;
1078  const char *pcErrorMsg;
1079 } sErrorMessages[] = {
1080  /* Chip-level errors (internal errors, RF errors, etc.) */
1081  { NFC_SUCCESS, "Success" },
1082  { NFC_EIO, "Input / Output Error" },
1083  { NFC_EINVARG, "Invalid argument(s)" },
1084  { NFC_EDEVNOTSUPP, "Not Supported by Device" },
1085  { NFC_ENOTSUCHDEV, "No Such Device" },
1086  { NFC_EOVFLOW, "Buffer Overflow" },
1087  { NFC_ETIMEOUT, "Timeout" },
1088  { NFC_EOPABORTED, "Operation Aborted" },
1089  { NFC_ENOTIMPL, "Not (yet) Implemented" },
1090  { NFC_ETGRELEASED, "Target Released" },
1091  { NFC_EMFCAUTHFAIL, "Mifare Authentication Failed" },
1092  { NFC_ERFTRANS, "RF Transmission Error" },
1093  { NFC_ECHIP, "Device's Internal Chip Error" },
1094 };
1095 
1102 const char *
1104 {
1105  const char *pcRes = "Unknown error";
1106  size_t i;
1107  for (i = 0; i < (sizeof(sErrorMessages) / sizeof(struct sErrorMessage)); i++) {
1108  if (sErrorMessages[i].iErrorCode == pnd->last_error) {
1109  pcRes = sErrorMessages[i].pcErrorMsg;
1110  break;
1111  }
1112  }
1113 
1114  return pcRes;
1115 }
1116 
1125 int
1126 nfc_strerror_r(const nfc_device *pnd, char *pcStrErrBuf, size_t szBufLen)
1127 {
1128  return (snprintf(pcStrErrBuf, szBufLen, "%s", nfc_strerror(pnd)) < 0) ? -1 : 0;
1129 }
1130 
1137 void
1138 nfc_perror(const nfc_device *pnd, const char *pcString)
1139 {
1140  fprintf(stderr, "%s: %s\n", pcString, nfc_strerror(pnd));
1141 }
1142 
1149 int
1151 {
1152  return pnd->last_error;
1153 }
1154 
1155 /* Special data accessors */
1156 
1163 const char *
1165 {
1166  return pnd->name;
1167 }
1168 
1175 const char *
1177 {
1178  return pnd->connstring;
1179 }
1180 
1189 int
1191 {
1192  HAL(get_supported_modulation, pnd, mode, supported_mt);
1193 }
1194 
1203 int
1205 {
1206  HAL(get_supported_baud_rate, pnd, nmt, supported_br);
1207 }
1208 
1209 /* Misc. functions */
1210 
1217 const char *
1219 {
1220 #ifdef GIT_REVISION
1221  return GIT_REVISION;
1222 #else
1223  return PACKAGE_VERSION;
1224 #endif // GIT_REVISION
1225 }
1226 
1232 void
1233 nfc_free(void *p)
1234 {
1235  free(p);
1236 }
1237 
1246 int
1248 {
1249  HAL(device_get_information_about, pnd, buf);
1250 }
1251 
1257 const char *
1259 {
1260  switch (nbr) {
1261  case NBR_UNDEFINED:
1262  return "undefined baud rate";
1263  break;
1264  case NBR_106:
1265  return "106 kbps";
1266  break;
1267  case NBR_212:
1268  return "212 kbps";
1269  break;
1270  case NBR_424:
1271  return "424 kbps";
1272  break;
1273  case NBR_847:
1274  return "847 kbps";
1275  break;
1276  }
1277  // Should never go there..
1278  return "";
1279 }
1280 
1286 const char *
1288 {
1289  switch (nmt) {
1290  case NMT_ISO14443A:
1291  return "ISO/IEC 14443A";
1292  break;
1293  case NMT_ISO14443B:
1294  return "ISO/IEC 14443-4B";
1295  break;
1296  case NMT_ISO14443BI:
1297  return "ISO/IEC 14443-4B'";
1298  break;
1299  case NMT_ISO14443B2CT:
1300  return "ISO/IEC 14443-2B ASK CTx";
1301  break;
1302  case NMT_ISO14443B2SR:
1303  return "ISO/IEC 14443-2B ST SRx";
1304  break;
1305  case NMT_FELICA:
1306  return "FeliCa";
1307  break;
1308  case NMT_JEWEL:
1309  return "Innovision Jewel";
1310  break;
1311  case NMT_DEP:
1312  return "D.E.P.";
1313  break;
1314  }
1315  // Should never go there..
1316  return "";
1317 }
1318 
1327 int
1328 str_nfc_target(char **buf, const nfc_target *pnt, bool verbose)
1329 {
1330  *buf = malloc(4096);
1331  if (! *buf)
1332  return NFC_ESOFT;
1333  (*buf)[0] = '\0';
1334  snprint_nfc_target(*buf, 4096, pnt, verbose);
1335  return strlen(*buf);
1336 }
nfc_initiator_init
int nfc_initiator_init(nfc_device *pnd)
Initialize NFC device as initiator (reader)
Definition: nfc.c:452
NFC_EINVARG
#define NFC_EINVARG
Definition: nfc.h:166
nfc_init
void nfc_init(nfc_context **context)
Initialize libnfc. This function must be called before calling any other libnfc function.
Definition: nfc.c:192
nfc_context
NFC library context Struct which contains internal options, references, pointers, etc....
Definition: nfc-internal.h:175
nfc_driver
struct nfc_driver nfc_driver
Definition: nfc-types.h:57
nfc_device
NFC device information.
Definition: nfc-internal.h:190
nfc_version
const char * nfc_version(void)
Returns the library version.
Definition: nfc.c:1218
nfc_target_init
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:933
nfc_device_get_supported_modulation
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:1190
NP_INFINITE_SELECT
@ NP_INFINITE_SELECT
Definition: nfc-types.h:114
NP_HANDLE_CRC
@ NP_HANDLE_CRC
Definition: nfc-types.h:93
nfc_dep_info
NFC target information in D.E.P. (Data Exchange Protocol) see ISO/IEC 18092 (NFCIP-1)
Definition: nfc-types.h:161
nfc_initiator_poll_dep_target
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:682
nfc_initiator_list_passive_targets
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:561
nfc_initiator_poll_target
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:627
NP_ACCEPT_INVALID_FRAMES
@ NP_ACCEPT_INVALID_FRAMES
Definition: nfc-types.h:118
nfc_exit
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
nfc_target
NFC target structure.
Definition: nfc-types.h:328
nfc_strerror_r
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:1126
str_nfc_baud_rate
const char * str_nfc_baud_rate(const nfc_baud_rate nbr)
Convert nfc_baud_rate value to string.
Definition: nfc.c:1258
NFC_ECHIP
#define NFC_ECHIP
Definition: nfc.h:221
nfc_register_driver
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:170
nfc_target_receive_bits
int nfc_target_receive_bits(nfc_device *pnd, uint8_t *pbtRx, const size_t szRx, uint8_t *pbtRxPar)
Receive bit-frames.
Definition: nfc.c:1071
NFC_ETGRELEASED
#define NFC_ETGRELEASED
Definition: nfc.h:201
nfc_initiator_select_dep_target
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:657
nfc_device::last_error
int last_error
Definition: nfc-internal.h:214
nfc_initiator_transceive_bits_timed
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:889
nfc_initiator_transceive_bytes
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:764
nfc_device_get_information_about
int nfc_device_get_information_about(nfc_device *pnd, char **buf)
Print information about NFC device.
Definition: nfc.c:1247
nfc_dep_mode
nfc_dep_mode
NFC D.E.P. (Data Exchange Protocol) active/passive mode.
Definition: nfc-types.h:151
NFC_EDEVNOTSUPP
#define NFC_EDEVNOTSUPP
Definition: nfc.h:171
NFC_SUCCESS
#define NFC_SUCCESS
Definition: nfc.h:156
nfc_initiator_transceive_bits
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:807
nfc_device::name
char name[DEVICE_NAME_LENGTH]
Definition: nfc-internal.h:197
nfc_modulation
NFC modulation structure.
Definition: nfc-types.h:319
nfc_connstring
char nfc_connstring[NFC_BUFSIZE_CONNSTRING]
Definition: nfc-types.h:62
nfc_device_get_connstring
const char * nfc_device_get_connstring(nfc_device *pnd)
Returns the device connection string.
Definition: nfc.c:1176
nfc_device_get_name
const char * nfc_device_get_name(nfc_device *pnd)
Returns the device name.
Definition: nfc.c:1164
nfc_strerror
const char * nfc_strerror(const nfc_device *pnd)
Return the last error string.
Definition: nfc.c:1103
nfc_open
nfc_device * nfc_open(nfc_context *context, const nfc_connstring connstring)
Open a NFC device.
Definition: nfc.c:238
HAL
#define HAL(FUNCTION,...)
Execute corresponding driver function if exists.
Definition: nfc-internal.h:47
nfc_baud_rate
nfc_baud_rate
NFC baud rate enumeration.
Definition: nfc-types.h:283
nfc_initiator_select_passive_target
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:521
NP_FORCE_SPEED_106
@ NP_FORCE_SPEED_106
Definition: nfc-types.h:141
NP_ACTIVATE_CRYPTO1
@ NP_ACTIVATE_CRYPTO1
Definition: nfc-types.h:108
nfc_perror
void nfc_perror(const nfc_device *pnd, const char *pcString)
Display the last error occured on a nfc_device.
Definition: nfc.c:1138
NP_FORCE_ISO14443_A
@ NP_FORCE_ISO14443_A
Definition: nfc-types.h:137
nfc_list_devices
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
NFC_EIO
#define NFC_EIO
Definition: nfc.h:161
NP_AUTO_ISO14443_4
@ NP_AUTO_ISO14443_4
Definition: nfc-types.h:133
NFC_EOVFLOW
#define NFC_EOVFLOW
Definition: nfc.h:181
nfc_initiator_init_secure_element
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_ETIMEOUT
#define NFC_ETIMEOUT
Definition: nfc.h:186
nfc_target_send_bits
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:1049
NFC_EOPABORTED
#define NFC_EOPABORTED
Definition: nfc.h:191
nfc_initiator_transceive_bytes_timed
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:843
nfc_device::bInfiniteSelect
bool bInfiniteSelect
Definition: nfc-internal.h:207
nfc_abort_command
int nfc_abort_command(nfc_device *pnd)
Abort current running command.
Definition: nfc.c:991
nfc_modulation_type
nfc_modulation_type
NFC modulation type enumeration.
Definition: nfc-types.h:295
nfc_device::connstring
nfc_connstring connstring
Definition: nfc-internal.h:199
nfc_device_set_property_int
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:407
nfc_close
void nfc_close(nfc_device *pnd)
Close from a NFC device.
Definition: nfc.c:300
nfc-internal.h
Internal defines and macros.
NFC_EMFCAUTHFAIL
#define NFC_EMFCAUTHFAIL
Definition: nfc.h:211
nfc_device_get_last_error
int nfc_device_get_last_error(const nfc_device *pnd)
Returns last error occured on a nfc_device.
Definition: nfc.c:1150
nfc_mode
nfc_mode
NFC mode type enumeration.
Definition: nfc-types.h:310
NP_EASY_FRAMING
@ NP_EASY_FRAMING
Definition: nfc-types.h:135
str_nfc_target
int str_nfc_target(char **buf, const nfc_target *pnt, bool verbose)
Convert nfc_modulation_type value to string.
Definition: nfc.c:1328
NFC_ENOTIMPL
#define NFC_ENOTIMPL
Definition: nfc.h:196
nfc_idle
int nfc_idle(nfc_device *pnd)
Turn NFC device in idle mode.
Definition: nfc.c:974
nfc_target_receive_bytes
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:1032
NFC_ERFTRANS
#define NFC_ERFTRANS
Definition: nfc.h:206
nfc_target_send_bytes
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:1012
nfc_property
nfc_property
Definition: nfc-types.h:67
NFC_ESOFT
#define NFC_ESOFT
Definition: nfc.h:216
NFC_ENOTSUCHDEV
#define NFC_ENOTSUCHDEV
Definition: nfc.h:176
NP_HANDLE_PARITY
@ NP_HANDLE_PARITY
Definition: nfc-types.h:101
nfc_device_set_property_bool
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:426
str_nfc_modulation_type
const char * str_nfc_modulation_type(const nfc_modulation_type nmt)
Convert nfc_modulation_type value to string.
Definition: nfc.c:1287
nfc_initiator_target_is_present
int nfc_initiator_target_is_present(nfc_device *pnd, const nfc_target *pnt)
Check target presence.
Definition: nfc.c:862
nfc.h
libnfc interface
nfc_device_get_supported_baud_rate
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:1204
NP_ACTIVATE_FIELD
@ NP_ACTIVATE_FIELD
Definition: nfc-types.h:104
NP_ACCEPT_MULTIPLE_FRAMES
@ NP_ACCEPT_MULTIPLE_FRAMES
Definition: nfc-types.h:125
nfc_free
void nfc_free(void *p)
Free buffer allocated by libnfc.
Definition: nfc.c:1233