74 #endif // HAVE_CONFIG_H
85 #include "target-subr.h"
88 #if defined (DRIVER_ACR122_PCSC_ENABLED)
89 # include "drivers/acr122_pcsc.h"
92 #if defined (DRIVER_ACR122_USB_ENABLED)
93 # include "drivers/acr122_usb.h"
96 #if defined (DRIVER_ACR122S_ENABLED)
97 # include "drivers/acr122s.h"
100 #if defined (DRIVER_PN53X_USB_ENABLED)
101 # include "drivers/pn53x_usb.h"
104 #if defined (DRIVER_ARYGON_ENABLED)
105 # include "drivers/arygon.h"
108 #if defined (DRIVER_PN532_UART_ENABLED)
109 # include "drivers/pn532_uart.h"
112 #if defined (DRIVER_PN532_SPI_ENABLED)
113 # include "drivers/pn532_spi.h"
117 #define LOG_CATEGORY "libnfc.general"
118 #define LOG_GROUP NFC_LOG_GROUP_GENERAL
120 struct nfc_driver_list {
121 const struct nfc_driver_list *next;
125 const struct nfc_driver_list *nfc_drivers = NULL;
128 nfc_drivers_init(
void)
130 #if defined (DRIVER_PN53X_USB_ENABLED)
133 #if defined (DRIVER_ACR122_PCSC_ENABLED)
136 #if defined (DRIVER_ACR122_USB_ENABLED)
139 #if defined (DRIVER_ACR122S_ENABLED)
142 #if defined (DRIVER_PN532_UART_ENABLED)
145 #if defined (DRIVER_PN532_SPI_ENABLED)
148 #if defined (DRIVER_ARYGON_ENABLED)
166 struct nfc_driver_list *pndl = (
struct nfc_driver_list *)malloc(
sizeof(
struct nfc_driver_list));
171 pndl->next = nfc_drivers;
185 *context = nfc_context_new();
202 while (nfc_drivers) {
203 struct nfc_driver_list *pndl = (
struct nfc_driver_list *) nfc_drivers;
204 nfc_drivers = pndl->next;
208 nfc_context_free(context);
234 if (connstring == NULL) {
244 const struct nfc_driver_list *pndl = nfc_drivers;
249 if (0 != strncmp(ndr->name, ncs, strlen(ndr->name))) {
251 if ((0 != strncmp(
"usb", ncs, strlen(
"usb"))) || 0 != strncmp(
"_usb", ndr->name + (strlen(ndr->name) - 4), 4)) {
257 pnd = ndr->open(context, ncs);
260 if (0 == strncmp(
"usb", ncs, strlen(
"usb"))) {
265 log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG,
"Unable to open \"%s\".", ncs);
268 for (uint32_t i = 0; i > context->user_defined_device_count; i++) {
269 if (strcmp(ncs, context->user_defined_devices[i].connstring) == 0) {
271 strcpy(pnd->
name, context->user_defined_devices[i].name);
275 log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG,
"\"%s\" (%s) has been claimed.", pnd->
name, pnd->
connstring);
280 log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG,
"No driver available to handle \"%s\".", ncs);
295 pnd->driver->close(pnd);
310 size_t device_found = 0;
315 for (uint32_t i = 0; i < context->user_defined_device_count; i++) {
316 if (context->user_defined_devices[i].optional) {
321 char *env_log_level = getenv(
"LIBNFC_LOG_LEVEL");
322 char *old_env_log_level = NULL;
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()");
329 strcpy(old_env_log_level, env_log_level);
331 setenv(
"LIBNFC_LOG_LEVEL",
"0", 1);
334 pnd =
nfc_open(context, context->user_defined_devices[i].connstring);
337 if (old_env_log_level) {
338 setenv(
"LIBNFC_LOG_LEVEL", old_env_log_level, 1);
339 free(old_env_log_level);
341 unsetenv(
"LIBNFC_LOG_LEVEL");
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);
350 if (device_found == connstrings_len)
355 strcpy((
char *)(connstrings + device_found), context->user_defined_devices[i].connstring);
357 if (device_found >= connstrings_len)
364 if (context->allow_autoscan) {
365 const struct nfc_driver_list *pndl = nfc_drivers;
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)
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");
400 HAL(device_set_property_int, pnd, property, value);
419 HAL(device_set_property_bool, pnd, property, bEnable);
470 HAL(initiator_init, pnd);
485 HAL(initiator_init_secure_element, pnd);
511 const uint8_t *pbtInitData,
const size_t szInitData,
514 uint8_t abtInit[MAX(12, szInitData)];
519 iso14443_cascade_uid(pbtInitData, szInitData, abtInit, &szInit);
525 case NMT_ISO14443B2SR:
526 case NMT_ISO14443B2CT:
529 memcpy(abtInit, pbtInitData, szInitData);
534 HAL(initiator_select_passive_target, pnd, nm, abtInit, szInit, pnt);
559 size_t szTargetFound = 0;
560 uint8_t *pbtInitData = NULL;
561 size_t szInitDataLen = 0;
571 prepare_initiator_data(nm, &pbtInitData, &szInitDataLen);
577 for (i = 0; i < szTargetFound; i++) {
578 if (memcmp(&(ant[i]), &nt,
sizeof(
nfc_target)) == 0) {
585 memcpy(&(ant[szTargetFound]), &nt,
sizeof(
nfc_target));
587 if (szTargets == szTargetFound) {
590 nfc_initiator_deselect_target(pnd);
593 if ((nm.nmt == NMT_FELICA) || (nm.nmt == NMT_JEWEL) || (nm.nmt == NMT_ISO14443BI) || (nm.nmt == NMT_ISO14443B2SR) || (nm.nmt == NMT_ISO14443B2CT)) {
597 return szTargetFound;
616 const uint8_t uiPollNr,
const uint8_t uiPeriod,
619 HAL(initiator_poll_target, pnd, pnmModulations, szModulations, uiPollNr, uiPeriod, pnt);
648 HAL(initiator_select_dep_target, pnd, ndm, nbr, pndiInitiator, pnt, timeout);
675 const int period = 300;
676 int remaining_time = timeout;
680 while (remaining_time > 0) {
687 remaining_time -= period;
705 nfc_initiator_deselect_target(
nfc_device *pnd)
707 HAL(initiator_deselect_target, pnd);
740 const size_t szRx,
int timeout)
742 HAL(initiator_transceive_bytes, pnd, pbtTx, szTx, pbtRx, szRx, timeout)
783 const uint8_t *pbtTx,
const size_t szTxBits,
const uint8_t *pbtTxPar,
784 uint8_t *pbtRx,
const size_t szRx,
788 HAL(initiator_transceive_bits, pnd, pbtTx, szTxBits, pbtTxPar, pbtRx, pbtRxPar);
819 const uint8_t *pbtTx,
const size_t szTx,
820 uint8_t *pbtRx,
const size_t szRx,
823 HAL(initiator_transceive_bytes_timed, pnd, pbtTx, szTx, pbtRx, szRx, cycles);
837 HAL(initiator_target_is_present, pnd, pnt);
863 const uint8_t *pbtTx,
const size_t szTxBits,
const uint8_t *pbtTxPar,
864 uint8_t *pbtRx,
const size_t szRx,
869 HAL(initiator_transceive_bits_timed, pnd, pbtTx, szTxBits, pbtTxPar, pbtRx, pbtRxPar, cycles);
933 HAL(target_init, pnd, pnt, pbtRx, szRx, timeout);
966 HAL(abort_command, pnd);
987 HAL(target_send_bytes, pnd, pbtTx, szTx, timeout);
1007 HAL(target_receive_bytes, pnd, pbtRx, szRx, timeout);
1024 HAL(target_send_bits, pnd, pbtTx, szTxBits, pbtTxPar);
1046 HAL(target_receive_bits, pnd, pbtRx, szRx, pbtRxPar);
1049 static struct sErrorMessage {
1051 const char *pcErrorMsg;
1052 } sErrorMessages[] = {
1055 {
NFC_EIO,
"Input / Output Error" },
1066 {
NFC_ECHIP,
"Device's Internal Chip Error" },
1078 const char *pcRes =
"Unknown error";
1080 for (i = 0; i < (
sizeof(sErrorMessages) /
sizeof(
struct sErrorMessage)); i++) {
1081 if (sErrorMessages[i].iErrorCode == pnd->
last_error) {
1082 pcRes = sErrorMessages[i].pcErrorMsg;
1101 return (snprintf(pcStrErrBuf, szBufLen,
"%s",
nfc_strerror(pnd)) < 0) ? -1 : 0;
1113 fprintf(stderr,
"%s: %s\n", pcString,
nfc_strerror(pnd));
1165 HAL(get_supported_modulation, pnd, mode, supported_mt);
1179 HAL(get_supported_baud_rate, pnd, nmt, supported_br);
1194 return GIT_REVISION;
1196 return PACKAGE_VERSION;
1197 #endif // GIT_REVISION
1222 HAL(device_get_information_about, pnd, buf);
1235 return "undefined baud rate";
1264 return "ISO/IEC 14443A";
1267 return "ISO/IEC 14443-4B";
1269 case NMT_ISO14443BI:
1270 return "ISO/IEC 14443-4B'";
1272 case NMT_ISO14443B2CT:
1273 return "ISO/IEC 14443-2B ASK CTx";
1275 case NMT_ISO14443B2SR:
1276 return "ISO/IEC 14443-2B ST SRx";
1282 return "Innovision Jewel";
1303 *buf = malloc(4096);
1307 snprint_nfc_target(*buf, 4096, pnt, verbose);
1308 return strlen(*buf);