Renamed identifiers for consistency: s/complex_float/32fc/ s/complex_16/16sc/.
[debian/gnuradio] / usrp2 / host / lib / usrp2_impl.h
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2008 Free Software Foundation, Inc.
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18
19 #ifndef INCLUDED_USRP2_IMPL_H
20 #define INCLUDED_USRP2_IMPL_H
21
22 #include <usrp2/usrp2.h>
23 #include <usrp2/data_handler.h>
24 #include <usrp2_eth_packet.h>
25 #include <boost/scoped_ptr.hpp>
26 #include "control.h"
27 #include "ring.h"
28 #include <string>
29
30 namespace usrp2 {
31   
32   class eth_buffer;
33   class pktfilter;
34   class usrp2_thread;
35   class usrp2_tune_result;
36   class pending_reply;
37   class ring;
38
39   class usrp2::impl : private data_handler
40   {
41     static const size_t NRIDS = 256;
42     static const size_t NCHANS = 32;
43
44     eth_buffer    *d_eth_buf;
45     pktfilter     *d_pf;
46     std::string    d_addr;       // FIXME: use u2_mac_addr_t instead
47     usrp2_thread  *d_bg_thread;
48     volatile bool  d_bg_running; // TODO: multistate if needed
49     
50     int            d_rx_decim;
51     int            d_rx_seqno;
52     int            d_tx_seqno;
53     int            d_next_rid;
54     unsigned int   d_num_rx_frames;
55     unsigned int   d_num_rx_missing;
56     unsigned int   d_num_rx_overruns;
57     unsigned int   d_num_rx_bytes;
58
59     unsigned int   d_num_enqueued;
60     omni_mutex     d_enqueued_mutex;
61     omni_condition d_bg_pending_cond;
62
63     // all pending_replies are stack allocated, thus no possibility of leaking these
64     pending_reply *d_pending_replies[NRIDS]; // indexed by 8-bit reply id
65
66     std::vector<ring_sptr>   d_channel_rings; // indexed by 5-bit channel number
67
68     void inc_enqueued() {
69       omni_mutex_lock l(d_enqueued_mutex);
70       d_num_enqueued++;
71     }
72     
73     void dec_enqueued() {
74       omni_mutex_lock l(d_enqueued_mutex);
75       if (--d_num_enqueued == 0)
76         d_bg_pending_cond.signal();
77     }
78     
79     static bool parse_mac_addr(const std::string &s, u2_mac_addr_t *p);
80     void init_et_hdrs(u2_eth_packet_t *p, const std::string &dst);
81     void init_etf_hdrs(u2_eth_packet_t *p, const std::string &dst,
82                        int word0_flags, int chan, uint32_t timestamp);
83     void stop_bg();
84     void init_config_rx_v2_cmd(op_config_rx_v2_cmd *cmd);
85     void init_config_tx_v2_cmd(op_config_tx_v2_cmd *cmd);
86     bool transmit_cmd(void *cmd, size_t len, pending_reply *p, double secs=0.0);
87     virtual data_handler::result operator()(const void *base, size_t len);
88     data_handler::result handle_control_packet(const void *base, size_t len);
89     data_handler::result handle_data_packet(const void *base, size_t len);
90
91   public:
92     impl(const std::string &ifc, props *p);
93     ~impl();
94     
95     void bg_loop();
96
97     std::string mac_addr() const { return d_addr; } // FIXME: convert from u2_mac_addr_t
98     bool burn_mac_addr(const std::string &new_addr);
99
100     bool set_rx_gain(double gain);
101     bool set_rx_center_freq(double frequency, tune_result *result);
102     bool set_rx_decim(int decimation_factor);
103     bool set_rx_scale_iq(int scale_i, int scale_q);
104     bool start_rx_streaming(unsigned int channel, unsigned int items_per_frame);
105     bool rx_samples(unsigned int channel, rx_sample_handler *handler);
106     bool stop_rx_streaming(unsigned int channel);
107     unsigned int rx_overruns() const { return d_num_rx_overruns; }
108     unsigned int rx_missing() const { return d_num_rx_missing; }
109
110     bool set_tx_gain(double gain);
111     bool set_tx_center_freq(double frequency, tune_result *result);
112     bool set_tx_interp(int interpolation_factor);
113     bool set_tx_scale_iq(int scale_i, int scale_q);
114
115     bool tx_32fc(unsigned int channel,
116                  const std::complex<float> *samples,
117                  size_t nsamples,
118                  const tx_metadata *metadata);
119
120     bool tx_16sc(unsigned int channel,
121                  const std::complex<int16_t> *samples,
122                  size_t nsamples,
123                  const tx_metadata *metadata);
124
125     bool tx_raw(unsigned int channel,
126                 const uint32_t *items,
127                 size_t nitems,
128                 const tx_metadata *metadata);
129   };
130   
131 } // namespace usrp2
132
133 #endif /* INCLUDED_USRP2_IMPL_H */