diff --git a/examples/mifare-classic-format.c b/examples/mifare-classic-format.c new file mode 100644 index 0000000..2fb30ec --- /dev/null +++ b/examples/mifare-classic-format.c @@ -0,0 +1,201 @@ +#include +#include +#include + +#include + +#include +#include + +#define DEBUG 1 + +// Useful macros +#ifdef DEBUG +// #define DBG(x, args...) printf("DBG %s:%d: " x "\n", __FILE__, __LINE__,## args ) + #define DBG(x, ...) fprintf(stderr, "DBG %s:%d: " x "\n", __FILE__, __LINE__, ## __VA_ARGS__ ) +#else + #define DBG(...) {} +#endif + +static nfc_device_t *device = NULL; +static MifareClassicTag *tags = NULL; + +int +mifare_classic_setup (MifareClassicTag *tag) +{ + int res = 0; + + *tag = NULL; + + device = nfc_connect (NULL); + if (!device) + res = -1; + + if (0 == res) { + tags = mifare_classic_get_tags (device); + if (!tags) { + nfc_disconnect (device); + device = NULL; + res = -2; + } + } + + if (0 == res) { + if (!tags[0]) { + mifare_classic_free_tags (tags); + tags = NULL; + nfc_disconnect (device); + device = NULL; + res = -4; + } + } + + if (0 == res) { + *tag = tags[0]; + + res = mifare_classic_connect (*tag); + if (res != 0) { + mifare_classic_disconnect (*tag); + nfc_disconnect (device); + *tag = NULL; + device = NULL; + res = -3; + } + } + + return res; +} + +int +mifare_classic_teardown (MifareClassicTag tag) +{ + int res; + + if (tag) + res = mifare_classic_disconnect (tag); + + if (0 == res) { + if (tags) + mifare_classic_free_tags (tags); + + if (device) + nfc_disconnect (device); + } + + return res; +} + +void +print_block(MifareClassicBlock *data) +{ + for(size_t n=0;n