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