diff options
Diffstat (limited to 'reader_yhy.c')
-rw-r--r-- | reader_yhy.c | 74 |
1 files changed, 69 insertions, 5 deletions
diff --git a/reader_yhy.c b/reader_yhy.c index 51ac796..58bd345 100644 --- a/reader_yhy.c +++ b/reader_yhy.c @@ -1,22 +1,86 @@ #include <stdio.h> +#include <unistd.h> +#include <sys/timerfd.h> #include "nfcommander.h" #include "config.h" -static void __attribute__((constructor)) init() { - printf("This is the yhy reader plugin\n"); +#include "libyhy.h" +static int reader_fd = -1; +static int timer_fd = -1; + +int init(){ + char reader_version[100]; size_t poll_timeout = 1000; + struct itimerspec timer_cfg = { + 0 + }; + if(config_get("nfc", "interval")){ poll_timeout = strtoul(config_get("nfc", "interval"), NULL, 10); } + timer_cfg.it_value.tv_sec = timer_cfg.it_interval.tv_sec = poll_timeout / 1000; + timer_cfg.it_value.tv_nsec = timer_cfg.it_interval.tv_nsec = (poll_timeout % 1000) * 1e6; + char* port = config_get("nfc", "device"); + if(!port){ + printf("Please configure nfc.device to the reader serial port\n"); + return 1; + } + + //TODO set cloexec, nonblock + printf("Opening %s with polltime %lu\n", port, poll_timeout); + reader_fd = yhy_open(port); + if(reader_fd < 0){ + printf("Failed to open reader connection at %s\n", port); + return 1; + } + + if(yhy_sync_read_version(reader_fd, reader_version, sizeof(reader_version)) > 0){ + printf("Connected YHY reader reports firmware: %s\n", reader_version); + } + + //create timerfd + timer_fd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC); + if(timer_fd < 0){ + printf("Failed to create timer fd\n"); + return 1; + } + + if(timerfd_settime(timer_fd, 0, &timer_cfg, NULL)){ + printf("Failed to set timer\n"); + return 1; + } - printf("Opening %s with polltime %d\n", port, poll_timeout); + core_manage_fd(reader_fd, 1, system_reader); + core_manage_fd(timer_fd, 1, system_reader); + + return 0; +} + +int handle(int fd){ + uint8_t buffer[1024]; + + if(fd == timer_fd){ + //acknowledge the timer + read(fd, buffer, sizeof(buffer)); + + //see if there is a tag + //TODO + return 0; + } + printf("Unknown FD in reader_yhy handler\n"); + return 1; } -static void __attribute__((destructor)) cleanup() { - printf("This is the yhy reader plugin cleanup\n"); +static void __attribute__((destructor)) cleanup(){ + core_manage_fd(reader_fd, 0, system_reader); + close(reader_fd); + reader_fd = -1; + core_manage_fd(timer_fd, 0, system_reader); + close(timer_fd); + timer_fd = -1; } |