Merged r11377:11390 from jcorgan/usrp-headers in to trunk.
[debian/gnuradio] / usrp / limbo / inband / usrp_tx.cc
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2007,2008 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 <mblock/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 /*!
70  * \brief Handles incoming signals to to the m-block, wihch should only ever be
71  * a single message: cmd-usrp-tx-write.
72  */
73 void
74 usrp_tx::handle_message(mb_message_sptr msg)
75 {
76   pmt_t event = msg->signal();
77   pmt_t port_id = msg->port_id();
78   pmt_t data = msg->data(); 
79
80   // Theoretically only have 1 message to ever expect, but
81   // want to make sure its at least what we want
82   if(pmt_eq(port_id, d_cs->port_symbol())) {
83     
84     if(pmt_eqv(event, s_cmd_usrp_tx_write))
85       write(data);
86   }
87 }
88
89 /*!
90  * \brief Performs the actual writing of data to the USB bus, called by
91  * handle_message() when a cmd-usrp-tx-write signal is received.
92  *
93  * The \p data parameter is a PMT list which contains three mandatory elements,
94  * in the following order: an invocation handle, a channel, and a uniform vector
95  * of memory which contains the packets to be written to the bus.
96  */
97 void
98 usrp_tx::write(pmt_t data)
99 {
100   pmt_t invocation_handle = pmt_nth(0, data);
101   pmt_t channel = pmt_nth(1, data);
102   pmt_t v_packets = pmt_nth(2, data);
103   d_utx = boost::any_cast<usrp_standard_tx_sptr>(pmt_any_ref(pmt_nth(3, data)));
104
105   size_t n_bytes;
106   bool underrun;  // this will need to go, as it is taken care of in the packet headers
107
108   transport_pkt *pkts = (transport_pkt *) pmt_u8vector_writable_elements(v_packets, n_bytes);
109
110   int ret = d_utx->write (pkts, n_bytes, &underrun);
111   
112   if (0 && underrun)
113     fprintf(stderr, "uU");
114
115   if (ret == (int) n_bytes) {
116     if (verbose)
117       std::cout << "[usrp_server] Write of " << n_bytes << " successful\n";
118     // need to respond with the channel so the USRP server knows who to forward the result of
119     // the write to by looking up the owner of the channel
120     d_cs->send(s_response_usrp_tx_write,
121                pmt_list3(invocation_handle, PMT_T, channel));
122   }
123   else {
124     if (verbose)
125       std::cout << "[usrp_server] Error writing " << n_bytes << " bytes to USB bus\n";
126     d_cs->send(s_response_usrp_tx_write,
127                pmt_list3(invocation_handle, PMT_F, channel));
128   }
129     
130   long n_packets = 
131     static_cast<long>(std::ceil(n_bytes / (double)transport_pkt::max_pkt_size()));
132
133   for(int i=0; i < n_packets; i++) {
134     
135     if(d_disk_write) {
136       if(pkts[i].chan() == CONTROL_CHAN)
137         d_cs_ofile.write((const char *)&pkts[i], transport_pkt::max_pkt_size());
138       else
139         d_ofile.write((const char *)&pkts[i], transport_pkt::max_pkt_size());
140
141       d_cs_ofile.flush();
142       d_ofile.flush();
143     }
144   }
145
146
147   return;
148 }
149
150 REGISTER_MBLOCK_CLASS(usrp_tx);