-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathexample.c
More file actions
82 lines (66 loc) · 1.58 KB
/
example.c
File metadata and controls
82 lines (66 loc) · 1.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include <stdlib.h>
#include <nfc/nfc.h>
/* Print uint8_t array data in hex string
*/
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[])
{
nfc_device *pnd;
nfc_target nt;
nfc_context *context;
// initialize the driver
nfc_init(&context);
if(context == NULL)
{
printf("Unable to init libnfc (malloc)\n");
exit(EXIT_FAILURE);
}
// open device
pnd = nfc_open(context, NULL);
if(pnd == NULL) {
printf("ERR: Unable to open NFC device");
nfc_exit(context);
exit(EXIT_FAILURE);
}
// initialize the initiator
if(nfc_initiator_init(pnd) < 0)
{
nfc_perror(pnd, "nfc_initiator_init");
nfc_exit(context);
exit(EXIT_FAILURE);
}
printf("NFC reader: %s opened\n", nfc_device_get_name(pnd));
// poll for a 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("ISO14443A tag was found:\n");
printf("\tATQS (SENS_RES): ");
print_hex(nt.nti.nai.abtAtqa, 2);
printf("\tUID (NFCID%c): ", (nt.nti.nai.abtUid[0] == 0x08 ? '3':'1'));
print_hex(nt.nti.nai.abtUid, nt.nti.nai.szUidLen);
printf("\tSAK (SEL_RES): ");
print_hex(&nt.nti.nai.btSak, 1);
if(nt.nti.nai.szAtsLen)
{
printf("\t\tATS (ATR): ");
print_hex(nt.nti.nai.abtAts, nt.nti.nai.szAtsLen);
}
}
// close the device
nfc_close(pnd);
// release the context
nfc_exit(context);
exit(EXIT_SUCCESS);
}