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