aboutsummaryrefslogtreecommitdiffstats
path: root/host/lib/transport/libusb1_base.hpp
blob: 47d078cdef9b6c709e81a988168a57efefdbd4a5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
//
// Copyright 2010 Ettus Research LLC
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
//

#ifndef INCLUDED_LIBUHD_TRANSPORT_LIBUSB_HPP
#define INCLUDED_LIBUHD_TRANSPORT_LIBUSB_HPP

#include <uhd/config.hpp>
#include <boost/utility.hpp>
#include <boost/shared_ptr.hpp>
#include <uhd/transport/usb_device_handle.hpp>
#include <libusb-1.0/libusb.h>

namespace uhd { namespace transport {

namespace libusb {

    /*!
     * This session class holds a global libusb context for this process.
     * The get global session call will create a new context if none exists.
     * When all references to session are destroyed, the context will be freed.
     */
    class session : boost::noncopyable {
    public:
        typedef boost::shared_ptr<session> sptr;

        /*!
         *   Level 0: no messages ever printed by the library (default)
         *   Level 1: error messages are printed to stderr
         *   Level 2: warning and error messages are printed to stderr
         *   Level 3: informational messages are printed to stdout, warning
         *            and error messages are printed to stderr
         */
        static const int debug_level = 0;

        //! get a shared pointer to the global session
        static sptr get_global_session(void);

        //! get the underlying libusb context pointer
        virtual libusb_context *get_context(void) const = 0;
    };

    /*!
     * Holds a device pointer with a reference to the session.
     */
    class device : boost::noncopyable {
    public:
        typedef boost::shared_ptr<device> sptr;

        //! get the underlying device pointer
        virtual libusb_device *get(void) const = 0;
    };

    /*!
     * This device list class holds a device list that will be
     * automatically freed when the last reference is destroyed.
     */
    class device_list : boost::noncopyable {
    public:
        typedef boost::shared_ptr<device_list> sptr;

        //! make a new device list
        static sptr make(void);

        //! the number of devices in this list
        virtual size_t size() const = 0;

        //! get the device pointer at a particular index
        virtual device::sptr at(size_t index) const = 0;
    };

    /*!
     * Holds a device descriptor and a reference to the device.
     */
    class device_descriptor : boost::noncopyable {
    public:
        typedef boost::shared_ptr<device_descriptor> sptr;

        //! make a new descriptor from a device reference
        static sptr make(device::sptr);

        //! get the underlying device descriptor
        virtual const libusb_device_descriptor &get(void) const = 0;

        virtual std::string get_ascii_serial(void) const = 0;
    };

    /*!
     * Holds a device handle and a reference to the device.
     */
    class device_handle : boost::noncopyable {
    public:
        typedef boost::shared_ptr<device_handle> sptr;

        //! get a cached handle or make a new one given the device
        static sptr get_cached_handle(device::sptr);

        //! get the underlying device handle
        virtual libusb_device_handle *get(void) const = 0;

        /*!
         * Open USB interfaces for control using magic value
         * IN interface:      2
         * OUT interface:     1
         * Control interface: 0
         */
        virtual void claim_interface(int) = 0;
    };

    /*!
     * The special handle is our internal implementation of the
     * usb device handle which is used publicly to identify a device.
     */
    class special_handle : public usb_device_handle {
    public:
        typedef boost::shared_ptr<special_handle> sptr;

        //! make a new special handle from device
        static sptr make(device::sptr);

        //! get the underlying device reference
        virtual device::sptr get_device(void) const = 0;
    };

}

}} //namespace

#endif /* INCLUDED_LIBUHD_TRANSPORT_LIBUSB_HPP */