]> rtime.felk.cvut.cz Git - coffee/mt-apps.git/blob - mt_sim.c
mt_server: Use "%s" format string in syslog()
[coffee/mt-apps.git] / mt_sim.c
1 #include <unistd.h>
2 #include <termios.h>
3
4 #include "mt_sim.h"
5 #include "json_helpers.h"
6
7 static ev_io w;
8 static int pipefd;
9 static struct termios saved_term;
10
11 static void stdin_cb(EV_P_ ev_io *w, int revents)
12 {
13     char ch;
14
15     read(w->fd, &ch, sizeof(ch));
16
17     char uid[15];
18
19     switch (ch) {
20     case 'A' ... 'F':
21         keys_json_print(pipefd, ch);
22         break;
23     case 'a' ... 'f':
24         keys_json_print(pipefd, ch + 'A' - 'a');
25         break;
26     case '0' ... '9':
27         memset(uid, 0, sizeof(uid));
28         for (int i = 0; i < 14; i++)
29             uid[i] = ch;
30         uid[13] = 'A';
31         rfid_json_print(pipefd, 42, 32, uid, (strlen(uid)+1)/2);
32         break;
33     }
34 }
35
36 int mt_sim_init(struct ev_loop *loop, int fd)
37 {
38     pipefd = fd;
39
40     if (!isatty(STDIN_FILENO)) {
41         fprintf(stderr, "stdin is not a terminal\n");
42         return -1;
43     }
44
45     struct termios term;
46     tcgetattr(STDIN_FILENO, &term);
47     saved_term = term;
48     term.c_lflag &= ~ICANON;
49     tcsetattr(STDIN_FILENO, TCSADRAIN, &term);
50
51     ev_io_init(&w, stdin_cb, STDIN_FILENO, EV_READ);
52     ev_io_start(loop, &w);
53
54     return 0;
55 }
56
57 void mt_sim_deinit(void)
58 {
59     tcsetattr(STDIN_FILENO, TCSADRAIN, &saved_term);
60 }