Merged features/inband-usb r5224:6306 into trunk.
[debian/gnuradio] / usrp / host / lib / inband / usrp_tx.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_tx.h>
27 #include <iostream>
28 #include <usb.h>
29 #include <mb_class_registry.h>
30 #include <usrp_inband_usb_packet.h>
31 #include <fpga_regs_common.h>
32 #include <usrp_standard.h>
33 #include <stdio.h>
34
35 #include <symbols_usrp_tx_cs.h>
36
37 typedef usrp_inband_usb_packet transport_pkt;
38
39 static const bool verbose = false;
40
41 usrp_tx::usrp_tx(mb_runtime *rt, const std::string &instance_name, pmt_t user_arg)
42   : mb_mblock(rt, instance_name, user_arg),
43     d_disk_write(false)
44 {
45   d_cs = define_port("cs", "usrp-tx-cs", true, mb_port::EXTERNAL);
46   
47   //d_disk_write=true;
48   
49   if(d_disk_write) {
50     d_ofile.open("tx_data.dat",std::ios::binary|std::ios::out);
51     d_cs_ofile.open("tx_cs.dat",std::ios::binary|std::ios::out);
52   }
53 }
54
55 usrp_tx::~usrp_tx() 
56 {
57   if(d_disk_write) {
58     d_ofile.close();
59     d_cs_ofile.close();
60   }
61 }
62
63 void 
64 usrp_tx::initial_transition()
65 {
66   
67 }
68
69 void
70 usrp_tx::handle_message(mb_message_sptr msg)
71 {
72   pmt_t event = msg->signal();
73   pmt_t port_id = msg->port_id();
74   pmt_t data = msg->data(); 
75
76   // Theoretically only have 1 message to ever expect, but
77   // want to make sure its at least what we want
78   if(pmt_eq(port_id, d_cs->port_symbol())) {
79     
80     if(pmt_eqv(event, s_cmd_usrp_tx_write))
81       write(data);
82   }
83 }
84
85 void
86 usrp_tx::write(pmt_t data)
87 {
88   pmt_t invocation_handle = pmt_nth(0, data);
89   pmt_t channel = pmt_nth(1, data);
90   pmt_t v_packets = pmt_nth(2, data);
91   d_utx = boost::any_cast<usrp_standard_tx *>(pmt_any_ref(pmt_nth(3, data)));
92
93   size_t n_bytes;
94   bool underrun;  // this will need to go, as it is taken care of in the packet headers
95
96   transport_pkt *pkts = (transport_pkt *) pmt_u8vector_writeable_elements(v_packets, n_bytes);
97
98   int ret = d_utx->write (pkts, n_bytes, &underrun);
99   
100   if (0 && underrun)
101     fprintf(stderr, "uU");
102
103   if (ret == (int) n_bytes) {
104     if (verbose)
105       std::cout << "[usrp_server] Write of " << n_bytes << " successful\n";
106     // need to respond with the channel so the USRP server knows who to forward the result of
107     // the write to by looking up the owner of the channel
108     d_cs->send(s_response_usrp_tx_write,
109                pmt_list3(invocation_handle, PMT_T, channel));
110   }
111   else {
112     if (verbose)
113       std::cout << "[usrp_server] Error writing " << n_bytes << " bytes to USB bus\n";
114     d_cs->send(s_response_usrp_tx_write,
115                pmt_list3(invocation_handle, PMT_F, channel));
116   }
117     
118   long n_packets = 
119     static_cast<long>(std::ceil(n_bytes / (double)transport_pkt::max_pkt_size()));
120
121   for(int i=0; i < n_packets; i++) {
122     
123     if(d_disk_write) {
124       if(pkts[i].chan() == 0x1f)
125         d_cs_ofile.write((const char *)&pkts[i], transport_pkt::max_pkt_size());
126       else
127         d_ofile.write((const char *)&pkts[i], transport_pkt::max_pkt_size());
128
129       d_cs_ofile.flush();
130       d_ofile.flush();
131     }
132   }
133
134
135   return;
136 }
137
138 REGISTER_MBLOCK_CLASS(usrp_tx);