7 Copyright (C) 2005-2013, Chris Frey
8 Portions Copyright (C) 2011, RealVNC Ltd.
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
19 See the GNU General Public License in the COPYING file at the
20 root directory of this project for more details.
36 #ifndef __DEBUG_MODE__
37 #define __DEBUG_MODE__
41 // Pull in the correct Usb::LibraryInterface
42 #if defined USE_LIBUSB_0_1
43 #include "usbwrap_libusb.h"
44 #elif defined USE_LIBUSB_1_0
45 #include "usbwrap_libusb_1_0.h"
47 #error No usb library interface selected.
53 ///////////////////////////////////////////////////////////////////////////////
54 // Usb::Error exception class
56 static std::string
GetErrorString(int libusb_errcode
, const std::string
&str
)
58 std::ostringstream oss
;
61 if( libusb_errcode
) {
62 oss
<< std::dec
<< libusb_errcode
<< " ("
63 << LibraryInterface::TranslateErrcode(libusb_errcode
)
66 oss
<< LibraryInterface::GetLastErrorString(libusb_errcode
) << "): ";
71 Error::Error(const std::string
&str
)
77 Error::Error(int libusb_errcode
, const std::string
&str
)
78 : Barry::Error(libusb_errcode
== 0 ? str
: GetErrorString(libusb_errcode
, str
))
79 , m_libusb_errcode(libusb_errcode
)
83 int Error::system_errcode() const
85 return LibraryInterface::TranslateErrcode(m_libusb_errcode
);
88 ///////////////////////////////////////////////////////////////////////////////
89 // Usb::Timeout exception class
91 Timeout::Timeout(const std::string
&str
)
96 Timeout::Timeout(int errcode
, const std::string
&str
)
101 ///////////////////////////////////////////////////////////////////////////////
104 EndpointPair::EndpointPair()
105 : read(0), write(0), type(EndpointDescriptor::InvalidType
)
109 bool EndpointPair::IsTypeSet() const
111 return type
!= EndpointDescriptor::InvalidType
;
114 bool EndpointPair::IsComplete() const
116 return read
&& write
&& IsTypeSet();
119 bool EndpointPair::IsBulk() const
121 return type
== EndpointDescriptor::BulkType
;
125 ///////////////////////////////////////////////////////////////////////////////
128 EndpointPairings::EndpointPairings(const std::vector
<EndpointDescriptor
*>& eps
)
131 // parse the endpoint into read/write sets, if possible,
132 // going in discovery order...
134 // - endpoints of related utility will be grouped
135 // - endpoints with same type will be grouped
136 // - endpoints that do not meet the above assumptions
137 // do not belong in a pair
140 if( eps
.size() == 0 ) {
141 dout("EndpointPairing:: empty interface pointer");
145 std::vector
<EndpointDescriptor
*>::const_iterator iter
= eps
.begin();
146 while( iter
!= eps
.end() ) {
147 const EndpointDescriptor
& desc
= **iter
;
148 if( desc
.IsRead() ) {
150 pair
.read
= desc
.GetAddress();
151 dout(" pair.read = 0x" << std::hex
<< (unsigned int)pair
.read
);
152 if( pair
.IsTypeSet() && pair
.type
!= desc
.GetType() ) {
153 // if type is already set, we must start over
158 pair
.write
= desc
.GetAddress();
159 dout(" pair.write = 0x" << std::hex
<< (unsigned int)pair
.write
);
160 if( pair
.IsTypeSet() && pair
.type
!= desc
.GetType() ) {
161 // if type is already set, we must start over
165 pair
.type
= desc
.GetType();
167 dout(" pair.type = 0x" << std::hex
<< (unsigned int)pair
.type
);
169 // if pair is complete, add to array
170 if( pair
.IsComplete() ) {
173 dout(" pair added! ("
174 << "read: 0x" << std::hex
<< (unsigned int)pair
.read
<< ","
175 << "write: 0x" << std::hex
<< (unsigned int)pair
.write
<< ","
176 << "type: 0x" << std::hex
<< (unsigned int)pair
.type
<< ")");
177 pair
= EndpointPair(); // clear
185 EndpointPairings::~EndpointPairings()
189 bool EndpointPairings::IsValid() const
194 ///////////////////////////////////////////////////////////////////////////////
195 // EndpointDescriptor
197 bool EndpointDescriptor::IsRead() const
202 uint8_t EndpointDescriptor::GetAddress() const
207 EndpointDescriptor::EpType
EndpointDescriptor::GetType() const
212 ///////////////////////////////////////////////////////////////////////////////
215 Match::Match(DeviceList
& devices
,
216 int vendor
, int product
,
217 const char *busname
, const char *devname
)
218 : m_list(devices
.MatchDevices(vendor
, product
, busname
, devname
))
219 , m_iter(m_list
.begin())
228 bool Match::next_device(Usb::DeviceID
& devid
)
230 if( m_iter
!= m_list
.end() ) {