summaryrefslogtreecommitdiff
path: root/reader.c
diff options
context:
space:
mode:
authorcbdev <cb@cbcdn.com>2023-07-09 01:16:37 +0200
committercbdev <cb@cbcdn.com>2023-07-09 01:16:37 +0200
commit2712c7bee486ec889434d30889364ce5e89958ad (patch)
tree4640961e9957a4f1715ce2f15df7a2a37c134dd0 /reader.c
parent24c97f5b1fe1b666a3c53e8d36b0bea0ede307e0 (diff)
downloadnfcommander-2712c7bee486ec889434d30889364ce5e89958ad.tar.gz
nfcommander-2712c7bee486ec889434d30889364ce5e89958ad.tar.bz2
nfcommander-2712c7bee486ec889434d30889364ce5e89958ad.zip
Implement NTAG writing for YHY
Diffstat (limited to 'reader.c')
-rw-r--r--reader.c43
1 files changed, 43 insertions, 0 deletions
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;