summaryrefslogtreecommitdiff
path: root/reader_yhy.c
blob: 4c332ac0e8ed597287050ab3688bfb2b579b3fe5 (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
#include <stdio.h>
#include <unistd.h>
#include <sys/timerfd.h>

#include "nfcommander.h"
#include "config.h"

#include "libyhy.h"

static int reader_fd = -1;

int init(){
	char reader_version[100];

	char* port = config_get("nfc", "device");
	if(!port){
		printf("Please configure nfc.device to the reader serial port\n");
		return 1;
	}

	//TODO set cloexec, nonblock
	printf("Opening YHY reader on %s\n", port);
	reader_fd = yhy_open(port);
	if(reader_fd < 0){
		printf("Failed to open reader connection at %s\n", port);
		return 1;
	}

	if(yhy_sync_read_version(reader_fd, reader_version, sizeof(reader_version)) > 0){
		printf("Connected YHY reader reports firmware: %s\n", reader_version);
	}

	core_manage_fd(reader_fd, 1, system_reader);
	return 0;
}

int handle(int fd){
	if(fd == reader_fd){
		printf("Unhandled data on YHY fd\n");
	}
	return 0;
}

int scan(){
	nfc_tag_info_t card = {
		0
	};

	uint16_t atqa = 0;
	uint8_t sak = 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;
		}

		yhy_sync_select(reader_fd, card.uid, card.uid_length, &sak);
		//TODO parse ATQA and SAK to detect tag type

		if(reader_tag_present(card) == TAG_READ_REQUESTED){
			//TODO read card data
		}

		yhy_sync_hlta(reader_fd);
	}

	return 0;
}

static void __attribute__((destructor)) cleanup(){
	core_manage_fd(reader_fd, 0, system_reader);
	close(reader_fd);
	reader_fd = -1;
}