]> rtime.felk.cvut.cz Git - coffee/mt-apps.git/blob - mt_rfid.c
4f3b07c5d7c8d9dfa4e790cf4c783491b793c7ec
[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 <uFCoder.h>
18
19 #include "mt_rfid.h"
20 #include "ev_signal_exit.h"
21
22 static 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 static int set_rts(int fd, int level)
38 {
39     int uart_status;
40
41     if (ioctl(fd, TIOCMGET, &uart_status) == -1) {
42         perror("ioctl (TIOCMGET)");
43         return -1;
44     }
45
46     if (level) {
47         uart_status |= TIOCM_RTS;
48     } else {
49         uart_status &= ~TIOCM_RTS;
50     }
51
52     if (ioctl(fd, TIOCMSET, &uart_status) == -1) {
53         perror("TIOCMSET");
54         return -1;
55     }
56
57     fprintf(stderr, "set rts %d\n", level);
58
59     return 0;
60 }
61
62 static void set_baud_rate(int fd, int br) //TODO add some checking
63 {
64     struct termios options;
65
66     tcgetattr(fd, &options);
67     cfsetispeed(&options, br);
68     cfsetospeed(&options, br);
69     tcsetattr(fd, TCSANOW, &options);
70 }
71
72 static int tty_open(const char *port, int baud_rate)
73 {
74
75     int fd = open(port, O_RDONLY | O_NOCTTY);
76     if (fd < 0) {
77         perror("open");
78         return fd;
79     } else {
80         fprintf(stderr, "opened %s as %d\n", port, fd);
81     }
82
83     set_nonblock(fd);
84     set_rts(fd, 0);
85     set_baud_rate(fd, baud_rate);
86     usleep(1200000); //value by d-logic
87     tcflush(fd, TCIFLUSH);
88
89     return fd;
90 }
91
92 // really simple JSON helpers
93 #define JSON_START()   dprintf(fd,"{")
94 #define JSON_NUM(NAME) dprintf(fd,"\"" #NAME "\": %d", NAME)
95 #define JSON_NEXT()    dprintf(fd,",")
96 #define JSON_STR(NAME) dprintf(fd,"\"" #NAME "\": \"%s\"", NAME)
97 #define JSON_END()     dprintf(fd,"}\n")
98
99 // print complete json
100 #define JSON_PRINT() do { \
101     JSON_START();         \
102     JSON_STR(type);       \
103     JSON_NEXT();          \
104     JSON_NUM(card_type);  \
105     JSON_NEXT();          \
106     JSON_NUM(sak);        \
107     JSON_NEXT();          \
108     JSON_NUM(size);       \
109     JSON_NEXT();          \
110     JSON_STR(uid);        \
111     JSON_END();           \
112 } while (0)
113
114 static void ufr_read(char *uid, int fd)
115 {
116     static char *type = "rfid";
117
118     UFR_STATUS status;
119     uint8_t card_type;
120     uint8_t sak;           //select acknowledge
121     uint8_t uid_bytes[10]; //uid as bytes
122     uint8_t size;
123
124     status = GetDlogicCardType(&card_type);
125     if (status != UFR_OK) {
126         fprintf(stderr, "GetDlogicCardType: %s\n", UFR_Status2String(status));
127         return;
128     }
129
130     status = GetCardIdEx(&sak, uid_bytes, &size);
131     if (status != UFR_OK) {
132         fprintf(stderr, "GetCardIdEx: %s\n", UFR_Status2String(status));
133         return;
134     }
135
136     JSON_PRINT();
137
138 #ifdef UFR_BEEP
139     ReaderUISignal(0, 1); // no light, one beep
140 #endif
141 }
142
143 static void ufr_cb(EV_P_ ev_io *w_, int revents)
144 {
145     ev_io_ufr *w = (ev_io_ufr *)w_;
146     char uid;
147     read(w->w.fd, &uid, 1);
148     *(w->uid++) = uid;
149
150     if (uid == ASYNC_SUFFIX) {
151         //*(w->uid - 1) = 0; // no need if ASYNC_SUFFIX is 0
152         w->uid = w->uid_data;
153         ufr_read(w->uid, w->fd);
154     }
155 }
156
157 static int ufr_open(unsigned reader_type, char *port_name,
158                     unsigned port_interface)
159 {
160     UFR_STATUS status;
161
162     fprintf(stderr, "uFCoder version: %s\n", GetDllVersionStr());
163
164     status = ReaderOpenEx(reader_type, port_name, port_interface, 0);
165     if (status != UFR_OK) {
166         fprintf(stderr, "ReaderOpenEx: %s\n", UFR_Status2String(status));
167         return -1;
168     }
169
170     fprintf(stderr, "%s\n", GetReaderDescription());
171
172     status = SetAsyncCardIdSendConfig(
173                  1,            //enable send
174                  0,            //disable prefix
175                  0,            //prefix
176                  ASYNC_SUFFIX, //suffix
177                  0,            //disable send removed
178                  ASYNC_BAUD_RATE
179              );
180     fprintf(stderr, "SetAsyncCardIdSendConfig: %s\n", UFR_Status2String(status));
181     if (status != UFR_OK) {
182         return -1;
183     }
184
185     return 0;
186 }
187
188 int mt_rfid_init(mt_rfid_t *self, struct ev_loop *loop, int fd)
189 {
190     if (ufr_open(READER_TYPE, PORT_NAME, PORT_INTERFACE) == -1) {
191         return -1;
192     }
193
194     int tty = tty_open(PORT_NAME, CONCAT(B, ASYNC_BAUD_RATE));
195     if (tty < 0) {
196         return -2;
197     }
198     self->fd = tty;
199
200     ev_io_ufr *w = &(self->w);
201     w->uid = w->uid_data;
202     w->fd = fd;
203     ev_io_init(&(w->w), ufr_cb, tty, EV_READ);
204     ev_io_start(loop, (ev_io *)w);
205
206     return 0;
207 }
208
209 void mt_rfid_deinit(mt_rfid_t *self)
210 {
211     if (close(self->fd) == 0) {
212         fprintf(stderr, "closed %d\n", self->fd);
213     } else {
214         perror("close");
215     }
216
217     UFR_STATUS status = ReaderClose();
218     fprintf(stderr, "ReaderClose: %s\n", UFR_Status2String(status));
219 }
220
221 #ifdef IS_MAIN
222 int main(int argc, char **argv)
223 {
224     struct ev_loop *loop = EV_DEFAULT;
225     mt_rfid_t rfid;
226
227     set_signal_exit(loop);
228
229     if (mt_rfid_init(&rfid, loop, STDOUT_FILENO) != 0) {
230         return -1;
231     }
232
233     ev_run(loop, 0);
234
235     mt_rfid_deinit(&rfid);
236
237     return 0;
238 }
239 #endif
240
241 /* other tty options
242     //Enable the receiver and set local mode...
243     options.c_cflag |= (CLOCAL | CREAD);
244
245     options.c_cflag &= ~PARENB;
246     options.c_cflag &= ~CSTOPB;
247     options.c_cflag &= ~CSIZE;
248     options.c_cflag |= CS8;
249     options.c_cflag &= ~CRTSCTS;
250     options.c_lflag |= (ICANON | ECHO | ECHOE);
251     //Disable XON/XOFF both i/p and o/p
252     options.c_iflag &= ~(IXON | IXOFF | IXANY);
253     //Non Cannon mode
254     options.c_iflag &= ~(ICANON | ECHO | ECHOE | ISIG);
255
256     options.c_oflag |= OPOST;
257 */
258