OUTPUT_DIR = build
-CFLAGS = -Ilibwebsockets/include -Llibwebsockets/lib
can_compile = $(shell if echo '$(1)' | $(CC) $(CFLAGS) -c -xc - -o /dev/null >/dev/null 2>&1; then echo yes; fi)
HAVE_RFID := $(call can_compile,\#include <uFCoder.h>)
clean:
rm -rf $(OUTPUT_DIR)
+# Check LWS version and compile our own if not found
+ifeq ($(shell pkg-config libwebsockets = 2.2.1 || echo no),no)
+libwebsockets/CMakeLists.txt:
+ git submodupe update --init libwebsockets
+
+libwebsockets/CMakeCache.txt: libwebsockets/CMakeLists.txt
+ cd libwebsockets && cmake -DLWS_WITH_LIBEV=ON -DLWS_WITH_SHARED=OFF .
+
+libwebsockets/lib/libwebsockets.a: libwebsockets/CMakeCache.txt
+ cd libwebsockets && cmake --build .
+
+mt_server.c: libwebsockets/lib/libwebsockets.a
+
+CFLAGS += -Ilibwebsockets/include -Llibwebsockets/lib -g -lz $(shell pkg-config --libs openssl)
+endif
+
.SECONDEXPANSION:
-mt%: $$($$@_SRCS)
+mtserver mtkeys mtaio mtrfid: $$($$@_SRCS)
mkdir -p $(OUTPUT_DIR)
$(CC) $(CFLAGS) $($@_DEFS) -o $(OUTPUT_DIR)/$@ $^ $($@_LIBS)
--- /dev/null
+#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);
+}