caa2d717540db09f2d11eb805c0ee9436f2b220a
[debian/gnuradio] / usrp / host / lib / 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 <mb_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 usrp_rx::usrp_rx(mb_runtime *rt, const std::string &instance_name, pmt_t user_arg)
44   : mb_mblock(rt, instance_name, user_arg),
45     d_disk_write(false)
46 {
47   d_cs = define_port("cs", "usrp-rx-cs", true, mb_port::EXTERNAL);
48   
49   //d_disk_write=true;
50   
51   if(d_disk_write) {
52     d_ofile.open("rx_data.dat",std::ios::binary|std::ios::out);
53     d_cs_ofile.open("rx_cs.dat",std::ios::binary|std::ios::out);
54   }
55
56 }
57
58 usrp_rx::~usrp_rx() 
59 {
60   if(d_disk_write) {
61     d_ofile.close();
62     d_cs_ofile.close();
63   }
64 }
65
66 void 
67 usrp_rx::initial_transition()
68 {
69   
70 }
71
72 void
73 usrp_rx::handle_message(mb_message_sptr msg)
74 {
75   pmt_t event = msg->signal();
76   pmt_t port_id = msg->port_id();
77   pmt_t data = msg->data(); 
78
79   // Theoretically only have 1 message to ever expect, but
80   // want to make sure its at least what we want
81   if(pmt_eq(port_id, d_cs->port_symbol())) {
82     
83     if(pmt_eqv(event, s_cmd_usrp_rx_start_reading))
84       read_and_respond(data);
85   }
86 }
87
88 void
89 usrp_rx::read_and_respond(pmt_t data)
90 {
91   size_t ignore;
92   bool underrun;
93   unsigned int n_read;
94   unsigned int pkt_size = sizeof(transport_pkt);
95
96   pmt_t invocation_handle = pmt_nth(0, data);
97
98   // Need the handle to the RX port to send responses, this is passed
99   // by the USRP interface m-block
100   d_urx = 
101     boost::any_cast<usrp_standard_rx *>(pmt_any_ref(pmt_nth(1, data)));
102
103   if(verbose)
104     std::cout << "[usrp_rx] Waiting for packets..\n";
105
106   // Read by 512 which is packet size and send them back up
107   while(1) {
108
109     pmt_t v_pkt = pmt_make_u8vector(pkt_size, 0);
110     transport_pkt *pkt = 
111       (transport_pkt *) pmt_u8vector_writeable_elements(v_pkt, ignore);
112
113     n_read = d_urx->read(pkt, pkt_size, &underrun);
114
115     if(n_read != pkt_size) {
116       std::cerr << "[usrp_rx] Error reading packet, shutting down\n";
117       d_cs->send(s_response_usrp_rx_read, 
118                  pmt_list3(PMT_NIL, PMT_F, PMT_NIL));
119       return;
120     }
121
122     if(underrun && verbose && 0)
123       std::cout << "[usrp_rx] Underrun\n";
124
125     d_cs->send(s_response_usrp_rx_read, 
126                pmt_list3(PMT_NIL, PMT_T, v_pkt));
127     if(verbose)
128       std::cout << "[usrp_rx] Read 1 packet\n";
129     
130     if(d_disk_write) {
131       if(pkt->chan() == 0x1f)
132         d_cs_ofile.write((const char *)pkt, transport_pkt::max_pkt_size());
133       else
134         d_ofile.write((const char *)pkt, transport_pkt::max_pkt_size());
135
136       d_cs_ofile.flush();
137       d_ofile.flush();
138     }
139   }
140 }
141
142 REGISTER_MBLOCK_CLASS(usrp_rx);