- added ctags generation to buildgen.sh
[barry.git] / src / usbwrap.h
blobbf0d6f4cae003c63fb4aa213f361e19cc9d535c2
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 public:
46 Error(const std::string &str) : Barry::Error(str) {}
49 class Timeout : public Error
51 public:
52 Timeout(const std::string &str) : Error(str) {}
55 /// @}
57 /// Typedefs used by the wrapper class, in the hope to make it
58 /// easier to switch from libusb stable to devel and back.
59 typedef struct usb_device* DeviceIDType;
60 typedef struct usb_dev_handle* DeviceHandleType;
62 class Match
64 private:
65 struct usb_bus *m_busses;
66 struct usb_device *m_dev;
67 int m_vendor, m_product;
68 int m_lasterror;
69 public:
70 Match(int vendor, int product);
71 ~Match();
73 // searches for next match, and if found, fills devid with
74 // something you can pass on to DeviceDiscover, etc
75 // returns true if next is found, false if no more
76 bool next_device(Usb::DeviceIDType *devid);
80 class Device
82 private:
83 Usb::DeviceIDType m_id;
84 Usb::DeviceHandleType m_handle;
86 int m_timeout;
87 int m_lasterror;
89 public:
90 Device(Usb::DeviceIDType id, int timeout = USBWRAP_DEFAULT_TIMEOUT);
91 ~Device();
93 /////////////////////////////
94 // Data access
96 Usb::DeviceIDType GetID() const { return m_id; }
97 Usb::DeviceHandleType GetHandle() const { return m_handle; }
98 int GetLastError() const { return m_lasterror; }
101 /////////////////////////////
102 // Device manipulation
104 bool SetConfiguration(unsigned char cfg);
105 bool ClearHalt(int ep);
106 bool Reset();
109 /////////////////////////////
110 // IO functions
112 bool BulkRead(int ep, Barry::Data &data, int timeout = -1);
113 bool BulkWrite(int ep, const Barry::Data &data, int timeout = -1);
114 bool BulkWrite(int ep, const void *data, size_t size, int timeout = -1);
115 bool InterruptRead(int ep, Barry::Data &data, int timeout = -1);
116 bool InterruptWrite(int ep, const Barry::Data &data, int timeout = -1);
118 void BulkDrain(int ep);
121 class Interface
123 Device &m_dev;
124 int m_iface;
125 public:
126 Interface(Device &dev, int iface)
127 : m_dev(dev), m_iface(iface)
129 if( usb_claim_interface(dev.GetHandle(), iface) < 0 )
130 throw Error("claim interface failed");
133 ~Interface()
135 usb_release_interface(m_dev.GetHandle(), m_iface);
142 // Map of Endpoint numbers (not indexes) to endpoint descriptors
143 struct EndpointPair
145 unsigned char read;
146 unsigned char write;
147 unsigned char type;
149 EndpointPair() : read(0), write(0), type(0xff) {}
150 bool IsTypeSet() const { return type != 0xff; }
151 bool IsComplete() const { return read && write && IsTypeSet(); }
154 class EndpointDiscovery : public std::map<unsigned char, usb_endpoint_descriptor>
156 friend class InterfaceDiscovery;
158 public:
159 typedef std::map<unsigned char, usb_endpoint_descriptor>base_type;
160 typedef std::vector<EndpointPair> endpoint_array_type;
162 private:
163 bool m_valid;
164 endpoint_array_type m_endpoints;
166 bool Discover(struct usb_interface_descriptor *interface, int epcount);
168 public:
169 EndpointDiscovery() : m_valid(false) {}
171 bool IsValid() const { return m_valid; }
173 const endpoint_array_type & GetEndpointPairs() const { return m_endpoints; }
178 // Map of Interface numbers (not indexes) to interface descriptors and endpoint map
179 struct InterfaceDesc
181 usb_interface_descriptor desc;
182 EndpointDiscovery endpoints;
185 class InterfaceDiscovery : public std::map<int, InterfaceDesc>
187 public:
188 typedef std::map<int, InterfaceDesc> base_type;
190 private:
191 bool m_valid;
193 bool DiscoverInterface(struct usb_interface *interface);
195 public:
196 InterfaceDiscovery() : m_valid(false) {}
198 bool Discover(Usb::DeviceIDType devid, int cfgidx, int ifcount);
199 bool IsValid() const { return m_valid; }
205 // Map of Config numbers (not indexes) to config descriptors and interface map
206 struct ConfigDesc
208 usb_config_descriptor desc;
209 InterfaceDiscovery interfaces;
212 class ConfigDiscovery : public std::map<unsigned char, ConfigDesc>
214 public:
215 typedef std::map<unsigned char, ConfigDesc> base_type;
217 private:
218 bool m_valid;
220 public:
221 ConfigDiscovery() : m_valid(false) {}
223 bool Discover(Usb::DeviceIDType devid, int cfgcount);
224 bool IsValid() const { return m_valid; }
229 // Discovers all configurations, interfaces, and endpoints for a given device
230 class DeviceDiscovery
232 bool m_valid;
234 public:
235 usb_device_descriptor desc;
236 ConfigDiscovery configs;
238 public:
239 DeviceDiscovery(Usb::DeviceIDType devid);
241 bool Discover(Usb::DeviceIDType devid);
242 bool IsValid() const { return m_valid; }
245 } // namespace Usb
247 #endif