3 * poll the uFR reader and print JSON formatted card info to stdout.
6 // shit to avoid constant repetition
7 #define CONCAT_AGAIN(A,B) A ## B
8 #define CONCAT(A,B) CONCAT_AGAIN(A,B)
15 #include <sys/ioctl.h>
22 int set_nonblock(int fd)
24 int flags = fcntl(fd, F_GETFL, 0);
26 perror("fcntl (get)");
29 if (fcntl(fd, F_SETFL, flags | O_NONBLOCK) == -1){
30 perror("fcntl (set)");
33 fprintf(stderr, "set non-blocking\n");
37 int set_rts(int fd, int level) {
40 if (ioctl(fd, TIOCMGET, &uart_status) == -1) {
41 perror("ioctl (TIOCMGET)");
46 uart_status |= TIOCM_RTS;
48 uart_status &= ~TIOCM_RTS;
51 if (ioctl(fd, TIOCMSET, &uart_status) == -1) {
56 fprintf(stderr, "set rts %d\n", level);
61 void set_baud_rate(int fd, int br) {
62 struct termios options;
64 tcgetattr(fd, &options);
65 cfsetispeed(&options, br);
66 cfsetospeed(&options, br);
67 tcsetattr(fd, TCSANOW, &options);
70 int tty_open(const char *port, int baud_rate) {
72 int fd = open(port, O_RDONLY | O_NOCTTY);
77 fprintf(stderr, "opened %s as %d\n", port, fd);
82 set_baud_rate(fd, baud_rate);
83 usleep(1200000); //value by d-logic
84 tcflush(fd, TCIFLUSH);
89 typedef struct ev_io_ufr {
95 static void sigint_cb(EV_P_ ev_signal *w, int revents)
97 ev_break (EV_A_ EVBREAK_ALL);
100 static void ufr_read(char *uid)
104 uint8_t sak; //select acknowledge
105 uint8_t uid_bytes[10]; //uid as bytes
108 status = GetDlogicCardType(&card_type);
109 if (status != UFR_OK) {
110 fprintf(stderr, "GetDlogicCardType: %s\n", UFR_Status2String(status));
114 status = GetCardIdEx(&sak, uid_bytes, &uid_size);
115 if (status != UFR_OK) {
116 fprintf(stderr, "GetCardIdEx: %s\n", UFR_Status2String(status));
121 JSON_NUM(card_type); JSON_NEXT();
122 JSON_NUM(sak); JSON_NEXT();
123 JSON_NUM(uid_size); JSON_NEXT();
124 JSON_STR(uid); JSON_END();
127 ReaderUISignal(0, 1);
131 static void ufr_cb(EV_P_ ev_io *w_, int revents)
133 ev_io_ufr *w = (ev_io_ufr *)w_;
135 read(w->w.fd, &uid, 1);
138 if (uid == ASYNC_SUFFIX) {
140 w->uid = w->uid_data;
144 //for one-shot events, one must manually stop the watcher with its corresponding stop function.
145 //ev_io_stop (EV_A_ w);
147 //this causes all nested ev_run's to stop iterating
148 //ev_break (EV_A_ EVBREAK_ALL);
151 ev_io_ufr ufr_watcher;
152 ev_signal int_watcher, term_watcher;
154 int libev_run(int fd)
156 struct ev_loop *loop = EV_DEFAULT;
158 ufr_watcher.uid = ufr_watcher.uid_data;
159 ev_io_init (&(ufr_watcher.w), ufr_cb, fd, EV_READ);
160 ev_io_start (loop, (ev_io *)&ufr_watcher);
162 ev_signal_init (&int_watcher, sigint_cb, SIGINT);
163 ev_signal_start (loop, &int_watcher);
164 ev_signal_init (&term_watcher, sigint_cb, SIGTERM);
165 ev_signal_start (loop, &term_watcher);
172 int ufr_open(unsigned reader_type, char *port_name, unsigned port_interface)
176 fprintf(stderr, "uFCoder version: %s\n", GetDllVersionStr());
178 status = ReaderOpenEx(reader_type, port_name, port_interface, 0);
179 if (status != UFR_OK) {
180 fprintf(stderr, "ReaderOpenEx: %s\n", UFR_Status2String(status));
184 fprintf(stderr, "%s\n", GetReaderDescription());
186 status = SetAsyncCardIdSendConfig(
190 ASYNC_SUFFIX, //suffix
191 0, //disable send removed
194 fprintf(stderr, "SetAsyncCardIdSendConfig: %s\n", UFR_Status2String(status));
195 if (status != UFR_OK) {
202 int main(int argc, char **argv)
204 if (ufr_open(READER_TYPE, PORT_NAME, PORT_INTERFACE) == -1) {
208 int fd = tty_open(PORT_NAME, CONCAT(B,ASYNC_BAUD_RATE));
215 if (close(fd) == 0) {
216 fprintf(stderr, "closed %d\n", fd);
222 status = ReaderClose();
223 fprintf(stderr, "ReaderClose: %s\n", UFR_Status2String(status));
229 //Enable the receiver and set local mode...
230 options.c_cflag |= (CLOCAL | CREAD);
232 options.c_cflag &= ~PARENB;
233 options.c_cflag &= ~CSTOPB;
234 options.c_cflag &= ~CSIZE;
235 options.c_cflag |= CS8;
236 options.c_cflag &= ~CRTSCTS;
237 options.c_lflag |= (ICANON | ECHO | ECHOE);
238 //Disable XON/XOFF both i/p and o/p
239 options.c_iflag &= ~(IXON | IXOFF | IXANY);
241 options.c_iflag &= ~(ICANON | ECHO | ECHOE | ISIG);
243 options.c_oflag |= OPOST;