Release tarball for barry-0.9
[barry.git] / src / usbwrap.h
blob31878ebf45d2b8e717a09ed2734049d30f451a82
1 ///
2 /// \file usbwrap.h
3 /// USB API wrapper
4 ///
6 /*
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__
26 #include <usb.h>
27 #include <vector>
28 #include <map>
29 #include "error.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.
37 namespace Usb {
39 /// \addtogroup exceptions
40 /// @{
42 /// Thrown on low level USB errors.
43 class Error : public Barry::Error
45 int m_libusb_errcode;
47 public:
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
57 public:
58 Timeout(const std::string &str) : Error(str) {}
59 Timeout(int libusb_errcode, const std::string &str)
60 : Error(libusb_errcode, str) {}
63 /// @}
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;
70 class Match
72 private:
73 struct usb_bus *m_busses;
74 struct usb_device *m_dev;
75 int m_vendor, m_product;
76 int m_lasterror;
77 public:
78 Match(int vendor, int product);
79 ~Match();
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);
88 class Device
90 private:
91 Usb::DeviceIDType m_id;
92 Usb::DeviceHandleType m_handle;
94 int m_timeout;
95 int m_lasterror;
97 public:
98 Device(Usb::DeviceIDType id, int timeout = USBWRAP_DEFAULT_TIMEOUT);
99 ~Device();
101 /////////////////////////////
102 // Data access
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);
114 bool Reset();
117 /////////////////////////////
118 // IO functions
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);
129 class Interface
131 Device &m_dev;
132 int m_iface;
133 public:
134 Interface(Device &dev, int iface);
135 ~Interface();
141 // Map of Endpoint numbers (not indexes) to endpoint descriptors
142 struct EndpointPair
144 unsigned char read;
145 unsigned char write;
146 unsigned char type;
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;
157 public:
158 typedef std::map<unsigned char, usb_endpoint_descriptor>base_type;
159 typedef std::vector<EndpointPair> endpoint_array_type;
161 private:
162 bool m_valid;
163 endpoint_array_type m_endpoints;
165 bool Discover(struct usb_interface_descriptor *interface, int epcount);
167 public:
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
178 struct InterfaceDesc
180 usb_interface_descriptor desc;
181 EndpointDiscovery endpoints;
184 class InterfaceDiscovery : public std::map<int, InterfaceDesc>
186 public:
187 typedef std::map<int, InterfaceDesc> base_type;
189 private:
190 bool m_valid;
192 bool DiscoverInterface(struct usb_interface *interface);
194 public:
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
205 struct ConfigDesc
207 usb_config_descriptor desc;
208 InterfaceDiscovery interfaces;
211 class ConfigDiscovery : public std::map<unsigned char, ConfigDesc>
213 public:
214 typedef std::map<unsigned char, ConfigDesc> base_type;
216 private:
217 bool m_valid;
219 public:
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
231 bool m_valid;
233 public:
234 usb_device_descriptor desc;
235 ConfigDiscovery configs;
237 public:
238 DeviceDiscovery(Usb::DeviceIDType devid);
240 bool Discover(Usb::DeviceIDType devid);
241 bool IsValid() const { return m_valid; }
244 } // namespace Usb
246 #endif