Use default arguments instead of overloaded virtual constructors for cleaner interface
[debian/gnuradio] / usrp / host / lib / fusb.h
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2003 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
18  * along with GNU Radio; see the file COPYING.  If not, write to
19  * the Free Software Foundation, Inc., 51 Franklin Street,
20  * Boston, MA 02110-1301, USA.
21  */
22
23 // Fast USB interface
24
25 #ifndef _FUSB_H_
26 #define _FUSB_H_
27
28 #ifdef HAVE_CONFIG_H
29 #include "config.h"
30 #endif
31
32 #ifdef HAVE_LIBUSB_1 
33 struct  libusb_device_handle;
34 #else
35 struct  usb_dev_handle;
36 typedef struct usb_dev_handle libusb_device_handle;
37 #endif
38
39 struct  libusb_context;
40 class   fusb_ephandle;
41
42 /*!
43  * \brief abstract usb device handle
44  */
45 class fusb_devhandle {
46 private:
47   // NOT IMPLEMENTED
48   fusb_devhandle (const fusb_devhandle &rhs);             // no copy constructor
49   fusb_devhandle &operator= (const fusb_devhandle &rhs);  // no assignment operator
50
51 protected:
52   libusb_device_handle          *d_udh;
53
54 public:
55   // CREATORS
56   fusb_devhandle (libusb_device_handle *udh);
57   virtual ~fusb_devhandle ();
58
59   // MANIPULATORS
60   
61   /*!
62    * \brief return an ephandle of the correct subtype
63    */
64   virtual fusb_ephandle *make_ephandle (int endpoint, bool input_p,
65                                         int block_size = 0, int nblocks = 0) = 0;
66   
67   // ACCESSORS
68   libusb_device_handle *get_libusb_device_handle () const { return d_udh; }
69 };
70
71
72 /*!
73  * \brief abstract usb end point handle
74  */
75 class fusb_ephandle {
76 private:
77   // NOT IMPLEMENTED
78   fusb_ephandle (const fusb_ephandle &rhs);             // no copy constructor
79   fusb_ephandle &operator= (const fusb_ephandle &rhs);  // no assignment operator
80
81 protected:
82   int                           d_endpoint;
83   bool                          d_input_p;
84   int                           d_block_size;
85   int                           d_nblocks;
86   bool                          d_started;
87
88 public:
89   fusb_ephandle (int endpoint, bool input_p,
90                  int block_size = 0, int nblocks = 0);
91   virtual ~fusb_ephandle ();
92
93   virtual bool start () = 0;    //!< begin streaming i/o
94   virtual bool stop () = 0;     //!< stop streaming i/o
95
96   /*!
97    * \returns \p nbytes if write was successfully enqueued, else -1.
98    * Will block if no free buffers available.
99    */
100   virtual int write (const void *buffer, int nbytes) = 0;
101
102   /*!
103    * \returns number of bytes read or -1 if error.
104    * number of bytes read will be <= nbytes.
105    * Will block if no input available.
106    */
107   virtual int read (void *buffer, int nbytes) = 0;
108
109   /*
110    * block until all outstanding writes have completed
111    */
112   virtual void wait_for_completion () = 0;
113
114   /*!
115    * \brief returns current block size.
116    */
117   int block_size () { return d_block_size; };
118 };
119
120
121 /*!
122  * \brief factory for creating concrete instances of the appropriate subtype.
123  */
124 class fusb_sysconfig {
125 public:
126   /*!
127    * \brief returns fusb_devhandle or throws if trouble
128    */
129   static fusb_devhandle *make_devhandle (libusb_device_handle *udh,
130                                          libusb_context *ctx = 0);
131
132   /*!
133    * \brief Returns max block size in bytes (hard limit).
134    */
135   static int max_block_size ();
136
137   /*!
138    * \brief Returns default block size in bytes.
139    */
140   static int default_block_size ();
141
142   /*!
143    * \brief Returns the default buffer size in bytes.
144    */
145   static int default_buffer_size ();
146
147 };
148
149 #endif /* _FUSB_H_ */