From 0da2f58c7528b504ee54e380b755ea5f0801b4bb Mon Sep 17 00:00:00 2001 From: cbdev Date: Tue, 20 Jun 2023 20:58:01 +0200 Subject: Proper select looping in reader_yhy --- commander.cfg | 4 ++-- nfcommander.h | 9 +++++++++ reader.c | 31 ++++++++++++++++++++----------- reader_yhy.c | 23 +++++++++++++---------- 4 files changed, 44 insertions(+), 23 deletions(-) diff --git a/commander.cfg b/commander.cfg index f466c02..87b5940 100644 --- a/commander.cfg +++ b/commander.cfg @@ -1,7 +1,7 @@ [nfc] reader=yhy device=/dev/ttyUSB0 -mifare.defaultkeys=FFFFFFFFFFFF,000000000000 +mifare.defaultkey=FFFFFFFFFFFF mifare.prodkey=123456123456 interval=1000 @@ -9,6 +9,6 @@ interval=1000 bind = 0.0.0.0 7812 [command] -#commanddir = commands +;commanddir = commands chdir = commands/ handler = handle_tag diff --git a/nfcommander.h b/nfcommander.h index 1f2e1e1..049380d 100644 --- a/nfcommander.h +++ b/nfcommander.h @@ -33,4 +33,13 @@ typedef enum { } notification_target_t; int core_manage_fd(int fd, int manage, notification_target_t system); + +/* + * This API is to be called by reader backends to indicate presence of a tag. + * This can be done as soon as the UID of the tag has been read (ie. before + * reading EEPROM contents) to check whether the card has already been read. + * If this API returns 1, a full read is requested and the nfc_tag_info_t should + * be submitted again with the data fully read. + */ +#define TAG_READ_REQUESTED 1 int reader_tag_present(nfc_tag_info_t tag); diff --git a/reader.c b/reader.c index 196d556..0704fca 100644 --- a/reader.c +++ b/reader.c @@ -13,9 +13,10 @@ #define DEFAULT_POLL_TIMEOUT 1000 #define MAX_TAGS 5 -#define FLAG_CORE_NOTIFIED 1 -#define FLAG_ACTIVE 2 +#define FLAG_PRESENT 1 +#define FLAG_FULLY_READ 2 #define FLAG_UNPROGRAMMED 4 +#define FLAG_ACTIVE 8 static void* reader_module = NULL; static reader_plugin_handle reader_backend_handle = NULL; @@ -90,23 +91,25 @@ int reader_init(){ static void reader_expire(){ size_t n = 0; for(n = 0; n < MAX_TAGS; n++){ - tags[n].flags &= ~(FLAG_ACTIVE); + tags[n].flags &= ~(FLAG_PRESENT); } } 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)){ + if(tags[n].flags & FLAG_PRESENT){ + //TODO verify tag is fully read before pushing + + if(!(tags[n].flags & FLAG_ACTIVE)){ //new tag //TODO printf("Tag index %lu detected\n", n); - tags[n].flags |= FLAG_CORE_NOTIFIED; + tags[n].flags |= FLAG_ACTIVE; } } else{ - if(tags[n].flags & FLAG_CORE_NOTIFIED){ + if(tags[n].flags & FLAG_ACTIVE){ //tag was removed //TODO printf("Tag index %lu removed\n", n); @@ -124,20 +127,26 @@ int reader_tag_present(nfc_tag_info_t tag){ //check if tag already known for(n = 0; n < MAX_TAGS; n++){ - if((tags[n].flags & (FLAG_ACTIVE | FLAG_CORE_NOTIFIED)) + if((tags[n].flags & (FLAG_PRESENT | FLAG_ACTIVE)) && 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; + tags[n].flags |= FLAG_PRESENT; + + //TODO if full data submitted, copy in + + //if data has not yet been fully read, request a full read + return (tags[n].flags & (FLAG_FULLY_READ | FLAG_UNPROGRAMMED)) ? 0 : TAG_READ_REQUESTED; } } //if not, add it for(n = 0; n < MAX_TAGS; n++){ if(!tags[n].flags){ - tags[n].flags |= FLAG_ACTIVE; + tags[n].flags |= FLAG_PRESENT; memcpy(&(tags[n].info), &tag, sizeof(tag)); + + //TODO set FULLY_READ flag if necessary return 0; } } diff --git a/reader_yhy.c b/reader_yhy.c index 9cb386f..4c332ac 100644 --- a/reader_yhy.c +++ b/reader_yhy.c @@ -49,19 +49,22 @@ int scan(){ uint16_t atqa = 0; uint8_t sak = 0; - if(!yhy_sync_request(reader_fd, 1, &atqa)){ - return 0; - } + for(yhy_sync_request(reader_fd, 1, &atqa); atqa; yhy_sync_request(reader_fd, 0, &atqa)){ + card.uid_length = yhy_sync_anticoll(reader_fd, card.uid, sizeof(card.uid)); + if(!card.uid_length){ + 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); + //TODO parse ATQA and SAK to detect tag type - yhy_sync_select(reader_fd, card.uid, card.uid_length, &sak); - yhy_sync_hlta(reader_fd); + if(reader_tag_present(card) == TAG_READ_REQUESTED){ + //TODO read card data + } + + yhy_sync_hlta(reader_fd); + } - reader_tag_present(card); return 0; } -- cgit v1.2.3