]> rtime.felk.cvut.cz Git - coffee/mt-apps.git/blobdiff - mt_rfid.c
websockets server added
[coffee/mt-apps.git] / mt_rfid.c
index d281371918f1d1238ff89eeadbe578fad8894cec..2741277302ccaf6de545d475d98701e04d833866 100644 (file)
--- a/mt_rfid.c
+++ b/mt_rfid.c
 int set_nonblock(int fd)
 {
     int flags = fcntl(fd, F_GETFL, 0);
-    if (flags == -1){
+    if (flags == -1) {
         perror("fcntl (get)");
         return -1;
     }
-    if (fcntl(fd, F_SETFL, flags | O_NONBLOCK) == -1){
+    if (fcntl(fd, F_SETFL, flags | O_NONBLOCK) == -1) {
         perror("fcntl (set)");
         return -1;
     }
@@ -34,76 +34,83 @@ int set_nonblock(int fd)
     return 0;
 }
 
-int set_rts(int fd, int level) {
-       int uart_status;
+int set_rts(int fd, int level)
+{
+    int uart_status;
 
-       if (ioctl(fd, TIOCMGET, &uart_status) == -1) {
-               perror("ioctl (TIOCMGET)");
-               return -1;
-       }
+    if (ioctl(fd, TIOCMGET, &uart_status) == -1) {
+        perror("ioctl (TIOCMGET)");
+        return -1;
+    }
 
-       if (level) {
-               uart_status |= TIOCM_RTS;
+    if (level) {
+        uart_status |= TIOCM_RTS;
     } else {
-               uart_status &= ~TIOCM_RTS;
+        uart_status &= ~TIOCM_RTS;
     }
 
-       if (ioctl(fd, TIOCMSET, &uart_status) == -1) {
-               perror("TIOCMSET");
+    if (ioctl(fd, TIOCMSET, &uart_status) == -1) {
+        perror("TIOCMSET");
         return -1;
-       }
+    }
 
     fprintf(stderr, "set rts %d\n", level);
 
     return 0;
 }
 
-void set_baud_rate(int fd, int br) {
-       struct termios options;
+void set_baud_rate(int fd, int br)
+{
+    struct termios options;
 
-       tcgetattr(fd, &options);
-       cfsetispeed(&options, br);
-       cfsetospeed(&options, br);
-       tcsetattr(fd, TCSANOW, &options);
+    tcgetattr(fd, &options);
+    cfsetispeed(&options, br);
+    cfsetospeed(&options, br);
+    tcsetattr(fd, TCSANOW, &options);
 }
 
-int tty_open(const char *port, int baud_rate) {
+int tty_open(const char *port, int baud_rate)
+{
 
-       int fd = open(port, O_RDONLY | O_NOCTTY);
-       if (fd < 0) {
+    int fd = open(port, O_RDONLY | O_NOCTTY);
+    if (fd < 0) {
         perror("open");
         return fd;
-       } else {
+    } else {
         fprintf(stderr, "opened %s as %d\n", port, fd);
     }
 
     set_nonblock(fd);
-       set_rts(fd, 0);
-       set_baud_rate(fd, baud_rate);
-       usleep(1200000); //value by d-logic
-       tcflush(fd, TCIFLUSH);
+    set_rts(fd, 0);
+    set_baud_rate(fd, baud_rate);
+    usleep(1200000); //value by d-logic
+    tcflush(fd, TCIFLUSH);
 
-       return fd;
+    return fd;
 }
 
 typedef struct ev_io_ufr {
     ev_io w;
+    //lws_context *context;
+    //ufr_session_data *session_data;
     char uid_data[24];
     char *uid;
+    int fd;
 } ev_io_ufr;
 
 static void sigint_cb(EV_P_ ev_signal *w, int revents)
 {
-    ev_break (EV_A_  EVBREAK_ALL);
+    ev_break(EV_A_  EVBREAK_ALL);
 }
 
-static void ufr_read(char *uid)
+static void ufr_read(char *uid, int fd)
 {
     UFR_STATUS status;
     uint8_t card_type;
-    uint8_t sak;         //select acknowledge
+    uint8_t sak;           //select acknowledge
     uint8_t uid_bytes[10]; //uid as bytes
-    uint8_t uid_size;
+    uint8_t size;
+       char *type = "rfid";
 
     status = GetDlogicCardType(&card_type);
     if (status != UFR_OK) {
@@ -111,21 +118,30 @@ static void ufr_read(char *uid)
         return;
     }
 
-    status = GetCardIdEx(&sak, uid_bytes, &uid_size);
+    status = GetCardIdEx(&sak, uid_bytes, &size);
     if (status != UFR_OK) {
         fprintf(stderr, "GetCardIdEx: %s\n", UFR_Status2String(status));
         return;
     }
 
     JSON_START();
-    JSON_NUM(card_type); JSON_NEXT();
-    JSON_NUM(sak);       JSON_NEXT();
-    JSON_NUM(uid_size);  JSON_NEXT();
-    JSON_STR(uid);    JSON_END();
+       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();
 
 #ifdef UFR_BEEP
-    ReaderUISignal(0, 1);
+    ReaderUISignal(0, 1); // no light, one beep
 #endif
+
+    /*lws_callback_on_writable_all_protocol(self->context,
+                                          &protocols[PROTOCOL_DUMB_INCREMENT]);*/
 }
 
 static void ufr_cb(EV_P_ ev_io *w_, int revents)
@@ -138,7 +154,7 @@ static void ufr_cb(EV_P_ ev_io *w_, int revents)
     if (uid == ASYNC_SUFFIX) {
         //*(w->uid - 1) = 0;
         w->uid = w->uid_data;
-        ufr_read(w->uid);
+        ufr_read(w->uid, w->fd);
     }
 
     //for one-shot events, one must manually stop the watcher with its corresponding stop function.
@@ -156,15 +172,16 @@ int libev_run(int fd)
     struct ev_loop *loop = EV_DEFAULT;
 
     ufr_watcher.uid = ufr_watcher.uid_data;
-    ev_io_init (&(ufr_watcher.w), ufr_cb, fd, EV_READ);
-    ev_io_start (loop, (ev_io *)&ufr_watcher);
+    ufr_watcher.fd = 1;
+    ev_io_init(&(ufr_watcher.w), ufr_cb, fd, EV_READ);
+    ev_io_start(loop, (ev_io *)&ufr_watcher);
 
-    ev_signal_init (&int_watcher, sigint_cb, SIGINT);
-    ev_signal_start (loop, &int_watcher);
-    ev_signal_init (&term_watcher, sigint_cb, SIGTERM);
-    ev_signal_start (loop, &term_watcher);
+    ev_signal_init(&int_watcher, sigint_cb, SIGINT);
+    ev_signal_start(loop, &int_watcher);
+    ev_signal_init(&term_watcher, sigint_cb, SIGTERM);
+    ev_signal_start(loop, &term_watcher);
 
-    ev_run (loop, 0);
+    ev_run(loop, 0);
 
     return 0;
 }
@@ -184,13 +201,13 @@ int ufr_open(unsigned reader_type, char *port_name, unsigned port_interface)
     fprintf(stderr, "%s\n", GetReaderDescription());
 
     status = SetAsyncCardIdSendConfig(
-        1,            //enable send
-        0,            //disable prefix
-        0,            //prefix
-        ASYNC_SUFFIX, //suffix
-        0,            //disable send removed
-        ASYNC_BAUD_RATE
-    );
+                 1,            //enable send
+                 0,            //disable prefix
+                 0,            //prefix
+                 ASYNC_SUFFIX, //suffix
+                 0,            //disable send removed
+                 ASYNC_BAUD_RATE
+             );
     fprintf(stderr, "SetAsyncCardIdSendConfig: %s\n", UFR_Status2String(status));
     if (status != UFR_OK) {
         return -1;
@@ -205,12 +222,12 @@ int main(int argc, char **argv)
         return -1;
     }
 
-    int fd = tty_open(PORT_NAME, CONCAT(B,ASYNC_BAUD_RATE));
+    int fd = tty_open(PORT_NAME, CONCAT(B, ASYNC_BAUD_RATE));
     if (fd < 0) {
         return -2;
     }
 
-       libev_run(fd);
+    libev_run(fd);
 
     if (close(fd) == 0) {
         fprintf(stderr, "closed %d\n", fd);