1
|
/* ISO14443A Manchester encoder for OpenPICC
|
2
|
* (C) 2006 by Harald Welte <hwelte@hmw-consulting.de>
|
3
|
* (C) 2007 by Henryk Plötz <henryk@ploetzli.ch>
|
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
|
/*
|
23
|
* Definitions for 106kBps, at sampling clock 1695kHz
|
24
|
*
|
25
|
* bit sample pattern for one bit cycle
|
26
|
* MSB first LSB first hex LSB first
|
27
|
* Sequence D 1010101000000000 0000000001010101 0x0055
|
28
|
* Sequence E 0000000010101010 0101010100000000 0x5500
|
29
|
* Sequence F 0000000000000000 0000000000000000 0x0000
|
30
|
*
|
31
|
* Logic 1 Sequence D
|
32
|
* Logic 0 Sequence E
|
33
|
* SOF Sequence D
|
34
|
* EOF Sequence F
|
35
|
*
|
36
|
* 212/424/848kBps: BPSK.
|
37
|
*
|
38
|
* SOF: 32 subcarrier clocks + bit '0'
|
39
|
*
|
40
|
* SOF: hex LSB first: 0x55555555 55555555 + bit '0'
|
41
|
*
|
42
|
* EOF: even parity of last byte (!)
|
43
|
*
|
44
|
*/
|
45
|
|
46
|
#define MANCHESTER_SEQ_D 0x0055
|
47
|
#define MANCHESTER_SEQ_E 0x5500
|
48
|
#define MANCHESTER_SEQ_F 0x0000
|
49
|
|
50
|
#include <errno.h>
|
51
|
#include <string.h>
|
52
|
#include "openpicc.h"
|
53
|
#include "iso14443_layer3a.h"
|
54
|
#include "iso14443a_manchester.h"
|
55
|
|
56
|
enum parity {
|
57
|
PARITY_NONE, /* Don't add a parity bit */
|
58
|
ODD_PARITY, EVEN_PARITY, /* Calculate parity */
|
59
|
PARITY_0, PARITY_1 /* Set fixed parity */
|
60
|
};
|
61
|
|
62
|
static inline void manchester_enc_byte(u_int16_t **s16, const u_int8_t data, const enum parity parity)
|
63
|
{
|
64
|
int i;
|
65
|
u_int8_t sum_1 = 0;
|
66
|
u_int16_t *samples16 = *s16;
|
67
|
|
68
|
/* append 8 sample blobs, one for each bit */
|
69
|
for (i = 0; i < 8; i++) {
|
70
|
if (data & (1 << i)) {
|
71
|
*(samples16) = MANCHESTER_SEQ_D;
|
72
|
sum_1++;
|
73
|
} else {
|
74
|
*(samples16) = MANCHESTER_SEQ_E;
|
75
|
}
|
76
|
samples16++;
|
77
|
}
|
78
|
if(parity != PARITY_NONE) {
|
79
|
/* Append parity */
|
80
|
u_int8_t par=0;
|
81
|
switch(parity) {
|
82
|
case PARITY_NONE: break;
|
83
|
case PARITY_0: par = 0; break;
|
84
|
case PARITY_1: par = 1; break;
|
85
|
case ODD_PARITY: par = (sum_1 & 0x1) ? 0 : 1; break;
|
86
|
case EVEN_PARITY: par = (sum_1 & 0x1) ? 1 : 0; break;
|
87
|
}
|
88
|
if (par)
|
89
|
*(samples16) = MANCHESTER_SEQ_D;
|
90
|
else
|
91
|
*(samples16) = MANCHESTER_SEQ_E;
|
92
|
samples16++;
|
93
|
}
|
94
|
*s16 = samples16;
|
95
|
}
|
96
|
|
97
|
int manchester_encode(u_int8_t *sample_buf, u_int16_t sample_buf_len,
|
98
|
const iso14443_frame *frame)
|
99
|
{
|
100
|
unsigned int i, enc_size;
|
101
|
u_int16_t *samples16;
|
102
|
|
103
|
if(frame->type != TYPE_A) return -EINVAL;
|
104
|
if(frame->parameters.a.format != STANDARD_FRAME) return -EINVAL; /* AC not implemented yet */
|
105
|
|
106
|
/* One bit data is 16 bit is 2 byte modulation data */
|
107
|
enc_size = 1 /* SOF */
|
108
|
+ frame->numbytes * ((frame->parameters.a.parity != NO_PARITY) ? 9 : 8) /* bits per byte */
|
109
|
+ 1 /* EOF */;
|
110
|
enc_size = enc_size /* in bits */ * 2 /* bytes modulation data per bit raw data*/;
|
111
|
|
112
|
if (sample_buf_len < enc_size)
|
113
|
return -EINVAL;
|
114
|
|
115
|
memset(sample_buf, 0, enc_size);
|
116
|
|
117
|
samples16 = (u_int16_t*)sample_buf;
|
118
|
|
119
|
/* SOF */
|
120
|
*(samples16++) = MANCHESTER_SEQ_D;
|
121
|
|
122
|
if(frame->parameters.a.parity == NO_PARITY)
|
123
|
for (i = 0; i < frame->numbytes; i++)
|
124
|
manchester_enc_byte(&samples16, frame->data[i], PARITY_NONE);
|
125
|
else if(frame->parameters.a.parity == GIVEN_PARITY)
|
126
|
for (i = 0; i < frame->numbytes; i++)
|
127
|
manchester_enc_byte(&samples16, frame->data[i],
|
128
|
(frame->parity[i/8]&(1<<(i%8))) ?PARITY_1:PARITY_0);
|
129
|
else if(frame->parameters.a.parity == PARITY)
|
130
|
for (i = 0; i < frame->numbytes; i++)
|
131
|
manchester_enc_byte(&samples16, frame->data[i], ODD_PARITY);
|
132
|
|
133
|
/* EOF */
|
134
|
*(samples16++) = MANCHESTER_SEQ_F;
|
135
|
|
136
|
return enc_size;
|
137
|
}
|
138
|
|
139
|
#if 0
|
140
|
/* Broken? */
|
141
|
#define BPSK_SPEED_212
|
142
|
|
143
|
|
144
|
static u_int32_t bpsk_sample_size(u_int8_t frame_bytelen);
|
145
|
|
146
|
int bpsk_encode(char *sample_buf, u_int16_t sample_buf_len,
|
147
|
const char *data, u_int8_t data_len)
|
148
|
{
|
149
|
/* burst of 32 sub carrier cycles */
|
150
|
memset(sample_buf, 0x55, 8);
|
151
|
|
152
|
}
|
153
|
#endif
|