Potential fix to MSDD warnings by setting sequence number from buffer more explicitly.
[debian/gnuradio] / gr-msdd6000 / src / msdd_rs_source_simple.cc
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2008,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_rs_source_simple.h>
26 #include <gr_io_signature.h>
27 #include <string.h>
28 #include <cstdio>
29
30
31 msdd_rs_source_simple_sptr
32 msdd_rs_make_source_simple ( const char *src, unsigned short port_src) 
33 {
34   return gnuradio::get_initial_sptr(new msdd_rs_source_simple ( src, port_src)); 
35 }
36
37
38 msdd_rs_source_simple::msdd_rs_source_simple (
39                     const char *src, 
40                     unsigned short port_src) 
41                 : gr_sync_block("MSDD_RS_SOURCE_SIMPLE",
42                                 gr_make_io_signature (0,0,0),
43                                 gr_make_io_signature (1, 1, sizeof (short))),
44                   rcv(new MSDD6000_RS((char*) src)), d_lastseq(0)
45 {
46 }
47
48 msdd_rs_source_simple::~msdd_rs_source_simple ()
49 {
50 }
51
52
53 int
54 msdd_rs_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 (366*sizeof(short)*2 + 6)
60
61         float* out1 =(float*) output_items[0];
62
63         char buffer[BUF_LEN];
64         /* Read a buffer out -- looking at UDP payload at this point.*/
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         char type = buffer[0];
72         //printf("Sequence %d\n",seq);
73         
74         // FIXME get rid of these magic 366's!
75         if(d_lastseq == -366)
76         {
77         if (type != 0){
78             /* Received control packet -- parse and update locally stored parameters */
79             printf("Parsing control Packet\n");
80             rcv->parse_control(&buffer[0], seq);       
81         }
82         else{   
83             // not started case
84             if(seq == 0){
85                 d_lastseq = 0;
86             } 
87             else 
88             {
89                 // THROW AWAY SAMPLES WE ARE NOT STARTED YET!
90                 return 0;
91             }
92         }
93         } 
94         // Started case
95         else 
96         {
97         if (type != 0){
98                         /* Received control packet -- parse and update locally stored parameters */
99             printf("Parsing control Packet\n");
100             rcv->parse_control(&buffer[0], seq);       
101         }
102             
103         else {
104             int samples_missed = seq - d_lastseq - 366;
105             if(samples_missed > 0)
106             {
107                 printf("dropped %d samples.\n", samples_missed);
108             }
109             d_lastseq = seq;
110         }
111         }
112         
113         if(noutput_items< 366*2){
114                 printf("NOT ENOUGH SPACE IN OUTPUT BUFFER!!! >:-(\n");
115                 }
116         
117         memcpy(&out1[0], &buffer[6], BUF_LEN - 6);
118         
119 //      for(int i = 0; i < 366*2; i++){
120 //              out1[i] = (float)  (*((short*) &buffer[6+2*i]) );
121 //      }
122         
123         return 366*2;
124 }
125
126 //bool msdd_rs_source_simple::set_decim_rate(unsigned int rate)
127 //{
128 //      // FIXME seems buggy.  How about a floor or ceil?
129 //        rcv->set_decim((int) log2(rate));
130 //      return true;
131 //}
132
133 bool msdd_rs_source_simple::set_rx_freq(double freq)
134 {
135         long new_fc = (long)freq;
136         rcv->set_fc( new_fc/1000000, new_fc%1000000);
137         return true;
138 }
139
140
141 bool msdd_rs_source_simple::set_ddc_gain(double gain)
142 {
143         if(gain < 0 || gain > 7){ // only 3 bits available.
144                 printf("GAIN IS OUTSIDE ACCEPTABLE RANGE!\n");
145                 return false;
146         }
147         //decimation gain
148         rcv->set_ddc_gain((int)gain);
149         return true;
150 }
151
152 // Set desired sample rate of MSDD6000 -- Note bounds checking is 
153 // done by the module and it will return the value actually used in the hardware.
154 bool msdd_rs_source_simple::set_ddc_samp_rate(double rate)
155 {
156         rcv->set_ddc_samp_rate((float) rate);
157         return true;            
158 }
159
160 // Set desired input BW of MSDD6000 -- Note bounds checking is 
161 // done by the module and it will return the value actually used in the hardware.
162 bool msdd_rs_source_simple::set_ddc_bw(double bw)
163 {
164         rcv->set_ddc_bw((float) bw);
165         return true;            
166 }
167
168 bool msdd_rs_source_simple::set_rf_atten(double rf_atten)
169 {
170         rcv->set_rf_attn((int) rf_atten);
171         return true;
172 }
173
174 bool msdd_rs_source_simple::start()
175 {
176         rcv->start();
177     rcv->stop_data();
178         return true;
179 }
180
181 bool msdd_rs_source_simple::stop()
182 {
183         rcv->stop();
184         return true;
185 }
186
187 int msdd_rs_source_simple::start_data()
188 {
189         return rcv->start_data();
190 }
191
192 int msdd_rs_source_simple::stop_data()
193 {
194         return rcv->stop_data();
195 }
196
197 /* Query functions */
198 long msdd_rs_source_simple::pull_adc_freq(){
199         return 102400000;
200 }
201
202 /* Request the current ddc sample rate */
203 float msdd_rs_source_simple::pull_ddc_samp_rate(){
204         return(rcv->pull_ddc_samp_rate());
205 }
206
207 /* Request the current ddc bandwidth */
208 float msdd_rs_source_simple::pull_ddc_bw(){
209         return(rcv->pull_ddc_bw());
210         
211 }
212
213 /* Request the current rx freq */
214 float msdd_rs_source_simple::pull_rx_freq(){
215         return(rcv->pull_rx_freq());
216 }
217
218 /* Request current ddc gain */
219 int msdd_rs_source_simple::pull_ddc_gain(){
220         return(rcv->pull_ddc_gain());
221 }
222
223 /* Request current RF attenuation */
224 int msdd_rs_source_simple::pull_rf_atten(){
225         return(rcv->pull_rf_atten());
226 }
227
228 std::vector<int> msdd_rs_source_simple::gain_range(){
229         static std::vector<int> r;
230         r.push_back(0);
231         r.push_back(12);
232         return r;
233 }
234
235 std::vector<float> msdd_rs_source_simple::freq_range(){
236         std::vector<float> r;
237         r.push_back(30.0*1000*1000);
238         r.push_back(6.0*1000*1000*1000);
239         return r;
240 }