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 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++--- reader_yhy.c | 21 +++++++++++++++- 2 files changed, 96 insertions(+), 4 deletions(-) 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){ diff --git a/reader_yhy.c b/reader_yhy.c index 7ece702..9cb386f 100644 --- a/reader_yhy.c +++ b/reader_yhy.c @@ -42,7 +42,26 @@ int handle(int fd){ } int scan(){ - printf("Scanning field\n"); + nfc_tag_info_t card = { + 0 + }; + + uint16_t atqa = 0; + uint8_t sak = 0; + + if(!yhy_sync_request(reader_fd, 1, &atqa)){ + return 0; + } + + card.uid_length = yhy_sync_anticoll(reader_fd, card.uid, sizeof(card.uid)); + if(!card.uid_length){ + return 0; + } + + yhy_sync_select(reader_fd, card.uid, card.uid_length, &sak); + yhy_sync_hlta(reader_fd); + + reader_tag_present(card); return 0; } -- cgit v1.2.3