]> rtime.felk.cvut.cz Git - coffee/mt-apps.git/commitdiff
key
authorJiří Matěják <jiri.matejak@fel.cvut.cz>
Thu, 3 May 2018 18:01:58 +0000 (20:01 +0200)
committerJiří Matěják <jiri.matejak@fel.cvut.cz>
Thu, 3 May 2018 18:01:58 +0000 (20:01 +0200)
Makefile
html/index.html
json_helpers.h [new file with mode: 0644]
mt_aio.c
mt_gpio.c [deleted file]
mt_gpio.h [deleted file]
mt_keys.c [new file with mode: 0644]
mt_keys.h [new file with mode: 0644]
mt_rfid.c
mt_server.c

index d19b0944f20ba704add53777b042a53509af19a3..470e9a1d6b490c2b39d8aef5c1e758922c3e4452 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -6,14 +6,14 @@ mtrfid_LIBS   = -lev -luFCoder-armhf
 mtserver_SRCS = signal_exit.c mt_server.c
 mtserver_LIBS = -lev -lwebsockets
 
-mtgpio_SRCS   = signal_exit.c mt_gpio.c
-mtgpio_LIBS   = -lev
+mtkeys_SRCS   = signal_exit.c mt_keys.c
+mtkeys_LIBS   = -lev
 
-mtaio_SRCS    = signal_exit.c mt_rfid.c  mt_server.c mt_aio.c
+mtaio_SRCS    = signal_exit.c mt_rfid.c mt_keys.c mt_server.c mt_aio.c
 mtaio_LIBS    = -lev -luFCoder-armhf -lwebsockets
 mtaio_DEFS    = -DNO_MAIN
 
-all: mtrfid mtserver mtgpio mtaio
+all: mtrfid mtserver mtkeys mtaio
 
 .PHONY: clean
 
index 399938d285e72fee8358b42c6dec151b3cdfd14d..f32a14c876abe2838607d6661881858def36d58e 100644 (file)
                     "sak: " + msg.sak
                 );
                 break;
+            case "keys":
+                update("text",
+                    "key: " + msg.key
+                );
         }
     }
 
diff --git a/json_helpers.h b/json_helpers.h
new file mode 100644 (file)
index 0000000..f064f36
--- /dev/null
@@ -0,0 +1,12 @@
+#ifndef JSON_HELPERS_H
+#define JSON_HELPERS_H
+
+// really simple JSON helpers
+#define JSON_START()    dprintf(fd,"{")
+#define JSON_NUM(NAME)  dprintf(fd,"\"" #NAME "\":%d", NAME) //see the int?
+#define JSON_NEXT()     dprintf(fd,",")
+#define JSON_STR(NAME)  dprintf(fd,"\"" #NAME "\":\"%s\"", NAME)
+#define JSON_CHAR(NAME) dprintf(fd,"\"" #NAME "\":\"%c\"", NAME)
+#define JSON_END()      dprintf(fd,"}\n")
+
+#endif
index b6805a9bee795bf8547a1b8be9214e86344e4d09..e01de28843e3d582b2b1d02977f117bd70134b87 100644 (file)
--- a/mt_aio.c
+++ b/mt_aio.c
@@ -1,5 +1,6 @@
 // merica terminal all in one
 #include "mt_rfid.h"
+#include "mt_keys.h"
 #include "mt_server.h"
 #include "signal_exit.h"
 
@@ -8,6 +9,7 @@ int main(int argc, char **argv)
     struct ev_loop *loop = EV_DEFAULT;
     int pipefd[2]; // read <- write
     mt_rfid_t rfid;
+    mt_keys_t keys;
     mt_server_t server;
 
     if (pipe(pipefd) == -1) {
@@ -21,6 +23,10 @@ int main(int argc, char **argv)
         return -1;
     }
 
+    if (mt_keys_init(&keys, loop, pipefd[1]) != 0) {
+        return -1;
+    }
+
     if (mt_server_init(&server, loop, pipefd[0]) != 0) {
         return -2;
     }
@@ -28,6 +34,7 @@ int main(int argc, char **argv)
     ev_run(loop, 0);
 
     mt_server_deinit(&server);
+    mt_keys_deinit(&keys);
     mt_rfid_deinit(&rfid);
 
     return 0;
