81dceb1f4f1df99627059554e2286f52631ae359
[debian/gnuradio] / usrp / host / lib / inband / usrp_server.h
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2007 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 #ifndef INCLUDED_USRP_SERVER_H
22 #define INCLUDED_USRP_SERVER_H
23
24 #include <mb_mblock.h>
25 #include <vector>
26 #include <queue>
27 #include <fstream>
28 #include <usrp_inband_usb_packet.h>
29
30 typedef usrp_inband_usb_packet transport_pkt;   // makes conversion to gigabit easy
31
32 /*!
33  * \brief Implements the lowest-level mblock usb_interface to the USRP
34  */
35 class usrp_server : public mb_mblock
36 {
37 public:
38
39   // our ports
40   enum port_types {
41     RX_PORT = 0,
42     TX_PORT = 1
43   };
44   static const int N_PORTS = 4;
45   std::vector<mb_port_sptr> d_tx, d_rx;
46   mb_port_sptr  d_cs;
47   mb_port_sptr  d_cs_usrp;
48
49   static const int D_USB_CAPACITY = 32 * 1024 * 1024;
50   static const int D_MAX_CHANNELS = 16;
51   long d_ntx_chan;
52   long d_nrx_chan;
53
54   // Keep track of the request IDs
55   struct rid_info {
56     pmt_t owner;
57     long user_rid;
58
59     rid_info() {
60       owner = PMT_NIL;
61       user_rid = 0;
62     }
63   };
64
65   static const long D_MAX_RID = 64;
66   std::vector<rid_info> d_rids;
67   
68   struct channel_info {
69     long assigned_capacity;   // the capacity currently assignedby the channel
70     pmt_t owner;              // port ID of the owner of the channel
71
72     channel_info() {
73       assigned_capacity = 0;
74       owner = PMT_NIL;
75     }
76   };
77
78   long d_rx_chan_mask;    // A bitmask representing the channels in the
79                           // receiving state
80
81   std::vector<struct channel_info> d_chaninfo_tx;
82   std::vector<struct channel_info> d_chaninfo_rx;
83
84   std::queue<mb_message_sptr> d_defer_queue;
85
86   bool d_defer;
87   bool d_opened;
88
89   bool d_fake_rx;
90
91 public:
92   usrp_server(mb_runtime *rt, const std::string &instance_name, pmt_t user_arg);
93   ~usrp_server();
94
95   void initial_transition();
96   void handle_message(mb_message_sptr msg);
97
98 protected:
99   static int max_capacity() { return D_USB_CAPACITY; }
100
101 private:
102   void handle_cmd_allocate_channel(mb_port_sptr port, std::vector<struct channel_info> &chan_info, pmt_t data);
103   void handle_cmd_deallocate_channel(mb_port_sptr port, std::vector<struct channel_info> &chan_info, pmt_t data);
104   void handle_cmd_xmit_raw_frame(mb_port_sptr port, std::vector<struct channel_info> &chan_info, pmt_t data);
105   void handle_cmd_to_control_channel(mb_port_sptr port, std::vector<struct channel_info> &chan_info, pmt_t data);
106   void handle_cmd_start_recv_raw_samples(mb_port_sptr port, std::vector<struct channel_info> &chan_info, pmt_t data);
107   void handle_cmd_stop_recv_raw_samples(mb_port_sptr port, std::vector<struct channel_info> &chan_info, pmt_t data);
108   int rx_port_index(pmt_t port_id);
109   int tx_port_index(pmt_t port_id);
110   long current_capacity_allocation();
111   void recall_defer_queue();
112   void reset_channels();
113   void handle_response_usrp_read(pmt_t data);
114   bool check_valid(mb_port_sptr port, long channel, std::vector<struct channel_info> &chan_info, pmt_t signal_info);
115   void parse_control_pkt(pmt_t invocation_handle, transport_pkt *pkt);
116   long next_rid();
117 };
118
119 #endif /* INCLUDED_USRP_SERVER_H */