]> rtime.felk.cvut.cz Git - coffee/mt-apps.git/commitdiff
Add simulator mode (for debugging without terminal)
authorMichal Sojka <michal.sojka@cvut.cz>
Wed, 8 Aug 2018 10:58:57 +0000 (12:58 +0200)
committerMichal Sojka <michal.sojka@cvut.cz>
Wed, 8 Aug 2018 10:58:57 +0000 (12:58 +0200)
Makefile
json_helpers.h
mt_aio.c
mt_keys.c
mt_keys.h
mt_rfid.c
mt_server.h

index 9aa981771c75f5e3b5ac5100137c5a1f60cda04b..37068568d17fbac3824d3fadd07ac63792b7e7a7 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -27,6 +27,9 @@ mtaio_DEFS    = -DNO_MAIN -DHAVE_RFID=$(if $(HAVE_RFID),1,0)
 ifeq ($(HAVE_RFID),yes)
 mtaio_SRCS += mt_rfid.c
 mtaio_LIBS += -luFCoder-armhf
+else
+mtaio_SRCS += mt_sim.c
+mtaio_DEFS += -DSIM=1
 endif
 
 all: mtserver mtkeys mtaio
index f064f36b7c0bbddbd6f5408557de19253681358f..629d13faad499418395f1c52f027588a6a3b8f26 100644 (file)
@@ -1,6 +1,9 @@
 #ifndef JSON_HELPERS_H
 #define JSON_HELPERS_H
 
+#include <stdio.h>
+#include <stdint.h>
+
 // really simple JSON helpers
 #define JSON_START()    dprintf(fd,"{")
 #define JSON_NUM(NAME)  dprintf(fd,"\"" #NAME "\":%d", NAME) //see the int?
@@ -9,4 +12,38 @@
 #define JSON_CHAR(NAME) dprintf(fd,"\"" #NAME "\":\"%c\"", NAME)
 #define JSON_END()      dprintf(fd,"}\n")
 
+static inline void
+keys_json_print(int fd, char key)
+{
+       static const char *type = "keys";
+
+        JSON_START();
+        JSON_STR(type);
+        JSON_NEXT();
+        JSON_CHAR(key);
+        JSON_END();
+}
+
+static inline void
+rfid_json_print(int fd,
+               uint8_t card_type,
+               uint8_t sak,           //select acknowledge
+               char *uid,
+               uint8_t size)
+{
+    static const char *type = "rfid";
+
+    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();
+}
+
 #endif
index 92f2354421687827a08d9269e1ba62391d332da7..076d49d44138c328cc1433695b30b7e8c2836140 100644 (file)
--- a/mt_aio.c
+++ b/mt_aio.c
@@ -4,6 +4,7 @@
 #include "mt_server.h"
 #include "signal_exit.h"
 #include "mt_blank.h"
+#include "mt_sim.h"
 
 int main(int argc, char **argv)
 {
@@ -20,6 +21,7 @@ int main(int argc, char **argv)
 
     set_signal_exit(loop);
 
+#if !defined(SIM)
     if (mt_rfid_init(&rfid, loop, pipefd[1]) != 0) {
         return -1;
     }
@@ -28,19 +30,28 @@ int main(int argc, char **argv)
         return -1;
     }
 
-    if (mt_server_init(&server, loop, pipefd[0]) != 0) {
-        return -2;
-    }
-
     if (mt_blank_init(loop) != 0) {
         return -3;
     }
+#else
+    if (mt_sim_init(loop, pipefd[1]) != 0) {
+        return -1;
+    }
+#endif
+
+    if (mt_server_init(&server, loop, pipefd[0]) != 0) {
+        return -2;
+    }
 
     ev_run(loop, 0);
 
     mt_server_deinit(&server);
+#if !defined(SIM)
     mt_keys_deinit(&keys);
     mt_rfid_deinit(&rfid);
+#else
+    mt_sim_deinit();
+#endif
     ev_loop_destroy(loop);
 
     return 0;
index e72b17f65d914108911dd6774acd5de575f15594..015b8ae0095d34eb901e97ff7a9f4101d2cdb655 100644 (file)
--- a/mt_keys.c
+++ b/mt_keys.c
 #include "signal_exit.h"
 #include "json_helpers.h"
 
-void keys_json_print(int fd, char key)
-{
-       static const char *type = "keys";
-
-        JSON_START();
-        JSON_STR(type);
-        JSON_NEXT();
-        JSON_CHAR(key);
-        JSON_END();
-}
-
 static void keys_cb(EV_P_ ev_io *w_, int revents)
 {
     ev_io_keys *w = (ev_io_keys *)w_;
index 38deea245b7e5e1b9135663378d60df7e9fabfaf..1764b100697c8ac08e0df9a50d5177584bdceac6 100644 (file)
--- a/mt_keys.h
+++ b/mt_keys.h
@@ -47,6 +47,4 @@ int mt_keys_init(mt_keys_t *self, struct ev_loop *loop, int fd);
 
 void mt_keys_deinit(mt_keys_t *self);
 
-void keys_json_print(int fd, char key);
-
 #endif
index e987f827c79be7f2d17acf0934550ca70be96c0c..76a94c52d8f162431fd29238dbd6e90ec52d185d 100644 (file)
--- a/mt_rfid.c
+++ b/mt_rfid.c
@@ -85,28 +85,6 @@ static int tty_open(const char *port, int br)
     return fd;
 }
 
-// print complete json
-void rfid_json_print(int fd,
-                    uint8_t card_type,
-                    uint8_t sak,           //select acknowledge
-                    char *uid,
-                    uint8_t size)
-{
-    static const char *type = "rfid";
-
-    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();
-}
-
 static void ufr_read(char *uid, int fd)
 {
     UFR_STATUS status;
index aecab1723a9ec3f623b19e88ee9a6a363af2d65b..77d737102d1cf127acfdb660c3744dccb5d9466b 100644 (file)
@@ -9,9 +9,14 @@
 #define MT_PROTOCOL_RX_BUFFER_SIZE 128
 #define INPUT_LINE_LENGTH          MT_PROTOCOL_RX_BUFFER_SIZE
 
+#if defined(SIM)
+#define HTTP_PORT       8080                    // listen here
+#define HTTP_ORIGIN     "html" // where the html files are
+#else
 #define HTTP_PORT       80                    // listen here
-#define HTTP_MOUNTPOINT "/"                   // mountpoint URL
 #define HTTP_ORIGIN     "/usr/share/mtserver" // where the html files are
+#endif
+#define HTTP_MOUNTPOINT "/"                   // mountpoint URL
 #define HTTP_DEFAULT    "index.html"          // default filename
 
 #define JSON_EMPTY "{\"type\":\"empty\"}" // default message