-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnfcV2.c
More file actions
118 lines (102 loc) · 4.2 KB
/
nfcV2.c
File metadata and controls
118 lines (102 loc) · 4.2 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <nfc/nfc.h>
#define ATS_MAX_LEN 64
#define UID_MAX_LEN 10 // ISO14443A UID 최대 길이 (4, 7, 10 바이트)
#define CHECK_INTERVAL_uSEC 500000 // 500000 마이크로초 = 0.5초
int count = 0;
// 바이트 배열을 HEX 문자열로 변환
void bytes_to_hexstr(const uint8_t *bytes, size_t len, char *str, size_t str_size) {
size_t i;
if (str_size < (len * 2 + 1)) {
fprintf(stderr, "Buffer too small for hex string\n");
exit(EXIT_FAILURE);
}
for (i = 0; i < len; i++) {
sprintf(&str[i*2], "%02X", bytes[i]);
}
str[len*2] = '\0';
}
int main(void) {
nfc_device *pnd = NULL;
nfc_context *context;
nfc_target nt;
nfc_init(&context);
if (context == NULL) {
fprintf(stderr, "Unable to init libnfc\n");
return EXIT_FAILURE;
}
// PN532 I2C 접속 (libnfc.conf 설정에 따라 달라질 수 있음)
// 만약 I2C가 아닌 다른 인터페이스를 사용한다면 nfc_open의 두 번째 인자를 수정해야 합니다.
pnd = nfc_open(context, NULL); // NULL은 기본 설정 사용
if (pnd == NULL) {
fprintf(stderr, "Unable to open NFC device.\n");
nfc_exit(context);
return EXIT_FAILURE;
}
if (nfc_initiator_init(pnd) < 0) {
nfc_perror(pnd, "nfc_initiator_init");
nfc_close(pnd);
nfc_exit(context);
return EXIT_FAILURE;
}
printf("PN532 NFC reader initialized.\n");
FILE *output_file = NULL;
while (1) {
// NFC 카드 감지 시도 (ISO14443A 태그)
const nfc_modulation nmModulations[1] = {
{ .nmt = NMT_ISO14443A, .nbr = NBR_106 },
};
const size_t szModulations = 1;
int res = nfc_initiator_poll_target(pnd, nmModulations, szModulations, 1, 2, &nt);
if (res > 0) {
// NFC 태그 감지됨
output_file = fopen("uid.txt", "w+");
if (output_file == NULL) {
perror("파일 열기 실패");
} else {
if (nt.nti.nai.szAtsLen > 0) {
// ATS 정보가 있을 경우
char ats_hex[ATS_MAX_LEN*2 + 1];
bytes_to_hexstr(nt.nti.nai.abtAts, nt.nti.nai.szAtsLen, ats_hex, sizeof(ats_hex));
printf("Detected ATS: %s\n", ats_hex);
fprintf(output_file, "%s\n", ats_hex);
printf("ATS 값을 파일에 저장했습니다. %d\n\n", count + 1); // count++ 전에 출력
} else {
// ATS 정보가 없을 경우, UID 정보를 사용
char uid_hex[UID_MAX_LEN*2 + 1];
// UID 길이를 확인하여 버퍼 오버플로우 방지
size_t uid_len = nt.nti.nai.szUidLen;
if (uid_len > UID_MAX_LEN) {
fprintf(stderr, "Warning: UID length (%zu) exceeds buffer size (%d). Truncating.\n", uid_len, UID_MAX_LEN);
uid_len = UID_MAX_LEN; // 또는 오류 처리
}
bytes_to_hexstr(nt.nti.nai.abtUid, uid_len, uid_hex, sizeof(uid_hex));
printf("Detected UID: %s (No ATS)\n", uid_hex);
fprintf(output_file, "%s\n", uid_hex);
printf("UID 값을 파일에 저장했습니다. (ATS 없음) %d\n\n", count + 1); // count++ 전에 출력
}
fclose(output_file);
count++; // 파일 저장 성공 시에만 count 증가
}
} else {
// NFC 태그 미감지
printf("NFC 태그 미감지...\n");
output_file = fopen("uid.txt", "w+");
if (output_file == NULL) {
perror("파일 열기 실패");
} else {
fprintf(output_file, "NO TAG\n");
fclose(output_file);
printf("'NO TAG'를 파일에 저장했습니다. %d\n\n", count + 1); // count++ 전에 출력
count++; // 파일 저장 성공 시에만 count 증가
}
}
usleep(CHECK_INTERVAL_uSEC);
}
nfc_close(pnd);
nfc_exit(context);
return 0;
}