7 Copyright (C) 2005-2007, Chris Frey
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
18 See the GNU General Public License in the COPYING file at the
19 root directory of this project for more details.
23 #ifndef __SB_USBWRAP_H__
24 #define __SB_USBWRAP_H__
31 #define USBWRAP_DEFAULT_TIMEOUT 30000
33 namespace Barry
{ class Data
; }
35 /// Namespace for the libusb-related wrapper classes. This namespace
36 /// may change in the future.
39 /// \addtogroup exceptions
42 /// Thrown on low level USB errors.
43 class Error
: public Barry::Error
46 Error(const std::string
&str
) : Barry::Error(str
) {}
49 class Timeout
: public Error
52 Timeout(const std::string
&str
) : Error(str
) {}
57 /// Typedefs used by the wrapper class, in the hope to make it
58 /// easier to switch from libusb stable to devel and back.
59 typedef struct usb_device
* DeviceIDType
;
60 typedef struct usb_dev_handle
* DeviceHandleType
;
65 struct usb_bus
*m_busses
;
66 struct usb_device
*m_dev
;
67 int m_vendor
, m_product
;
70 Match(int vendor
, int product
);
73 // searches for next match, and if found, fills devid with
74 // something you can pass on to DeviceDiscover, etc
75 // returns true if next is found, false if no more
76 bool next_device(Usb::DeviceIDType
*devid
);
83 Usb::DeviceIDType m_id
;
84 Usb::DeviceHandleType m_handle
;
90 Device(Usb::DeviceIDType id
, int timeout
= USBWRAP_DEFAULT_TIMEOUT
);
93 /////////////////////////////
96 Usb::DeviceIDType
GetID() const { return m_id
; }
97 Usb::DeviceHandleType
GetHandle() const { return m_handle
; }
98 int GetLastError() const { return m_lasterror
; }
101 /////////////////////////////
102 // Device manipulation
104 bool SetConfiguration(unsigned char cfg
);
105 bool ClearHalt(int ep
);
109 /////////////////////////////
112 bool BulkRead(int ep
, Barry::Data
&data
, int timeout
= -1);
113 bool BulkWrite(int ep
, const Barry::Data
&data
, int timeout
= -1);
114 bool BulkWrite(int ep
, const void *data
, size_t size
, int timeout
= -1);
115 bool InterruptRead(int ep
, Barry::Data
&data
, int timeout
= -1);
116 bool InterruptWrite(int ep
, const Barry::Data
&data
, int timeout
= -1);
118 void BulkDrain(int ep
);
126 Interface(Device
&dev
, int iface
)
127 : m_dev(dev
), m_iface(iface
)
129 if( usb_claim_interface(dev
.GetHandle(), iface
) < 0 )
130 throw Error("claim interface failed");
135 usb_release_interface(m_dev
.GetHandle(), m_iface
);
142 // Map of Endpoint numbers (not indexes) to endpoint descriptors
149 EndpointPair() : read(0), write(0), type(0xff) {}
150 bool IsTypeSet() const { return type
!= 0xff; }
151 bool IsComplete() const { return read
&& write
&& IsTypeSet(); }
154 class EndpointDiscovery
: public std::map
<unsigned char, usb_endpoint_descriptor
>
156 friend class InterfaceDiscovery
;
159 typedef std::map
<unsigned char, usb_endpoint_descriptor
>base_type
;
160 typedef std::vector
<EndpointPair
> endpoint_array_type
;
164 endpoint_array_type m_endpoints
;
166 bool Discover(struct usb_interface_descriptor
*interface
, int epcount
);
169 EndpointDiscovery() : m_valid(false) {}
171 bool IsValid() const { return m_valid
; }
173 const endpoint_array_type
& GetEndpointPairs() const { return m_endpoints
; }
178 // Map of Interface numbers (not indexes) to interface descriptors and endpoint map
181 usb_interface_descriptor desc
;
182 EndpointDiscovery endpoints
;
185 class InterfaceDiscovery
: public std::map
<int, InterfaceDesc
>
188 typedef std::map
<int, InterfaceDesc
> base_type
;
193 bool DiscoverInterface(struct usb_interface
*interface
);
196 InterfaceDiscovery() : m_valid(false) {}
198 bool Discover(Usb::DeviceIDType devid
, int cfgidx
, int ifcount
);
199 bool IsValid() const { return m_valid
; }
205 // Map of Config numbers (not indexes) to config descriptors and interface map
208 usb_config_descriptor desc
;
209 InterfaceDiscovery interfaces
;
212 class ConfigDiscovery
: public std::map
<unsigned char, ConfigDesc
>
215 typedef std::map
<unsigned char, ConfigDesc
> base_type
;
221 ConfigDiscovery() : m_valid(false) {}
223 bool Discover(Usb::DeviceIDType devid
, int cfgcount
);
224 bool IsValid() const { return m_valid
; }
229 // Discovers all configurations, interfaces, and endpoints for a given device
230 class DeviceDiscovery
235 usb_device_descriptor desc
;
236 ConfigDiscovery configs
;
239 DeviceDiscovery(Usb::DeviceIDType devid
);
241 bool Discover(Usb::DeviceIDType devid
);
242 bool IsValid() const { return m_valid
; }