diff --git a/mt_gpio.c b/mt_gpio.c
deleted file mode 100644 (file)
index 3176d77..0000000
--- a/mt_gpio.c
+++ /dev/null
@@ -1,237 +0,0 @@
-#include <stdio.h>
-#include <stdlib.h>
-#include <unistd.h>
-#include <fcntl.h>
-#include <termios.h>
-#include <sys/ioctl.h>
-
-#include <uFCoder.h>
-
-#include "mt_rfid.h"
-#include "signal_exit.h"
-
-// shit to avoid constant repetition
-#define CONCAT_AGAIN(A,B) A ## B
-#define CONCAT(A,B) CONCAT_AGAIN(A,B)
-
-static int set_nonblock(int fd)
-{
-    int flags = fcntl(fd, F_GETFL, 0);
-    if (flags == -1) {
-        perror("fcntl (get)");
-        return -1;
-    }
-    if (fcntl(fd, F_SETFL, flags | O_NONBLOCK) == -1) {
-        perror("fcntl (set)");
-        return -1;
-    }
-    fprintf(stderr, "set %d non-blocking\n", fd);
-    return 0;
-}
-
-static int set_rts(int fd, int level)
-{
-    int uart_status;
-
-    if (ioctl(fd, TIOCMGET, &uart_status) == -1) {
-        perror("ioctl (TIOCMGET)");
-        return -1;
-    }
-
-    if (level) {
-        uart_status |= TIOCM_RTS;
-    } else {
-        uart_status &= ~TIOCM_RTS;
-    }
-
-    if (ioctl(fd, TIOCMSET, &uart_status) == -1) {
-        perror("TIOCMSET");
-        return -1;
-    }
-
-    fprintf(stderr, "set %d rts %d\n", fd, level);
-
-    return 0;
-}
-
-static void set_baud_rate(int fd, int br) //TODO add some checking
-{
-    struct termios options;
-
-    tcgetattr(fd, &options);
-    cfsetispeed(&options, br);
-    cfsetospeed(&options, br);
-    tcsetattr(fd, TCSANOW, &options);
-}
-
-static int tty_open(const char *port, int br)
-{
-
-    int fd = open(port, O_RDONLY | O_NOCTTY);
-    if (fd < 0) {
-        perror("open");
-        return fd;
-    } else {
-        fprintf(stderr, "opened %s as %d\n", port, fd);
-    }
-
-    set_nonblock(fd);
-    set_rts(fd, 0);        //disable
-    set_baud_rate(fd, br);
-    usleep(1200000);       //value by d-logic
-    tcflush(fd, TCIFLUSH);
-
-    return fd;
-}
-
-// really simple JSON helpers
-#define JSON_START()   dprintf(fd,"{")
-#define JSON_NUM(NAME) dprintf(fd,"\"" #NAME "\":%d", NAME) //see the int?
-#define JSON_NEXT()    dprintf(fd,",")
-#define JSON_STR(NAME) dprintf(fd,"\"" #NAME "\":\"%s\"", NAME)
-#define JSON_END()     dprintf(fd,"}\n")
-
-// print complete json
-#define JSON_PRINT() do { \
-    JSON_START();         \
-    JSON_STR(type);       \
-    JSON_NEXT();          \
-    JSON_NUM(card_type);  \
-    JSON_NEXT();          \
-    JSON_NUM(sak);        \
-    JSON_NEXT();          \
-    JSON_NUM(size);       \
-    JSON_NEXT();          \
-    JSON_STR(uid);        \
-    JSON_END();           \
-} while (0)
-
-static void ufr_read(char *uid, int fd)
-{
-    static char *type = "rfid";
-
-    UFR_STATUS status;
-    uint8_t card_type;
-    uint8_t sak;           //select acknowledge
-    uint8_t uid_bytes[10]; //uid as bytes
-    uint8_t size;
-
-    status = GetDlogicCardType(&card_type);
-    if (status != UFR_OK) {
-        fprintf(stderr, "GetDlogicCardType: %s\n", UFR_Status2String(status));
-        return;
-    }
-
-    status = GetCardIdEx(&sak, uid_bytes, &size);
-    if (status != UFR_OK) {
-        fprintf(stderr, "GetCardIdEx: %s\n", UFR_Status2String(status));
-        return;
-    }
-
-    JSON_PRINT();
-
-#ifdef UFR_BEEP
-    ReaderUISignal(0, 1); // no light, one beep
-#endif
-}
-
-#define UFR_ASYNC_SUFFIX 0 // keep it zero: separates uids, terminates strings
-
-static void ufr_cb(EV_P_ ev_io *w_, int revents)
-{
-    ev_io_ufr *w = (ev_io_ufr *)w_;
-    char uid;
-
-    read(w->w.fd, &uid, 1);
-    *(w->uid++) = uid;
-
-    if (uid == UFR_ASYNC_SUFFIX) {
-        //*(w->uid - 1) = 0; // no need if UFR_ASYNC_SUFFIX is 0
-        w->uid = w->uid_data;
-        ufr_read(w->uid, w->fd);
-    }
-}
-
-static int ufr_open(unsigned reader_type, char *port_name,
-                    unsigned port_interface)
-{
-    UFR_STATUS status;
-
-    fprintf(stderr, "uFCoder version: %s\n", GetDllVersionStr());
-
-    status = ReaderOpenEx(reader_type, port_name, port_interface, 0);
-    if (status != UFR_OK) {
-        fprintf(stderr, "ReaderOpenEx: %s\n", UFR_Status2String(status));
-        return -1;
-    }
-
-    fprintf(stderr, "%s\n", GetReaderDescription());
-
-    status = SetAsyncCardIdSendConfig(
-                 1,                  //enable send
-                 0,                  //disable prefix
-                 0,                  //prefix
-                 UFR_ASYNC_SUFFIX,   //suffix
-                 0,                  //disable send removed
-                 UFR_ASYNC_BAUD_RATE
-             );
-    fprintf(stderr, "SetAsyncCardIdSendConfig: %s\n", UFR_Status2String(status));
-    if (status != UFR_OK) {
-        return -1;
-    }
-
-    return 0;
-}
-
-int mt_rfid_init(mt_rfid_t *self, struct ev_loop *loop, int fd)
-{
-    if (ufr_open(UFR_READER_TYPE, UFR_PORT_NAME, UFR_PORT_INTERFACE) == -1) {
-        return -1;
-    }
-
-    int tty = tty_open(UFR_PORT_NAME, CONCAT(B, UFR_ASYNC_BAUD_RATE));
-    if (tty < 0) {
-        return -2;
-    }
-    self->fd = tty;
-
-    ev_io_ufr *w = &(self->w);
-    w->uid = w->uid_data;
-    w->fd = fd;
-    ev_io_init(&(w->w), ufr_cb, tty, EV_READ);
-    ev_io_start(loop, (ev_io *)w);
-
-    return 0;
-}
-
-void mt_rfid_deinit(mt_rfid_t *self)
-{
-    if (close(self->fd) == 0) {
-        fprintf(stderr, "closed %d\n", self->fd);
-    } else {
-        perror("close");
-    }
-
-    UFR_STATUS status = ReaderClose();
-    fprintf(stderr, "ReaderClose: %s\n", UFR_Status2String(status));
-}
-
-#ifndef NO_MAIN
-int main(int argc, char **argv)
-{
-    struct ev_loop *loop = EV_DEFAULT;
-    mt_rfid_t rfid;
-
-    set_signal_exit(loop);
-
-    if (mt_rfid_init(&rfid, loop, STDOUT_FILENO) != 0) {
-        return -1;
-    }
-
-    ev_run(loop, 0);
-
-    mt_rfid_deinit(&rfid);
-
-    return 0;
-}
-#endif
diff --git a/mt_gpio.h b/mt_gpio.h
deleted file mode 100644 (file)
index fad9390..0000000
--- a/mt_gpio.h
+++ /dev/null
@@ -1,33 +0,0 @@
-#ifndef MT_RFID_H
-#define MT_RFID_H
-
-#include <ev.h>
-
-// reader open parameters, see uFR manual
-#define UFR_READER_TYPE     1       // uFR type (1Mbps)
-#define UFR_PORT_INTERFACE  1       // serial; auto->ftdi->FAIL
-#define UFR_PORT_NAME       "/dev/ttyUSB0" // reader device
-#define UFR_ASYNC_BAUD_RATE 1000000 // 1Mbps, otherwise UFR_COMMUNICATION_BREAK
-
-#define UFR_BEEP // define this to annoy people
-
-typedef struct ev_io_ufr {
-    ev_io w;           // fd watcher
-    char uid_data[24]; // store uid here (uid is 10 bytes max)
-    char *uid;         // current position in uid_data
-    int fd;            // PORT_NAME file descriptor
-} ev_io_ufr;
-
-typedef struct mt_rfid_t {
-    ev_io_ufr w; // reader watcher
-    int fd;      // print JSON output here
-} mt_rfid_t;
-
-// connect to the reader, add self to loop and make it write to fd
-// return 0 on success, negative number otherwise
-int mt_rfid_init(mt_rfid_t *self, struct ev_loop *loop, int fd);
-
-// disconnect from reader
-void mt_rfid_deinit(mt_rfid_t *self);
-
-#endif
diff --git a/mt_keys.c b/mt_keys.c
new file mode 100644 (file)
index 0000000..59436f4
--- /dev/null
+++ b/mt_keys.c
@@ -0,0 +1,145 @@
+#include <stdlib.h>
+#include <stdio.h>
+#include <unistd.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <linux/input.h>
+#include <linux/input-event-codes.h>
+
+#include "mt_keys.h"
+#include "signal_exit.h"
+#include "json_helpers.h"
+
+static void keys_cb(EV_P_ ev_io *w_, int revents)
+{
+    static char *type = "keys";
+
+    ev_io_keys *w = (ev_io_keys *)w_;
+    int fd = w->fd;
+    struct input_event ev;
+    char key;
+    ssize_t n = read(w->w.fd, &ev, sizeof(ev));
+    if (n != sizeof(ev)) {
+        perror("read");
+        return;
+    }
+    if (ev.type == 1 && ev.value == 1) {
+        switch (ev.code) {
+            case KEY_A:
+                key = 'A';
+                break;
+            case KEY_B:
+                key = 'B';
+                break;
+            case KEY_C:
+                key = 'C';
+                break;
+            case KEY_D:
+                key = 'D';
+                break;
+            case KEY_E:
+                key = 'E';
+                break;
+            case KEY_F:
+                key = 'F';
+                break;
+            case KEY_G:
+                key = 'G';
+                break;
+            case KEY_H:
+                key = 'H';
+                break;
+            case KEY_I:
+                key = 'I';
+                break;
+            case KEY_J:
+                key = 'J';
+                break;
+            case KEY_K:
+                key = 'K';
+                break;
+            case KEY_L:
+                key = 'L';
+                break;
+            case KEY_M:
+                key = 'M';
+                break;
+            case KEY_N:
+                key = 'N';
+                break;
+            case KEY_O:
+                key = 'O';
+                break;
+            case KEY_P:
+                key = 'P';
+                break;
+            case KEY_Q:
+                key = 'Q';
+                break;
+            case KEY_R:
+                key = 'R';
+                break;
+            case KEY_S:
+                key = 'S';
+                break;
+            case KEY_T:
+                key = 'T';
+                break;
+            default:
+                fprintf(stderr, "unsupported event code: %d\n", ev.code);
+                return;
+        }
+
+        JSON_START();
+        JSON_STR(type);
+        JSON_NEXT();
+        JSON_CHAR(key);
+        JSON_END();
+    }
+}
+
+int mt_keys_init(mt_keys_t *self, struct ev_loop *loop, int fd)
+{
+    int ev = open("/dev/input/by-path/platform-gpio-keys-event", O_RDONLY);
+    if (fd == -1) {
+        perror("open");
+        return -1;
+    }
+
+    self->fd = ev;
+    ev_io_keys *w = &(self->w);
+    w->fd = fd;
+    ev_io_init(&(w->w), keys_cb, ev, EV_READ);
+    ev_io_start(loop, (ev_io *)w);
+
+    return 0;
+}
+
+void mt_keys_deinit(mt_keys_t *self)
+{
+    if (close(self->fd) == 0) {
+        fprintf(stderr, "closed %d\n", self->fd);
+    } else {
+        perror("close");
+    }
+}
+
+#ifndef NO_MAIN
+int main(int argc, char **argv)
+{
+    struct ev_loop *loop = EV_DEFAULT;
+    mt_keys_t keys;
+
+    set_signal_exit(loop);
+
+    if (mt_keys_init(&keys, loop, STDOUT_FILENO) != 0) {
+        return -1;
+    }
+
+    ev_run(loop, 0);
+
+    mt_keys_deinit(&keys);
+
+    return 0;
+}
+#endif
diff --git a/mt_keys.h b/mt_keys.h
new file mode 100644 (file)
index 0000000..1764b10
--- /dev/null
+++ b/mt_keys.h
@@ -0,0 +1,50 @@
+#ifndef MT_KEYS_H
+#define MT_KEYS_H
+
+#include <ev.h>
+
+typedef struct mt_keys_dev_t {
+    char chip[16];
+    unsigned int offset;
+} mt_keys_dev_t;
+
+#define DEV "/dev/gpiochip"
+#define ADV_GPIO_PINS \
+{ \
+    {DEV "0", 27}, \
+    {DEV "0", 29}, \
+    {DEV "0", 25}, \
+    {DEV "0", 30}, \
+    {DEV "5", 31}, \
+    {DEV "2", 30}, \
+    {DEV "2", 31}, \
+    {DEV "2", 21}, \
+    {DEV "4",  2}, \
+    {DEV "2", 20}, \
+    {DEV "2", 23}, \
+    {DEV "5", 11}, \
+    {DEV "1",  2}, \
+    {DEV "5",  9}, \
+    {DEV "1",  3}, \
+    {DEV "5", 16}, \
+    {DEV "5",  7}, \
+    {DEV "1",  4}, \
+    {DEV "1",  0}, \
+    {DEV "5",  8}  \
+}
+
+typedef struct ev_io_keys {
+    ev_io w;
+    int fd;
+} ev_io_keys;
+
+typedef struct mt_keys_t {
+    ev_io_keys w;
+    int fd;
+} mt_keys_t;
+
+int mt_keys_init(mt_keys_t *self, struct ev_loop *loop, int fd);
+
+void mt_keys_deinit(mt_keys_t *self);
+
+#endif
index 3176d778d0cdc081dff5686d1e025c2ed9b3337d..e661337651c3f1059a4578b231569477537ce91a 100644 (file)
--- a/mt_rfid.c
+++ b/mt_rfid.c
@@ -9,6 +9,7 @@
 
 #include "mt_rfid.h"
 #include "signal_exit.h"
