3 * Copyright 2009 Free Software Foundation, Inc.
5 * This file is part of GNU Radio
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)
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.
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.
22 #include <vrt/quadradio.h>
24 #include <vrt/copiers.h>
28 #include <boost/scoped_ptr.hpp>
29 #include <boost/shared_ptr.hpp>
36 #include <gruel/realtime.h>
39 #define MIN_IP_LOCAL_PORT 32768
40 #define MAX_IP_LOCAL_PORT 61000
42 static volatile bool signaled = false;
51 install_sig_handler(int signum,
52 void (*new_handler)(int))
54 struct sigaction new_action;
55 memset (&new_action, 0, sizeof (new_action));
57 new_action.sa_handler = new_handler;
58 sigemptyset (&new_action.sa_mask);
59 new_action.sa_flags = 0;
61 if (sigaction (signum, &new_action, 0) < 0){
62 perror ("sigaction (install new)");
63 throw std::runtime_error ("sigaction");
67 // ------------------------------------------------------------------------
69 class rx_nop_handler : public vrt::rx_packet_handler
72 uint64_t d_max_samples;
73 uint64_t d_max_quantum;
77 uint64_t d_nwrong_pkt_cnt;
84 // Shared pointer to an instance of this class
85 typedef boost::shared_ptr<rx_nop_handler> sptr;
90 * \param max_samples Maximum number of samples to copy. Use zero for no maximum.
91 * \param max_quantum Maximum number of samples required to accept in one call.
92 * Use 0 to indicate no maximum.
94 rx_nop_handler(uint64_t max_samples, uint64_t max_quantum=0)
95 : d_max_samples(max_samples), d_max_quantum(max_quantum),
96 d_nsamples(0), d_npackets(0),
97 d_last_pkt_cnt(0xf), d_nwrong_pkt_cnt(0),
103 bool operator()(const uint32_t *payload,
104 size_t n32_bit_words,
105 const vrt::expanded_header *hdr);
108 * \brief Returns number of packets this copier was called with
110 uint64_t npackets() const { return d_npackets; }
113 * \brief Returns actual number of samples copied
115 uint64_t nsamples() const { return d_nsamples; }
118 * \brief Returns maximum number of samples that will be copied
120 uint64_t max_samples() const { return d_max_samples; }
123 * Returns true if an error has occurred. Derived classes must set d_err to true
124 * when an error occurs in the () operator
126 bool has_errored_p() const { return d_err; }
129 * \brief Returns true if this instance has reached the maximum number of samples
131 bool has_finished_p() const
132 { return d_max_samples == 0 ? false : d_nsamples >= d_max_samples-d_max_quantum; }
134 uint64_t nwrong_pkt_cnt() const { return d_nwrong_pkt_cnt; }
140 rx_nop_handler::~rx_nop_handler()
145 rx_nop_handler::operator()(const uint32_t *payload,
146 size_t n32_bit_words,
147 const vrt::expanded_header *hdr)
149 if (d_npackets != 0 && hdr->pkt_cnt() != ((d_last_pkt_cnt + 1) & 0xf)){
151 fprintf(stderr, "bad cnt (pkt %lld)\n", d_npackets);
153 d_last_pkt_cnt = hdr->pkt_cnt();
155 d_nsamples += n32_bit_words;
158 return !has_finished_p();
161 // ------------------------------------------------------------------------
163 class file_writer_16sc : public rx_nop_handler
166 std::string d_filename;
170 file_writer_16sc(const std::string &filename, uint64_t max_samples)
171 : rx_nop_handler(max_samples), d_filename(filename)
173 d_fp = fopen(filename.c_str(), "wb");
175 perror(filename.c_str());
176 throw std::invalid_argument(filename);
183 operator()(const uint32_t *items, size_t nitems, const vrt::expanded_header *hdr)
185 bool ok = rx_nop_handler::operator()(items, nitems, hdr);
187 size_t host_nitems = nitems;
188 std::complex<int16_t> host_items[host_nitems];
190 vrt::copy_net_16sc_to_host_16sc(nitems, items, host_items);
193 while (n < host_nitems){
194 size_t r = fwrite(&host_items[n], sizeof(host_items[0]), host_nitems - n, d_fp);
196 if (r == 0){ // out of space?
198 perror(d_filename.c_str());
208 file_writer_16sc::~file_writer_16sc()
213 // ------------------------------------------------------------------------
215 class file_writer_32fc : public rx_nop_handler
218 std::string d_filename;
222 file_writer_32fc(const std::string &filename, uint64_t max_samples)
223 : rx_nop_handler(max_samples), d_filename(filename)
225 d_fp = fopen(filename.c_str(), "wb");
227 perror(filename.c_str());
228 throw std::invalid_argument(filename);
235 operator()(const uint32_t *items, size_t nitems, const vrt::expanded_header *hdr)
237 bool ok = rx_nop_handler::operator()(items, nitems, hdr);
239 size_t host_nitems = nitems;
240 std::complex<float> host_items[host_nitems];
242 vrt::copy_net_16sc_to_host_32fc(nitems, items, host_items);
245 while (n < host_nitems){
246 size_t r = fwrite(&host_items[n], sizeof(host_items[0]), host_nitems - n, d_fp);
248 if (r == 0){ // out of space?
250 perror(d_filename.c_str());
260 file_writer_32fc::~file_writer_32fc()
265 // ------------------------------------------------------------------------
268 usage(const char *progname)
270 const char *p = strrchr(progname, '/'); // drop leading directory path
274 if (strncmp(p, "lt-", 3) == 0) // drop lt- libtool prefix
277 fprintf(stderr, "Usage: %s [options]\n\n", p);
278 fprintf(stderr, "Options:\n");
279 fprintf(stderr, " -h show this message and exit\n");
280 //fprintf(stderr, " -e ETH_INTERFACE specify ethernet interface [default=eth0]\n");
281 //fprintf(stderr, " -m MAC_ADDR mac address of USRP2 HH:HH [default=first one found]\n");
282 //fprintf(stderr, " -f FREQUENCY specify receive center frequency in Hz [default=0.0]\n");
283 //fprintf(stderr, " -d DECIM specify receive decimation rate [default=5]\n");
284 //fprintf(stderr, " -g GAIN specify receive daughterboard gain [default=0]\n");
285 fprintf(stderr, " -N NSAMPLES specify number of samples to receive [default=infinite]\n");
286 fprintf(stderr, " -o OUTPUT_FILENAME specify file to receive samples [default=none]\n");
287 fprintf(stderr, " -s write complex<short> [default=complex<float>]\n");
288 fprintf(stderr, " -S samples_per_pkt specify # of samples per pkt [default=maximum]\n");
289 //fprintf(stderr, " -v verbose output\n");
294 main(int argc, char **argv)
296 const char *quad_radio_ip = "192.168.123.123";
297 size_t rx_bufsize = 62.5e6; // sizeof memory mapped network buffer
298 int samples_per_pkt = 0; // use default
299 uint64_t nsamples = 0;
300 char *output_filename = 0;
301 bool output_shorts = false;
306 while ((ch = getopt(argc, argv, "hN:o:sS:")) != EOF){
309 nsamples = (uint64_t) strtod(optarg, 0);
313 output_filename = optarg;
317 output_shorts = true;
322 t = strtol(optarg, 0, 0);
338 install_sig_handler(SIGINT, sig_handler);
340 gruel::rt_status_t rt = gruel::enable_realtime_scheduling();
341 if (rt != gruel::RT_OK)
342 std::cerr << "Failed to enable realtime scheduling" << std::endl;
345 vrt::quadradio::sptr qr;
347 qr = vrt::quadradio::sptr(new vrt::quadradio(quad_radio_ip, rx_bufsize));
350 std::cerr << "Failed to create vrt::quadradio\n";
355 rx_nop_handler::sptr handler;
356 if (output_filename){
358 handler = rx_nop_handler::sptr(new file_writer_16sc(output_filename, nsamples));
360 handler = rx_nop_handler::sptr(new file_writer_32fc(output_filename, nsamples));
363 handler = rx_nop_handler::sptr(new rx_nop_handler(nsamples));
366 printf("samples_per_pkt = %d\n", samples_per_pkt);
368 if (!qr->start_streaming(0, samples_per_pkt)){
369 fprintf(stderr, "failed to send_rx_command\n");
373 // start receiving packets
377 && !handler->has_errored_p()
378 && !handler->has_finished_p()){
379 bool ok = qr->vrt_rx()->rx_packets(handler.get());
381 fprintf(stderr, "vrt->rx_packets failed\n");
386 qr->stop_streaming(0);
388 printf("%llu packets received, %llu bad pkt_cnt field values, %llu samples\n",
389 handler->npackets(), handler->nwrong_pkt_cnt(), handler->nsamples());