lib: added operator=() to DBData class
[barry.git] / src / usbwrap.h
blob02120382ecdace4a110835935876faf6bf6709bf
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"
28 // On Windows systems, usb.h includes <windows.h> which defines min/max,
29 // which causes trouble for other headers
30 #include <usb.h>
31 #undef min
32 #undef max
34 #include <vector>
35 #include <map>
36 #include "error.h"
38 #define USBWRAP_DEFAULT_TIMEOUT 30000
40 namespace Barry { class Data; }
42 /// Namespace for the libusb-related wrapper classes. This namespace
43 /// may change in the future.
44 namespace Usb {
46 /// \addtogroup exceptions
47 /// @{
49 /// Thrown on low level USB errors.
50 class BXEXPORT Error : public Barry::Error
52 int m_libusb_errcode;
54 public:
55 Error(const std::string &str);
56 Error(int libusb_errcode, const std::string &str);
58 // can return 0 in some case, if unknown error code
59 int libusb_errcode() const { return m_libusb_errcode; }
62 class BXEXPORT Timeout : public Error
64 public:
65 Timeout(const std::string &str) : Error(str) {}
66 Timeout(int libusb_errcode, const std::string &str)
67 : Error(libusb_errcode, str) {}
70 /// @}
72 /// Typedefs used by the wrapper class, in the hope to make it
73 /// easier to switch from libusb stable to devel and back.
74 typedef struct usb_device* DeviceIDType;
75 typedef struct usb_dev_handle* DeviceHandleType;
77 class BXEXPORT Match
79 private:
80 struct usb_bus *m_busses;
81 struct usb_device *m_dev;
82 int m_vendor, m_product;
83 int m_lasterror;
84 const char *m_busname;
85 const char *m_devname;
86 protected:
87 static bool ToNum(const char *str, long &num);
88 static bool NameCompare(const char *n1, const char *n2);
89 public:
90 Match(int vendor, int product,
91 const char *busname = 0, const char *devname = 0);
92 ~Match();
94 // searches for next match, and if found, fills devid with
95 // something you can pass on to DeviceDiscover, etc
96 // returns true if next is found, false if no more
97 bool next_device(Usb::DeviceIDType *devid);
101 class BXEXPORT Device
103 private:
104 Usb::DeviceIDType m_id;
105 Usb::DeviceHandleType m_handle;
107 int m_timeout;
108 int m_lasterror;
110 public:
111 Device(Usb::DeviceIDType id, int timeout = USBWRAP_DEFAULT_TIMEOUT);
112 ~Device();
114 /////////////////////////////
115 // Data access
117 Usb::DeviceIDType GetID() const { return m_id; }
118 Usb::DeviceHandleType GetHandle() const { return m_handle; }
119 int GetLastError() const { return m_lasterror; } //< not thread safe...
120 //< use the error code stored in the exceptions to track
121 //< errors in threaded usage
122 int GetDefaultTimeout() const { return m_timeout; }
125 /////////////////////////////
126 // Device manipulation
128 bool SetConfiguration(unsigned char cfg);
129 bool ClearHalt(int ep);
130 bool Reset();
133 /////////////////////////////
134 // IO functions
136 bool BulkRead(int ep, Barry::Data &data, int timeout = -1);
137 bool BulkWrite(int ep, const Barry::Data &data, int timeout = -1);
138 bool BulkWrite(int ep, const void *data, size_t size, int timeout = -1);
139 bool InterruptRead(int ep, Barry::Data &data, int timeout = -1);
140 bool InterruptWrite(int ep, const Barry::Data &data, int timeout = -1);
142 void BulkDrain(int ep, int timeout = 100);
145 /////////////////////////////
146 // Combo functions
148 bool GetConfiguration(unsigned char &cfg);
149 bool SetAltInterface(int iface);
152 class BXEXPORT Interface
154 Device &m_dev;
155 int m_iface;
156 public:
157 Interface(Device &dev, int iface);
158 ~Interface();
164 // Map of Endpoint numbers (not indexes) to endpoint descriptors
165 struct BXEXPORT EndpointPair
167 unsigned char read;
168 unsigned char write;
169 unsigned char type;
171 EndpointPair() : read(0), write(0), type(0xff) {}
172 bool IsTypeSet() const { return type != 0xff; }
173 bool IsComplete() const { return read && write && IsTypeSet(); }
176 class BXEXPORT EndpointDiscovery : public std::map<unsigned char, usb_endpoint_descriptor>
178 friend class InterfaceDiscovery;
180 public:
181 typedef std::map<unsigned char, usb_endpoint_descriptor>base_type;
182 typedef std::vector<EndpointPair> endpoint_array_type;
184 private:
185 bool m_valid;
186 endpoint_array_type m_endpoints;
188 BXLOCAL bool Discover(struct usb_interface_descriptor *interface, int epcount);
190 public:
191 EndpointDiscovery() : m_valid(false) {}
193 bool IsValid() const { return m_valid; }
195 const endpoint_array_type & GetEndpointPairs() const { return m_endpoints; }
200 // Map of Interface numbers (not indexes) to interface descriptors and endpoint map
201 struct BXEXPORT InterfaceDesc
203 usb_interface_descriptor desc;
204 EndpointDiscovery endpoints;
207 class BXEXPORT InterfaceDiscovery : public std::map<int, InterfaceDesc>
209 public:
210 typedef std::map<int, InterfaceDesc> base_type;
212 private:
213 bool m_valid;
215 BXLOCAL bool DiscoverInterface(struct usb_interface *interface);
217 public:
218 InterfaceDiscovery() : m_valid(false) {}
220 bool Discover(Usb::DeviceIDType devid, int cfgidx, int ifcount);
221 bool IsValid() const { return m_valid; }
227 // Map of Config numbers (not indexes) to config descriptors and interface map
228 struct BXEXPORT ConfigDesc
230 usb_config_descriptor desc;
231 InterfaceDiscovery interfaces;
234 class BXEXPORT ConfigDiscovery : public std::map<unsigned char, ConfigDesc>
236 public:
237 typedef std::map<unsigned char, ConfigDesc> base_type;
239 private:
240 bool m_valid;
242 public:
243 ConfigDiscovery() : m_valid(false) {}
245 bool Discover(Usb::DeviceIDType devid, int cfgcount);
246 bool IsValid() const { return m_valid; }
251 // Discovers all configurations, interfaces, and endpoints for a given device
252 class BXEXPORT DeviceDiscovery
254 bool m_valid;
256 public:
257 usb_device_descriptor desc;
258 ConfigDiscovery configs;
260 public:
261 DeviceDiscovery(Usb::DeviceIDType devid);
263 bool Discover(Usb::DeviceIDType devid);
264 bool IsValid() const { return m_valid; }
267 } // namespace Usb
269 #endif