75 #endif // HAVE_CONFIG_H
86 #include "target-subr.h"
89 #if defined (DRIVER_ACR122_PCSC_ENABLED)
90 # include "drivers/acr122_pcsc.h"
93 #if defined (DRIVER_ACR122_USB_ENABLED)
94 # include "drivers/acr122_usb.h"
97 #if defined (DRIVER_ACR122S_ENABLED)
98 # include "drivers/acr122s.h"
101 #if defined (DRIVER_PN53X_USB_ENABLED)
102 # include "drivers/pn53x_usb.h"
105 #if defined (DRIVER_ARYGON_ENABLED)
106 # include "drivers/arygon.h"
109 #if defined (DRIVER_PN532_UART_ENABLED)
110 # include "drivers/pn532_uart.h"
113 #if defined (DRIVER_PN532_SPI_ENABLED)
114 # include "drivers/pn532_spi.h"
117 #if defined (DRIVER_PN532_I2C_ENABLED)
118 # include "drivers/pn532_i2c.h"
122 #define LOG_CATEGORY "libnfc.general"
123 #define LOG_GROUP NFC_LOG_GROUP_GENERAL
125 struct nfc_driver_list {
126 const struct nfc_driver_list *next;
130 const struct nfc_driver_list *nfc_drivers = NULL;
133 nfc_drivers_init(
void)
135 #if defined (DRIVER_PN53X_USB_ENABLED)
138 #if defined (DRIVER_ACR122_PCSC_ENABLED)
141 #if defined (DRIVER_ACR122_USB_ENABLED)
144 #if defined (DRIVER_ACR122S_ENABLED)
147 #if defined (DRIVER_PN532_UART_ENABLED)
150 #if defined (DRIVER_PN532_SPI_ENABLED)
153 #if defined (DRIVER_PN532_I2C_ENABLED)
156 #if defined (DRIVER_ARYGON_ENABLED)
175 struct nfc_driver_list *pndl = (
struct nfc_driver_list *)malloc(
sizeof(
struct nfc_driver_list));
180 pndl->next = nfc_drivers;
194 *context = nfc_context_new();
211 while (nfc_drivers) {
212 struct nfc_driver_list *pndl = (
struct nfc_driver_list *) nfc_drivers;
213 nfc_drivers = pndl->next;
217 nfc_context_free(context);
243 if (connstring == NULL) {
253 const struct nfc_driver_list *pndl = nfc_drivers;
258 if (0 != strncmp(ndr->name, ncs, strlen(ndr->name))) {
260 if ((0 != strncmp(
"usb", ncs, strlen(
"usb"))) || 0 != strncmp(
"_usb", ndr->name + (strlen(ndr->name) - 4), 4)) {
266 pnd = ndr->open(context, ncs);
269 if (0 == strncmp(
"usb", ncs, strlen(
"usb"))) {
274 log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG,
"Unable to open \"%s\".", ncs);
277 for (uint32_t i = 0; i > context->user_defined_device_count; i++) {
278 if (strcmp(ncs, context->user_defined_devices[i].connstring) == 0) {
280 strcpy(pnd->
name, context->user_defined_devices[i].name);
284 log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG,
"\"%s\" (%s) has been claimed.", pnd->
name, pnd->
connstring);
289 log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG,
"No driver available to handle \"%s\".", ncs);
304 pnd->driver->close(pnd);
319 size_t device_found = 0;
324 for (uint32_t i = 0; i < context->user_defined_device_count; i++) {
325 if (context->user_defined_devices[i].optional) {
330 char *env_log_level = getenv(
"LIBNFC_LOG_LEVEL");
331 char *old_env_log_level = NULL;
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()");
338 strcpy(old_env_log_level, env_log_level);
340 setenv(
"LIBNFC_LOG_LEVEL",
"0", 1);
343 pnd =
nfc_open(context, context->user_defined_devices[i].connstring);
346 if (old_env_log_level) {
347 setenv(
"LIBNFC_LOG_LEVEL", old_env_log_level, 1);
348 free(old_env_log_level);
350 unsetenv(
"LIBNFC_LOG_LEVEL");
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);
359 if (device_found == connstrings_len)
364 strcpy((
char *)(connstrings + device_found), context->user_defined_devices[i].connstring);
366 if (device_found >= connstrings_len)
373 if (context->allow_autoscan) {
374 const struct nfc_driver_list *pndl = nfc_drivers;
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)
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");
409 HAL(device_set_property_int, pnd, property, value);
428 HAL(device_set_property_bool, pnd, property, bEnable);
479 HAL(initiator_init, pnd);
494 HAL(initiator_init_secure_element, pnd);
523 const uint8_t *pbtInitData,
const size_t szInitData,
526 uint8_t *abtInit = NULL;
527 uint8_t abtTmpInit[MAX(12, szInitData)];
529 if (szInitData == 0) {
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);
536 abtInit = abtTmpInit;
537 memcpy(abtInit, pbtInitData, szInitData);
541 HAL(initiator_select_passive_target, pnd, nm, abtInit, szInit, pnt);
566 size_t szTargetFound = 0;
567 uint8_t *pbtInitData = NULL;
568 size_t szInitDataLen = 0;
579 prepare_initiator_data(nm, &pbtInitData, &szInitDataLen);
585 for (i = 0; i < szTargetFound; i++) {
586 if (memcmp(&(ant[i]), &nt,
sizeof(
nfc_target)) == 0) {
593 memcpy(&(ant[szTargetFound]), &nt,
sizeof(
nfc_target));
595 if (szTargets == szTargetFound) {
598 nfc_initiator_deselect_target(pnd);
601 if ((nm.nmt == NMT_FELICA) || (nm.nmt == NMT_JEWEL) || (nm.nmt == NMT_ISO14443BI) || (nm.nmt == NMT_ISO14443B2SR) || (nm.nmt == NMT_ISO14443B2CT)) {
605 if (bInfiniteSelect) {
610 return szTargetFound;
629 const uint8_t uiPollNr,
const uint8_t uiPeriod,
632 HAL(initiator_poll_target, pnd, pnmModulations, szModulations, uiPollNr, uiPeriod, pnt);
661 HAL(initiator_select_dep_target, pnd, ndm, nbr, pndiInitiator, pnt, timeout);
688 const int period = 300;
689 int remaining_time = timeout;
695 while (remaining_time > 0) {
706 remaining_time -= period;
709 if (! bInfiniteSelect) {
730 nfc_initiator_deselect_target(
nfc_device *pnd)
732 HAL(initiator_deselect_target, pnd);
765 const size_t szRx,
int timeout)
767 HAL(initiator_transceive_bytes, pnd, pbtTx, szTx, pbtRx, szRx, timeout)
808 const uint8_t *pbtTx,
const size_t szTxBits,
const uint8_t *pbtTxPar,
809 uint8_t *pbtRx,
const size_t szRx,
813 HAL(initiator_transceive_bits, pnd, pbtTx, szTxBits, pbtTxPar, pbtRx, pbtRxPar);
844 const uint8_t *pbtTx,
const size_t szTx,
845 uint8_t *pbtRx,
const size_t szRx,
848 HAL(initiator_transceive_bytes_timed, pnd, pbtTx, szTx, pbtRx, szRx, cycles);
864 HAL(initiator_target_is_present, pnd, pnt);
890 const uint8_t *pbtTx,
const size_t szTxBits,
const uint8_t *pbtTxPar,
891 uint8_t *pbtRx,
const size_t szRx,
896 HAL(initiator_transceive_bits_timed, pnd, pbtTx, szTxBits, pbtTxPar, pbtRx, pbtRxPar, cycles);
960 HAL(target_init, pnd, pnt, pbtRx, szRx, timeout);
993 HAL(abort_command, pnd);
1014 HAL(target_send_bytes, pnd, pbtTx, szTx, timeout);
1034 HAL(target_receive_bytes, pnd, pbtRx, szRx, timeout);
1051 HAL(target_send_bits, pnd, pbtTx, szTxBits, pbtTxPar);
1073 HAL(target_receive_bits, pnd, pbtRx, szRx, pbtRxPar);
1076 static struct sErrorMessage {
1078 const char *pcErrorMsg;
1079 } sErrorMessages[] = {
1082 {
NFC_EIO,
"Input / Output Error" },
1093 {
NFC_ECHIP,
"Device's Internal Chip Error" },
1105 const char *pcRes =
"Unknown error";
1107 for (i = 0; i < (
sizeof(sErrorMessages) /
sizeof(
struct sErrorMessage)); i++) {
1108 if (sErrorMessages[i].iErrorCode == pnd->
last_error) {
1109 pcRes = sErrorMessages[i].pcErrorMsg;
1128 return (snprintf(pcStrErrBuf, szBufLen,
"%s",
nfc_strerror(pnd)) < 0) ? -1 : 0;
1140 fprintf(stderr,
"%s: %s\n", pcString,
nfc_strerror(pnd));
1192 HAL(get_supported_modulation, pnd, mode, supported_mt);
1206 HAL(get_supported_baud_rate, pnd, nmt, supported_br);
1221 return GIT_REVISION;
1223 return PACKAGE_VERSION;
1224 #endif // GIT_REVISION
1249 HAL(device_get_information_about, pnd, buf);
1262 return "undefined baud rate";
1291 return "ISO/IEC 14443A";
1294 return "ISO/IEC 14443-4B";
1296 case NMT_ISO14443BI:
1297 return "ISO/IEC 14443-4B'";
1299 case NMT_ISO14443B2CT:
1300 return "ISO/IEC 14443-2B ASK CTx";
1302 case NMT_ISO14443B2SR:
1303 return "ISO/IEC 14443-2B ST SRx";
1309 return "Innovision Jewel";
1330 *buf = malloc(4096);
1334 snprint_nfc_target(*buf, 4096, pnt, verbose);
1335 return strlen(*buf);