From f75a57f37770949aa1643a20fe47a56b85393ba3 Mon Sep 17 00:00:00 2001 From: Michal Sojka Date: Wed, 8 Aug 2018 12:58:57 +0200 Subject: [PATCH 1/1] Add simulator mode (for debugging without terminal) --- Makefile | 3 +++ json_helpers.h | 37 +++++++++++++++++++++++++++++++++++++ mt_aio.c | 19 +++++++++++++++---- mt_keys.c | 11 ----------- mt_keys.h | 2 -- mt_rfid.c | 22 ---------------------- mt_server.h | 7 ++++++- 7 files changed, 61 insertions(+), 40 deletions(-) diff --git a/Makefile b/Makefile index 9aa9817..3706856 100644 --- 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 diff --git a/json_helpers.h b/json_helpers.h index f064f36..629d13f 100644 --- a/json_helpers.h +++ b/json_helpers.h @@ -1,6 +1,9 @@ #ifndef JSON_HELPERS_H #define JSON_HELPERS_H +#include +#include + // 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 diff --git a/mt_aio.c b/mt_aio.c index 92f2354..076d49d 100644 --- 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; diff --git a/mt_keys.c b/mt_keys.c index e72b17f..015b8ae 100644 --- a/mt_keys.c +++ b/mt_keys.c @@ -10,17 +10,6 @@ #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_; diff --git a/mt_keys.h b/mt_keys.h index 38deea2..1764b10 100644 --- 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 diff --git a/mt_rfid.c b/mt_rfid.c index e987f82..76a94c5 100644 --- 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; diff --git a/mt_server.h b/mt_server.h index aecab17..77d7371 100644 --- a/mt_server.h +++ b/mt_server.h @@ -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 -- 2.39.2