libvrt: remove from 3.3 API.
[debian/gnuradio] / gr-noaa / lib / noaa_hrpt_deframer.cc
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2009 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 3, 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 <noaa_hrpt_deframer.h>
28 #include <gr_io_signature.h>
29 #include <noaa_hrpt.h>
30 #include <cstring>
31 #include <cstdio>
32
33 #define ST_IDLE   0
34 #define ST_SYNCED 1
35
36 noaa_hrpt_deframer_sptr
37 noaa_make_hrpt_deframer()
38 {
39   return gnuradio::get_initial_sptr(new noaa_hrpt_deframer());
40 }
41
42 noaa_hrpt_deframer::noaa_hrpt_deframer()
43   : gr_block("noaa_hrpt_deframer",
44              gr_make_io_signature(1, 1, sizeof(char)),
45              gr_make_io_signature(1, 1, sizeof(short)))
46 {
47   set_output_multiple(6); // room for writing full sync when received
48   d_mid_bit = true;
49   d_last_bit = 0;
50   enter_idle();
51 }
52
53 void
54 noaa_hrpt_deframer::enter_idle()
55 {
56   d_state = ST_IDLE;
57 }
58
59 void
60 noaa_hrpt_deframer::enter_synced()
61 {
62   d_state = ST_SYNCED;
63   d_bit_count = HRPT_BITS_PER_WORD;
64   d_word_count = HRPT_MINOR_FRAME_WORDS-HRPT_SYNC_WORDS;
65   d_word = 0;
66 }
67
68 int
69 noaa_hrpt_deframer::general_work(int noutput_items,
70                                  gr_vector_int &ninput_items,
71                                  gr_vector_const_void_star &input_items,
72                                  gr_vector_void_star &output_items)
73 {
74   int ninputs = ninput_items[0];
75   const char *in = (const char *)input_items[0];
76   unsigned short *out = (unsigned short *)output_items[0];
77
78   int i = 0, j = 0;
79   while (i < ninputs && j < noutput_items) {
80     char bit = in[i++];
81     char diff = bit^d_last_bit;
82     d_last_bit = bit;
83
84     // Wait for transition if not synced, otherwise, alternate bits
85     if (d_mid_bit && (diff | (d_state == ST_SYNCED))) {
86       switch (d_state) {
87       case ST_IDLE:
88         d_shifter = (d_shifter << 1) | bit; // MSB transmitted first
89         
90         if ((d_shifter & 0x0FFFFFFFFFFFFFFFLL) == HRPT_MINOR_FRAME_SYNC) {
91           out[j++] = HRPT_SYNC1;
92           out[j++] = HRPT_SYNC2;
93           out[j++] = HRPT_SYNC3;
94           out[j++] = HRPT_SYNC4;
95           out[j++] = HRPT_SYNC5;
96           out[j++] = HRPT_SYNC6;
97           enter_synced();
98         }
99         break;
100         
101       case ST_SYNCED:
102         d_word = (d_word << 1) | bit; // MSB transmitted first
103         if (--d_bit_count == 0) {
104           out[j++] = d_word;
105           d_word = 0;
106           d_bit_count = HRPT_BITS_PER_WORD;
107           if (--d_word_count == 0) {
108             enter_idle();
109           }
110         }
111         break;
112         
113       default:
114         throw std::runtime_error("noaa_hrpt_deframer: bad state\n");
115       }
116
117       d_mid_bit = false;
118     }
119     else {
120       d_mid_bit = true;
121     }
122   }
123
124   consume_each(i);
125   return j;
126 }