Potential fix to MSDD warnings by setting sequence number from buffer more explicitly.
[debian/gnuradio] / gr-msdd6000 / src / msdd_source_simple.cc
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2008,2009,2010 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 along
18  * with this program; if not, write to the Free Software Foundation, Inc.,
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20  */
21 #ifdef HAVE_CONFIG_H
22 #include <config.h>
23 #endif
24
25 #include <msdd_source_simple.h>
26 #include <gr_io_signature.h>
27 #include <string.h>
28 #include <cstdio>
29
30
31 msdd_source_simple_sptr
32 msdd_make_source_simple (const char *src, unsigned short port_src) 
33 {
34   return gnuradio::get_initial_sptr(new msdd_source_simple ( src, port_src)); 
35 }
36
37
38 msdd_source_simple::msdd_source_simple (const char *src, 
39                                         unsigned short port_src) 
40   : gr_sync_block("MSDD_SOURCE_SIMPLE",
41                   gr_make_io_signature (0,0,0),
42                   gr_make_io_signature (1, 1, sizeof (short))),
43     rcv(new MSDD6000((char*) src)), d_lastseq(0), d_firstrun(true)
44 {
45   set_output_multiple(MSDD_COMPLEX_SAMPLES_PER_PACKET*2);
46 }
47
48 msdd_source_simple::~msdd_source_simple ()
49 {
50 }
51
52
53 int
54 msdd_source_simple::work (int noutput_items,
55                          gr_vector_const_void_star &input_items,
56                          gr_vector_void_star &output_items)
57 {
58         
59 #define BUF_LEN        (MSDD_COMPLEX_SAMPLES_PER_PACKET*sizeof(short)*2 + 6)
60
61   signed short* out1 =(signed short*) output_items[0];
62
63   for(int i=0; i<floor(noutput_items*1.0/(2*MSDD_COMPLEX_SAMPLES_PER_PACKET));i++){
64     char buffer[BUF_LEN];
65     rcv->read( &buffer[0], BUF_LEN );
66
67     //int seq = *((int*) &buffer[2]);
68     int seq;
69     memcpy(&seq, &buffer[2], 4*sizeof(char));
70
71     if(d_lastseq == -MSDD_COMPLEX_SAMPLES_PER_PACKET){
72       // not started case
73       if(seq == 0){
74         d_lastseq = 0;
75       } else {
76         // THROW AWAY SAMPLES WE ARE NOT STARTED YET!
77         return 0;
78       }
79
80     } else {
81       // started case
82       int samples_missed = seq - d_lastseq - MSDD_COMPLEX_SAMPLES_PER_PACKET;
83       if(samples_missed > 0){
84         if(d_firstrun == true){
85           // we may have missed some initial samples off the beginning of
86           // a stream but there are no drop outs in the middle of what we have
87         } else {
88           printf("dropped %d samples.\n", samples_missed);
89         }
90       }
91       d_lastseq = seq;
92     }
93
94     int out_idx = i*MSDD_COMPLEX_SAMPLES_PER_PACKET*2;
95     memcpy(&out1[out_idx], &buffer[6], BUF_LEN - 6);
96     d_firstrun = false;
97   }
98
99   return noutput_items;
100
101 }
102
103 bool msdd_source_simple::set_decim_rate(unsigned int rate)
104 {
105   rcv->set_decim((int) floor(log2(rate)));
106   return true;
107 }
108
109
110 bool msdd_source_simple::set_rx_freq(int channel, double freq)
111 {
112   long new_fc = (long)freq;
113   rcv->set_fc( new_fc/1000000, new_fc%1000000);
114   return true;
115 }
116
117
118 bool msdd_source_simple::set_pga(int which, double gain)
119 {
120   if(gain < 0 || gain > 10){
121     printf("GAIN IS OUTSIDE ACCEPTABLE RANGE!\n");
122     return false;
123   }
124   // ok i lied this is not really a pga, its decimation gain
125   rcv->set_ddc_gain((int)gain);
126   return true;
127 }
128
129
130 bool msdd_source_simple::start()
131 {
132   rcv->start();
133   return true;
134 }
135
136
137 bool msdd_source_simple::stop()
138 {
139   rcv->stop();
140   return true;
141 }
142
143 long msdd_source_simple::adc_freq()
144 {
145   return 102400000;
146 }
147
148 int msdd_source_simple::decim_rate()
149 {
150   return 1 << rcv->d_decim;
151 }
152
153
154 std::vector<int> msdd_source_simple::gain_range()
155 {
156   static std::vector<int> r;
157   r.push_back(0);
158   r.push_back(12);
159   return r;
160 }
161
162 std::vector<float> msdd_source_simple::freq_range()
163 {
164   std::vector<float> r;
165   r.push_back(30.0*1000*1000);
166   r.push_back(6.0*1000*1000*1000);
167   return r;
168 }