summaryrefslogtreecommitdiff
path: root/usrp/host/lib/inband/usrp_rx.cc
blob: 45d41bed76fa4c2c6aae0cd6a7c64b7a93b547df (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/* -*- c++ -*- */
/*
 * Copyright 2007 Free Software Foundation, Inc.
 * 
 * This file is part of GNU Radio
 * 
 * GNU Radio is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3, or (at your option)
 * any later version.
 * 
 * GNU Radio is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <usrp_rx.h>

#include <usrp_standard.h>
#include <iostream>
#include <vector>
#include <usb.h>
#include <mblock/class_registry.h>
#include <usrp_inband_usb_packet.h>
#include <fpga_regs_common.h>
#include <stdio.h>

#include <symbols_usrp_rx_cs.h>

typedef usrp_inband_usb_packet transport_pkt;

static const bool verbose = false;

bool usrp_rx_stop;

usrp_rx::usrp_rx(mb_runtime *rt, const std::string &instance_name, pmt_t user_arg)
  : mb_mblock(rt, instance_name, user_arg),
    d_disk_write(false),
    d_disk_write_pkt(false)   // if true, writes full packet, else just the payload
{
  d_cs = define_port("cs", "usrp-rx-cs", true, mb_port::EXTERNAL);
  
  if(d_disk_write) {
    d_ofile0.open("rx_data_chan0.dat",std::ios::binary|std::ios::out);
    d_ofile1.open("rx_data_chan1.dat",std::ios::binary|std::ios::out);
    d_cs_ofile.open("rx_cs.dat",std::ios::binary|std::ios::out);
  }
  
  usrp_rx_stop = false;

}

usrp_rx::~usrp_rx() 
{
  if(d_disk_write) {
    d_ofile0.close();
    d_ofile1.close();
    d_cs_ofile.close();
  }
}

void 
usrp_rx::initial_transition()
{
  
}

/*!
 * \brief Handles incoming signals to to the m-block, wihch should only ever be
 * a single message: cmd-usrrp-rx-start-reading.  There is no signal to stop
 * reading as the m-block goes in to a forever loop to read inband packets from
 * the bus.
 */
void
usrp_rx::handle_message(mb_message_sptr msg)
{
  pmt_t event = msg->signal();
  pmt_t port_id = msg->port_id();
  pmt_t data = msg->data(); 

  // Theoretically only have 1 message to ever expect, but
  // want to make sure its at least what we want
  if(pmt_eq(port_id, d_cs->port_symbol())) {
    
    if(pmt_eqv(event, s_cmd_usrp_rx_start_reading))
      read_and_respond(data);
  }
}

/*!
 * \brief Performs the actual reading of data from the USB bus, called by
 * handle_message() when a cmd-usrp-rx-start-reading signal is received.  
 *
 * The method enters a forever loop where it continues to read data from the bus
 * and generate read responses to the higher layer.  Currently, shared memory is
 * used to exit this loop.
 *
 * The \p data parameter is a PMT list which contains only a single element, an
 * invocation handle which will be returned with all read respones.
 */
void
usrp_rx::read_and_respond(pmt_t data)
{
  size_t ignore;
  bool underrun;
  unsigned int n_read;
  unsigned int pkt_size = sizeof(transport_pkt);

  pmt_t invocation_handle = pmt_nth(0, data);

  // Need the handle to the RX port to send responses, this is passed
  // by the USRP interface m-block
  d_urx = 
    boost::any_cast<usrp_standard_rx *>(pmt_any_ref(pmt_nth(1, data)));

  if(verbose)
    std::cout << "[usrp_rx] Waiting for packets..\n";

  // Read by 512 which is packet size and send them back up
  while(!usrp_rx_stop) {

    pmt_t v_pkt = pmt_make_u8vector(pkt_size, 0);
    transport_pkt *pkt = 
      (transport_pkt *) pmt_u8vector_writable_elements(v_pkt, ignore);

    n_read = d_urx->read(pkt, pkt_size, &underrun);

    if(n_read != pkt_size) {
      std::cerr << "[usrp_rx] Error reading packet, shutting down\n";
      d_cs->send(s_response_usrp_rx_read, 
                 pmt_list3(PMT_NIL, PMT_F, PMT_NIL));
      return;
    }

    if(underrun && verbose && 0)
      std::cout << "[usrp_rx] Underrun\n";

    d_cs->send(s_response_usrp_rx_read, 
               pmt_list3(PMT_NIL, PMT_T, v_pkt));
    if(verbose && 0)
      std::cout << "[usrp_rx] Read 1 packet\n";
    
    if(d_disk_write) {
      if(pkt->chan() == CONTROL_CHAN)
        d_cs_ofile.write((const char *)pkt, transport_pkt::max_pkt_size());
      else {
        if(d_disk_write_pkt) {
          if(pkt->chan() == 0)
            d_ofile0.write((const char *)pkt, transport_pkt::max_pkt_size());
          else if(pkt->chan() == 1)
            d_ofile1.write((const char *)pkt, transport_pkt::max_pkt_size());
        } else {
          if(pkt->chan() == 0)
            d_ofile0.write((const char *)pkt->payload(), transport_pkt::max_payload());
          else if(pkt->chan() == 1)
            d_ofile1.write((const char *)pkt->payload(), transport_pkt::max_payload());
        }
      }

      d_cs_ofile.flush();
      d_ofile0.flush();
      d_ofile1.flush();
    }
  }
  
  usrp_rx_stop = false;

  if(verbose) {
    std::cout << "[USRP_RX] Stopping...\n";
    fflush(stdout);
  }
}

REGISTER_MBLOCK_CLASS(usrp_rx);