Newer
Older
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
/*
============================================================================
Project Name: project_name
Name : file_name.c
Author : d-logic (http://www.d-logic.net/nfc-rfid-reader-sdk/)
Version :
Copyright : 2017.
Description : Project in C (Language standard: c99)
Dependencies: uFR firmware - min. version x.y.z {define in ini.h}
uFRCoder library - min. version x.y.z {define in ini.h}
============================================================================
*/
/* includes:
* stdio.h & stdlib.h are included by default (for printf and LARGE_INTEGER.QuadPart (long long) use %lld or %016llx).
* inttypes.h, stdbool.h & string.h included for various type support and utilities.
* conio.h is included for windows(dos) console input functions.
* windows.h is needed for various timer functions (GetTickCount(), QueryPerformanceFrequency(), QueryPerformanceCounter())
*/
#include <stdio.h>
#include <stdlib.h>
#include <inttypes.h>
#include <stdbool.h>
#include <string.h>
#if __WIN32 || __WIN64
# include <conio.h>
# include <windows.h>
#elif linux || __linux__ || __APPLE__
# define __USE_MISC
# include <unistd.h>
# include <termios.h>
# undef __USE_MISC
# include "conio_gnu.h"
#else
# error "Unknown build platform."
#endif
#include <uFCoder.h>
#include "ini.h"
#include "uFR.h"
#include "utils.h"
//------------------------------------------------------------------------------
void usage(void);
void menu(char key);
UFR_STATUS NewCardInField(uint8_t sak, uint8_t *uid, uint8_t uid_size);
void Operation1(void);
void Operation2(void);
void Operation3(void);
//------------------------------------------------------------------------------
int main(void)
{
char key;
bool card_in_field = false;
uint8_t old_sak = 0, old_uid_size = 0, old_uid[10];
uint8_t sak, uid_size, uid[10];
UFR_STATUS status;
usage();
printf(" --------------------------------------------------\n");
printf(" Please wait while opening uFR NFC reader.\n");
printf(" --------------------------------------------------\n");
#ifdef __DEBUG
status = ReaderOpenEx(1, PORT_NAME, 1, NULL);
#else
status = ReaderOpen();
#endif
if (status != UFR_OK)
{
printf("Error while opening device, status is: 0x%08X\n", status);
getchar();
return EXIT_FAILURE;
}
printf("Error while opening device, status is: 0x%08X\n", status);
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
getchar();
return EXIT_FAILURE;
}
printf(" --------------------------------------------------\n");
printf(" uFR NFC reader successfully opened.\n");
printf(" --------------------------------------------------\n");
#if linux || __linux__ || __APPLE__
_initTermios(0);
#endif
do
{
while (!_kbhit())
{
status = GetCardIdEx(&sak, uid, &uid_size);
switch (status)
{
case UFR_OK:
if (card_in_field)
{
if (old_sak != sak || old_uid_size != uid_size || memcmp(old_uid, uid, uid_size))
{
old_sak = sak;
old_uid_size = uid_size;
memcpy(old_uid, uid, uid_size);
NewCardInField(sak, uid, uid_size);
}
}
else
{
old_sak = sak;
old_uid_size = uid_size;
memcpy(old_uid, uid, uid_size);
NewCardInField(sak, uid, uid_size);
card_in_field = true;
}
break;
case UFR_NO_CARD:
card_in_field = false;
status = UFR_OK;
break;
default:
ReaderClose();
printf(" Fatal error while trying to read card, status is: 0x%08X\n", status);
getchar();
#if linux || __linux__ || __APPLE__
_resetTermios();
tcflush(0, TCIFLUSH); // Clear stdin to prevent characters appearing on prompt
#endif
return EXIT_FAILURE;
}
#if __WIN32 || __WIN64
Sleep(300);
#else // if linux || __linux__ || __APPLE__
usleep(300000);
#endif
}
key = _getch();
menu(key);
}
while (key != '\x1b');
ReaderClose();
#if linux || __linux__ || __APPLE__
_resetTermios();
tcflush(0, TCIFLUSH); // Clear stdin to prevent characters appearing on prompt
#endif
return EXIT_SUCCESS;
}
//------------------------------------------------------------------------------
void menu(char key)
{
switch (key)
{
case '1':
Operation1();
break;
case '2':
Operation2();
break;
case '3':
Operation3();
break;
case '\x1b':
break;
default:
usage();
break;
}
}
//------------------------------------------------------------------------------
void usage(void)
{
printf(" +------------------------------------------------+\n"
" | Application title |\n"
" | version "APP_VERSION" |\n"
" +------------------------------------------------+\n"
" For exit, hit escape.\n");
printf(" --------------------------------------------------\n");
printf(" (1) - Option 1\n"
" (2) - Option 2\n"
" (3) - Option 3\n");
}
//------------------------------------------------------------------------------
UFR_STATUS NewCardInField(uint8_t sak, uint8_t *uid, uint8_t uid_size)
{
UFR_STATUS status;
uint8_t dl_card_type;
status = GetDlogicCardType(&dl_card_type);
if (status != UFR_OK)
return status;
printf(" \a-------------------------------------------------------------------\n");
printf(" Card type: %s, sak = 0x%02X, uid[%d] = ", GetDlTypeName(dl_card_type), sak, uid_size);
print_hex_ln(uid, uid_size, ":");
printf(" -------------------------------------------------------------------\n");
return UFR_OK;
}
//------------------------------------------------------------------------------
void Operation1(void)
{
printf(" -------------------------------------------------------------------\n");
printf(" Operation 1 \n");
printf(" -------------------------------------------------------------------\n");
}
//------------------------------------------------------------------------------
void Operation2(void)
{
printf(" -------------------------------------------------------------------\n");
printf(" Operation 2 \n");
printf(" -------------------------------------------------------------------\n");
}
//------------------------------------------------------------------------------
void Operation3(void)
{
printf(" -------------------------------------------------------------------\n");
printf(" Operation 3 \n");
printf(" -------------------------------------------------------------------\n");
}
//------------------------------------------------------------------------------