Merge branch 'vrt' of http://gnuradio.org/git/jblum
[debian/gnuradio] / gr-vrt / src / vrt_source_32fc.cc
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2008,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 <vrt_source_32fc.h>
28 #include <vrt/expanded_header.h>
29 #include <vrt/copiers.h>
30 #include <vrt/types.h>
31 #include <vrt/if_context.h>
32 #include <gr_io_signature.h>
33 #include <missing_pkt_checker.h>
34 #include <iostream>
35 #include <gruel/inet.h>
36 #include <cstdio>
37 #include <cstring>
38
39 #define VERBOSE 1               // define to 0 or 1
40
41
42 class rx_32fc_handler : public vrt::rx_packet_handler
43 {
44   int                                 d_noutput_items;
45   std::complex<float>                *d_out;
46   int                                *d_oo;             // output index
47   std::vector< std::complex<float> > &d_remainder;
48   missing_pkt_checker                &d_checker;
49   all_context_t d_if_context;
50   size_t d_if_context_n32_bit_words;
51
52 public:
53
54   rx_32fc_handler(int noutput_items, std::complex<float> *out,
55                   int *oo, std::vector< std::complex<float> > &remainder,
56                   missing_pkt_checker &checker)
57     : d_noutput_items(noutput_items), d_out(out),
58       d_oo(oo), d_remainder(remainder), d_checker(checker),
59       d_if_context_n32_bit_words(0) {}
60
61   ~rx_32fc_handler();
62
63   bool operator()(const uint32_t *payload,
64                   size_t n32_bit_words,
65                   const vrt::expanded_header *hdr);
66   all_context_t* get_if_context(void){
67     return d_if_context_n32_bit_words? &d_if_context : NULL;};
68 };
69
70 rx_32fc_handler::~rx_32fc_handler()
71 {
72 }
73
74 bool
75 rx_32fc_handler::operator()(const uint32_t *payload,
76                             size_t n32_bit_words,
77                             const vrt::expanded_header *hdr)
78 {
79   if (hdr->if_data_p()){
80     int nmissing = d_checker.check(hdr);
81     if (VERBOSE && nmissing != 0){
82       std::cerr << "S" << nmissing;
83     }
84
85     // copy as many as will fit into the output buffer.
86
87     size_t n = std::min(n32_bit_words, (size_t)(d_noutput_items - *d_oo));
88     vrt::copy_net_16sc_to_host_32fc(n, payload, &d_out[*d_oo]);
89     *d_oo += n;
90
91     // if there are any left over, copy them into remainder and tell
92     // our caller we're had enough for now.
93
94     size_t r = n32_bit_words - n;
95     if (r > 0){
96       assert(d_remainder.size() == 0);
97       d_remainder.resize(r);
98       vrt::copy_net_16sc_to_host_32fc(r, &payload[n], &d_remainder[0]);
99       return false;             // Stop calling us.
100     }
101
102     return true;                        // Keep calling us, we've got more room
103   }
104   else if (hdr->if_context_p()){
105     // print the IF-Context packet
106     fprintf(stderr, "\nIF-Context:\n");
107     //for (size_t i = 0; i < n32_bit_words; i++)
108     //  fprintf(stderr, "%04x: %08x\n", (unsigned int) i, ntohl(payload[i]));
109     memcpy(&d_if_context, payload, sizeof(uint32_t)*n32_bit_words);
110     d_if_context_n32_bit_words = n32_bit_words;
111     return true;
112   }
113   else {
114     // It's most likely an Extension Data or Extension Context packet
115     // (that we don't know how to interpret...)
116     fprintf(stderr, "\nIF-Extension:\n");
117     for (size_t i = 0; i < n32_bit_words; i++)
118       fprintf(stderr, "%04x: %08x\n", (unsigned int) i, ntohl(payload[i]));
119     return true;
120   }
121 }
122
123
124 // ------------------------------------------------------------------------
125
126 vrt_source_32fc::vrt_source_32fc(const char *name)
127
128   : vrt_source_base(name,
129                     gr_make_io_signature(1, 1, sizeof(gr_complex)))
130 {
131 }
132
133 vrt_source_32fc::~vrt_source_32fc()
134 {
135   if (VERBOSE){
136     std::cerr << "\nvrt_source_32fc: npackets = " << d_checker.npackets()
137               << " nwrong_pkt_cnt = " << d_checker.nwrong_pkt_cnt()
138               << " nmissing_pkt_est = " << d_checker.nmissing_pkt_est()
139               << std::endl;
140   }
141 }
142
143 int
144 vrt_source_32fc::work(int noutput_items,
145                       gr_vector_const_void_star &input_items,
146                       gr_vector_void_star &output_items)
147 {
148   gr_complex *out = (gr_complex *)output_items[0];
149   int   oo = 0;
150
151   // Handle any samples left over from the last call.
152   int t = std::min(noutput_items, (int)d_remainder.size());
153   if (t != 0){
154     for (int i = 0; i < t; i++)
155       out[i] = d_remainder[i];
156     d_remainder.erase(d_remainder.begin(), d_remainder.begin()+t);
157     oo = t;
158   }
159   if (noutput_items - oo == 0)
160     return oo;
161   
162   // While we've got room, and there are packets, handle them
163   rx_32fc_handler h(noutput_items, out, &oo, d_remainder, d_checker);
164   bool ok = vrt_rx()->rx_packets(&h);
165
166   if (!ok){
167     std::cerr << "vrt_source_32fc: vrt::rx_packets() failed" << std::endl;
168     return -1;  // say we're done
169   }
170
171   //we have a context packet, grab its useful information...
172   //remember that things are in network byte order!
173   if (h.get_if_context()){
174     //extract caldiv stuff
175     d_lo_freq = vrt_freq_to_double(ntohx(h.get_if_context()->caldiv.lo_freq));
176     d_cal_freq = vrt_freq_to_double(ntohx(h.get_if_context()->caldiv.cal_freq));
177     d_lo_locked = bool(ntohx(h.get_if_context()->caldiv.lo_locked));
178     d_cal_locked = bool(ntohx(h.get_if_context()->caldiv.cal_locked));
179     d_cal_enabled = bool(ntohx(h.get_if_context()->caldiv.cal_enabled));
180     d_caldiv_temp = vrt_temp_to_double(ntohx(h.get_if_context()->caldiv.temp));
181     d_caldiv_rev = ntohx(h.get_if_context()->caldiv.rev);
182     d_caldiv_ser = ntohx(h.get_if_context()->caldiv.ser);
183     d_caldiv_mod = ntohx(h.get_if_context()->caldiv.mod);
184     //extract gps stuff
185     d_utc_time = ntohx(h.get_if_context()->gps.formatted_gps.integer_secs);
186     d_altitude = vrt_altitude_to_double(ntohx(h.get_if_context()->gps.formatted_gps.altitude));
187     d_longitude = vrt_geo_angle_to_double(ntohx(h.get_if_context()->gps.formatted_gps.longitude));
188     d_latitude = vrt_geo_angle_to_double(ntohx(h.get_if_context()->gps.formatted_gps.latitude));
189   }
190
191   return oo;
192 }