1
|
/* Decoder Core for OpenPICC
|
2
|
* (C) 2006 by Harald Welte <hwelte@hmw-consulting.de>
|
3
|
*
|
4
|
* This program is free software; you can redistribute it and/or modify
|
5
|
* it under the terms of the GNU General Public License as published by
|
6
|
* the Free Software Foundation; either version 2 of the License, or
|
7
|
* (at your option) any later version.
|
8
|
*
|
9
|
* This program is distributed in the hope that it will be useful,
|
10
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
11
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
12
|
* GNU General Public License for more details.
|
13
|
*
|
14
|
* You should have received a copy of the GNU General Public License
|
15
|
* along with this program; if not, write to the Free Software
|
16
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
17
|
*
|
18
|
*/
|
19
|
|
20
|
#include <errno.h>
|
21
|
#include <sys/types.h>
|
22
|
#include <picc/decoder.h>
|
23
|
|
24
|
#include <os/dbgu.h>
|
25
|
|
26
|
static struct decoder_algo *decoder_algo[DECODER_NUM_ALGOS];
|
27
|
|
28
|
static int get_next_data(struct decoder_state *st, uint8_t *data)
|
29
|
{
|
30
|
uint8_t parity_sample;
|
31
|
uint32_t bytesample;
|
32
|
|
33
|
bytesample = st->algo->get_next_bytesample(st, &parity_sample);
|
34
|
|
35
|
return st->algo->decode_sample(bytesample, data);
|
36
|
}
|
37
|
|
38
|
/* iterate over sample buffer (size N bytes) and decode data */
|
39
|
int decoder_decode(uint8_t algo, const char *sample_buf,
|
40
|
int sample_buf_size, char *data_buf)
|
41
|
{
|
42
|
int i, ret;
|
43
|
struct decoder_state st;
|
44
|
|
45
|
if (algo >= DECODER_NUM_ALGOS)
|
46
|
return -EINVAL;
|
47
|
|
48
|
st.buf = sample_buf;
|
49
|
st.buf32 = (uint32_t *) st.buf;
|
50
|
st.bit_ofs = 0;
|
51
|
st.algo = decoder_algo[algo];
|
52
|
|
53
|
for (i = 0; i < (sample_buf_size*8)/st.algo->bits_per_sampled_char;
|
54
|
i++) {
|
55
|
ret = get_next_data(&st, &data_buf[i]);
|
56
|
if (ret < 0) {
|
57
|
DEBUGPCR("decoder error %d at data byte %u",
|
58
|
ret, i);
|
59
|
return ret;
|
60
|
}
|
61
|
}
|
62
|
|
63
|
return i+1;
|
64
|
}
|
65
|
|
66
|
int decoder_register(int algnum, struct decoder_algo *algo)
|
67
|
{
|
68
|
if (algnum >= DECODER_NUM_ALGOS)
|
69
|
return -EINVAL;
|
70
|
|
71
|
decoder_algo[algnum] = algo;
|
72
|
|
73
|
return 0;
|
74
|
}
|
75
|
|
76
|
void decoder_init(void)
|
77
|
{
|
78
|
decoder_register(DECODER_MILLER, &miller_decoder);
|
79
|
decoder_register(DECODER_NRZL, &nrzl_decoder);
|
80
|
}
|