Skip to content

Instantly share code, notes, and snippets.

@doegox
Created August 31, 2017 23:37
Show Gist options
  • Save doegox/5205cdad9862e74af75304d599fcbd8f to your computer and use it in GitHub Desktop.
Save doegox/5205cdad9862e74af75304d599fcbd8f to your computer and use it in GitHub Desktop.
LIBNFC: Quick start example that presents how to discover a ST SRx tag
/**
* @file st_srx.c
* @brief Quick start example that presents how to discover a ST SRx tag
*/
// To compile this simple example:
// $ gcc -o st_srx st_srx.c -lnfc
#include <stdlib.h>
#include <nfc/nfc.h>
int
main(int argc, const char *argv[])
{
nfc_device *pnd;
nfc_target nt;
// Allocate only a pointer to nfc_context
nfc_context *context;
// Initialize libnfc and set the nfc_context
nfc_init(&context);
if (context == NULL) {
printf("Unable to init libnfc (malloc)\n");
exit(EXIT_FAILURE);
}
// Display libnfc version
const char *acLibnfcVersion = nfc_version();
(void)argc;
printf("%s uses libnfc %s\n", argv[0], acLibnfcVersion);
// Open, using the first available NFC device which can be in order of selection:
// - default device specified using environment variable or
// - first specified device in libnfc.conf (/etc/nfc) or
// - first specified device in device-configuration directory (/etc/nfc/devices.d) or
// - first auto-detected (if feature is not disabled in libnfc.conf) device
pnd = nfc_open(context, NULL);
if (pnd == NULL) {
printf("ERROR: %s\n", "Unable to open NFC device.");
exit(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));
// We need first to get the firmware activating the type B by polling for regular Type B tag:
int res = 0;
nfc_target ant[1];
nfc_modulation nm = {
.nmt = NMT_ISO14443B,
.nbr = NBR_106,
};
nfc_initiator_list_passive_targets(pnd, nm, ant, 1);
// Poll for a ST SRx tag
nm.nmt = NMT_ISO14443B2SR;
if (nfc_initiator_select_passive_target(pnd, nm, NULL, 0, &nt) > 0) {
char *s;
str_nfc_target(&s, &nt, true);
printf("%s", s);
nfc_free(s);
printf("\n");
}
// Close NFC device
nfc_close(pnd);
// Release the context
nfc_exit(context);
exit(EXIT_SUCCESS);
}
@userDevop123
Copy link

And how can i get/print the data that on the card?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment