summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--nfcommander.c8
-rw-r--r--nfcommander.h4
-rw-r--r--reader.c72
-rw-r--r--reader_yhy.c49
-rw-r--r--reader_yhy.h1
5 files changed, 84 insertions, 50 deletions
diff --git a/nfcommander.c b/nfcommander.c
index 0521fc4..50cbda7 100644
--- a/nfcommander.c
+++ b/nfcommander.c
@@ -42,6 +42,10 @@ int core_manage_fd(int fd, int manage, notification_target_t system){
.data.u64 = data.u64
};
+ if(fd < 0){
+ return 0;
+ }
+
if(epoll_ctl(epoll_fd, manage ? EPOLL_CTL_ADD : EPOLL_CTL_DEL, fd, &event)){
fprintf(stderr, "Failed to modify epoll instance\n");
shutdown_requested = 1;
@@ -119,10 +123,10 @@ int main(int argc, char** argv){
}
//clean up
- close(epoll_fd);
- epoll_fd = -1;
reader_free();
control_free();
command_free();
config_free();
+ close(epoll_fd);
+ epoll_fd = -1;
}
diff --git a/nfcommander.h b/nfcommander.h
index 7990198..1f2e1e1 100644
--- a/nfcommander.h
+++ b/nfcommander.h
@@ -24,6 +24,7 @@ typedef struct {
typedef int (*reader_plugin_init)(void);
typedef int (*reader_plugin_handle)(int fd);
+typedef int (*reader_plugin_scan)(void);
typedef enum {
system_control,
@@ -32,5 +33,4 @@ typedef enum {
} notification_target_t;
int core_manage_fd(int fd, int manage, notification_target_t system);
-int core_tag_present(nfc_tag_info_t tag);
-int core_tag_removed(nfc_tag_info_t tag);
+int reader_tag_present(nfc_tag_info_t tag);
diff --git a/reader.c b/reader.c
index 9064c80..bcfd28b 100644
--- a/reader.c
+++ b/reader.c
@@ -1,16 +1,21 @@
#include <stdio.h>
#include <limits.h>
#include <dlfcn.h>
+#include <sys/timerfd.h>
+#include <unistd.h>
#include "nfcommander.h"
#include "reader.h"
#include "config.h"
#define MAX_PLUGIN_PATH NAME_MAX
-void* reader_module = NULL;
-reader_plugin_handle reader_backend_handle = NULL;
+static void* reader_module = NULL;
+static reader_plugin_handle reader_backend_handle = NULL;
+static reader_plugin_scan reader_backend_scan = NULL;
-int reader_init(){
+static int timer_fd = -1;
+
+static int reader_init_backend(){
char plugin[MAX_PLUGIN_PATH] = "";
char* reader = config_get("nfc", "reader");
@@ -28,10 +33,11 @@ int reader_init(){
}
reader_backend_handle = dlsym(reader_module, "handle");
+ reader_backend_scan = dlsym(reader_module, "scan");
//call initializer
reader_plugin_init init = dlsym(reader_module, "init");
- if(!reader_backend_handle || !init || init()){
+ if(!reader_backend_scan || !init || init()){
printf("Failed to initialize the reader\n");
return 1;
}
@@ -39,14 +45,70 @@ int reader_init(){
return 0;
}
+int reader_init(){
+ //initialize poll timer
+ 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;
+
+ //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;
+ }
+
+ core_manage_fd(timer_fd, 1, system_reader);
+ return reader_init_backend();
+}
+
int reader_handle(int fd){
- return reader_backend_handle(fd);
+ uint8_t buffer[1024];
+
+ if(fd == timer_fd){
+ //acknowledge timer
+ read(fd, buffer, sizeof(buffer));
+
+ //mark all tags expired
+ //TODO
+
+ //scan all tags
+ if(reader_backend_scan()){
+ return 1;
+ }
+
+ //handle changes in active set
+ //TODO
+ }
+
+ if(reader_backend_handle){
+ return reader_backend_handle(fd);
+ }
+ return 0;
}
void reader_free(){
if(reader_module){
dlclose(reader_module);
reader_backend_handle = NULL;
+ reader_backend_scan = NULL;
reader_module = NULL;
}
+
+ core_manage_fd(timer_fd, 0, system_reader);
+ close(timer_fd);
+ timer_fd = -1;
}
diff --git a/reader_yhy.c b/reader_yhy.c
index 58bd345..7ece702 100644
--- a/reader_yhy.c
+++ b/reader_yhy.c
@@ -8,21 +8,9 @@
#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){
@@ -31,7 +19,7 @@ int init(){
}
//TODO set cloexec, nonblock
- printf("Opening %s with polltime %lu\n", port, poll_timeout);
+ printf("Opening YHY reader on %s\n", port);
reader_fd = yhy_open(port);
if(reader_fd < 0){
printf("Failed to open reader connection at %s\n", port);
@@ -42,45 +30,24 @@ int init(){
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;
- }
-
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;
+ if(fd == reader_fd){
+ printf("Unhandled data on YHY fd\n");
}
+ return 0;
+}
- printf("Unknown FD in reader_yhy handler\n");
- return 1;
+int scan(){
+ printf("Scanning field\n");
+ return 0;
}
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;
}
diff --git a/reader_yhy.h b/reader_yhy.h
index 7ed6a1f..303fbaa 100644
--- a/reader_yhy.h
+++ b/reader_yhy.h
@@ -1,2 +1,3 @@
int init();
int handle(int fd);
+int scan();