5 // shit to avoid constant repetition
6 #define CONCAT_AGAIN(A,B) A ## B
7 #define CONCAT(A,B) CONCAT_AGAIN(A,B)
14 #include <sys/ioctl.h>
19 #include "ev_signal_exit.h"
21 static int set_nonblock(int fd)
23 int flags = fcntl(fd, F_GETFL, 0);
25 perror("fcntl (get)");
28 if (fcntl(fd, F_SETFL, flags | O_NONBLOCK) == -1) {
29 perror("fcntl (set)");
32 fprintf(stderr, "set non-blocking\n");
36 static int set_rts(int fd, int level)
40 if (ioctl(fd, TIOCMGET, &uart_status) == -1) {
41 perror("ioctl (TIOCMGET)");
46 uart_status |= TIOCM_RTS;
48 uart_status &= ~TIOCM_RTS;
51 if (ioctl(fd, TIOCMSET, &uart_status) == -1) {
56 fprintf(stderr, "set rts %d\n", level);
61 static void set_baud_rate(int fd, int br) //TODO add some checking
63 struct termios options;
65 tcgetattr(fd, &options);
66 cfsetispeed(&options, br);
67 cfsetospeed(&options, br);
68 tcsetattr(fd, TCSANOW, &options);
71 static int tty_open(const char *port, int baud_rate)
74 int fd = open(port, O_RDONLY | O_NOCTTY);
79 fprintf(stderr, "opened %s as %d\n", port, fd);
84 set_baud_rate(fd, baud_rate);
85 usleep(1200000); //value by d-logic
86 tcflush(fd, TCIFLUSH);
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")
98 // print complete json
99 #define JSON_PRINT() do { \
103 JSON_NUM(card_type); \
113 static void ufr_read(char *uid, int fd)
115 static char *type = "rfid";
119 uint8_t sak; //select acknowledge
120 uint8_t uid_bytes[10]; //uid as bytes
123 status = GetDlogicCardType(&card_type);
124 if (status != UFR_OK) {
125 fprintf(stderr, "GetDlogicCardType: %s\n", UFR_Status2String(status));
129 status = GetCardIdEx(&sak, uid_bytes, &size);
130 if (status != UFR_OK) {
131 fprintf(stderr, "GetCardIdEx: %s\n", UFR_Status2String(status));
138 ReaderUISignal(0, 1); // no light, one beep
142 static void ufr_cb(EV_P_ ev_io *w_, int revents)
144 ev_io_ufr *w = (ev_io_ufr *)w_;
146 read(w->w.fd, &uid, 1);
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);
156 static int ufr_open(unsigned reader_type, char *port_name,
157 unsigned port_interface)
161 fprintf(stderr, "uFCoder version: %s\n", GetDllVersionStr());
163 status = ReaderOpenEx(reader_type, port_name, port_interface, 0);
164 if (status != UFR_OK) {
165 fprintf(stderr, "ReaderOpenEx: %s\n", UFR_Status2String(status));
169 fprintf(stderr, "%s\n", GetReaderDescription());
171 status = SetAsyncCardIdSendConfig(
175 ASYNC_SUFFIX, //suffix
176 0, //disable send removed
179 fprintf(stderr, "SetAsyncCardIdSendConfig: %s\n", UFR_Status2String(status));
180 if (status != UFR_OK) {
187 int mt_rfid_init(mt_rfid_t *self, struct ev_loop *loop, int fd)
189 if (ufr_open(READER_TYPE, PORT_NAME, PORT_INTERFACE) == -1) {
193 int tty = tty_open(PORT_NAME, CONCAT(B, ASYNC_BAUD_RATE));
199 ev_io_ufr *w = &(self->w);
200 w->uid = w->uid_data;
202 ev_io_init(&(w->w), ufr_cb, tty, EV_READ);
203 ev_io_start(loop, (ev_io *)w);
208 void mt_rfid_deinit(mt_rfid_t *self)
210 if (close(self->fd) == 0) {
211 fprintf(stderr, "closed %d\n", self->fd);
216 UFR_STATUS status = ReaderClose();
217 fprintf(stderr, "ReaderClose: %s\n", UFR_Status2String(status));
221 int main(int argc, char **argv)
223 struct ev_loop *loop = EV_DEFAULT;
226 set_signal_exit(loop);
228 if (mt_rfid_init(&rfid, loop, STDOUT_FILENO) != 0) {
234 mt_rfid_deinit(&rfid);
241 //Enable the receiver and set local mode...
242 options.c_cflag |= (CLOCAL | CREAD);
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);
253 options.c_iflag &= ~(ICANON | ECHO | ECHOE | ISIG);
255 options.c_oflag |= OPOST;