summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorcbdev <cb@cbcdn.com>2023-06-17 23:04:38 +0200
committercbdev <cb@cbcdn.com>2023-06-17 23:04:38 +0200
commitaa692f0dcdeefc846d7fa11222d6b2f6af17f5fa (patch)
treec641297c5c8b5caf819f74248a105b0bf5ba9741
parent8b6e2b063183fd647145f48e8746512350002b87 (diff)
downloadnfcommander-aa692f0dcdeefc846d7fa11222d6b2f6af17f5fa.tar.gz
nfcommander-aa692f0dcdeefc846d7fa11222d6b2f6af17f5fa.tar.bz2
nfcommander-aa692f0dcdeefc846d7fa11222d6b2f6af17f5fa.zip
Core loop base
-rw-r--r--commander.cfg8
-rw-r--r--nfcommander.c52
-rw-r--r--nfcommander.h7
3 files changed, 61 insertions, 6 deletions
diff --git a/commander.cfg b/commander.cfg
index ec9895f..4576bd9 100644
--- a/commander.cfg
+++ b/commander.cfg
@@ -3,10 +3,12 @@ reader=yhy
device=/dev/ttyACM0
mifare.defaultkeys=FFFFFFFFFFFF,000000000000
mifare.prodkey=123456123456
+interval=2000
[control]
bind = 0.0.0.0 7812
-[command play]
-chdir = commands
-exec = ./play
+[command]
+#commanddir = commands
+chdir = commands/
+handler = handle_tag
diff --git a/nfcommander.c b/nfcommander.c
index de50ef4..ceadec7 100644
--- a/nfcommander.c
+++ b/nfcommander.c
@@ -1,21 +1,54 @@
#include <stdio.h>
+#include <signal.h>
+#include <sys/epoll.h>
+#include <unistd.h>
#include "nfcommander.h"
#include "config.h"
#include "reader.h"
#include "control.h"
-int usage(char* fn){
- printf("NFCommander - trigger actions based on near-field tags\n");
+static volatile sig_atomic_t shutdown_requested = 0;
+static int epoll_fd = -1;
+
+static void signal_handler(int signum){
+ shutdown_requested = 1;
+}
+
+static int usage(char* fn){
+ printf("NFCommander %s - trigger actions based on near-field tags\n",
+ NFCOMMANDER_VERSION);
printf("\tUsage: %s <config-file>\n", fn);
return EXIT_FAILURE;
}
+int core_manage_fd(int fd, int manage, notification_target_t system){
+ struct epoll_event event = {
+ .events = EPOLLIN,
+ .data.u32 = system
+ };
+
+ 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;
+ return 1;
+ }
+ return 0;
+}
+
int main(int argc, char** argv){
if(argc < 2){
return usage(argv[0]);
}
+ //set up epoll instance
+ struct epoll_event events[10];
+ epoll_fd = epoll_create1(EPOLL_CLOEXEC);
+ if(epoll_fd < 0){
+ printf("Failed to create epoll instance\n");
+ return EXIT_FAILURE;
+ }
+
//read configuration
if(config_read(argv[1])){
return usage(argv[0]);
@@ -27,10 +60,23 @@ int main(int argc, char** argv){
//start control api
control_start();
+ //handle signals
+ signal(SIGINT, signal_handler);
+ signal(SIGPIPE, SIG_IGN);
+
//handle events
- //TODO
+ while(!shutdown_requested){
+ int event_count = epoll_wait(epoll_fd, events, sizeof(events) / sizeof(struct epoll_event), 1000);
+ if(event_count < 0){
+ break;
+ }
+
+ printf("%d events\n", event_count);
+ }
//clean up
+ close(epoll_fd);
+ epoll_fd = -1;
reader_free();
control_free();
config_free();
diff --git a/nfcommander.h b/nfcommander.h
index dbab99f..65ca9d2 100644
--- a/nfcommander.h
+++ b/nfcommander.h
@@ -6,3 +6,10 @@
#define NFCOMMANDER_VERSION "v0.1-dev"
#endif
+typedef enum {
+ system_control,
+ system_reader,
+ system_command
+} notification_target_t;
+
+int core_manage_fd(int fd, int manage, notification_target_t system);