91c94d2a6425540b3df2446f7eace9c4f73fc0b7
[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   enter_idle();
59 }
60
61 void
62 noaa_hrpt_deframer::enter_idle()
63 {
64   d_state = ST_IDLE;
65 }
66
67 void
68 noaa_hrpt_deframer::enter_synced()
69 {
70   d_state = ST_SYNCED;
71   d_bit_count = HRPT_BITS_PER_WORD;
72   d_word_count = HRPT_MINOR_FRAME_WORDS-HRPT_SYNC_WORDS;
73   d_word = 0;
74 }
75
76 int
77 noaa_hrpt_deframer::general_work(int noutput_items,
78                                  gr_vector_int &ninput_items,
79                                  gr_vector_const_void_star &input_items,
80                                  gr_vector_void_star &output_items)
81 {
82   int ninputs = ninput_items[0];
83   const char *in = (const char *)input_items[0];
84   unsigned short *out = (unsigned short *)output_items[0];
85
86   int i = 0, j = 0;
87   while (i < ninputs && j < noutput_items) {
88     char bit = in[i++];
89
90     switch (d_state) {
91     case ST_IDLE:
92       d_shifter = (d_shifter << 1) | bit; // MSB transmitted first
93       
94       if ((d_shifter & 0x0FFFFFFFFFFFFFFFLL) == HRPT_MINOR_FRAME_SYNC) {
95         fprintf(stderr, "SYNC #%i", frames_seen++);
96         out[j++] = SYNC1;
97         out[j++] = SYNC2;
98         out[j++] = SYNC3;
99         out[j++] = SYNC4;
100         out[j++] = SYNC5;
101         out[j++] = SYNC6;
102         enter_synced();
103       }
104       break;
105
106     case ST_SYNCED:
107       d_word = (d_word << 1) | bit; // MSB transmitted first
108       if (--d_bit_count == 0) {
109         out[j++] = d_word;
110         d_word = 0;
111         d_bit_count = HRPT_BITS_PER_WORD;
112         if (--d_word_count == 0) {
113           fprintf(stderr, "...done\n");
114           enter_idle();
115         }
116       }
117       break;
118
119     default:
120       throw std::runtime_error("noaa_hrpt_deframer: bad state\n");
121     }
122   }
123
124   consume_each(i);
125   return j;
126 }