1 |
29ea5bbf
|
henryk
|
/* ISO14443A Manchester encoder for OpenPICC
|
2 |
|
|
* (C) 2006 by Harald Welte <hwelte@hmw-consulting.de>
|
3 |
202b56a4
|
henryk
|
* (C) 2007 by Henryk Plötz <henryk@ploetzli.ch>
|
4 |
29ea5bbf
|
henryk
|
*
|
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 |
16d42d5f
|
henryk
|
* Sequence F 0000000000000000 0000000000000000 0x0000
|
30 |
29ea5bbf
|
henryk
|
*
|
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 |
16d42d5f
|
henryk
|
#define MANCHESTER_SEQ_F 0x0000
|
49 |
29ea5bbf
|
henryk
|
|
50 |
|
|
#include <errno.h>
|
51 |
|
|
#include <string.h>
|
52 |
|
|
#include "openpicc.h"
|
53 |
16d42d5f
|
henryk
|
#include "iso14443_layer3a.h"
|
54 |
|
|
#include "iso14443a_manchester.h"
|
55 |
29ea5bbf
|
henryk
|
|
56 |
16d42d5f
|
henryk
|
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 |
29ea5bbf
|
henryk
|
};
|
61 |
|
|
|
62 |
9cde1dd0
|
henryk
|
static inline void manchester_enc_byte(u_int16_t **s16, const u_int8_t data, const enum parity parity)
|
63 |
29ea5bbf
|
henryk
|
{
|
64 |
|
|
int i;
|
65 |
|
|
u_int8_t sum_1 = 0;
|
66 |
16d42d5f
|
henryk
|
u_int16_t *samples16 = *s16;
|
67 |
29ea5bbf
|
henryk
|
|
68 |
|
|
/* append 8 sample blobs, one for each bit */
|
69 |
|
|
for (i = 0; i < 8; i++) {
|
70 |
|
|
if (data & (1 << i)) {
|
71 |
16d42d5f
|
henryk
|
*(samples16) = MANCHESTER_SEQ_D;
|
72 |
29ea5bbf
|
henryk
|
sum_1++;
|
73 |
|
|
} else {
|
74 |
16d42d5f
|
henryk
|
*(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 |
29ea5bbf
|
henryk
|
}
|
88 |
16d42d5f
|
henryk
|
if (par)
|
89 |
|
|
*(samples16) = MANCHESTER_SEQ_D;
|
90 |
|
|
else
|
91 |
|
|
*(samples16) = MANCHESTER_SEQ_E;
|
92 |
|
|
samples16++;
|
93 |
29ea5bbf
|
henryk
|
}
|
94 |
16d42d5f
|
henryk
|
*s16 = samples16;
|
95 |
29ea5bbf
|
henryk
|
}
|
96 |
|
|
|
97 |
16d42d5f
|
henryk
|
int manchester_encode(u_int8_t *sample_buf, u_int16_t sample_buf_len,
|
98 |
|
|
const iso14443_frame *frame)
|
99 |
29ea5bbf
|
henryk
|
{
|
100 |
16d42d5f
|
henryk
|
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 |
be5251b6
|
henryk
|
/* One bit data is 16 bit is 2 byte modulation data */
|
107 |
d3bab6e9
|
henryk
|
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 |
29ea5bbf
|
henryk
|
|
112 |
|
|
if (sample_buf_len < enc_size)
|
113 |
|
|
return -EINVAL;
|
114 |
16d42d5f
|
henryk
|
|
115 |
fa61e60d
|
henryk
|
memset(sample_buf, 0, enc_size);
|
116 |
|
|
|
117 |
16d42d5f
|
henryk
|
samples16 = (u_int16_t*)sample_buf;
|
118 |
29ea5bbf
|
henryk
|
|
119 |
|
|
/* SOF */
|
120 |
16d42d5f
|
henryk
|
*(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 |
be5251b6
|
henryk
|
manchester_enc_byte(&samples16, frame->data[i],
|
128 |
|
|
(frame->parity[i/8]&(1<<(i%8))) ?PARITY_1:PARITY_0);
|
129 |
16d42d5f
|
henryk
|
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 |
29ea5bbf
|
henryk
|
|
133 |
|
|
/* EOF */
|
134 |
16d42d5f
|
henryk
|
*(samples16++) = MANCHESTER_SEQ_F;
|
135 |
29ea5bbf
|
henryk
|
|
136 |
|
|
return enc_size;
|
137 |
|
|
}
|
138 |
16d42d5f
|
henryk
|
|
139 |
29ea5bbf
|
henryk
|
#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
|