]> rtime.felk.cvut.cz Git - coffee/mt-apps.git/blobdiff - mt_sim.c
Compile our version of libwebsockets if not found in the system
[coffee/mt-apps.git] / mt_sim.c
diff --git a/mt_sim.c b/mt_sim.c
new file mode 100644 (file)
index 0000000..081a11e
--- /dev/null
+++ b/mt_sim.c
@@ -0,0 +1,58 @@
+#include <unistd.h>
+#include <termios.h>
+
+#include "mt_sim.h"
+#include "json_helpers.h"
+
+static ev_io w;
+static int pipefd;
+static struct termios saved_term;
+
+static void stdin_cb(EV_P_ ev_io *w, int revents)
+{
+    char ch;
+
+    read(w->fd, &ch, sizeof(ch));
+
+    char uid[15];
+
+    switch (ch) {
+    case 'A' ... 'F':
+       keys_json_print(pipefd, ch);
+       break;
+    case 'a' ... 'f':
+       keys_json_print(pipefd, ch + 'A' - 'a');
+       break;
+    case '0' ... '9':
+       strcpy(uid, "?ABC0123456789");
+       uid[0] = ch;
+       rfid_json_print(pipefd, 42, 32, uid, (strlen(uid)+1)/2);
+       break;
+    }
+}
+
+int mt_sim_init(struct ev_loop *loop, int fd)
+{
+    pipefd = fd;
+
+    if (!isatty(STDIN_FILENO)) {
+       fprintf(stderr, "stdin is not a terminal\n");
+       return -1;
+    }
+
+    struct termios term;
+    tcgetattr(STDIN_FILENO, &term);
+    saved_term = term;
+    term.c_lflag &= ~ICANON;
+    tcsetattr(STDIN_FILENO, TCSADRAIN, &term);
+
+    ev_io_init(&w, stdin_cb, STDIN_FILENO, EV_READ);
+    ev_io_start(loop, &w);
+
+    return 0;
+}
+
+void mt_sim_deinit(void)
+{
+    tcsetattr(STDIN_FILENO, TCSADRAIN, &saved_term);
+}