2d741e908f668bc8960b65c6e6fb9d109bf5e463
[debian/gnuradio] / vrt / lib / rx.cc
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2009 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 #ifdef HAVE_CONFIG_H
22 #include <config.h>
23 #endif
24 #include <vrt/rx.h>
25 #include <vrt/expanded_header.h>
26 #include "socket_rx_buffer.h"
27 #include "data_handler.h"
28 #include <unistd.h>
29 #include <stdio.h>
30 #include <stdexcept>
31
32 static void
33 print_words(FILE *fp, size_t offset, const uint32_t *buf, size_t n)
34 {
35   size_t i;
36   for (i = 0; i < n; i++){
37     if (i % 4 == 0){
38       fprintf(fp, "%04zx:", i);
39     }
40
41     putc(' ', fp);
42     fprintf(fp, "%08x", buf[i]);
43     if (i % 4 == 3)
44       putc('\n', fp);
45   }
46
47   putc('\n', fp);
48 }
49
50
51
52 namespace vrt {
53   
54   rx::sptr
55   rx::make(int socket_fd, size_t rx_bufsize)
56   {
57     return sptr(new rx(socket_fd, rx_bufsize));
58   }
59
60   rx::rx(int socket_fd, size_t rx_bufsize)
61     : d_socket_fd(socket_fd),
62       d_srb(new socket_rx_buffer(socket_fd, rx_bufsize))
63   {
64   }
65
66   rx::~rx()
67   {
68     delete d_srb;
69     ::close(d_socket_fd);
70   }
71
72
73   class vrt_data_handler : public data_handler
74   {
75     rx_packet_handler   *d_handler;
76
77   public:
78     vrt_data_handler(rx_packet_handler *handler)
79       : d_handler(handler){}
80
81     ~vrt_data_handler();
82
83     result operator()(const void *base, size_t len);
84   };
85
86   vrt_data_handler::~vrt_data_handler(){}
87
88   // N.B., There may be more than 1 VRT packet in a frame (usually IF-Context packets)
89   data_handler::result
90   vrt_data_handler::operator()(const void *base, size_t len)
91   {
92     const uint32_t *word_base = (const uint32_t *) base;
93     size_t word_len = len/(sizeof(uint32_t));
94
95     bool want_more = true;
96     while (word_len > 0 && want_more){
97       const uint32_t *payload;
98       size_t n32_bit_words;
99       expanded_header hdr;
100       if (!expanded_header::parse(word_base, word_len,
101                                   &hdr, &payload, &n32_bit_words)){
102         if (1){
103           fprintf(stderr, "vrt_data_handler: malformed VRT packet!\n");
104           print_words(stderr, 0, word_base, word_len);
105         }
106         return 0;
107       }
108       want_more = (*d_handler)(payload, n32_bit_words, &hdr);
109       word_base += hdr.pkt_size();
110       word_len -= hdr.pkt_size();
111     }
112     return !want_more ? data_handler::DONE : 0;
113   }
114
115
116   bool
117   rx::rx_packets(rx_packet_handler *handler, bool dont_wait)
118   {
119     vrt_data_handler h(handler);
120     socket_rx_buffer::result r = d_srb->rx_frames(&h, dont_wait ? 0 : -1);
121     return r == socket_rx_buffer::EB_OK || r == socket_rx_buffer::EB_WOULD_BLOCK;
122   }
123
124 }; // vrt