Bumped copyright dates for 2013
[barry.git] / src / usbwrap.cc
blob30ccd0b138b5eec2fc10cc1e58e0d765527fd672
1 ///
2 /// \file usbwrap.cc
3 /// USB API wrapper
4 ///
6 /*
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.
24 #include "usbwrap.h"
25 #include "data.h"
26 #include "error.h"
27 #include "config.h"
28 #include "debug.h"
30 #include <iomanip>
31 #include <sstream>
32 #include <stdlib.h>
33 #include <errno.h>
34 #include <limits.h>
36 #ifndef __DEBUG_MODE__
37 #define __DEBUG_MODE__
38 #endif
39 #include "debug.h"
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"
46 #else
47 #error No usb library interface selected.
48 #endif
51 namespace Usb {
53 ///////////////////////////////////////////////////////////////////////////////
54 // Usb::Error exception class
56 static std::string GetErrorString(int libusb_errcode, const std::string &str)
58 std::ostringstream oss;
59 oss << "(";
61 if( libusb_errcode ) {
62 oss << std::dec << libusb_errcode << " ("
63 << LibraryInterface::TranslateErrcode(libusb_errcode)
64 << "), ";
66 oss << LibraryInterface::GetLastErrorString(libusb_errcode) << "): ";
67 oss << str;
68 return oss.str();
71 Error::Error(const std::string &str)
72 : Barry::Error(str)
73 , m_libusb_errcode(0)
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)
92 : Error(str)
96 Timeout::Timeout(int errcode, const std::string &str)
97 : Error(errcode, str)
101 ///////////////////////////////////////////////////////////////////////////////
102 // EndpointPair
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 ///////////////////////////////////////////////////////////////////////////////
126 // EndpointPairings
128 EndpointPairings::EndpointPairings(const std::vector<EndpointDescriptor*>& eps)
129 : m_valid(false)
131 // parse the endpoint into read/write sets, if possible,
132 // going in discovery order...
133 // Assumptions:
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
138 EndpointPair pair;
140 if( eps.size() == 0 ) {
141 dout("EndpointPairing:: empty interface pointer");
142 return;
145 std::vector<EndpointDescriptor*>::const_iterator iter = eps.begin();
146 while( iter != eps.end() ) {
147 const EndpointDescriptor& desc = **iter;
148 if( desc.IsRead() ) {
149 // Read endpoint
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
154 pair.write = 0;
156 } else {
157 // Write endpoint
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
162 pair.read = 0;
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() ) {
171 push_back(pair);
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
179 ++iter;
182 m_valid = true;
185 EndpointPairings::~EndpointPairings()
189 bool EndpointPairings::IsValid() const
191 return m_valid;
194 ///////////////////////////////////////////////////////////////////////////////
195 // EndpointDescriptor
197 bool EndpointDescriptor::IsRead() const
199 return m_read;
202 uint8_t EndpointDescriptor::GetAddress() const
204 return m_addr;
207 EndpointDescriptor::EpType EndpointDescriptor::GetType() const
209 return m_type;
212 ///////////////////////////////////////////////////////////////////////////////
213 // Match
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())
224 Match::~Match()
228 bool Match::next_device(Usb::DeviceID& devid)
230 if( m_iter != m_list.end() ) {
231 devid = *m_iter;
232 ++m_iter;
233 return true;
235 return false;
238 } // namespace Usb