blob: c9f03482490d67e94a6668d84d9995a13a16cfe2 [file] [log] [blame]
piotrfaacc722014-07-20 23:48:32 +02001/* -*- c++ -*- */
2/*
3 * Copyright 2005 Free Software Foundation, Inc.
4 *
5 * This file is part of GNU Radio
6 *
7 * GNU Radio is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2, or (at your option)
10 * any later version.
11 *
12 * GNU Radio is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with GNU Radio; see the file COPYING. If not, write to
19 * the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
21 */
22
23#include "fire_crc.h"
24#include <stdio.h>
25#include <string.h>
26
27#define REM(x, y) (x) % (y)
28
29static int FC_syndrome_shift(FC_CTX *ctx, unsigned int bit);
30
31static int
32outit(int *data, int len)
33{
34 int i;
35
36 for (i = 0; i < len; i++)
37 printf("%d ", data[i]);
38 printf("\n");
39}
40
41int
42FC_init(FC_CTX *ctx, unsigned int crc_size, unsigned int data_size)
43{
44 ctx->crc_size = crc_size;
45 ctx->data_size = data_size;
46 ctx->syn_start = 0;
47
48 return 0;
49}
50
51int
52FC_check_crc(FC_CTX *ctx, unsigned char *input_bits, unsigned char *control_data)
53{
54 int j,error_count = 0, error_index = 0, success_flag = 0, syn_index = 0;
55 unsigned int i;
56
57 ctx->syn_start = 0;
58 // reset the syndrome register
59 memset(ctx->syndrome_reg, 0, sizeof ctx->syndrome_reg);
60
61 // shift in the data bits
62 for (i=0; i < ctx->data_size; i++) {
63 error_count = FC_syndrome_shift(ctx, input_bits[i]);
64 control_data[i] = input_bits[i];
65 }
66
67 // shift in the crc bits
68 for (i=0; i < ctx->crc_size; i++) {
69 error_count = FC_syndrome_shift(ctx, 1-input_bits[i+ctx->data_size]);
70 }
71
72 // Find position of error burst
73 if (error_count == 0) {
74 error_index = 0;
75 }
76 else {
77 error_index = 1;
78 error_count = FC_syndrome_shift(ctx, 0);
79 error_index += 1;
80 while (error_index < (ctx->data_size + ctx->crc_size) ) {
81 error_count = FC_syndrome_shift(ctx, 0);
82 error_index += 1;
83 if ( error_count == 0 ) break;
84 }
85 }
86
87 // Test for correctable errors
88 //printf("error_index %d\n",error_index);
89 if (error_index == 224) success_flag = 0;
90 else {
91
92 // correct index depending on the position of the error
93 if (error_index == 0) syn_index = error_index;
94 else syn_index = error_index - 1;
95
96 // error burst lies within data bits
97 if (error_index < 184) {
98 //printf("error < bit 184,%d\n",error_index);
99 j = error_index;
100 while ( j < (error_index+12) ) {
101 if (j < 184) {
102 control_data[j] = control_data[j] ^
103 ctx->syndrome_reg[REM(ctx->syn_start+39-j+syn_index,40)];
104 }
105 else break;
106 j = j + 1;
107 }
108 }
109 else if ( error_index > 212 ) {
110 //printf("error > bit 212,%d\n",error_index);
111 j = 0;
112 while ( j < (error_index - 212) ) {
113 control_data[j] = control_data[j] ^
114 ctx->syndrome_reg[REM(ctx->syn_start+39-j-224+syn_index,40)];
115 j = j + 1;
116 }
117 }
118 // for 183 < error_index < 213 error in parity alone so ignore
119 success_flag = 1;
120 }
121 return success_flag;
122}
123
124static int
125FC_syndrome_shift(FC_CTX *ctx, unsigned int bit)
126{
127 int error_count = 0;
128 unsigned int i;
129
130 if (ctx->syn_start == 0)
131 ctx->syn_start = 39;
132 else ctx->syn_start -= 1;
133
134 int temp_syndrome_reg[sizeof ctx->syndrome_reg];
135
136 memcpy(temp_syndrome_reg, ctx->syndrome_reg, sizeof temp_syndrome_reg);
137
138 temp_syndrome_reg[REM(ctx->syn_start+3,40)] =
139 ctx->syndrome_reg[REM(ctx->syn_start+3,40)] ^
140 ctx->syndrome_reg[ctx->syn_start];
141 temp_syndrome_reg[REM(ctx->syn_start+17,40)] =
142 ctx->syndrome_reg[REM(ctx->syn_start+17,40)] ^
143 ctx->syndrome_reg[ctx->syn_start];
144 temp_syndrome_reg[REM(ctx->syn_start+23,40)] =
145 ctx->syndrome_reg[REM(ctx->syn_start+23,40)] ^
146 ctx->syndrome_reg[ctx->syn_start];
147 temp_syndrome_reg[REM(ctx->syn_start+26,40)] =
148 ctx->syndrome_reg[REM(ctx->syn_start+26,40)] ^
149 ctx->syndrome_reg[ctx->syn_start];
150
151 temp_syndrome_reg[REM(ctx->syn_start+4,40)] =
152 ctx->syndrome_reg[REM(ctx->syn_start+4,40)] ^ bit;
153 temp_syndrome_reg[REM(ctx->syn_start+6,40)] =
154 ctx->syndrome_reg[REM(ctx->syn_start+6,40)] ^ bit;
155 temp_syndrome_reg[REM(ctx->syn_start+10,40)] =
156 ctx->syndrome_reg[REM(ctx->syn_start+10,40)] ^ bit;
157 temp_syndrome_reg[REM(ctx->syn_start+16,40)] =
158 ctx->syndrome_reg[REM(ctx->syn_start+16,40)] ^ bit;
159 temp_syndrome_reg[REM(ctx->syn_start+27,40)] =
160 ctx->syndrome_reg[REM(ctx->syn_start+27,40)] ^ bit;
161 temp_syndrome_reg[REM(ctx->syn_start+29,40)] =
162 ctx->syndrome_reg[REM(ctx->syn_start+29,40)] ^ bit;
163 temp_syndrome_reg[REM(ctx->syn_start+33,40)] =
164 ctx->syndrome_reg[REM(ctx->syn_start+33,40)] ^ bit;
165 temp_syndrome_reg[REM(ctx->syn_start+39,40)] =
166 ctx->syndrome_reg[REM(ctx->syn_start+39,40)] ^ bit;
167
168 temp_syndrome_reg[ctx->syn_start] = ctx->syndrome_reg[ctx->syn_start] ^ bit;
169
170 memcpy(ctx->syndrome_reg, temp_syndrome_reg, sizeof ctx->syndrome_reg);
171
172 for (i = 0; i < 28; i++) {
173 error_count = error_count + ctx->syndrome_reg[REM(ctx->syn_start+i,40)];
174 }
175 return error_count;
176}
177
178