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)
41 if (ioctl(fd, TIOCMGET, &uart_status) == -1) {
42 perror("ioctl (TIOCMGET)");
47 uart_status |= TIOCM_RTS;
49 uart_status &= ~TIOCM_RTS;
52 if (ioctl(fd, TIOCMSET, &uart_status) == -1) {
57 fprintf(stderr, "set rts %d\n", level);
62 void set_baud_rate(int fd, int br)
64 struct termios options;
66 tcgetattr(fd, &options);
67 cfsetispeed(&options, br);
68 cfsetospeed(&options, br);
69 tcsetattr(fd, TCSANOW, &options);
72 int tty_open(const char *port, int baud_rate)
75 int fd = open(port, O_RDONLY | O_NOCTTY);
80 fprintf(stderr, "opened %s as %d\n", port, fd);
85 set_baud_rate(fd, baud_rate);
86 usleep(1200000); //value by d-logic
87 tcflush(fd, TCIFLUSH);
92 typedef struct ev_io_ufr {
94 //lws_context *context;
95 //ufr_session_data *session_data;
101 static void sigint_cb(EV_P_ ev_signal *w, int revents)
103 ev_break(EV_A_ EVBREAK_ALL);
106 static void ufr_read(char *uid, int fd)
110 uint8_t sak; //select acknowledge
111 uint8_t uid_bytes[10]; //uid as bytes
115 status = GetDlogicCardType(&card_type);
116 if (status != UFR_OK) {
117 fprintf(stderr, "GetDlogicCardType: %s\n", UFR_Status2String(status));
121 status = GetCardIdEx(&sak, uid_bytes, &size);
122 if (status != UFR_OK) {
123 fprintf(stderr, "GetCardIdEx: %s\n", UFR_Status2String(status));
140 ReaderUISignal(0, 1); // no light, one beep
143 /*lws_callback_on_writable_all_protocol(self->context,
144 &protocols[PROTOCOL_DUMB_INCREMENT]);*/
147 static void ufr_cb(EV_P_ ev_io *w_, int revents)
149 ev_io_ufr *w = (ev_io_ufr *)w_;
151 read(w->w.fd, &uid, 1);
154 if (uid == ASYNC_SUFFIX) {
156 w->uid = w->uid_data;
157 ufr_read(w->uid, w->fd);
160 //for one-shot events, one must manually stop the watcher with its corresponding stop function.
161 //ev_io_stop (EV_A_ w);
163 //this causes all nested ev_run's to stop iterating
164 //ev_break (EV_A_ EVBREAK_ALL);
167 ev_io_ufr ufr_watcher;
168 ev_signal int_watcher, term_watcher;
170 int libev_run(int fd)
172 struct ev_loop *loop = EV_DEFAULT;
174 ufr_watcher.uid = ufr_watcher.uid_data;
176 ev_io_init(&(ufr_watcher.w), ufr_cb, fd, EV_READ);
177 ev_io_start(loop, (ev_io *)&ufr_watcher);
179 ev_signal_init(&int_watcher, sigint_cb, SIGINT);
180 ev_signal_start(loop, &int_watcher);
181 ev_signal_init(&term_watcher, sigint_cb, SIGTERM);
182 ev_signal_start(loop, &term_watcher);
189 int ufr_open(unsigned reader_type, char *port_name, unsigned port_interface)
193 fprintf(stderr, "uFCoder version: %s\n", GetDllVersionStr());
195 status = ReaderOpenEx(reader_type, port_name, port_interface, 0);
196 if (status != UFR_OK) {
197 fprintf(stderr, "ReaderOpenEx: %s\n", UFR_Status2String(status));
201 fprintf(stderr, "%s\n", GetReaderDescription());
203 status = SetAsyncCardIdSendConfig(
207 ASYNC_SUFFIX, //suffix
208 0, //disable send removed
211 fprintf(stderr, "SetAsyncCardIdSendConfig: %s\n", UFR_Status2String(status));
212 if (status != UFR_OK) {
219 int main(int argc, char **argv)
221 if (ufr_open(READER_TYPE, PORT_NAME, PORT_INTERFACE) == -1) {
225 int fd = tty_open(PORT_NAME, CONCAT(B, ASYNC_BAUD_RATE));
232 if (close(fd) == 0) {
233 fprintf(stderr, "closed %d\n", fd);
239 status = ReaderClose();
240 fprintf(stderr, "ReaderClose: %s\n", UFR_Status2String(status));
246 //Enable the receiver and set local mode...
247 options.c_cflag |= (CLOCAL | CREAD);
249 options.c_cflag &= ~PARENB;
250 options.c_cflag &= ~CSTOPB;
251 options.c_cflag &= ~CSIZE;
252 options.c_cflag |= CS8;
253 options.c_cflag &= ~CRTSCTS;
254 options.c_lflag |= (ICANON | ECHO | ECHOE);
255 //Disable XON/XOFF both i/p and o/p
256 options.c_iflag &= ~(IXON | IXOFF | IXANY);
258 options.c_iflag &= ~(ICANON | ECHO | ECHOE | ISIG);
260 options.c_oflag |= OPOST;