libnfc  1.7.0-rc7
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  * Additional contributors of this file:
11  *
12  * This program is free software: you can redistribute it and/or modify it
13  * under the terms of the GNU Lesser General Public License as published by the
14  * Free Software Foundation, either version 3 of the License, or (at your
15  * option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful, but WITHOUT
18  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
19  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
20  * more details.
21  *
22  * You should have received a copy of the GNU Lesser General Public License
23  * along with this program. If not, see <http://www.gnu.org/licenses/>
24  */
25 
72 #ifdef HAVE_CONFIG_H
73 # include "config.h"
74 #endif // HAVE_CONFIG_H
75 
76 #include <fcntl.h>
77 #include <stdio.h>
78 #include <stdlib.h>
79 #include <stddef.h>
80 #include <string.h>
81 
82 #include <nfc/nfc.h>
83 
84 #include "nfc-internal.h"
85 #include "target-subr.h"
86 #include "drivers.h"
87 
88 #if defined (DRIVER_ACR122_PCSC_ENABLED)
89 # include "drivers/acr122_pcsc.h"
90 #endif /* DRIVER_ACR122_PCSC_ENABLED */
91 
92 #if defined (DRIVER_ACR122_USB_ENABLED)
93 # include "drivers/acr122_usb.h"
94 #endif /* DRIVER_ACR122_USB_ENABLED */
95 
96 #if defined (DRIVER_ACR122S_ENABLED)
97 # include "drivers/acr122s.h"
98 #endif /* DRIVER_ACR122S_ENABLED */
99 
100 #if defined (DRIVER_PN53X_USB_ENABLED)
101 # include "drivers/pn53x_usb.h"
102 #endif /* DRIVER_PN53X_USB_ENABLED */
103 
104 #if defined (DRIVER_ARYGON_ENABLED)
105 # include "drivers/arygon.h"
106 #endif /* DRIVER_ARYGON_ENABLED */
107 
108 #if defined (DRIVER_PN532_UART_ENABLED)
109 # include "drivers/pn532_uart.h"
110 #endif /* DRIVER_PN532_UART_ENABLED */
111 
112 #if defined (DRIVER_PN532_SPI_ENABLED)
113 # include "drivers/pn532_spi.h"
114 #endif /* DRIVER_PN532_SPI_ENABLED */
115 
116 
117 #define LOG_CATEGORY "libnfc.general"
118 #define LOG_GROUP NFC_LOG_GROUP_GENERAL
119 
120 struct nfc_driver_list {
121  const struct nfc_driver_list *next;
122  const struct nfc_driver *driver;
123 };
124 
125 const struct nfc_driver_list *nfc_drivers = NULL;
126 
127 static void
128 nfc_drivers_init(void)
129 {
130 #if defined (DRIVER_PN53X_USB_ENABLED)
131  nfc_register_driver(&pn53x_usb_driver);
132 #endif /* DRIVER_PN53X_USB_ENABLED */
133 #if defined (DRIVER_ACR122_PCSC_ENABLED)
134  nfc_register_driver(&acr122_pcsc_driver);
135 #endif /* DRIVER_ACR122_PCSC_ENABLED */
136 #if defined (DRIVER_ACR122_USB_ENABLED)
137  nfc_register_driver(&acr122_usb_driver);
138 #endif /* DRIVER_ACR122_USB_ENABLED */
139 #if defined (DRIVER_ACR122S_ENABLED)
140  nfc_register_driver(&acr122s_driver);
141 #endif /* DRIVER_ACR122S_ENABLED */
142 #if defined (DRIVER_PN532_UART_ENABLED)
143  nfc_register_driver(&pn532_uart_driver);
144 #endif /* DRIVER_PN532_UART_ENABLED */
145 #if defined (DRIVER_PN532_SPI_ENABLED)
146  nfc_register_driver(&pn532_spi_driver);
147 #endif /* DRIVER_PN532_SPI_ENABLED */
148 #if defined (DRIVER_ARYGON_ENABLED)
149  nfc_register_driver(&arygon_driver);
150 #endif /* DRIVER_ARYGON_ENABLED */
151 }
152 
160 int
161 nfc_register_driver(const struct nfc_driver *ndr)
162 {
163  if (!ndr)
164  return NFC_EINVARG;
165 
166  struct nfc_driver_list *pndl = (struct nfc_driver_list *)malloc(sizeof(struct nfc_driver_list));
167  if (!pndl)
168  return NFC_ESOFT;
169 
170  pndl->driver = ndr;
171  pndl->next = nfc_drivers;
172  nfc_drivers = pndl;
173 
174  return NFC_SUCCESS;
175 }
176 
182 void
184 {
185  *context = nfc_context_new();
186  if (!*context) {
187  perror("malloc");
188  return;
189  }
190  if (!nfc_drivers)
191  nfc_drivers_init();
192 }
193 
199 void
201 {
202  while (nfc_drivers) {
203  struct nfc_driver_list *pndl = (struct nfc_driver_list *) nfc_drivers;
204  nfc_drivers = pndl->next;
205  free(pndl);
206  }
207 
208  nfc_context_free(context);
209 }
210 
228 nfc_device *
229 nfc_open(nfc_context *context, const nfc_connstring connstring)
230 {
231  nfc_device *pnd = NULL;
232 
233  nfc_connstring ncs;
234  if (connstring == NULL) {
235  if (!nfc_list_devices(context, &ncs, 1)) {
236  return NULL;
237  }
238  } else {
239  strncpy(ncs, connstring, sizeof(nfc_connstring));
240  ncs[sizeof(nfc_connstring) - 1] = '\0';
241  }
242 
243  // Search through the device list for an available device
244  const struct nfc_driver_list *pndl = nfc_drivers;
245  while (pndl) {
246  const struct nfc_driver *ndr = pndl->driver;
247 
248  // Specific device is requested: using device description
249  if (0 != strncmp(ndr->name, ncs, strlen(ndr->name))) {
250  // Check if connstring driver is usb -> accept any driver *_usb
251  if ((0 != strncmp("usb", ncs, strlen("usb"))) || 0 != strncmp("_usb", ndr->name + (strlen(ndr->name) - 4), 4)) {
252  pndl = pndl->next;
253  continue;
254  }
255  }
256 
257  pnd = ndr->open(context, ncs);
258  // Test if the opening was successful
259  if (pnd == NULL) {
260  if (0 == strncmp("usb", ncs, strlen("usb"))) {
261  // We've to test the other usb drivers before giving up
262  pndl = pndl->next;
263  continue;
264  }
265  log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG, "Unable to open \"%s\".", ncs);
266  return NULL;
267  }
268  for (uint32_t i = 0; i > context->user_defined_device_count; i++) {
269  if (strcmp(ncs, context->user_defined_devices[i].connstring) == 0) {
270  // This is a device sets by user, we use the device name given by user
271  strcpy(pnd->name, context->user_defined_devices[i].name);
272  break;
273  }
274  }
275  log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG, "\"%s\" (%s) has been claimed.", pnd->name, pnd->connstring);
276  return pnd;
277  }
278 
279  // Too bad, no driver can decode connstring
280  log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG, "No driver available to handle \"%s\".", ncs);
281  return NULL;
282 }
283 
290 void
292 {
293  if (pnd) {
294  // Close, clean up and release the device
295  pnd->driver->close(pnd);
296  }
297 }
298 
307 size_t
308 nfc_list_devices(nfc_context *context, nfc_connstring connstrings[], const size_t connstrings_len)
309 {
310  size_t device_found = 0;
311 
312 #ifdef CONFFILES
313  // Load manually configured devices (from config file and env variables)
314  // TODO From env var...
315  for (uint32_t i = 0; i < context->user_defined_device_count; i++) {
316  if (context->user_defined_devices[i].optional) {
317  // let's make sure the device exists
318  nfc_device *pnd = NULL;
319 
320 #ifdef ENVVARS
321  char *env_log_level = getenv("LIBNFC_LOG_LEVEL");
322  char *old_env_log_level = NULL;
323  // do it silently
324  if (env_log_level) {
325  if ((old_env_log_level = malloc(strlen(env_log_level) + 1)) == NULL) {
326  log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "%s", "Unable to malloc()");
327  return 0;
328  }
329  strcpy(old_env_log_level, env_log_level);
330  }
331  setenv("LIBNFC_LOG_LEVEL", "0", 1);
332 #endif // ENVVARS
333 
334  pnd = nfc_open(context, context->user_defined_devices[i].connstring);
335 
336 #ifdef ENVVARS
337  if (old_env_log_level) {
338  setenv("LIBNFC_LOG_LEVEL", old_env_log_level, 1);
339  free(old_env_log_level);
340  } else {
341  unsetenv("LIBNFC_LOG_LEVEL");
342  }
343 #endif // ENVVARS
344 
345  if (pnd) {
346  nfc_close(pnd);
347  log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG, "User device %s found", context->user_defined_devices[i].name);
348  strcpy((char *)(connstrings + device_found), context->user_defined_devices[i].connstring);
349  device_found ++;
350  if (device_found == connstrings_len)
351  break;
352  }
353  } else {
354  // manual choice is not marked as optional so let's take it blindly
355  strcpy((char *)(connstrings + device_found), context->user_defined_devices[i].connstring);
356  device_found++;
357  if (device_found >= connstrings_len)
358  return device_found;
359  }
360  }
361 #endif // CONFFILES
362 
363  // Device auto-detection
364  if (context->allow_autoscan) {
365  const struct nfc_driver_list *pndl = nfc_drivers;
366  while (pndl) {
367  const struct nfc_driver *ndr = pndl->driver;
368  if ((ndr->scan_type == NOT_INTRUSIVE) || ((context->allow_intrusive_scan) && (ndr->scan_type == INTRUSIVE))) {
369  size_t _device_found = ndr->scan(context, connstrings + (device_found), connstrings_len - (device_found));
370  log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG, "%ld device(s) found using %s driver", (unsigned long) _device_found, ndr->name);
371  if (_device_found > 0) {
372  device_found += _device_found;
373  if (device_found == connstrings_len)
374  break;
375  }
376  } // scan_type is INTRUSIVE but not allowed or NOT_AVAILABLE
377  pndl = pndl->next;
378  }
379  } else if (context->user_defined_device_count == 0) {
380  log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_INFO, "Warning: %s" , "user must specify device(s) manually when autoscan is disabled");
381  }
382 
383  return device_found;
384 }
385 
397 int
398 nfc_device_set_property_int(nfc_device *pnd, const nfc_property property, const int value)
399 {
400  HAL(device_set_property_int, pnd, property, value);
401 }
402 
403 
416 int
417 nfc_device_set_property_bool(nfc_device *pnd, const nfc_property property, const bool bEnable)
418 {
419  HAL(device_set_property_bool, pnd, property, bEnable);
420 }
421 
442 int
444 {
445  int res = 0;
446  // Drop the field for a while
447  if ((res = nfc_device_set_property_bool(pnd, NP_ACTIVATE_FIELD, false)) < 0)
448  return res;
449  // Enable field so more power consuming cards can power themselves up
450  if ((res = nfc_device_set_property_bool(pnd, NP_ACTIVATE_FIELD, true)) < 0)
451  return res;
452  // Let the device try forever to find a target/tag
453  if ((res = nfc_device_set_property_bool(pnd, NP_INFINITE_SELECT, true)) < 0)
454  return res;
455  // Activate auto ISO14443-4 switching by default
456  if ((res = nfc_device_set_property_bool(pnd, NP_AUTO_ISO14443_4, true)) < 0)
457  return res;
458  // Force 14443-A mode
459  if ((res = nfc_device_set_property_bool(pnd, NP_FORCE_ISO14443_A, true)) < 0)
460  return res;
461  // Force speed at 106kbps
462  if ((res = nfc_device_set_property_bool(pnd, NP_FORCE_SPEED_106, true)) < 0)
463  return res;
464  // Disallow invalid frame
465  if ((res = nfc_device_set_property_bool(pnd, NP_ACCEPT_INVALID_FRAMES, false)) < 0)
466  return res;
467  // Disallow multiple frames
468  if ((res = nfc_device_set_property_bool(pnd, NP_ACCEPT_MULTIPLE_FRAMES, false)) < 0)
469  return res;
470  HAL(initiator_init, pnd);
471 }
472 
482 int
484 {
485  HAL(initiator_init_secure_element, pnd);
486 }
487 
508 int
510  const nfc_modulation nm,
511  const uint8_t *pbtInitData, const size_t szInitData,
512  nfc_target *pnt)
513 {
514  uint8_t abtInit[MAX(12, szInitData)];
515  size_t szInit;
516 
517  switch (nm.nmt) {
518  case NMT_ISO14443A:
519  iso14443_cascade_uid(pbtInitData, szInitData, abtInit, &szInit);
520  break;
521 
522  case NMT_JEWEL:
523  case NMT_ISO14443B:
524  case NMT_ISO14443BI:
525  case NMT_ISO14443B2SR:
526  case NMT_ISO14443B2CT:
527  case NMT_FELICA:
528  case NMT_DEP:
529  memcpy(abtInit, pbtInitData, szInitData);
530  szInit = szInitData;
531  break;
532  }
533 
534  HAL(initiator_select_passive_target, pnd, nm, abtInit, szInit, pnt);
535 }
536 
553 int
555  const nfc_modulation nm,
556  nfc_target ant[], const size_t szTargets)
557 {
558  nfc_target nt;
559  size_t szTargetFound = 0;
560  uint8_t *pbtInitData = NULL;
561  size_t szInitDataLen = 0;
562  int res = 0;
563 
564  pnd->last_error = 0;
565 
566  // Let the reader only try once to find a tag
567  if ((res = nfc_device_set_property_bool(pnd, NP_INFINITE_SELECT, false)) < 0) {
568  return res;
569  }
570 
571  prepare_initiator_data(nm, &pbtInitData, &szInitDataLen);
572 
573  while (nfc_initiator_select_passive_target(pnd, nm, pbtInitData, szInitDataLen, &nt) > 0) {
574  size_t i;
575  bool seen = false;
576  // Check if we've already seen this tag
577  for (i = 0; i < szTargetFound; i++) {
578  if (memcmp(&(ant[i]), &nt, sizeof(nfc_target)) == 0) {
579  seen = true;
580  }
581  }
582  if (seen) {
583  break;
584  }
585  memcpy(&(ant[szTargetFound]), &nt, sizeof(nfc_target));
586  szTargetFound++;
587  if (szTargets == szTargetFound) {
588  break;
589  }
590  nfc_initiator_deselect_target(pnd);
591  // deselect has no effect on FeliCa and Jewel cards so we'll stop after one...
592  // ISO/IEC 14443 B' cards are polled at 100% probability so it's not possible to detect correctly two cards at the same time
593  if ((nm.nmt == NMT_FELICA) || (nm.nmt == NMT_JEWEL) || (nm.nmt == NMT_ISO14443BI) || (nm.nmt == NMT_ISO14443B2SR) || (nm.nmt == NMT_ISO14443B2CT)) {
594  break;
595  }
596  }
597  return szTargetFound;
598 }
599 
613 int
615  const nfc_modulation *pnmModulations, const size_t szModulations,
616  const uint8_t uiPollNr, const uint8_t uiPeriod,
617  nfc_target *pnt)
618 {
619  HAL(initiator_poll_target, pnd, pnmModulations, szModulations, uiPollNr, uiPeriod, pnt);
620 }
621 
622 
643 int
645  const nfc_dep_mode ndm, const nfc_baud_rate nbr,
646  const nfc_dep_info *pndiInitiator, nfc_target *pnt, const int timeout)
647 {
648  HAL(initiator_select_dep_target, pnd, ndm, nbr, pndiInitiator, pnt, timeout);
649 }
650 
668 int
670  const nfc_dep_mode ndm, const nfc_baud_rate nbr,
671  const nfc_dep_info *pndiInitiator,
672  nfc_target *pnt,
673  const int timeout)
674 {
675  const int period = 300;
676  int remaining_time = timeout;
677  int res;
678  if ((res = nfc_device_set_property_bool(pnd, NP_INFINITE_SELECT, true)) < 0)
679  return res;
680  while (remaining_time > 0) {
681  if ((res = nfc_initiator_select_dep_target(pnd, ndm, nbr, pndiInitiator, pnt, period)) < 0) {
682  if (res != NFC_ETIMEOUT)
683  return res;
684  }
685  if (res == 1)
686  return res;
687  remaining_time -= period;
688  }
689  return 0;
690 }
691 
704 int
705 nfc_initiator_deselect_target(nfc_device *pnd)
706 {
707  HAL(initiator_deselect_target, pnd);
708 }
709 
738 int
739 nfc_initiator_transceive_bytes(nfc_device *pnd, const uint8_t *pbtTx, const size_t szTx, uint8_t *pbtRx,
740  const size_t szRx, int timeout)
741 {
742  HAL(initiator_transceive_bytes, pnd, pbtTx, szTx, pbtRx, szRx, timeout)
743 }
744 
781 int
783  const uint8_t *pbtTx, const size_t szTxBits, const uint8_t *pbtTxPar,
784  uint8_t *pbtRx, const size_t szRx,
785  uint8_t *pbtRxPar)
786 {
787  (void)szRx;
788  HAL(initiator_transceive_bits, pnd, pbtTx, szTxBits, pbtTxPar, pbtRx, pbtRxPar);
789 }
790 
817 int
819  const uint8_t *pbtTx, const size_t szTx,
820  uint8_t *pbtRx, const size_t szRx,
821  uint32_t *cycles)
822 {
823  HAL(initiator_transceive_bytes_timed, pnd, pbtTx, szTx, pbtRx, szRx, cycles);
824 }
825 
834 int
836 {
837  HAL(initiator_target_is_present, pnd, pnt);
838 }
839 
861 int
863  const uint8_t *pbtTx, const size_t szTxBits, const uint8_t *pbtTxPar,
864  uint8_t *pbtRx, const size_t szRx,
865  uint8_t *pbtRxPar,
866  uint32_t *cycles)
867 {
868  (void)szRx;
869  HAL(initiator_transceive_bits_timed, pnd, pbtTx, szTxBits, pbtTxPar, pbtRx, pbtRxPar, cycles);
870 }
871 
905 int
906 nfc_target_init(nfc_device *pnd, nfc_target *pnt, uint8_t *pbtRx, const size_t szRx, int timeout)
907 {
908  int res = 0;
909  // Disallow invalid frame
910  if ((res = nfc_device_set_property_bool(pnd, NP_ACCEPT_INVALID_FRAMES, false)) < 0)
911  return res;
912  // Disallow multiple frames
913  if ((res = nfc_device_set_property_bool(pnd, NP_ACCEPT_MULTIPLE_FRAMES, false)) < 0)
914  return res;
915  // Make sure we reset the CRC and parity to chip handling.
916  if ((res = nfc_device_set_property_bool(pnd, NP_HANDLE_CRC, true)) < 0)
917  return res;
918  if ((res = nfc_device_set_property_bool(pnd, NP_HANDLE_PARITY, true)) < 0)
919  return res;
920  // Activate auto ISO14443-4 switching by default
921  if ((res = nfc_device_set_property_bool(pnd, NP_AUTO_ISO14443_4, true)) < 0)
922  return res;
923  // Activate "easy framing" feature by default
924  if ((res = nfc_device_set_property_bool(pnd, NP_EASY_FRAMING, true)) < 0)
925  return res;
926  // Deactivate the CRYPTO1 cipher, it may could cause problems when still active
927  if ((res = nfc_device_set_property_bool(pnd, NP_ACTIVATE_CRYPTO1, false)) < 0)
928  return res;
929  // Drop explicitely the field
930  if ((res = nfc_device_set_property_bool(pnd, NP_ACTIVATE_FIELD, false)) < 0)
931  return res;
932 
933  HAL(target_init, pnd, pnt, pbtRx, szRx, timeout);
934 }
935 
946 int
948 {
949  HAL(idle, pnd);
950 }
951 
963 int
965 {
966  HAL(abort_command, pnd);
967 }
968 
984 int
985 nfc_target_send_bytes(nfc_device *pnd, const uint8_t *pbtTx, const size_t szTx, int timeout)
986 {
987  HAL(target_send_bytes, pnd, pbtTx, szTx, timeout);
988 }
989 
1004 int
1005 nfc_target_receive_bytes(nfc_device *pnd, uint8_t *pbtRx, const size_t szRx, int timeout)
1006 {
1007  HAL(target_receive_bytes, pnd, pbtRx, szRx, timeout);
1008 }
1009 
1021 int
1022 nfc_target_send_bits(nfc_device *pnd, const uint8_t *pbtTx, const size_t szTxBits, const uint8_t *pbtTxPar)
1023 {
1024  HAL(target_send_bits, pnd, pbtTx, szTxBits, pbtTxPar);
1025 }
1026 
1043 int
1044 nfc_target_receive_bits(nfc_device *pnd, uint8_t *pbtRx, const size_t szRx, uint8_t *pbtRxPar)
1045 {
1046  HAL(target_receive_bits, pnd, pbtRx, szRx, pbtRxPar);
1047 }
1048 
1049 static struct sErrorMessage {
1050  int iErrorCode;
1051  const char *pcErrorMsg;
1052 } sErrorMessages[] = {
1053  /* Chip-level errors (internal errors, RF errors, etc.) */
1054  { NFC_SUCCESS, "Success" },
1055  { NFC_EIO, "Input / Output Error" },
1056  { NFC_EINVARG, "Invalid argument(s)" },
1057  { NFC_EDEVNOTSUPP, "Not Supported by Device" },
1058  { NFC_ENOTSUCHDEV, "No Such Device" },
1059  { NFC_EOVFLOW, "Buffer Overflow" },
1060  { NFC_ETIMEOUT, "Timeout" },
1061  { NFC_EOPABORTED, "Operation Aborted" },
1062  { NFC_ENOTIMPL, "Not (yet) Implemented" },
1063  { NFC_ETGRELEASED, "Target Released" },
1064  { NFC_EMFCAUTHFAIL, "Mifare Authentication Failed" },
1065  { NFC_ERFTRANS, "RF Transmission Error" },
1066  { NFC_ECHIP, "Device's Internal Chip Error" },
1067 };
1068 
1075 const char *
1077 {
1078  const char *pcRes = "Unknown error";
1079  size_t i;
1080  for (i = 0; i < (sizeof(sErrorMessages) / sizeof(struct sErrorMessage)); i++) {
1081  if (sErrorMessages[i].iErrorCode == pnd->last_error) {
1082  pcRes = sErrorMessages[i].pcErrorMsg;
1083  break;
1084  }
1085  }
1086 
1087  return pcRes;
1088 }
1089 
1098 int
1099 nfc_strerror_r(const nfc_device *pnd, char *pcStrErrBuf, size_t szBufLen)
1100 {
1101  return (snprintf(pcStrErrBuf, szBufLen, "%s", nfc_strerror(pnd)) < 0) ? -1 : 0;
1102 }
1103 
1110 void
1111 nfc_perror(const nfc_device *pnd, const char *pcString)
1112 {
1113  fprintf(stderr, "%s: %s\n", pcString, nfc_strerror(pnd));
1114 }
1115 
1122 int
1124 {
1125  return pnd->last_error;
1126 }
1127 
1128 /* Special data accessors */
1129 
1136 const char *
1138 {
1139  return pnd->name;
1140 }
1141 
1148 const char *
1150 {
1151  return pnd->connstring;
1152 }
1153 
1162 int
1164 {
1165  HAL(get_supported_modulation, pnd, mode, supported_mt);
1166 }
1167 
1176 int
1178 {
1179  HAL(get_supported_baud_rate, pnd, nmt, supported_br);
1180 }
1181 
1182 /* Misc. functions */
1183 
1190 const char *
1192 {
1193 #ifdef GIT_REVISION
1194  return GIT_REVISION;
1195 #else
1196  return PACKAGE_VERSION;
1197 #endif // GIT_REVISION
1198 }
1199 
1205 void
1206 nfc_free(void *p)
1207 {
1208  free(p);
1209 }
1210 
1219 int
1221 {
1222  HAL(device_get_information_about, pnd, buf);
1223 }
1224 
1230 const char *
1232 {
1233  switch (nbr) {
1234  case NBR_UNDEFINED:
1235  return "undefined baud rate";
1236  break;
1237  case NBR_106:
1238  return "106 kbps";
1239  break;
1240  case NBR_212:
1241  return "212 kbps";
1242  break;
1243  case NBR_424:
1244  return "424 kbps";
1245  break;
1246  case NBR_847:
1247  return "847 kbps";
1248  break;
1249  }
1250  // Should never go there..
1251  return "";
1252 }
1253 
1259 const char *
1261 {
1262  switch (nmt) {
1263  case NMT_ISO14443A:
1264  return "ISO/IEC 14443A";
1265  break;
1266  case NMT_ISO14443B:
1267  return "ISO/IEC 14443-4B";
1268  break;
1269  case NMT_ISO14443BI:
1270  return "ISO/IEC 14443-4B'";
1271  break;
1272  case NMT_ISO14443B2CT:
1273  return "ISO/IEC 14443-2B ASK CTx";
1274  break;
1275  case NMT_ISO14443B2SR:
1276  return "ISO/IEC 14443-2B ST SRx";
1277  break;
1278  case NMT_FELICA:
1279  return "FeliCa";
1280  break;
1281  case NMT_JEWEL:
1282  return "Innovision Jewel";
1283  break;
1284  case NMT_DEP:
1285  return "D.E.P.";
1286  break;
1287  }
1288  // Should never go there..
1289  return "";
1290 }
1291 
1300 int
1301 str_nfc_target(char **buf, const nfc_target *pnt, bool verbose)
1302 {
1303  *buf = malloc(4096);
1304  if (! *buf)
1305  return NFC_ESOFT;
1306  (*buf)[0] = '\0';
1307  snprint_nfc_target(*buf, 4096, pnt, verbose);
1308  return strlen(*buf);
1309 }