Imported Upstream version 3.0
[debian/gnuradio] / gnuradio-core / src / lib / general / gr_check_counting_s.cc
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2004 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., 51 Franklin Street,
20  * Boston, MA 02110-1301, USA.
21  */
22
23 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
26
27 #include <gr_check_counting_s.h>
28 #include <gr_io_signature.h>
29 #include <stdlib.h>
30 #include <stdio.h>
31
32 gr_check_counting_s_sptr
33 gr_make_check_counting_s (bool do_32bit)
34 {
35   return gr_check_counting_s_sptr (new gr_check_counting_s (do_32bit));
36 }
37
38 gr_check_counting_s::gr_check_counting_s (bool do_32bit)
39   : gr_sync_block ("gr_check_counting",
40     gr_make_io_signature (1, 1, sizeof (short)),
41     gr_make_io_signature (0, 0, 0)),
42     d_state(SEARCHING), d_history (0), d_current_count (0), d_current_count_32bit(0),
43     d_total_errors (0), d_total_shorts (0),
44     d_do_32bit(do_32bit)
45 {
46   enter_SEARCHING ();
47 }
48
49 int
50 gr_check_counting_s::work (int noutput_items,
51         gr_vector_const_void_star &input_items,
52         gr_vector_void_star &output_items)
53 {
54   unsigned short *in = (unsigned short *) input_items[0];
55   if(d_do_32bit)
56     return check_32bit(noutput_items,in);
57   else
58     return check_16bit(noutput_items,in);
59 }
60
61 int
62 gr_check_counting_s::check_16bit (int noutput_items,
63         unsigned short * in)
64 {
65   for (int i = 0; i < noutput_items; i++){
66     unsigned short  x = in[i];
67     
68     switch (d_state){
69
70     case SEARCHING:
71       if (x == d_current_count){
72        right ();
73        log_error (d_current_count, x);
74        d_current_count = d_current_count + 1;
75        if (right_three_times ())
76          enter_LOCKED ();
77       }
78       else {
79         wrong ();
80         log_error (d_current_count, x);
81         d_current_count = x + 1;
82       }
83       break;
84
85     case LOCKED:
86       if (x == d_current_count){
87         right ();
88         d_current_count = d_current_count + 1;
89       }
90       else {
91         wrong ();
92         log_error (d_current_count, x);
93         d_current_count = d_current_count + 1;
94         if (wrong_three_times ())
95           enter_SEARCHING ();
96       }
97       break;
98
99     default:
100       abort ();
101     }
102
103     d_total_shorts++;
104   }
105
106   return noutput_items;
107 }
108
109 int
110 gr_check_counting_s::check_32bit (int noutput_items,
111         unsigned short * in)
112 {
113
114   for (int i = 0; i < noutput_items-1; i+=2){
115     unsigned int  x_high16bits = in[i];
116     unsigned int  x_low16bits = in[i+1];
117     unsigned int  x = x_high16bits<<16 | x_low16bits;
118     
119     switch (d_state){
120
121     case SEARCHING:
122       if (x == d_current_count_32bit){
123         right ();
124         log_error_32bit (d_current_count_32bit, x);
125         d_current_count_32bit = d_current_count_32bit + 1;
126         if (right_three_times ())
127           enter_LOCKED ();
128       }
129       else {
130         wrong ();
131         log_error_32bit (d_current_count_32bit, x);
132         d_current_count_32bit = x + 1;
133       }
134       break;
135
136     case LOCKED:
137       if (x == d_current_count_32bit){
138         right ();
139         d_current_count_32bit = d_current_count_32bit + 1;
140       }
141       else {
142         wrong ();
143         log_error_32bit (d_current_count_32bit, x);
144         d_current_count_32bit = d_current_count_32bit + 1;
145         if (wrong_three_times ())
146           enter_SEARCHING ();
147       }
148       break;
149
150     default:
151       abort ();
152     }
153
154     d_total_shorts++;
155   }
156
157   return noutput_items;
158 }
159
160 void
161 gr_check_counting_s::enter_SEARCHING ()
162 {
163   d_state = SEARCHING;
164   fprintf (stdout, "gr_check_counting: enter_SEARCHING at offset %8ld (0x%08lx)\n",
165      d_total_shorts, d_total_shorts);
166 }
167
168 void
169 gr_check_counting_s::enter_LOCKED ()
170 {
171   d_state = LOCKED;
172   fprintf (stdout, "gr_check_counting: enter_LOCKED at offset %8ld (0x%08lx)\n",
173      d_total_shorts, d_total_shorts);
174 }
175
176 void
177 gr_check_counting_s::log_error (unsigned short expected, unsigned short actual)
178 {
179   fprintf (stdout, 
180 "gr_check_counting: expected %5d (0x%04x) got %5d (0x%04x) offset %8ld (0x%08lx)\n",
181      expected, expected, actual, actual, d_total_shorts, d_total_shorts);
182 }
183
184 void
185 gr_check_counting_s::log_error_32bit (unsigned int expected, unsigned int actual)
186 {
187   fprintf (stdout, 
188 "gr_check_counting: expected %10d (0x%08x) got %10d (0x%08x) offset %8ld (0x%08lx)\n",
189      expected, expected, actual, actual, d_total_shorts, d_total_shorts);
190 }