From 209897056d43ea0d682700e1078a8988a9b133f3 Mon Sep 17 00:00:00 2001 From: cbdev Date: Sat, 17 Jun 2023 23:20:39 +0200 Subject: Core fd handling --- nfcommander.c | 41 ++++++++++++++++++++++++++++++++++++++--- 1 file changed, 38 insertions(+), 3 deletions(-) (limited to 'nfcommander.c') diff --git a/nfcommander.c b/nfcommander.c index ceadec7..3c58d6c 100644 --- a/nfcommander.c +++ b/nfcommander.c @@ -7,6 +7,15 @@ #include "config.h" #include "reader.h" #include "control.h" +#include "command.h" + +typedef union { + struct { + uint32_t system; + uint32_t fd; + } components; + uint64_t u64; +} epoll_private_t; static volatile sig_atomic_t shutdown_requested = 0; static int epoll_fd = -1; @@ -23,9 +32,14 @@ static int usage(char* fn){ } int core_manage_fd(int fd, int manage, notification_target_t system){ + epoll_private_t data = { + .components.system = system, + .components.fd = fd + }; + struct epoll_event event = { .events = EPOLLIN, - .data.u32 = system + .data.u64 = data.u64 }; if(epoll_ctl(epoll_fd, manage ? EPOLL_CTL_ADD : EPOLL_CTL_DEL, fd, &event)){ @@ -37,6 +51,10 @@ int core_manage_fd(int fd, int manage, notification_target_t system){ } int main(int argc, char** argv){ + size_t n; + int event_count; + epoll_private_t polldata; + if(argc < 2){ return usage(argv[0]); } @@ -66,12 +84,28 @@ int main(int argc, char** argv){ //handle events while(!shutdown_requested){ - int event_count = epoll_wait(epoll_fd, events, sizeof(events) / sizeof(struct epoll_event), 1000); + 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); + for(n = 0; n < event_count; n++){ + polldata.u64 = events[n].data.u64; + switch(polldata.components.system){ + case system_control: + control_handle(polldata.components.fd); + break; + case system_reader: + reader_handle(polldata.components.fd); + break; + case system_command: + command_handle(polldata.components.fd); + break; + default: + printf("Unhandled target system for fd %d\n", polldata.components.fd); + break; + } + } } //clean up @@ -79,5 +113,6 @@ int main(int argc, char** argv){ epoll_fd = -1; reader_free(); control_free(); + command_free(); config_free(); } -- cgit v1.2.3