distcheck fix for gr-gcell
[debian/gnuradio] / usrp / host / lib / inband / usrp_rx_stub.cc
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2007 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
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <usrp_rx_stub.h>
27
28 #include <iostream>
29 #include <vector>
30 #include <usb.h>
31 #include <mb_class_registry.h>
32 #include <usrp_inband_usb_packet.h>
33 #include <fpga_regs_common.h>
34 #include "usrp_standard.h"
35 #include <stdio.h>
36 #include <string.h>
37 #include <ui_nco.h>
38 #include <fstream>
39
40 #include <symbols_usrp_rx_cs.h>
41
42 typedef usrp_inband_usb_packet transport_pkt;
43
44 static const bool verbose = false;
45
46 bool usrp_rx_stop;
47
48 // Used for the fake control packet response code to send the responses back up
49 // the RX.  The TX stub dumps responses in to this queue.
50 std::queue<pmt_t> d_cs_queue;
51
52 usrp_rx_stub::usrp_rx_stub(mb_runtime *rt, const std::string &instance_name, pmt_t user_arg)
53   : mb_mblock(rt, instance_name, user_arg),
54     d_samples_per_frame((long)(126)),
55     d_amplitude(16384),
56     d_disk_write(false)
57 {
58   d_cs = define_port("cs", "usrp-rx-cs", true, mb_port::EXTERNAL);
59   
60   // initialize NCO
61   double freq = 100e3;
62   int interp = 32;                          // 32 -> 4MS/s
63   double sample_rate = 128e6 / interp;  
64   d_nco.set_freq(2*M_PI * freq/sample_rate);
65
66   //d_disk_write = true;
67   
68   if(d_disk_write)
69     d_ofile.open("raw_rx.dat",std::ios::binary|std::ios::out);
70   
71   usrp_rx_stop = false;
72 }
73
74 usrp_rx_stub::~usrp_rx_stub() 
75 {
76   if(d_disk_write)
77     d_ofile.close();
78 }
79
80 void 
81 usrp_rx_stub::initial_transition()
82 {
83   
84 }
85
86 void
87 usrp_rx_stub::handle_message(mb_message_sptr msg)
88 {
89   pmt_t event = msg->signal();
90   pmt_t port_id = msg->port_id();
91   pmt_t data = msg->data(); 
92
93   // Theoretically only have 1 message to ever expect, but
94   // want to make sure its at least what we want
95   if(pmt_eq(port_id, d_cs->port_symbol())) {
96
97     if(verbose)
98       std::cout << "[USRP_RX_STUB] Starting...\n";
99     
100     if(pmt_eqv(event, s_cmd_usrp_rx_start_reading))
101       read_and_respond(data);
102   }
103 }
104
105 void
106 usrp_rx_stub::read_and_respond(pmt_t data)
107 {
108
109   while(!usrp_rx_stop) {
110
111     long nsamples_this_frame = d_samples_per_frame;
112
113     size_t nshorts = 2 * nsamples_this_frame;   // 16-bit I & Q
114     long channel = 0;
115     long n_bytes = nshorts*2;
116     pmt_t uvec = pmt_make_s16vector(nshorts, 0);
117     size_t ignore;
118     int16_t *samples = pmt_s16vector_writeable_elements(uvec, ignore);
119
120     // fill in the complex sinusoid
121
122     for (int i = 0; i < nsamples_this_frame; i++){
123
124       if (1){
125         gr_complex s;
126         d_nco.sincos(&s, 1, d_amplitude);
127         // write 16-bit i & q
128         samples[2*i] =   (int16_t) s.real();
129         samples[2*i+1] = (int16_t) s.imag();
130       }
131       else {
132         gr_complex s(d_amplitude, d_amplitude);
133
134         // write 16-bit i & q
135         samples[2*i] =   (int16_t) s.real();
136         samples[2*i+1] = (int16_t) s.imag();
137       }
138     }
139     
140     if(d_disk_write)
141       d_ofile.write((const char *)samples, n_bytes);
142   
143     pmt_t v_pkt = pmt_make_u8vector(sizeof(transport_pkt), 0);
144     transport_pkt *pkt =
145       (transport_pkt *) pmt_u8vector_writeable_elements(v_pkt, ignore);
146
147     pkt->set_header(0, channel, 0, n_bytes);
148     pkt->set_timestamp(0xffffffff);
149     memcpy(pkt->payload(), samples, n_bytes);
150     
151     d_cs->send(s_response_usrp_rx_read, pmt_list3(PMT_NIL, PMT_T, v_pkt));
152
153     // Now lets check the shared CS queue between the TX and RX stub.  Each
154     // element in a queue is a list where the first element is an invocation
155     // handle and the second element is a PMT u8 vect representation of the
156     // CS packet response which can just be passed transparently.
157     while(!d_cs_queue.empty()) {
158       
159       pmt_t cs_pkt = d_cs_queue.front();
160       d_cs_queue.pop();
161
162       pmt_t invocation_handle = pmt_nth(0, cs_pkt);
163       pmt_t v_pkt = pmt_nth(1, cs_pkt);
164
165       d_cs->send(s_response_usrp_rx_read,   
166                  pmt_list3(invocation_handle, 
167                            PMT_T, 
168                            v_pkt));  // Take the front CS pkt
169
170       
171       if(verbose)
172         std::cout << "[USRP_RX_STUB] Received CS response from TX stub\n";
173     }
174
175   }
176   
177   usrp_rx_stop = false;
178
179   if(verbose)
180     std::cout << "[USRP_RX_STUB] Got fake RX stop\n";
181
182 }
183
184 REGISTER_MBLOCK_CLASS(usrp_rx_stub);