Updated ChangeLog and whitespace
[barry.git] / src / usbwrap.h
blob25e1843047d7d5c3986a6e7d8c72511f9f1efd5c
1 ///
2 /// \file usbwrap.h
3 /// USB API wrapper
4 ///
6 /*
7 Copyright (C) 2005-2010, 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 "dll.h"
27 #include <usb.h>
28 #include <vector>
29 #include <map>
30 #include "error.h"
32 #define USBWRAP_DEFAULT_TIMEOUT 30000
34 namespace Barry { class Data; }
36 /// Namespace for the libusb-related wrapper classes. This namespace
37 /// may change in the future.
38 namespace Usb {
40 /// \addtogroup exceptions
41 /// @{
43 /// Thrown on low level USB errors.
44 class BXEXPORT Error : public Barry::Error
46 int m_libusb_errcode;
48 public:
49 Error(const std::string &str);
50 Error(int libusb_errcode, const std::string &str);
52 // can return 0 in some case, if unknown error code
53 int libusb_errcode() const { return m_libusb_errcode; }
56 class BXEXPORT Timeout : public Error
58 public:
59 Timeout(const std::string &str) : Error(str) {}
60 Timeout(int libusb_errcode, const std::string &str)
61 : Error(libusb_errcode, str) {}
64 /// @}
66 /// Typedefs used by the wrapper class, in the hope to make it
67 /// easier to switch from libusb stable to devel and back.
68 typedef struct usb_device* DeviceIDType;
69 typedef struct usb_dev_handle* DeviceHandleType;
71 class BXEXPORT Match
73 private:
74 struct usb_bus *m_busses;
75 struct usb_device *m_dev;
76 int m_vendor, m_product;
77 int m_lasterror;
78 const char *m_busname;
79 const char *m_devname;
80 protected:
81 static bool ToNum(const char *str, long &num);
82 static bool NameCompare(const char *n1, const char *n2);
83 public:
84 Match(int vendor, int product,
85 const char *busname = 0, const char *devname = 0);
86 ~Match();
88 // searches for next match, and if found, fills devid with
89 // something you can pass on to DeviceDiscover, etc
90 // returns true if next is found, false if no more
91 bool next_device(Usb::DeviceIDType *devid);
95 class BXEXPORT Device
97 private:
98 Usb::DeviceIDType m_id;
99 Usb::DeviceHandleType m_handle;
101 int m_timeout;
102 int m_lasterror;
104 public:
105 Device(Usb::DeviceIDType id, int timeout = USBWRAP_DEFAULT_TIMEOUT);
106 ~Device();
108 /////////////////////////////
109 // Data access
111 Usb::DeviceIDType GetID() const { return m_id; }
112 Usb::DeviceHandleType GetHandle() const { return m_handle; }
113 int GetLastError() const { return m_lasterror; } //< not thread safe...
114 //< use the error code stored in the exceptions to track
115 //< errors in threaded usage
118 /////////////////////////////
119 // Device manipulation
121 bool SetConfiguration(unsigned char cfg);
122 bool ClearHalt(int ep);
123 bool Reset();
126 /////////////////////////////
127 // IO functions
129 bool BulkRead(int ep, Barry::Data &data, int timeout = -1);
130 bool BulkWrite(int ep, const Barry::Data &data, int timeout = -1);
131 bool BulkWrite(int ep, const void *data, size_t size, int timeout = -1);
132 bool InterruptRead(int ep, Barry::Data &data, int timeout = -1);
133 bool InterruptWrite(int ep, const Barry::Data &data, int timeout = -1);
135 void BulkDrain(int ep, int timeout = 100);
138 /////////////////////////////
139 // Combo functions
141 bool GetConfiguration(unsigned char &cfg);
142 bool SetAltInterface(int iface);
145 class BXEXPORT Interface
147 Device &m_dev;
148 int m_iface;
149 public:
150 Interface(Device &dev, int iface);
151 ~Interface();
157 // Map of Endpoint numbers (not indexes) to endpoint descriptors
158 struct BXEXPORT EndpointPair
160 unsigned char read;
161 unsigned char write;
162 unsigned char type;
164 EndpointPair() : read(0), write(0), type(0xff) {}
165 bool IsTypeSet() const { return type != 0xff; }
166 bool IsComplete() const { return read && write && IsTypeSet(); }
169 class BXEXPORT EndpointDiscovery : public std::map<unsigned char, usb_endpoint_descriptor>
171 friend class InterfaceDiscovery;
173 public:
174 typedef std::map<unsigned char, usb_endpoint_descriptor>base_type;
175 typedef std::vector<EndpointPair> endpoint_array_type;
177 private:
178 bool m_valid;
179 endpoint_array_type m_endpoints;
181 BXLOCAL bool Discover(struct usb_interface_descriptor *interface, int epcount);
183 public:
184 EndpointDiscovery() : m_valid(false) {}
186 bool IsValid() const { return m_valid; }
188 const endpoint_array_type & GetEndpointPairs() const { return m_endpoints; }
193 // Map of Interface numbers (not indexes) to interface descriptors and endpoint map
194 struct BXEXPORT InterfaceDesc
196 usb_interface_descriptor desc;
197 EndpointDiscovery endpoints;
200 class BXEXPORT InterfaceDiscovery : public std::map<int, InterfaceDesc>
202 public:
203 typedef std::map<int, InterfaceDesc> base_type;
205 private:
206 bool m_valid;
208 BXLOCAL bool DiscoverInterface(struct usb_interface *interface);
210 public:
211 InterfaceDiscovery() : m_valid(false) {}
213 bool Discover(Usb::DeviceIDType devid, int cfgidx, int ifcount);
214 bool IsValid() const { return m_valid; }
220 // Map of Config numbers (not indexes) to config descriptors and interface map
221 struct BXEXPORT ConfigDesc
223 usb_config_descriptor desc;
224 InterfaceDiscovery interfaces;
227 class BXEXPORT ConfigDiscovery : public std::map<unsigned char, ConfigDesc>
229 public:
230 typedef std::map<unsigned char, ConfigDesc> base_type;
232 private:
233 bool m_valid;
235 public:
236 ConfigDiscovery() : m_valid(false) {}
238 bool Discover(Usb::DeviceIDType devid, int cfgcount);
239 bool IsValid() const { return m_valid; }
244 // Discovers all configurations, interfaces, and endpoints for a given device
245 class BXEXPORT DeviceDiscovery
247 bool m_valid;
249 public:
250 usb_device_descriptor desc;
251 ConfigDiscovery configs;
253 public:
254 DeviceDiscovery(Usb::DeviceIDType devid);
256 bool Discover(Usb::DeviceIDType devid);
257 bool IsValid() const { return m_valid; }
260 } // namespace Usb
262 #endif