summaryrefslogtreecommitdiff
path: root/reader.c
blob: 7bc20e32847cff6bfd03aea57f82f540cf642a97 (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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
#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"
#include "command.h"

#define MAX_PLUGIN_PATH NAME_MAX
#define DEFAULT_POLL_TIMEOUT 1000
#define MAX_TAGS 5

#define FLAG_PRESENT 16
#define FLAG_ACTIVE 32

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_print_tag(nfc_tag_info_t tag){
	size_t n = 0;
	char* type = "UNST";
	switch(tag.type){
		case tag_unset: type = "UNST"; break;
		case tag_unknown: type = "UNKN"; break;
		case tag_mifare1: type = "MIFR"; break;
		case tag_ntag: type = "NTAG"; break;
		case tag_desfire: type = "DESF"; break;
	}

	printf("[%s:%lu ", type, tag.bytes_available);
	if(tag.static_length){
		printf("D:%lu:%lu ", tag.static_length, tag.dynamic_length);
	}
	for(n = 0; n < tag.uid_length; n++){
		printf("%02X", tag.uid[n]);
	}
	printf("]");
}

static void reader_expire(){
	size_t n = 0;
	for(n = 0; n < MAX_TAGS; n++){
		tags[n].flags &= ~(FLAG_PRESENT);
	}
}

static void tag_info_free(nfc_tag_info_t* tag){
	free(tag->static_data);
	tag->static_data = NULL;
	tag->static_length = 0;

	free(tag->dynamic_data);
	tag->dynamic_data = NULL;
	tag->dynamic_length = 0;
}

static int reader_process(){
	size_t n = 0;
	for(n = 0; n < MAX_TAGS; n++){
		if(tags[n].flags & FLAG_PRESENT){
			if(!(tags[n].flags & FLAG_ACTIVE)){
				//new tag
				tags[n].flags |= FLAG_ACTIVE;
				if(tags[n].flags & FLAG_TAG_LOCKED){
					printf("Slot %2lu: ", n);
					reader_print_tag(tags[n].info);
					printf(" locked, unusable\n");
					return 0;
				}
				if(tags[n].flags & FLAG_TAG_UNPROGRAMMED){
					printf("Slot %2lu: ", n);
					reader_print_tag(tags[n].info);
					printf(" unprogrammed\n");
					//TODO
					return 0;
				}
				if(tags[n].flags & FLAG_TAG_DATA_VALID){
					printf("Slot %2lu: ", n);
					reader_print_tag(tags[n].info);
					printf(" valid\n");
					command_start(&(tags[n].info));
					return 0;
				}

				printf("Tag detected (slot %lu), but no state flags set by reader\n", n);
			}
		}
		else{
			if(tags[n].flags & FLAG_ACTIVE){
				//tag was removed - free any allocated members
				command_stop(&(tags[n].info));
				tag_info_free(&(tags[n].info));
				printf("Tag in slot %lu removed\n", n);
			}

			//reset all flags
			tags[n].flags = 0;
		}
	}
	return 0;
}

int reader_tag_present(uint8_t flags, nfc_tag_info_t* tag){
	size_t n = 0;

	//sanitize input flags
	flags &= (TAG_STATUS_FLAGS);

	//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 present
			tags[n].flags |= FLAG_PRESENT;

			//if full data submitted, copy in and set flags
			if(!(tags[n].flags & TAG_STATUS_FLAGS)){
				if(flags & TAG_STATUS_FLAGS){
					memcpy(&(tags[n].info), tag, sizeof(nfc_tag_info_t));
					tags[n].flags |= flags;
					return 0;
				}
				//if data has not yet been fully read, request a full read
				return TAG_READ_REQUESTED;
			}
			return 0;
		}
	}

	//if not, add it
	for(n = 0; n < MAX_TAGS; n++){
		if(!tags[n].flags){
			tags[n].flags |= FLAG_PRESENT | flags;
			memcpy(&(tags[n].info), tag, sizeof(nfc_tag_info_t));
			return (tags[n].flags & TAG_STATUS_FLAGS) ? 0 : TAG_READ_REQUESTED;
		}
	}

	//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;
}