Bumped copyright dates for 2013
[barry.git] / src / usbwrap.cc
blobffe6cdf758fe9c28807d9c6ae8dfd90e014c7a30
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 // EndpointPair
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 ///////////////////////////////////////////////////////////////////////////////
113 // EndpointPairings
115 EndpointPairings::EndpointPairings(const std::vector<EndpointDescriptor*>& eps)
116 : m_valid(false)
118 // parse the endpoint into read/write sets, if possible,
119 // going in discovery order...
120 // Assumptions:
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
125 EndpointPair pair;
127 if( eps.size() == 0 ) {
128 dout("EndpointPairing:: empty interface pointer");
129 return;
132 std::vector<EndpointDescriptor*>::const_iterator iter = eps.begin();
133 while( iter != eps.end() ) {
134 const EndpointDescriptor& desc = **iter;
135 if( desc.IsRead() ) {
136 // Read endpoint
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
141 pair.write = 0;
143 } else {
144 // Write endpoint
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
149 pair.read = 0;
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() ) {
158 push_back(pair);
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
166 ++iter;
169 m_valid = true;
172 EndpointPairings::~EndpointPairings()
176 bool EndpointPairings::IsValid() const
178 return m_valid;
181 ///////////////////////////////////////////////////////////////////////////////
182 // EndpointDescriptor
184 bool EndpointDescriptor::IsRead() const
186 return m_read;
189 uint8_t EndpointDescriptor::GetAddress() const
191 return m_addr;
194 EndpointDescriptor::EpType EndpointDescriptor::GetType() const
196 return m_type;
199 ///////////////////////////////////////////////////////////////////////////////
200 // Match
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())
211 Match::~Match()
215 bool Match::next_device(Usb::DeviceID& devid)
217 if( m_iter != m_list.end() ) {
218 devid = *m_iter;
219 ++m_iter;
220 return true;
222 return false;
225 } // namespace Usb