quick_start_example1.c

00001 #ifdef HAVE_CONFIG_H
00002 #  include "config.h"
00003 #endif // HAVE_CONFIG_H
00004 
00005 #include <stdlib.h>
00006 #include <nfc/nfc.h>
00007 #include <nfc/nfc-messages.h>
00008 
00009 int
00010 main (int argc, const char *argv[])
00011 {
00012   nfc_device_t *pnd;
00013   nfc_target_info_t nti;
00014 
00015   // Display libnfc version
00016   const char *acLibnfcVersion = nfc_version ();
00017   printf ("%s use libnfc %s\n", argv[0], acLibnfcVersion);
00018 
00019   // Connect using the first available NFC device
00020   pnd = nfc_connect (NULL);
00021 
00022   if (pnd == NULL) {
00023     ERR ("%s", "Unable to connect to NFC device.");
00024     return EXIT_FAILURE;
00025   }
00026   // Set connected NFC device to initiator mode
00027   nfc_initiator_init (pnd);
00028 
00029   // Enable field so more power consuming cards can power themselves up
00030   nfc_configure (pnd, NDO_ACTIVATE_FIELD, true);
00031 
00032   printf ("Connected to NFC reader: %s\n", pnd->acName);
00033 
00034   // Poll for a ISO14443A (MIFARE) tag
00035   const nfc_modulation_t nmMifare = {
00036     .nmt = NMT_ISO14443A,
00037     .nbr = NBR_106,
00038   };
00039   if (nfc_initiator_select_passive_target (pnd, PM_ISO14443A_106, NULL, 0, &nti)) {
00040     printf ("The following (NFC) ISO14443A tag was found:\n");
00041     printf ("    ATQA (SENS_RES): ");
00042     print_hex (nti.nai.abtAtqa, 2);
00043     printf ("       UID (NFCID%c): ", (nti.nai.abtUid[0] == 0x08 ? '3' : '1'));
00044     print_hex (nti.nai.abtUid, nti.nai.szUidLen);
00045     printf ("      SAK (SEL_RES): ");
00046     print_hex (&nti.nai.btSak, 1);
00047     if (nti.nai.szAtsLen) {
00048       printf ("          ATS (ATR): ");
00049       print_hex (nti.nai.abtAts, nti.nai.szAtsLen);
00050     }
00051   }
00052   // Disconnect from NFC device
00053   nfc_disconnect (pnd);
00054   return EXIT_SUCCESS;
00055 }