3 * poll the uFR reader and print JSON formatted card info to stdout.
6 // shit to avoid constant repetition
7 #define CONCAT_AGAIN(A,B) A ## B
8 #define CONCAT(A,B) CONCAT_AGAIN(A,B)
15 #include <sys/ioctl.h>
20 #include "ev_signal_exit.h"
22 static int set_nonblock(int fd)
24 int flags = fcntl(fd, F_GETFL, 0);
26 perror("fcntl (get)");
29 if (fcntl(fd, F_SETFL, flags | O_NONBLOCK) == -1) {
30 perror("fcntl (set)");
33 fprintf(stderr, "set non-blocking\n");
37 static int set_rts(int fd, int level)
41 if (ioctl(fd, TIOCMGET, &uart_status) == -1) {
42 perror("ioctl (TIOCMGET)");
47 uart_status |= TIOCM_RTS;
49 uart_status &= ~TIOCM_RTS;
52 if (ioctl(fd, TIOCMSET, &uart_status) == -1) {
57 fprintf(stderr, "set rts %d\n", level);
62 static void set_baud_rate(int fd, int br) //TODO add some checking
64 struct termios options;
66 tcgetattr(fd, &options);
67 cfsetispeed(&options, br);
68 cfsetospeed(&options, br);
69 tcsetattr(fd, TCSANOW, &options);
72 static int tty_open(const char *port, int baud_rate)
75 int fd = open(port, O_RDONLY | O_NOCTTY);
80 fprintf(stderr, "opened %s as %d\n", port, fd);
85 set_baud_rate(fd, baud_rate);
86 usleep(1200000); //value by d-logic
87 tcflush(fd, TCIFLUSH);
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")
99 // print complete json
100 #define JSON_PRINT() do { \
104 JSON_NUM(card_type); \
114 static void ufr_read(char *uid, int fd)
116 static char *type = "rfid";
120 uint8_t sak; //select acknowledge
121 uint8_t uid_bytes[10]; //uid as bytes
124 status = GetDlogicCardType(&card_type);
125 if (status != UFR_OK) {
126 fprintf(stderr, "GetDlogicCardType: %s\n", UFR_Status2String(status));
130 status = GetCardIdEx(&sak, uid_bytes, &size);
131 if (status != UFR_OK) {
132 fprintf(stderr, "GetCardIdEx: %s\n", UFR_Status2String(status));
139 ReaderUISignal(0, 1); // no light, one beep
143 static void ufr_cb(EV_P_ ev_io *w_, int revents)
145 ev_io_ufr *w = (ev_io_ufr *)w_;
147 read(w->w.fd, &uid, 1);
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);
157 static int ufr_open(unsigned reader_type, char *port_name,
158 unsigned port_interface)
162 fprintf(stderr, "uFCoder version: %s\n", GetDllVersionStr());
164 status = ReaderOpenEx(reader_type, port_name, port_interface, 0);
165 if (status != UFR_OK) {
166 fprintf(stderr, "ReaderOpenEx: %s\n", UFR_Status2String(status));
170 fprintf(stderr, "%s\n", GetReaderDescription());
172 status = SetAsyncCardIdSendConfig(
176 ASYNC_SUFFIX, //suffix
177 0, //disable send removed
180 fprintf(stderr, "SetAsyncCardIdSendConfig: %s\n", UFR_Status2String(status));
181 if (status != UFR_OK) {
188 int mt_rfid_init(mt_rfid_t *self, struct ev_loop *loop, int fd)
190 if (ufr_open(READER_TYPE, PORT_NAME, PORT_INTERFACE) == -1) {
194 int tty = tty_open(PORT_NAME, CONCAT(B, ASYNC_BAUD_RATE));
200 ev_io_ufr *w = &(self->w);
201 w->uid = w->uid_data;
203 ev_io_init(&(w->w), ufr_cb, tty, EV_READ);
204 ev_io_start(loop, (ev_io *)w);
209 void mt_rfid_deinit(mt_rfid_t *self)
211 if (close(self->fd) == 0) {
212 fprintf(stderr, "closed %d\n", self->fd);
217 UFR_STATUS status = ReaderClose();
218 fprintf(stderr, "ReaderClose: %s\n", UFR_Status2String(status));
222 int main(int argc, char **argv)
224 struct ev_loop *loop = EV_DEFAULT;
227 set_signal_exit(loop);
229 if (mt_rfid_init(&rfid, loop, STDOUT_FILENO) != 0) {
235 mt_rfid_deinit(&rfid);
242 //Enable the receiver and set local mode...
243 options.c_cflag |= (CLOCAL | CREAD);
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);
254 options.c_iflag &= ~(ICANON | ECHO | ECHOE | ISIG);
256 options.c_oflag |= OPOST;