paths changed
[kk_librfid.git] / firmware / src / picc / decoder.c
blobbdd910e8a634abd8d368aea0385072b27f0fc18c
1 /* Decoder Core for OpenPICC
2 * (C) 2006 by Harald Welte <hwelte@hmw-consulting.de>
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.
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.
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
20 #include <errno.h>
21 #include <sys/types.h>
22 #include <picc/decoder.h>
24 #include <os/dbgu.h>
26 static struct decoder_algo *decoder_algo[DECODER_NUM_ALGOS];
28 static int get_next_data(struct decoder_state *st, u_int8_t *data)
30 u_int8_t parity_sample;
31 u_int32_t bytesample;
33 bytesample = st->algo->get_next_bytesample(st, &parity_sample);
35 return st->algo->decode_sample(bytesample, data);
38 /* iterate over sample buffer (size N bytes) and decode data */
39 int decoder_decode(u_int8_t algo, const char *sample_buf,
40 int sample_buf_size, char *data_buf)
42 int i, ret;
43 struct decoder_state st;
45 if (algo >= DECODER_NUM_ALGOS)
46 return -EINVAL;
48 st.buf = sample_buf;
49 st.buf32 = (u_int32_t *) st.buf;
50 st.bit_ofs = 0;
51 st.algo = decoder_algo[algo];
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;
63 return i+1;
66 int decoder_register(int algnum, struct decoder_algo *algo)
68 if (algnum >= DECODER_NUM_ALGOS)
69 return -EINVAL;
71 decoder_algo[algnum] = algo;
73 return 0;
76 void decoder_init(void)
78 decoder_register(DECODER_MILLER, &miller_decoder);
79 decoder_register(DECODER_NRZL, &nrzl_decoder);