summaryrefslogtreecommitdiff
path: root/src/rfid_iso14443_common.c
blob: f72d44ccb5db821c501625730f02001adbf1aafa (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

/*
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License version 2 
 *  as published by the Free Software Foundation
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
 */


static unsigned int fsdi_table[] = { 16, 24, 32, 40, 48, 64, 96, 128, 256 };

int iso14443_fsdi_to_fsd(unsigned int *fsd, unsigned char fsdi)
{
	/* ISO 14443-4:2000(E) Section 5.1. */
	if (fsdi > sizeof(fsdi_table)/sizeof(unsigned int))
		return -1;

	*fsd = fsdi_table[fsdi];
	return 0;
}

int iso14443_fsd_to_fsdi(unsigned char *fsdi, unsigned int fsd)
{
	int i;

	for (i = 0; i < sizeof(fsdi_table)/sizeof(unsigned int); i++) {
		if (fsdi_table[i] == fsd) {
			*fsdi = i;
			return 0;
		}
	}

	return -1;
}

/* calculate the fsd that is equal or the closest smaller value
 * that can be coded as fsd */
unsigned int iso14443_fsd_approx(unsigned int fsd)
{
	unsigned int tbl_size = sizeof(fsdi_table)/sizeof(unsigned int);
	int i;

	for (i = tbl_size-1; i >= 0; i--) {
		if (fsdi_table[i] <= fsd)
			return fsdi_table[i];
	}
	/* not reached: return smallest possible FSD */
	return fsdi_table[0];
}

personal git repositories of Harald Welte. Your mileage may vary