]> rtime.felk.cvut.cz Git - coffee/mt-apps.git/blob - mt_rfid.c
conditional beep
[coffee/mt-apps.git] / mt_rfid.c
1 /**
2  * mt_rfid.c
3  * poll the uFR reader and print JSON formatted card info to stdout.
4  */
5
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <unistd.h>
9 #include <fcntl.h>
10 #include <termios.h>
11 #include <sys/ioctl.h>
12
13 #include <ev.h>
14 #include <uFCoder.h>
15
16 #include "mt_rfid.h"
17
18 int set_nonblock(int fd)
19 {
20     int flags = fcntl(fd, F_GETFL, 0);
21     if (flags == -1){
22         perror("fcntl (get)");
23         return -1;
24     }
25     if (fcntl(fd, F_SETFL, flags | O_NONBLOCK) == -1){
26         perror("fcntl (set)");
27         return -1;
28     }
29     fprintf(stderr, "set non-blocking\n");
30     return 0;
31 }
32
33 int set_rts(int fd, int level) {
34         int uart_status;
35
36         if (ioctl(fd, TIOCMGET, &uart_status) == -1) {
37                 perror("ioctl (TIOCMGET)");
38                 return -1;
39         }
40
41         if (level) {
42                 uart_status |= TIOCM_RTS;
43     } else {
44                 uart_status &= ~TIOCM_RTS;
45     }
46
47         if (ioctl(fd, TIOCMSET, &uart_status) == -1) {
48                 perror("TIOCMSET");
49         return -1;
50         }
51
52     fprintf(stderr, "set rts %d\n", level);
53
54     return 0;
55 }
56
57 void set_baud_rate(int fd, int br) {
58         struct termios options;
59
60         tcgetattr(fd, &options);
61         cfsetispeed(&options, br);
62         cfsetospeed(&options, br);
63         tcsetattr(fd, TCSANOW, &options);
64 }
65
66 int tty_open(const char *port, int baud_rate) {
67
68         int fd = open(port, O_RDONLY | O_NOCTTY);
69         if (fd < 0) {
70         perror("open");
71         return fd;
72         } else {
73         fprintf(stderr, "opened %s as %d\n", port, fd);
74     }
75
76     set_nonblock(fd);
77         set_rts(fd, 0);
78         set_baud_rate(fd, baud_rate);
79         usleep(1200000); //value by d-logic
80         tcflush(fd, TCIFLUSH);
81
82         return fd;
83 }
84
85 typedef struct ev_io_ufr {
86     ev_io w;
87     char uid_data[24];
88     char *uid;
89 } ev_io_ufr;
90
91 static void sigint_cb(EV_P_ ev_signal *w, int revents)
92 {
93     ev_break (EV_A_  EVBREAK_ALL);
94 }
95
96 static void ufr_read(char *uid)
97 {
98     UFR_STATUS status;
99     uint8_t card_type;
100     uint8_t sak;         //select acknowledge
101     uint8_t uid_bytes[10]; //uid as bytes
102     uint8_t uid_size;
103
104     status = GetDlogicCardType(&card_type);
105     if (status != UFR_OK) {
106         fprintf(stderr, "GetDlogicCardType: %s\n", UFR_Status2String(status));
107         return;
108     }
109
110     status = GetCardIdEx(&sak, uid_bytes, &uid_size);
111     if (status != UFR_OK) {
112         fprintf(stderr, "GetCardIdEx: %s\n", UFR_Status2String(status));
113         return;
114     }
115
116     JSON_START();
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();
121
122 #ifdef UFR_BEEP
123     ReaderUISignal(0, 1);
124 #endif
125 }
126
127 static void ufr_cb(EV_P_ ev_io *w_, int revents)
128 {
129     ev_io_ufr *w = (ev_io_ufr *)w_;
130     char uid;
131     read(w->w.fd, &uid, 1);
132     *(w->uid)++ = uid;
133
134     if (uid == ASYNC_SUFFIX) {
135         //*(w->uid - 1) = 0;
136         w->uid = w->uid_data;
137         ufr_read(w->uid);
138     }
139
140     //for one-shot events, one must manually stop the watcher with its corresponding stop function.
141     //ev_io_stop (EV_A_ w);
142
143     //this causes all nested ev_run's to stop iterating
144     //ev_break (EV_A_ EVBREAK_ALL);
145 }
146
147 ev_io_ufr ufr_watcher;
148 ev_signal int_watcher, term_watcher;
149
150 int libev_run(int fd)
151 {
152     struct ev_loop *loop = EV_DEFAULT;
153
154     ufr_watcher.uid = ufr_watcher.uid_data;
155     ev_io_init (&(ufr_watcher.w), ufr_cb, fd, EV_READ);
156     ev_io_start (loop, (ev_io *)&ufr_watcher);
157
158     ev_signal_init (&int_watcher, sigint_cb, SIGINT);
159     ev_signal_start (loop, &int_watcher);
160     ev_signal_init (&term_watcher, sigint_cb, SIGTERM);
161     ev_signal_start (loop, &term_watcher);
162
163     ev_run (loop, 0);
164
165     return 0;
166 }
167
168 int ufr_open(unsigned reader_type, char *port_name, unsigned port_interface)
169 {
170     UFR_STATUS status;
171
172     fprintf(stderr, "uFCoder version: %s\n", GetDllVersionStr());
173
174     status = ReaderOpenEx(reader_type, port_name, port_interface, 0);
175     if (status != UFR_OK) {
176         fprintf(stderr, "ReaderOpenEx: %s\n", UFR_Status2String(status));
177         return -1;
178     }
179
180     fprintf(stderr, "%s\n", GetReaderDescription());
181
182     status = SetAsyncCardIdSendConfig(
183         1,            //enable send
184         0,            //disable prefix
185         0,            //prefix
186         ASYNC_SUFFIX, //suffix
187         0,            //disable send removed
188         ASYNC_BAUD_RATE
189     );
190     fprintf(stderr, "SetAsyncCardIdSendConfig: %s\n", UFR_Status2String(status));
191     if (status != UFR_OK) {
192         return -1;
193     }
194
195     return 0;
196 }
197
198 int main(int argc, char **argv)
199 {
200     if (ufr_open(READER_TYPE, PORT_NAME, PORT_INTERFACE) == -1) {
201         return -1;
202     }
203
204     int fd = tty_open(PORT_NAME, BASYNC_BAUD_RATE);
205     if (fd < 0) {
206         return -2;
207     }
208
209         libev_run(fd);
210
211     close(fd);
212     fprintf(stderr, "closed %d\n", fd);
213
214     UFR_STATUS status;
215     status = ReaderClose();
216     fprintf(stderr, "ReaderClose: %s\n", UFR_Status2String(status));
217
218     return 0;
219 }
220
221 /* other tty options
222     //Enable the receiver and set local mode...
223     options.c_cflag |= (CLOCAL | CREAD);
224
225     options.c_cflag &= ~PARENB;
226     options.c_cflag &= ~CSTOPB;
227     options.c_cflag &= ~CSIZE;
228     options.c_cflag |= CS8;
229     options.c_cflag &= ~CRTSCTS;
230     options.c_lflag |= (ICANON | ECHO | ECHOE);
231     //Disable XON/XOFF both i/p and o/p
232     options.c_iflag &= ~(IXON | IXOFF | IXANY);
233     //Non Cannon mode
234     options.c_iflag &= ~(ICANON | ECHO | ECHOE | ISIG);
235
236     options.c_oflag |= OPOST;
237 */
238