3 * poll the uFR reader and print JSON formatted card info to stdout.
11 #include <sys/ioctl.h>
18 int set_nonblock(int fd)
20 int flags = fcntl(fd, F_GETFL, 0);
22 perror("fcntl (get)");
25 if (fcntl(fd, F_SETFL, flags | O_NONBLOCK) == -1){
26 perror("fcntl (set)");
29 fprintf(stderr, "set non-blocking\n");
33 int set_rts(int fd, int level) {
36 if (ioctl(fd, TIOCMGET, &uart_status) == -1) {
37 perror("ioctl (TIOCMGET)");
42 uart_status |= TIOCM_RTS;
44 uart_status &= ~TIOCM_RTS;
47 if (ioctl(fd, TIOCMSET, &uart_status) == -1) {
52 fprintf(stderr, "set rts %d\n", level);
57 void set_baud_rate(int fd, int br) {
58 struct termios options;
60 tcgetattr(fd, &options);
61 cfsetispeed(&options, br);
62 cfsetospeed(&options, br);
63 tcsetattr(fd, TCSANOW, &options);
66 int tty_open(const char *port, int baud_rate) {
68 int fd = open(port, O_RDONLY | O_NOCTTY);
73 fprintf(stderr, "opened %s as %d\n", port, fd);
78 set_baud_rate(fd, baud_rate);
79 usleep(1200000); //value by d-logic
80 tcflush(fd, TCIFLUSH);
85 typedef struct ev_io_ufr {
91 static void sigint_cb(EV_P_ ev_signal *w, int revents)
93 ev_break (EV_A_ EVBREAK_ALL);
96 static void ufr_read(char *uid)
100 uint8_t sak; //select acknowledge
101 uint8_t uid_bytes[10]; //uid as bytes
104 status = GetDlogicCardType(&card_type);
105 if (status != UFR_OK) {
106 fprintf(stderr, "GetDlogicCardType: %s\n", UFR_Status2String(status));
110 status = GetCardIdEx(&sak, uid_bytes, &uid_size);
111 if (status != UFR_OK) {
112 fprintf(stderr, "GetCardIdEx: %s\n", UFR_Status2String(status));
117 JSON_NUM(card_type); JSON_NEXT();
118 JSON_NUM(sak); JSON_NEXT();
119 JSON_NUM(uid_size); JSON_NEXT();
120 JSON_STR(uid); JSON_END();
122 ReaderUISignal(0, 1);
125 static void ufr_cb(EV_P_ ev_io *w_, int revents)
127 ev_io_ufr *w = (ev_io_ufr *)w_;
129 read(w->w.fd, &uid, 1);
132 if (uid == ASYNC_SUFFIX) {
134 w->uid = w->uid_data;
138 //for one-shot events, one must manually stop the watcher with its corresponding stop function.
139 //ev_io_stop (EV_A_ w);
141 //this causes all nested ev_run's to stop iterating
142 //ev_break (EV_A_ EVBREAK_ALL);
145 ev_io_ufr ufr_watcher;
146 ev_signal int_watcher, term_watcher;
148 int libev_run(int fd)
150 struct ev_loop *loop = EV_DEFAULT;
152 ufr_watcher.uid = ufr_watcher.uid_data;
153 ev_io_init (&(ufr_watcher.w), ufr_cb, fd, EV_READ);
154 ev_io_start (loop, (ev_io *)&ufr_watcher);
156 ev_signal_init (&int_watcher, sigint_cb, SIGINT);
157 ev_signal_start (loop, &int_watcher);
158 ev_signal_init (&term_watcher, sigint_cb, SIGTERM);
159 ev_signal_start (loop, &term_watcher);
166 int ufr_open(unsigned reader_type, char *port_name, unsigned port_interface)
170 fprintf(stderr, "uFCoder version: %s\n", GetDllVersionStr());
172 status = ReaderOpenEx(reader_type, port_name, port_interface, 0);
173 if (status != UFR_OK) {
174 fprintf(stderr, "ReaderOpenEx: %s\n", UFR_Status2String(status));
178 fprintf(stderr, "%s\n", GetReaderDescription());
180 status = SetAsyncCardIdSendConfig(
184 ASYNC_SUFFIX, //suffix
185 0, //disable send removed
188 fprintf(stderr, "SetAsyncCardIdSendConfig: %s\n", UFR_Status2String(status));
189 if (status != UFR_OK) {
196 int main(int argc, char **argv)
198 if (ufr_open(READER_TYPE, PORT_NAME, PORT_INTERFACE) == -1) {
202 int fd = tty_open(PORT_NAME, BASYNC_BAUD_RATE);
210 fprintf(stderr, "closed %d\n", fd);
213 status = ReaderClose();
214 fprintf(stderr, "ReaderClose: %s\n", UFR_Status2String(status));
220 //Enable the receiver and set local mode...
221 options.c_cflag |= (CLOCAL | CREAD);
223 options.c_cflag &= ~PARENB;
224 options.c_cflag &= ~CSTOPB;
225 options.c_cflag &= ~CSIZE;
226 options.c_cflag |= CS8;
227 options.c_cflag &= ~CRTSCTS;
228 options.c_lflag |= (ICANON | ECHO | ECHOE);
229 //Disable XON/XOFF both i/p and o/p
230 options.c_iflag &= ~(IXON | IXOFF | IXANY);
232 options.c_iflag &= ~(ICANON | ECHO | ECHOE | ISIG);
234 options.c_oflag |= OPOST;