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);
void nfc_init(nfc_context **context)
Initialize libnfc. This function must be called before calling any other libnfc function.
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.
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.
NFC target information in D.E.P. (Data Exchange Protocol) see ISO/IEC 18092 (NFCIP-1)
Internal defines and macros.
int nfc_initiator_target_is_present(nfc_device *pnd, const nfc_target *pnt)
Check target presence.
int nfc_device_set_property_bool(nfc_device *pnd, const nfc_property property, const bool bEnable)
Set a device's boolean-property value.
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.
const char * nfc_version(void)
Returns the library version.
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.
#define HAL(FUNCTION,...)
Execute corresponding driver function if exists.
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.
int nfc_initiator_init(nfc_device *pnd)
Initialize NFC device as initiator (reader)
nfc_connstring connstring
int nfc_device_get_information_about(nfc_device *pnd, char **buf)
Print information about NFC device.
int nfc_target_receive_bits(nfc_device *pnd, uint8_t *pbtRx, const size_t szRx, uint8_t *pbtRxPar)
Receive bit-frames.
const char * str_nfc_modulation_type(const nfc_modulation_type nmt)
Convert nfc_modulation_type value to string.
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)
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.
int nfc_target_send_bytes(nfc_device *pnd, const uint8_t *pbtTx, const size_t szTx, int timeout)
Send bytes and APDU frames.
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)
nfc_modulation_type
NFC modulation type enumeration.
void nfc_perror(const nfc_device *pnd, const char *pcString)
Display the last error occured on a nfc_device.
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)
void nfc_exit(nfc_context *context)
Deinitialize libnfc. Should be called after closing all open devices and before your application term...
nfc_device * nfc_open(nfc_context *context, const nfc_connstring connstring)
Open a NFC device.
int nfc_initiator_init_secure_element(nfc_device *pnd)
Initialize NFC device as initiator with its secure element initiator (reader)
int nfc_device_set_property_int(nfc_device *pnd, const nfc_property property, const int value)
Set a device's integer-property value.
const char * nfc_device_get_connstring(nfc_device *pnd)
Returns the device connection string.
struct nfc_driver nfc_driver
nfc_dep_mode
NFC D.E.P. (Data Exchange Protocol) active/passive mode.
char name[DEVICE_NAME_LENGTH]
nfc_baud_rate
NFC baud rate enumeration.
const char * nfc_strerror(const nfc_device *pnd)
Return the last error string.
int nfc_device_get_last_error(const nfc_device *pnd)
Returns last error occured on a nfc_device.
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.
NFC modulation structure.
char nfc_connstring[NFC_BUFSIZE_CONNSTRING]
const char * nfc_device_get_name(nfc_device *pnd)
Returns the device name.
int nfc_abort_command(nfc_device *pnd)
Abort current running command.
int nfc_idle(nfc_device *pnd)
Turn NFC device in idle mode.
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.
int str_nfc_target(char **buf, const nfc_target *pnt, bool verbose)
Convert nfc_modulation_type value to string.
const char * str_nfc_baud_rate(const nfc_baud_rate nbr)
Convert nfc_baud_rate value to string.
void nfc_free(void *p)
Free buffer allocated by libnfc.
int nfc_target_receive_bytes(nfc_device *pnd, uint8_t *pbtRx, const size_t szRx, int timeout)
Receive bytes and APDU frames.
int nfc_target_send_bits(nfc_device *pnd, const uint8_t *pbtTx, const size_t szTxBits, const uint8_t *pbtTxPar)
Send raw bit-frames.
int nfc_device_get_supported_modulation(nfc_device *pnd, const nfc_mode mode, const nfc_modulation_type **const supported_mt)
Get supported modulations.
int nfc_register_driver(const struct nfc_driver *ndr)
Register an NFC device driver with libnfc. This function registers a driver with libnfc,...
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.
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.
void nfc_close(nfc_device *pnd)
Close from a NFC device.
NFC library context Struct which contains internal options, references, pointers, etc....
nfc_mode
NFC mode type enumeration.