summaryrefslogtreecommitdiff
path: root/reader_yhy.c
blob: 92d5080d41306fd4ec4336e819a44d04a0a50927 (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
#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;
}

uint8_t read_ntag(nfc_tag_info_t* tag){
	uint8_t data[16];
	size_t bytes;

	//read 16 bytes starting at block 2
	//[0-3] UID, lock bytes
	//[4-7] CC
	//[8-11] Signature
	//[12-15] Data block offsets & lengths
	bytes = yhy_sync_read(reader_fd, 2, data, sizeof(data));
	if(bytes != 16){
		//read failed, probably not a usable tag
		return FLAG_TAG_LOCKED;
	}

	//check for NTAG signature CC bytes
	if(data[4] != 0xE1 || data[5] != 0x10 || data[7]){
		return FLAG_TAG_LOCKED;
	}

	switch(data[6]){
		case 0x12:
			//NTAG213
			tag->bytes_available = 144;
			break;
		case 0x3E:
			//NTAG215
			tag->bytes_available = 496;
			break;
		case 0x6D:
			//NTAG216
			tag->bytes_available = 872;
			break;
		default:
			//unknown
			return FLAG_TAG_LOCKED;
	}

	//TODO check lock bits

	//check nfcommander signature
	if(data[8] != 0xCB || data[9] != 'N' || data[10] != 'F' || data[11] != 'C'){
		return FLAG_TAG_UNPROGRAMMED;
	}

	
	return 0;
}

uint8_t read_mifare(nfc_tag_info_t* tag){
	//TODO
	return 0;
}

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

	uint16_t atqa = 0;
	uint8_t sak = 0;
	uint8_t flags;

	for(yhy_sync_request(reader_fd, 1, &atqa); atqa; yhy_sync_request(reader_fd, 0, &atqa)){
		sak = 0;
		//ATQA should normally not be used as an indicator for UID length
		//However, the YHY UL_SELECT command does not seem to work after CL1 anticollision for some reason

		//extended length UIDs
		if(atqa & 0x00C0){
			card.uid_length = yhy_sync_ulselect(reader_fd, card.uid, sizeof(card.uid));
			//since we dont get either the CL2 SAK or any other method of identifying the particular type of PICC, just guess whether it's an NTAG
			card.type = tag_unknown;
			if(atqa == 0x0044){
				card.type = tag_ntag;
				card.granularity = 4;
			}
			else{
				printf("PICC Parameters: ATQA %04X\n", atqa);
			}
		}
		else{
			card.uid_length = yhy_sync_anticoll(reader_fd, card.uid, sizeof(card.uid));
			yhy_sync_select(reader_fd, card.uid, card.uid_length, &sak);

			card.type = tag_unknown;
			if(atqa == 0x0004 && sak == 0x08){
				card.type = tag_mifare1;
				card.bytes_available = 752;
				card.granularity = 16;
			}
			else{
				printf("PICC Parameters: ATQA %04X SAK %02X\n", atqa, sak);
			}
		}

		if(reader_tag_present(0, &card) == TAG_READ_REQUESTED){
			//read card data
			if(card.type == tag_ntag){
				flags = read_ntag(&card);
			}
			else if(card.type == tag_mifare1){
				flags = read_mifare(&card);
			}
			reader_tag_present(flags, &card);
		}

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