From 15769aa7329789e4a996321f0b7897949c230e07 Mon Sep 17 00:00:00 2001 From: cbdev Date: Tue, 20 Jun 2023 00:45:35 +0200 Subject: Implement basic tag detection using reader_yhy --- reader.c | 79 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 76 insertions(+), 3 deletions(-) (limited to 'reader.c') diff --git a/reader.c b/reader.c index bcfd28b..196d556 100644 --- a/reader.c +++ b/reader.c @@ -3,17 +3,29 @@ #include #include #include +#include #include "nfcommander.h" #include "reader.h" #include "config.h" #define MAX_PLUGIN_PATH NAME_MAX +#define DEFAULT_POLL_TIMEOUT 1000 +#define MAX_TAGS 5 + +#define FLAG_CORE_NOTIFIED 1 +#define FLAG_ACTIVE 2 +#define FLAG_UNPROGRAMMED 4 + static void* reader_module = NULL; static reader_plugin_handle reader_backend_handle = NULL; static reader_plugin_scan reader_backend_scan = NULL; static int timer_fd = -1; +static struct { + uint8_t flags; + nfc_tag_info_t info; +} tags[MAX_TAGS] = { 0 }; static int reader_init_backend(){ char plugin[MAX_PLUGIN_PATH] = ""; @@ -47,7 +59,7 @@ static int reader_init_backend(){ int reader_init(){ //initialize poll timer - size_t poll_timeout = 1000; + size_t poll_timeout = DEFAULT_POLL_TIMEOUT; struct itimerspec timer_cfg = { 0 }; @@ -75,6 +87,67 @@ int reader_init(){ return reader_init_backend(); } +static void reader_expire(){ + size_t n = 0; + for(n = 0; n < MAX_TAGS; n++){ + tags[n].flags &= ~(FLAG_ACTIVE); + } +} + +static int reader_process(){ + size_t n = 0; + for(n = 0; n < MAX_TAGS; n++){ + if(tags[n].flags & FLAG_ACTIVE){ + if(!(tags[n].flags & FLAG_CORE_NOTIFIED)){ + //new tag + //TODO + printf("Tag index %lu detected\n", n); + tags[n].flags |= FLAG_CORE_NOTIFIED; + } + } + else{ + if(tags[n].flags & FLAG_CORE_NOTIFIED){ + //tag was removed + //TODO + printf("Tag index %lu removed\n", n); + } + + //reset all flags + tags[n].flags = 0; + } + } + return 0; +} + +int reader_tag_present(nfc_tag_info_t tag){ + size_t n = 0; + + //check if tag already known + for(n = 0; n < MAX_TAGS; n++){ + if((tags[n].flags & (FLAG_ACTIVE | FLAG_CORE_NOTIFIED)) + && tags[n].info.uid_length == tag.uid_length + && !memcmp(tags[n].info.uid, tag.uid, tag.uid_length)){ + //mark still active + tags[n].flags |= FLAG_ACTIVE; + return 0; + } + } + + //if not, add it + for(n = 0; n < MAX_TAGS; n++){ + if(!tags[n].flags){ + tags[n].flags |= FLAG_ACTIVE; + memcpy(&(tags[n].info), &tag, sizeof(tag)); + return 0; + } + } + + //realistically, this will never happen as most readers barely + //provide enough energy to power two tags in the field + printf("Maximum of active tags reached, ignored\n"); + return 0; +} + int reader_handle(int fd){ uint8_t buffer[1024]; @@ -83,7 +156,7 @@ int reader_handle(int fd){ read(fd, buffer, sizeof(buffer)); //mark all tags expired - //TODO + reader_expire(); //scan all tags if(reader_backend_scan()){ @@ -91,7 +164,7 @@ int reader_handle(int fd){ } //handle changes in active set - //TODO + return reader_process(); } if(reader_backend_handle){ -- cgit v1.2.3