summaryrefslogtreecommitdiff
path: root/reader.c
blob: 0704fca8fd99f59b82eeeb1035862d3fb7d641f7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#include <stdio.h>
#include <limits.h>
#include <dlfcn.h>
#include <sys/timerfd.h>
#include <unistd.h>
#include <string.h>

#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_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;
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] = "";
	char* reader = config_get("nfc", "reader");

	if(!reader){
		printf("No reader plugin configured\n");
		return -1;
	}

	snprintf(plugin, sizeof(plugin), "./reader_%s.so", reader);
	reader_module = dlopen(plugin, RTLD_NOW);

	if(!reader_module){
		printf("Failed to load reader plugin %s: %s\n", plugin, dlerror());
		return -1;
	}

	reader_backend_handle = dlsym(reader_module, "handle");
	reader_backend_scan = dlsym(reader_module, "scan");

	//call initializer
	reader_plugin_init init = dlsym(reader_module, "init");
	if(!reader_backend_scan || !init || init()){
		printf("Failed to initialize the reader\n");
		return 1;
	}

	return 0;
}

int reader_init(){
	//initialize poll timer
	size_t poll_timeout = DEFAULT_POLL_TIMEOUT;
	struct itimerspec timer_cfg = {
		0
	};

	if(config_get("nfc", "interval")){
		poll_timeout = strtoul(config_get("nfc", "interval"), NULL, 10);
	}

	timer_cfg.it_value.tv_sec = timer_cfg.it_interval.tv_sec = poll_timeout / 1000;
	timer_cfg.it_value.tv_nsec = timer_cfg.it_interval.tv_nsec = (poll_timeout % 1000) * 1e6;

	//create timerfd
	timer_fd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC);
	if(timer_fd < 0){
		printf("Failed to create timer fd\n");
		return 1;
	}

	if(timerfd_settime(timer_fd, 0, &timer_cfg, NULL)){
		printf("Failed to set timer\n");
		return 1;
	}

	core_manage_fd(timer_fd, 1, system_reader);
	return reader_init_backend();
}

static void reader_expire(){
	size_t n = 0;
	for(n = 0; n < MAX_TAGS; n++){
		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_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_ACTIVE;
			}
		}
		else{
			if(tags[n].flags & FLAG_ACTIVE){
				//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_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_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_PRESENT;
			memcpy(&(tags[n].info), &tag, sizeof(tag));

			//TODO set FULLY_READ flag if necessary
			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];

	if(fd == timer_fd){
		//acknowledge timer
		read(fd, buffer, sizeof(buffer));

		//mark all tags expired
		reader_expire();

		//scan all tags
		if(reader_backend_scan()){
			return 1;
		}

		//handle changes in active set
		return reader_process();
	}

	if(reader_backend_handle){
		return reader_backend_handle(fd);
	}
	return 0;
}

void reader_free(){
	if(reader_module){
		dlclose(reader_module);
		reader_backend_handle = NULL;
		reader_backend_scan = NULL;
		reader_module = NULL;
	}

	core_manage_fd(timer_fd, 0, system_reader);
	close(timer_fd);
	timer_fd = -1;
}