]> rtime.felk.cvut.cz Git - coffee/mt-apps.git/commitdiff
Compile our version of libwebsockets if not found in the system
authorMichal Sojka <michal.sojka@cvut.cz>
Wed, 8 Aug 2018 13:34:29 +0000 (15:34 +0200)
committerMichal Sojka <michal.sojka@cvut.cz>
Wed, 8 Aug 2018 13:34:29 +0000 (15:34 +0200)
This is mainly for simulator purpuses.

.gitmodules [new file with mode: 0644]
Makefile
libwebsockets [new submodule]
mt_sim.c [new file with mode: 0644]
mt_sim.h [new file with mode: 0644]

diff --git a/.gitmodules b/.gitmodules
new file mode 100644 (file)
index 0000000..56c12ef
--- /dev/null
@@ -0,0 +1,3 @@
+[submodule "libwebsockets"]
+       path = libwebsockets
+       url = https://github.com/warmcat/libwebsockets.git
index 37068568d17fbac3824d3fadd07ac63792b7e7a7..7207748741b389af6dc4df7177f2059e84eb4a21 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -1,6 +1,5 @@
 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>)
 
@@ -39,7 +38,23 @@ all: mtserver mtkeys mtaio
 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)
diff --git a/libwebsockets b/libwebsockets
new file mode 160000 (submodule)
index 0000000..6f13ccf
--- /dev/null
@@ -0,0 +1 @@
+Subproject commit 6f13ccf7c376b66cd7254aecb51ac162a58e274f
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);
+}
diff --git a/mt_sim.h b/mt_sim.h
new file mode 100644 (file)
index 0000000..24df511
--- /dev/null
+++ b/mt_sim.h
@@ -0,0 +1,9 @@
+#ifndef MT_SIM_H
+#define MT_SIM_H
+
+#include <ev.h>
+
+int mt_sim_init(struct ev_loop *loop, int fd);
+void mt_sim_deinit(void);
+
+#endif