From 2712c7bee486ec889434d30889364ce5e89958ad Mon Sep 17 00:00:00 2001 From: cbdev Date: Sun, 9 Jul 2023 01:16:37 +0200 Subject: Implement NTAG writing for YHY --- reader.c | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) (limited to 'reader.c') diff --git a/reader.c b/reader.c index 7bc20e3..2c3a06b 100644 --- a/reader.c +++ b/reader.c @@ -12,10 +12,21 @@ #define MAX_PLUGIN_PATH NAME_MAX #define DEFAULT_POLL_TIMEOUT 1000 + +/* + * Pointers into the tags array are used as stable identifiers + * across the different NFCommander modules, so this array should + * never be reallocated dynamically. + * + * As most readers have a limited number of tags they can power in + * the field, this should not pose a practical limitation. + */ #define MAX_TAGS 5 #define FLAG_PRESENT 16 #define FLAG_ACTIVE 32 +#define FLAG_WRITE_FULL 64 +#define FLAG_WRITE_DYNAMIC 128 static void* reader_module = NULL; static reader_plugin_handle reader_backend_handle = NULL; @@ -195,6 +206,15 @@ int reader_tag_present(uint8_t flags, nfc_tag_info_t* tag){ //if data has not yet been fully read, request a full read return TAG_READ_REQUESTED; } + + if(tags[n].flags & (FLAG_WRITE_FULL | FLAG_WRITE_DYNAMIC)){ + flags = tags[n].flags; + tags[n].flags &= ~(FLAG_WRITE_FULL | FLAG_WRITE_DYNAMIC); + //this is kinda dangerous as we hand the reader backend our pointers + memcpy(tag, &(tags[n].info), sizeof(nfc_tag_info_t)); + + return (flags & FLAG_WRITE_FULL) ? TAG_WRITE_FULL : TAG_WRITE_DYNAMIC; + } return 0; } } @@ -214,6 +234,23 @@ int reader_tag_present(uint8_t flags, nfc_tag_info_t* tag){ return 0; } +int reader_write(nfc_tag_info_t* tag, uint8_t flags){ + size_t n = 0; + + for(n = 0; n < MAX_TAGS; n++){ + if(tag == &(tags[n].info)){ + if(flags & TAG_WRITE_FULL){ + tags[n].flags |= FLAG_WRITE_FULL; + } + + if(flags & TAG_WRITE_DYNAMIC){ + tags[n].flags |= FLAG_WRITE_DYNAMIC; + } + } + } + return 0; +} + int reader_handle(int fd){ uint8_t buffer[1024]; @@ -240,6 +277,8 @@ int reader_handle(int fd){ } void reader_free(){ + size_t n = 0; + if(reader_module){ dlclose(reader_module); reader_backend_handle = NULL; @@ -247,6 +286,10 @@ void reader_free(){ reader_module = NULL; } + for(n = 0; n < MAX_TAGS; n++){ + tag_info_free(&(tags[n].info)); + } + core_manage_fd(timer_fd, 0, system_reader); close(timer_fd); timer_fd = -1; -- cgit v1.2.3