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 ///////////////////////////////////////////////////////////////////////////////
91 EndpointPair::EndpointPair()
92 : read(0), write(0), type(EndpointDescriptor::InvalidType
)
96 bool EndpointPair::IsTypeSet() const
98 return type
!= EndpointDescriptor::InvalidType
;
101 bool EndpointPair::IsComplete() const
103 return read
&& write
&& IsTypeSet();
106 bool EndpointPair::IsBulk() const
108 return type
== EndpointDescriptor::BulkType
;
112 ///////////////////////////////////////////////////////////////////////////////
115 EndpointPairings::EndpointPairings(const std::vector
<EndpointDescriptor
*>& eps
)
118 // parse the endpoint into read/write sets, if possible,
119 // going in discovery order...
121 // - endpoints of related utility will be grouped
122 // - endpoints with same type will be grouped
123 // - endpoints that do not meet the above assumptions
124 // do not belong in a pair
127 if( eps
.size() == 0 ) {
128 dout("EndpointPairing:: empty interface pointer");
132 std::vector
<EndpointDescriptor
*>::const_iterator iter
= eps
.begin();
133 while( iter
!= eps
.end() ) {
134 const EndpointDescriptor
& desc
= **iter
;
135 if( desc
.IsRead() ) {
137 pair
.read
= desc
.GetAddress();
138 dout(" pair.read = 0x" << std::hex
<< (unsigned int)pair
.read
);
139 if( pair
.IsTypeSet() && pair
.type
!= desc
.GetType() ) {
140 // if type is already set, we must start over
145 pair
.write
= desc
.GetAddress();
146 dout(" pair.write = 0x" << std::hex
<< (unsigned int)pair
.write
);
147 if( pair
.IsTypeSet() && pair
.type
!= desc
.GetType() ) {
148 // if type is already set, we must start over
152 pair
.type
= desc
.GetType();
154 dout(" pair.type = 0x" << std::hex
<< (unsigned int)pair
.type
);
156 // if pair is complete, add to array
157 if( pair
.IsComplete() ) {
160 dout(" pair added! ("
161 << "read: 0x" << std::hex
<< (unsigned int)pair
.read
<< ","
162 << "write: 0x" << std::hex
<< (unsigned int)pair
.write
<< ","
163 << "type: 0x" << std::hex
<< (unsigned int)pair
.type
<< ")");
164 pair
= EndpointPair(); // clear
172 EndpointPairings::~EndpointPairings()
176 bool EndpointPairings::IsValid() const
181 ///////////////////////////////////////////////////////////////////////////////
182 // EndpointDescriptor
184 bool EndpointDescriptor::IsRead() const
189 uint8_t EndpointDescriptor::GetAddress() const
194 EndpointDescriptor::EpType
EndpointDescriptor::GetType() const
199 ///////////////////////////////////////////////////////////////////////////////
202 Match::Match(DeviceList
& devices
,
203 int vendor
, int product
,
204 const char *busname
, const char *devname
)
205 : m_list(devices
.MatchDevices(vendor
, product
, busname
, devname
))
206 , m_iter(m_list
.begin())
215 bool Match::next_device(Usb::DeviceID
& devid
)
217 if( m_iter
!= m_list
.end() ) {