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
48 Error(const std::string
&str
);
49 Error(int libusb_errcode
, const std::string
&str
);
51 // can return 0 in some case, if unknown error code
52 int libusb_errcode() const { return m_libusb_errcode
; }
55 class Timeout
: public Error
58 Timeout(const std::string
&str
) : Error(str
) {}
59 Timeout(int libusb_errcode
, const std::string
&str
)
60 : Error(libusb_errcode
, str
) {}
65 /// Typedefs used by the wrapper class, in the hope to make it
66 /// easier to switch from libusb stable to devel and back.
67 typedef struct usb_device
* DeviceIDType
;
68 typedef struct usb_dev_handle
* DeviceHandleType
;
73 struct usb_bus
*m_busses
;
74 struct usb_device
*m_dev
;
75 int m_vendor
, m_product
;
78 Match(int vendor
, int product
);
81 // searches for next match, and if found, fills devid with
82 // something you can pass on to DeviceDiscover, etc
83 // returns true if next is found, false if no more
84 bool next_device(Usb::DeviceIDType
*devid
);
91 Usb::DeviceIDType m_id
;
92 Usb::DeviceHandleType m_handle
;
98 Device(Usb::DeviceIDType id
, int timeout
= USBWRAP_DEFAULT_TIMEOUT
);
101 /////////////////////////////
104 Usb::DeviceIDType
GetID() const { return m_id
; }
105 Usb::DeviceHandleType
GetHandle() const { return m_handle
; }
106 int GetLastError() const { return m_lasterror
; }
109 /////////////////////////////
110 // Device manipulation
112 bool SetConfiguration(unsigned char cfg
);
113 bool ClearHalt(int ep
);
117 /////////////////////////////
120 bool BulkRead(int ep
, Barry::Data
&data
, int timeout
= -1);
121 bool BulkWrite(int ep
, const Barry::Data
&data
, int timeout
= -1);
122 bool BulkWrite(int ep
, const void *data
, size_t size
, int timeout
= -1);
123 bool InterruptRead(int ep
, Barry::Data
&data
, int timeout
= -1);
124 bool InterruptWrite(int ep
, const Barry::Data
&data
, int timeout
= -1);
126 void BulkDrain(int ep
);
134 Interface(Device
&dev
, int iface
);
141 // Map of Endpoint numbers (not indexes) to endpoint descriptors
148 EndpointPair() : read(0), write(0), type(0xff) {}
149 bool IsTypeSet() const { return type
!= 0xff; }
150 bool IsComplete() const { return read
&& write
&& IsTypeSet(); }
153 class EndpointDiscovery
: public std::map
<unsigned char, usb_endpoint_descriptor
>
155 friend class InterfaceDiscovery
;
158 typedef std::map
<unsigned char, usb_endpoint_descriptor
>base_type
;
159 typedef std::vector
<EndpointPair
> endpoint_array_type
;
163 endpoint_array_type m_endpoints
;
165 bool Discover(struct usb_interface_descriptor
*interface
, int epcount
);
168 EndpointDiscovery() : m_valid(false) {}
170 bool IsValid() const { return m_valid
; }
172 const endpoint_array_type
& GetEndpointPairs() const { return m_endpoints
; }
177 // Map of Interface numbers (not indexes) to interface descriptors and endpoint map
180 usb_interface_descriptor desc
;
181 EndpointDiscovery endpoints
;
184 class InterfaceDiscovery
: public std::map
<int, InterfaceDesc
>
187 typedef std::map
<int, InterfaceDesc
> base_type
;
192 bool DiscoverInterface(struct usb_interface
*interface
);
195 InterfaceDiscovery() : m_valid(false) {}
197 bool Discover(Usb::DeviceIDType devid
, int cfgidx
, int ifcount
);
198 bool IsValid() const { return m_valid
; }
204 // Map of Config numbers (not indexes) to config descriptors and interface map
207 usb_config_descriptor desc
;
208 InterfaceDiscovery interfaces
;
211 class ConfigDiscovery
: public std::map
<unsigned char, ConfigDesc
>
214 typedef std::map
<unsigned char, ConfigDesc
> base_type
;
220 ConfigDiscovery() : m_valid(false) {}
222 bool Discover(Usb::DeviceIDType devid
, int cfgcount
);
223 bool IsValid() const { return m_valid
; }
228 // Discovers all configurations, interfaces, and endpoints for a given device
229 class DeviceDiscovery
234 usb_device_descriptor desc
;
235 ConfigDiscovery configs
;
238 DeviceDiscovery(Usb::DeviceIDType devid
);
240 bool Discover(Usb::DeviceIDType devid
);
241 bool IsValid() const { return m_valid
; }