diff --git a/examples/doc/quick_start_example1.c b/examples/doc/quick_start_example1.c index 9593db9..54d09d0 100644 --- a/examples/doc/quick_start_example1.c +++ b/examples/doc/quick_start_example1.c @@ -3,15 +3,27 @@ * @brief Quick start example that presents how to use libnfc */ +// To compile this simple example: +// $ gcc -o quick_start_example1 -lnfc quick_start_example1.c + #ifdef HAVE_CONFIG_H # include "config.h" #endif // HAVE_CONFIG_H #include +#include #include -#include "utils/nfc-utils.h" -#include "libnfc/chips/pn53x.h" +static void +print_hex(const uint8_t *pbtData, const size_t szBytes) +{ + size_t szPos; + + for (szPos = 0; szPos < szBytes; szPos++) { + printf("%02x ", pbtData[szPos]); + } + printf("\n"); +} int main(int argc, const char *argv[]) @@ -29,7 +41,7 @@ main(int argc, const char *argv[]) pnd = nfc_open(NULL, NULL); if (pnd == NULL) { - ERR("%s", "Unable to open NFC device."); + warnx("ERROR: %s", "Unable to open NFC device."); return EXIT_FAILURE; } // Set opened NFC device to initiator mode diff --git a/examples/doc/quick_start_example2.c b/examples/doc/quick_start_example2.c new file mode 100644 index 0000000..cc16eec --- /dev/null +++ b/examples/doc/quick_start_example2.c @@ -0,0 +1,69 @@ +/** + * @file quick_start_example2.c + * @brief Quick start example that presents how to use libnfc + */ + +// This is same example as quick_start_example1.c but using +// some helper functions existing in libnfc. +// Those functions are not available yet in a library +// so binary object must be linked statically: +// $ gcc -o quick_start_example2 -lnfc -I../.. quick_start_example2.c ../../utils/nfc-utils.o + +#ifdef HAVE_CONFIG_H +# include "config.h" +#endif // HAVE_CONFIG_H + +#include +#include +#include "utils/nfc-utils.h" + +int +main(int argc, const char *argv[]) +{ + nfc_device *pnd; + nfc_target nt; + + nfc_init(NULL); + + // Display libnfc version + const char *acLibnfcVersion = nfc_version(); + printf("%s uses libnfc %s\n", argv[0], acLibnfcVersion); + + // Open, using the first available NFC device + pnd = nfc_open(NULL, NULL); + + if (pnd == NULL) { + ERR("%s", "Unable to open NFC device."); + return EXIT_FAILURE; + } + // Set opened NFC device to initiator mode + if (nfc_initiator_init(pnd) < 0) { + nfc_perror(pnd, "nfc_initiator_init"); + exit(EXIT_FAILURE); + } + + printf("NFC reader: %s opened\n", nfc_device_get_name(pnd)); + + // Poll for a ISO14443A (MIFARE) tag + const nfc_modulation nmMifare = { + .nmt = NMT_ISO14443A, + .nbr = NBR_106, + }; + if (nfc_initiator_select_passive_target(pnd, nmMifare, NULL, 0, &nt) > 0) { + printf("The following (NFC) ISO14443A tag was found:\n"); + printf(" ATQA (SENS_RES): "); + print_hex(nt.nti.nai.abtAtqa, 2); + printf(" UID (NFCID%c): ", (nt.nti.nai.abtUid[0] == 0x08 ? '3' : '1')); + print_hex(nt.nti.nai.abtUid, nt.nti.nai.szUidLen); + printf(" SAK (SEL_RES): "); + print_hex(&nt.nti.nai.btSak, 1); + if (nt.nti.nai.szAtsLen) { + printf(" ATS (ATR): "); + print_hex(nt.nti.nai.abtAts, nt.nti.nai.szAtsLen); + } + } + // Close NFC device + nfc_close(pnd); + nfc_exit(NULL); + return EXIT_SUCCESS; +}