+#include "json_helpers.h"
 
 // shit to avoid constant repetition
 #define CONCAT_AGAIN(A,B) A ## B
@@ -84,15 +85,8 @@ static int tty_open(const char *port, int br)
     return fd;
 }
 
-// really simple JSON helpers
-#define JSON_START()   dprintf(fd,"{")
-#define JSON_NUM(NAME) dprintf(fd,"\"" #NAME "\":%d", NAME) //see the int?
-#define JSON_NEXT()    dprintf(fd,",")
-#define JSON_STR(NAME) dprintf(fd,"\"" #NAME "\":\"%s\"", NAME)
-#define JSON_END()     dprintf(fd,"}\n")
-
 // print complete json
-#define JSON_PRINT() do { \
+#define RFID_JSON_PRINT() do { \
     JSON_START();         \
     JSON_STR(type);       \
     JSON_NEXT();          \
@@ -128,7 +122,7 @@ static void ufr_read(char *uid, int fd)
         return;
     }
 
-    JSON_PRINT();
+    RFID_JSON_PRINT();
 
 #ifdef UFR_BEEP
     ReaderUISignal(0, 1); // no light, one beep
index 7ef9109ecc15b68dd974f8e3c1347416dda4fe8b..26dc90b2d45d730989ba99805265653367a1f9fa 100644 (file)
@@ -99,7 +99,7 @@ static int callback_merica_terminal(struct lws *wsi,
 
         case LWS_CALLBACK_SERVER_WRITEABLE:
             n = strlen(line);
-            m = lws_write(wsi, line, n, LWS_WRITE_TEXT);
+            m = lws_write(wsi, (unsigned char *)line, n, LWS_WRITE_TEXT);
             if (m < n) {
                 fprintf(stderr, "ERROR %d writing to di socket\n", n);
                 return -1;