1
|
/*
|
2
|
* ISO14443A modified Miller decoder for OpenPICC
|
3
|
* (C) 2006 by Harald Welte <hwelte@hmw-consulting.de>
|
4
|
*
|
5
|
* This program is free software; you can redistribute it and/or modify
|
6
|
* it under the terms of the GNU General Public License as published by
|
7
|
* the Free Software Foundation; either version 2 of the License, or
|
8
|
* (at your option) any later version.
|
9
|
*
|
10
|
* This program is distributed in the hope that it will be useful,
|
11
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
13
|
* GNU General Public License for more details.
|
14
|
*
|
15
|
* You should have received a copy of the GNU General Public License
|
16
|
* along with this program; if not, write to the Free Software
|
17
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
18
|
*
|
19
|
*/
|
20
|
|
21
|
/*
|
22
|
* LSB First LSB hex
|
23
|
* Sequence X 0010 0100 0x4
|
24
|
* Sequence Y 0000 0000 0x0
|
25
|
* Sequence Z 1000 0001 0x1
|
26
|
*
|
27
|
* Logic 1 Sequence X
|
28
|
* Logic 0 Sequence Y with two exceptions:
|
29
|
* - if there are more contiguous 0, Z used from second one
|
30
|
* - if the first bit after SOF is 0, sequence Z used for all contig 0's
|
31
|
* SOF Sequence Z
|
32
|
* EOF Logic 0 followed by Sequence Y
|
33
|
*
|
34
|
* cmd hex bits symbols hex (quad-sampled)
|
35
|
*
|
36
|
* REQA 0x26 S 0110010 E Z ZXXYZXY ZY 0x10410441
|
37
|
* WUPA 0x52 S 0100101 E Z ZXYZXYX YY 0x04041041
|
38
|
*
|
39
|
* SOF is 'eaten' by SSC start condition (Compare 0). Remaining bits are
|
40
|
* mirrored, e.g. samples for LSB of first byte are & 0xf
|
41
|
*
|
42
|
*/
|
43
|
|
44
|
#include <sys/types.h>
|
45
|
|
46
|
#include "openpicc.h"
|
47
|
#include "dbgu.h"
|
48
|
#include "decoder.h"
|
49
|
#include "iso14443_layer3a.h"
|
50
|
|
51
|
#ifdef FOUR_TIMES_OVERSAMPLING
|
52
|
#define OVERSAMPLING_RATE 4
|
53
|
|
54
|
/* definitions for four-times oversampling */
|
55
|
#define SEQ_X 0x4
|
56
|
#define SEQ_Y 0x0
|
57
|
#define SEQ_Z 0x1
|
58
|
#else
|
59
|
#define OVERSAMPLING_RATE 2
|
60
|
#define SEQ_X 0x2
|
61
|
#define SEQ_Y 0x0
|
62
|
#define SEQ_Z 0x1
|
63
|
#endif
|
64
|
|
65
|
/* decode a single sampled bit */
|
66
|
static inline u_int8_t miller_decode_sampled_bit(u_int32_t sampled_bit)
|
67
|
{
|
68
|
switch (sampled_bit) {
|
69
|
case SEQ_X:
|
70
|
return 1;
|
71
|
break;
|
72
|
case SEQ_Z:
|
73
|
case SEQ_Y:
|
74
|
return 0;
|
75
|
break;
|
76
|
default:
|
77
|
DEBUGP("unknown sequence sample `%x' ", sampled_bit);
|
78
|
return 2;
|
79
|
break;
|
80
|
}
|
81
|
}
|
82
|
|
83
|
/* decode a single 32bit data sample of an 8bit miller encoded word */
|
84
|
static int miller_decode_sample(u_int32_t sample, u_int8_t *data)
|
85
|
{
|
86
|
u_int8_t ret = 0;
|
87
|
unsigned int i;
|
88
|
|
89
|
for (i = 0; i < sizeof(sample)/OVERSAMPLING_RATE; i++) {
|
90
|
u_int8_t bit = miller_decode_sampled_bit(sample & 0xf);
|
91
|
|
92
|
if (bit == 1)
|
93
|
ret |= 1;
|
94
|
/* else do nothing since ret was initialized with 0 */
|
95
|
|
96
|
/* skip shifting in case of last data bit */
|
97
|
if (i == sizeof(sample)/OVERSAMPLING_RATE)
|
98
|
break;
|
99
|
|
100
|
sample = sample >> OVERSAMPLING_RATE;
|
101
|
ret = ret << 1;
|
102
|
}
|
103
|
|
104
|
*data = ret;
|
105
|
|
106
|
return ret;
|
107
|
}
|
108
|
|
109
|
static u_int32_t get_next_bytesample(struct decoder_state *ms,
|
110
|
u_int8_t *parity_sample)
|
111
|
{
|
112
|
u_int32_t ret = 0;
|
113
|
|
114
|
/* get remaining bits from the current word */
|
115
|
ret = *(ms->buf32) >> ms->bit_ofs;
|
116
|
/* move to next word */
|
117
|
ms->buf32++;
|
118
|
|
119
|
/* if required, get remaining bits from next word */
|
120
|
if (ms->bit_ofs)
|
121
|
ret |= *(ms->buf32) << (32 - ms->bit_ofs);
|
122
|
|
123
|
*parity_sample = (*(ms->buf32) >> ms->bit_ofs & 0xf);
|
124
|
|
125
|
/* increment bit offset (modulo 32) */
|
126
|
ms->bit_ofs = (ms->bit_ofs + OVERSAMPLING_RATE) % 32;
|
127
|
|
128
|
return ret;
|
129
|
}
|
130
|
|
131
|
struct decoder_algo miller_decoder = {
|
132
|
.oversampling_rate = OVERSAMPLING_RATE,
|
133
|
.bits_per_sampled_char = 9 * OVERSAMPLING_RATE,
|
134
|
.bytesample_mask = 0xffffffff,
|
135
|
.decode_sample = &miller_decode_sample,
|
136
|
.get_next_bytesample = &get_next_bytesample,
|
137
|
};
|