Convert gr-audio-portaudio to Boost via gruel
[debian/gnuradio] / usrp / limbo / inband / usrp_rx.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.h>
27
28 #include <usrp_standard.h>
29 #include <iostream>
30 #include <vector>
31 #include <usb.h>
32 #include <mblock/class_registry.h>
33 #include <usrp_inband_usb_packet.h>
34 #include <fpga_regs_common.h>
35 #include <stdio.h>
36
37 #include <symbols_usrp_rx_cs.h>
38
39 typedef usrp_inband_usb_packet transport_pkt;
40
41 static const bool verbose = false;
42
43 bool usrp_rx_stop;
44
45 usrp_rx::usrp_rx(mb_runtime *rt, const std::string &instance_name, pmt_t user_arg)
46   : mb_mblock(rt, instance_name, user_arg),
47     d_disk_write(false),
48     d_disk_write_pkt(false)   // if true, writes full packet, else just the payload
49 {
50   d_cs = define_port("cs", "usrp-rx-cs", true, mb_port::EXTERNAL);
51   
52   if(d_disk_write) {
53     d_ofile0.open("rx_data_chan0.dat",std::ios::binary|std::ios::out);
54     d_ofile1.open("rx_data_chan1.dat",std::ios::binary|std::ios::out);
55     d_cs_ofile.open("rx_cs.dat",std::ios::binary|std::ios::out);
56   }
57   
58   usrp_rx_stop = false;
59
60 }
61
62 usrp_rx::~usrp_rx() 
63 {
64   if(d_disk_write) {
65     d_ofile0.close();
66     d_ofile1.close();
67     d_cs_ofile.close();
68   }
69 }
70
71 void 
72 usrp_rx::initial_transition()
73 {
74   
75 }
76
77 /*!
78  * \brief Handles incoming signals to to the m-block, wihch should only ever be
79  * a single message: cmd-usrrp-rx-start-reading.  There is no signal to stop
80  * reading as the m-block goes in to a forever loop to read inband packets from
81  * the bus.
82  */
83 void
84 usrp_rx::handle_message(mb_message_sptr msg)
85 {
86   pmt_t event = msg->signal();
87   pmt_t port_id = msg->port_id();
88   pmt_t data = msg->data(); 
89
90   // Theoretically only have 1 message to ever expect, but
91   // want to make sure its at least what we want
92   if(pmt_eq(port_id, d_cs->port_symbol())) {
93     
94     if(pmt_eqv(event, s_cmd_usrp_rx_start_reading))
95       read_and_respond(data);
96   }
97 }
98
99 /*!
100  * \brief Performs the actual reading of data from the USB bus, called by
101  * handle_message() when a cmd-usrp-rx-start-reading signal is received.  
102  *
103  * The method enters a forever loop where it continues to read data from the bus
104  * and generate read responses to the higher layer.  Currently, shared memory is
105  * used to exit this loop.
106  *
107  * The \p data parameter is a PMT list which contains only a single element, an
108  * invocation handle which will be returned with all read respones.
109  */
110 void
111 usrp_rx::read_and_respond(pmt_t data)
112 {
113   size_t ignore;
114   bool underrun;
115   unsigned int n_read;
116   unsigned int pkt_size = sizeof(transport_pkt);
117
118   pmt_t invocation_handle = pmt_nth(0, data);
119
120   // Need the handle to the RX port to send responses, this is passed
121   // by the USRP interface m-block
122   pmt_t handle = pmt_nth(1, data);
123   d_urx = 
124     boost::any_cast<usrp_standard_rx_sptr>(pmt_any_ref(handle));
125
126   if(verbose)
127     std::cout << "[usrp_rx] Waiting for packets..\n";
128
129   // Read by 512 which is packet size and send them back up
130   while(!usrp_rx_stop) {
131
132     pmt_t v_pkt = pmt_make_u8vector(pkt_size, 0);
133     transport_pkt *pkt = 
134       (transport_pkt *) pmt_u8vector_writable_elements(v_pkt, ignore);
135
136     n_read = d_urx->read(pkt, pkt_size, &underrun);
137
138     if(n_read != pkt_size) {
139       std::cerr << "[usrp_rx] Error reading packet, shutting down\n";
140       d_cs->send(s_response_usrp_rx_read, 
141                  pmt_list3(PMT_NIL, PMT_F, PMT_NIL));
142       return;
143     }
144
145     if(underrun && verbose && 0)
146       std::cout << "[usrp_rx] Underrun\n";
147
148     d_cs->send(s_response_usrp_rx_read, 
149                pmt_list3(PMT_NIL, PMT_T, v_pkt));
150     if(verbose && 0)
151       std::cout << "[usrp_rx] Read 1 packet\n";
152     
153     if(d_disk_write) {
154       if(pkt->chan() == CONTROL_CHAN)
155         d_cs_ofile.write((const char *)pkt, transport_pkt::max_pkt_size());
156       else {
157         if(d_disk_write_pkt) {
158           if(pkt->chan() == 0)
159             d_ofile0.write((const char *)pkt, transport_pkt::max_pkt_size());
160           else if(pkt->chan() == 1)
161             d_ofile1.write((const char *)pkt, transport_pkt::max_pkt_size());
162         } else {
163           if(pkt->chan() == 0)
164             d_ofile0.write((const char *)pkt->payload(), transport_pkt::max_payload());
165           else if(pkt->chan() == 1)
166             d_ofile1.write((const char *)pkt->payload(), transport_pkt::max_payload());
167         }
168       }
169
170       d_cs_ofile.flush();
171       d_ofile0.flush();
172       d_ofile1.flush();
173     }
174   }
175   
176   usrp_rx_stop = false;
177
178   if(verbose) {
179     std::cout << "[USRP_RX] Stopping...\n";
180     fflush(stdout);
181   }
182 }
183
184 REGISTER_MBLOCK_CLASS(usrp_rx);