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