11 #include "signal_exit.h"
12 #include "json_helpers.h"
14 // shit to avoid constant repetition
15 #define CONCAT_AGAIN(A,B) A ## B
16 #define CONCAT(A,B) CONCAT_AGAIN(A,B)
18 static int set_nonblock(int fd)
20 int flags = fcntl(fd, F_GETFL, 0);
22 perror("fcntl (get)");
25 if (fcntl(fd, F_SETFL, flags | O_NONBLOCK) == -1) {
26 perror("fcntl (set)");
29 fprintf(stderr, "set %d non-blocking\n", fd);
33 static int set_rts(int fd, int level)
37 if (ioctl(fd, TIOCMGET, &uart_status) == -1) {
38 perror("ioctl (TIOCMGET)");
43 uart_status |= TIOCM_RTS;
45 uart_status &= ~TIOCM_RTS;
48 if (ioctl(fd, TIOCMSET, &uart_status) == -1) {
53 fprintf(stderr, "set %d rts %d\n", fd, level);
58 static void set_baud_rate(int fd, int br) //TODO add some checking
60 struct termios options;
62 tcgetattr(fd, &options);
63 cfsetispeed(&options, br);
64 cfsetospeed(&options, br);
65 tcsetattr(fd, TCSANOW, &options);
68 static int tty_open(const char *port, int br)
71 int fd = open(port, O_RDONLY | O_NOCTTY);
76 fprintf(stderr, "opened %s as %d\n", port, fd);
80 set_rts(fd, 0); //disable
81 set_baud_rate(fd, br);
82 usleep(1200000); //value by d-logic
83 tcflush(fd, TCIFLUSH);
88 // print complete json
89 #define RFID_JSON_PRINT() do { \
93 JSON_NUM(card_type); \
103 static void ufr_read(char *uid, int fd)
105 static char *type = "rfid";
109 uint8_t sak; //select acknowledge
110 uint8_t uid_bytes[10]; //uid as bytes
113 status = GetDlogicCardType(&card_type);
114 if (status != UFR_OK) {
115 fprintf(stderr, "GetDlogicCardType: %s\n", UFR_Status2String(status));
119 status = GetCardIdEx(&sak, uid_bytes, &size);
120 if (status != UFR_OK) {
121 fprintf(stderr, "GetCardIdEx: %s\n", UFR_Status2String(status));
128 ReaderUISignal(0, 1); // no light, one beep
132 #define UFR_ASYNC_SUFFIX 0 // keep it zero: separates uids, terminates strings
134 static void ufr_cb(EV_P_ ev_io *w_, int revents)
136 ev_io_ufr *w = (ev_io_ufr *)w_;
139 read(w->w.fd, &uid, 1);
142 if (uid == UFR_ASYNC_SUFFIX) {
143 //*(w->uid - 1) = 0; // no need if UFR_ASYNC_SUFFIX is 0
144 w->uid = w->uid_data;
145 ufr_read(w->uid, w->fd);
149 static int ufr_open(unsigned reader_type, char *port_name,
150 unsigned port_interface)
154 fprintf(stderr, "uFCoder version: %s\n", GetDllVersionStr());
156 status = ReaderOpenEx(reader_type, port_name, port_interface, 0);
157 if (status != UFR_OK) {
158 fprintf(stderr, "ReaderOpenEx: %s\n", UFR_Status2String(status));
162 fprintf(stderr, "%s\n", GetReaderDescription());
164 status = SetAsyncCardIdSendConfig(
168 UFR_ASYNC_SUFFIX, //suffix
169 0, //disable send removed
172 fprintf(stderr, "SetAsyncCardIdSendConfig: %s\n", UFR_Status2String(status));
173 if (status != UFR_OK) {
180 int mt_rfid_init(mt_rfid_t *self, struct ev_loop *loop, int fd)
182 if (ufr_open(UFR_READER_TYPE, UFR_PORT_NAME, UFR_PORT_INTERFACE) == -1) {
186 int tty = tty_open(UFR_PORT_NAME, CONCAT(B, UFR_ASYNC_BAUD_RATE));
192 ev_io_ufr *w = &(self->w);
193 w->uid = w->uid_data;
195 ev_io_init(&(w->w), ufr_cb, tty, EV_READ);
196 ev_io_start(loop, (ev_io *)w);
201 void mt_rfid_deinit(mt_rfid_t *self)
203 if (close(self->fd) == 0) {
204 fprintf(stderr, "closed %d\n", self->fd);
209 UFR_STATUS status = ReaderClose();
210 fprintf(stderr, "ReaderClose: %s\n", UFR_Status2String(status));
214 int main(int argc, char **argv)
216 struct ev_loop *loop = EV_DEFAULT;
219 set_signal_exit(loop);
221 if (mt_rfid_init(&rfid, loop, STDOUT_FILENO) != 0) {
227 mt_rfid_deinit(&rfid);
228 ev_loop_destroy(loop);