gr-noaa: Combined noaa_hrpt_bit_sync into noaa_hrpt_deframer
[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 <cstring>
30 #include <cstdio>
31
32 #define ST_IDLE   0
33 #define ST_SYNCED 1
34
35 #define SYNC1 0x0284
36 #define SYNC2 0x016F
37 #define SYNC3 0x035C
38 #define SYNC4 0x019D
39 #define SYNC5 0x020F
40 #define SYNC6 0x0095
41
42 #define HRPT_MINOR_FRAME_SYNC  0x0A116FD719D83C95LL
43
44 static int frames_seen = 0;
45
46 noaa_hrpt_deframer_sptr
47 noaa_make_hrpt_deframer()
48 {
49   return gnuradio::get_initial_sptr(new noaa_hrpt_deframer());
50 }
51
52 noaa_hrpt_deframer::noaa_hrpt_deframer()
53   : gr_block("noaa_hrpt_deframer",
54              gr_make_io_signature(1, 1, sizeof(char)),
55              gr_make_io_signature(1, 1, sizeof(short)))
56 {
57   set_output_multiple(6); // room for writing full sync when received
58   d_mid_bit = true;
59   d_last_bit = 0;
60   enter_idle();
61 }
62
63 void
64 noaa_hrpt_deframer::enter_idle()
65 {
66   d_state = ST_IDLE;
67 }
68
69 void
70 noaa_hrpt_deframer::enter_synced()
71 {
72   d_state = ST_SYNCED;
73   d_bit_count = HRPT_BITS_PER_WORD;
74   d_word_count = HRPT_MINOR_FRAME_WORDS-HRPT_SYNC_WORDS;
75   d_word = 0;
76 }
77
78 int
79 noaa_hrpt_deframer::general_work(int noutput_items,
80                                  gr_vector_int &ninput_items,
81                                  gr_vector_const_void_star &input_items,
82                                  gr_vector_void_star &output_items)
83 {
84   int ninputs = ninput_items[0];
85   const char *in = (const char *)input_items[0];
86   unsigned short *out = (unsigned short *)output_items[0];
87
88   int i = 0, j = 0;
89   while (i < ninputs && j < noutput_items) {
90     char bit = in[i++];
91     char diff = bit^d_last_bit;
92     d_last_bit = bit;
93
94     // Wait for transition if not synced, otherwise, alternate bits
95     if (d_mid_bit && (diff | (d_state == ST_SYNCED))) {
96       switch (d_state) {
97       case ST_IDLE:
98         d_shifter = (d_shifter << 1) | bit; // MSB transmitted first
99         
100         if ((d_shifter & 0x0FFFFFFFFFFFFFFFLL) == HRPT_MINOR_FRAME_SYNC) {
101           fprintf(stderr, "SYNC #%i", frames_seen++);
102           out[j++] = SYNC1;
103           out[j++] = SYNC2;
104           out[j++] = SYNC3;
105           out[j++] = SYNC4;
106           out[j++] = SYNC5;
107           out[j++] = SYNC6;
108           enter_synced();
109         }
110         break;
111         
112       case ST_SYNCED:
113         d_word = (d_word << 1) | bit; // MSB transmitted first
114         if (--d_bit_count == 0) {
115           out[j++] = d_word;
116           d_word = 0;
117           d_bit_count = HRPT_BITS_PER_WORD;
118           if (--d_word_count == 0) {
119             fprintf(stderr, "...done\n");
120             enter_idle();
121           }
122         }
123         break;
124         
125       default:
126         throw std::runtime_error("noaa_hrpt_deframer: bad state\n");
127       }
128
129       d_mid_bit = false;
130     }
131     else {
132       d_mid_bit = true;
133     }
134   }
135
136   consume_each(i);
137   return j;
138 }