1 |
548ec55e
|
(no author)
|
/*
|
2 |
32985a29
|
laforge
|
* ISO14443A modified Miller decoder for OpenPICC
|
3 |
548ec55e
|
(no author)
|
* (C) 2006 by Harald Welte <hwelte@hmw-consulting.de>
|
4 |
|
|
*
|
5 |
32985a29
|
laforge
|
* 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 |
548ec55e
|
(no author)
|
* 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 |
29d8974f
|
(no author)
|
|
46 |
|
|
#include <os/dbgu.h>
|
47 |
|
|
#include <picc/decoder.h>
|
48 |
548ec55e
|
(no author)
|
|
49 |
|
|
|
50 |
|
|
#define OVERSAMPLING_RATE 4
|
51 |
|
|
|
52 |
|
|
/* definitions for four-times oversampling */
|
53 |
|
|
#define SEQ_X 0x4
|
54 |
|
|
#define SEQ_Y 0x0
|
55 |
|
|
#define SEQ_Z 0x1
|
56 |
|
|
|
57 |
|
|
/* decode a single sampled bit */
|
58 |
373c172a
|
Harald Welte
|
static uint8_t miller_decode_sampled_bit(uint32_t sampled_bit)
|
59 |
548ec55e
|
(no author)
|
{
|
60 |
|
|
switch (sampled_bit) {
|
61 |
|
|
case SEQ_X:
|
62 |
|
|
return 1;
|
63 |
|
|
break;
|
64 |
|
|
case SEQ_Z:
|
65 |
|
|
case SEQ_Y:
|
66 |
|
|
return 0;
|
67 |
|
|
break;
|
68 |
|
|
default:
|
69 |
29d8974f
|
(no author)
|
DEBUGP("unknown sequence sample `%x' ", sampled_bit);
|
70 |
548ec55e
|
(no author)
|
return 2;
|
71 |
|
|
break;
|
72 |
|
|
}
|
73 |
|
|
}
|
74 |
|
|
|
75 |
|
|
/* decode a single 32bit data sample of an 8bit miller encoded word */
|
76 |
373c172a
|
Harald Welte
|
static int miller_decode_sample(uint32_t sample, uint8_t *data)
|
77 |
548ec55e
|
(no author)
|
{
|
78 |
373c172a
|
Harald Welte
|
uint8_t ret = 0;
|
79 |
29d8974f
|
(no author)
|
unsigned int i;
|
80 |
548ec55e
|
(no author)
|
|
81 |
|
|
for (i = 0; i < sizeof(sample)/OVERSAMPLING_RATE; i++) {
|
82 |
373c172a
|
Harald Welte
|
uint8_t bit = miller_decode_sampled_bit(sample & 0xf);
|
83 |
548ec55e
|
(no author)
|
|
84 |
|
|
if (bit == 1)
|
85 |
|
|
ret |= 1;
|
86 |
|
|
/* else do nothing since ret was initialized with 0 */
|
87 |
|
|
|
88 |
|
|
/* skip shifting in case of last data bit */
|
89 |
|
|
if (i == sizeof(sample)/OVERSAMPLING_RATE)
|
90 |
|
|
break;
|
91 |
|
|
|
92 |
|
|
sample = sample >> OVERSAMPLING_RATE;
|
93 |
|
|
ret = ret << 1;
|
94 |
|
|
}
|
95 |
|
|
|
96 |
|
|
*data = ret;
|
97 |
|
|
|
98 |
|
|
return ret;
|
99 |
|
|
}
|
100 |
|
|
|
101 |
373c172a
|
Harald Welte
|
static uint32_t get_next_bytesample(struct decoder_state *ms,
|
102 |
|
|
uint8_t *parity_sample)
|
103 |
548ec55e
|
(no author)
|
{
|
104 |
373c172a
|
Harald Welte
|
uint32_t ret = 0;
|
105 |
548ec55e
|
(no author)
|
|
106 |
|
|
/* get remaining bits from the current word */
|
107 |
|
|
ret = *(ms->buf32) >> ms->bit_ofs;
|
108 |
|
|
/* move to next word */
|
109 |
|
|
ms->buf32++;
|
110 |
|
|
|
111 |
|
|
/* if required, get remaining bits from next word */
|
112 |
29d8974f
|
(no author)
|
if (ms->bit_ofs)
|
113 |
548ec55e
|
(no author)
|
ret |= *(ms->buf32) << (32 - ms->bit_ofs);
|
114 |
|
|
|
115 |
|
|
*parity_sample = (*(ms->buf32) >> ms->bit_ofs & 0xf);
|
116 |
|
|
|
117 |
|
|
/* increment bit offset (modulo 32) */
|
118 |
|
|
ms->bit_ofs = (ms->bit_ofs + OVERSAMPLING_RATE) % 32;
|
119 |
|
|
|
120 |
|
|
return ret;
|
121 |
|
|
}
|
122 |
|
|
|
123 |
|
|
static struct decoder_algo miller_decoder = {
|
124 |
|
|
.oversampling_rate = OVERSAMPLING_RATE,
|
125 |
|
|
.bits_per_sampled_char = 9 * OVERSAMPLING_RATE,
|
126 |
|
|
.bytesample_mask = 0xffffffff,
|
127 |
|
|
.decode_sample = &miller_decode_sample,
|
128 |
|
|
.get_next_bytesample = &get_next_bytesample,
|
129 |
|
|
};
|