usbwrap.h
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #ifndef __SB_USBWRAP_H__
00024 #define __SB_USBWRAP_H__
00025
00026 #include "dll.h"
00027 #include <usb.h>
00028 #include <vector>
00029 #include <map>
00030 #include "error.h"
00031
00032 #define USBWRAP_DEFAULT_TIMEOUT 30000
00033
00034 namespace Barry { class Data; }
00035
00036
00037
00038 namespace Usb {
00039
00040
00041
00042
00043
00044 class BXEXPORT Error : public Barry::Error
00045 {
00046 int m_libusb_errcode;
00047
00048 public:
00049 Error(const std::string &str);
00050 Error(int libusb_errcode, const std::string &str);
00051
00052
00053 int libusb_errcode() const { return m_libusb_errcode; }
00054 };
00055
00056 class BXEXPORT Timeout : public Error
00057 {
00058 public:
00059 Timeout(const std::string &str) : Error(str) {}
00060 Timeout(int libusb_errcode, const std::string &str)
00061 : Error(libusb_errcode, str) {}
00062 };
00063
00064
00065
00066
00067
00068 typedef struct usb_device* DeviceIDType;
00069 typedef struct usb_dev_handle* DeviceHandleType;
00070
00071 class BXEXPORT Match
00072 {
00073 private:
00074 struct usb_bus *m_busses;
00075 struct usb_device *m_dev;
00076 int m_vendor, m_product;
00077 int m_lasterror;
00078 const char *m_busname;
00079 const char *m_devname;
00080 protected:
00081 static bool ToNum(const char *str, long &num);
00082 static bool NameCompare(const char *n1, const char *n2);
00083 public:
00084 Match(int vendor, int product,
00085 const char *busname = 0, const char *devname = 0);
00086 ~Match();
00087
00088
00089
00090
00091 bool next_device(Usb::DeviceIDType *devid);
00092 };
00093
00094
00095 class BXEXPORT Device
00096 {
00097 private:
00098 Usb::DeviceIDType m_id;
00099 Usb::DeviceHandleType m_handle;
00100
00101 int m_timeout;
00102 int m_lasterror;
00103
00104 public:
00105 Device(Usb::DeviceIDType id, int timeout = USBWRAP_DEFAULT_TIMEOUT);
00106 ~Device();
00107
00108
00109
00110
00111 Usb::DeviceIDType GetID() const { return m_id; }
00112 Usb::DeviceHandleType GetHandle() const { return m_handle; }
00113 int GetLastError() const { return m_lasterror; }
00114
00115
00116
00117
00118
00119
00120
00121 bool SetConfiguration(unsigned char cfg);
00122 bool ClearHalt(int ep);
00123 bool Reset();
00124
00125
00126
00127
00128
00129 bool BulkRead(int ep, Barry::Data &data, int timeout = -1);
00130 bool BulkWrite(int ep, const Barry::Data &data, int timeout = -1);
00131 bool BulkWrite(int ep, const void *data, size_t size, int timeout = -1);
00132 bool InterruptRead(int ep, Barry::Data &data, int timeout = -1);
00133 bool InterruptWrite(int ep, const Barry::Data &data, int timeout = -1);
00134
00135 void BulkDrain(int ep, int timeout = 100);
00136
00137
00138
00139
00140
00141 bool GetConfiguration(unsigned char &cfg);
00142 };
00143
00144 class BXEXPORT Interface
00145 {
00146 Device &m_dev;
00147 int m_iface;
00148 public:
00149 Interface(Device &dev, int iface);
00150 ~Interface();
00151 };
00152
00153
00154
00155
00156
00157 struct BXEXPORT EndpointPair
00158 {
00159 unsigned char read;
00160 unsigned char write;
00161 unsigned char type;
00162
00163 EndpointPair() : read(0), write(0), type(0xff) {}
00164 bool IsTypeSet() const { return type != 0xff; }
00165 bool IsComplete() const { return read && write && IsTypeSet(); }
00166 };
00167
00168 class BXEXPORT EndpointDiscovery : public std::map<unsigned char, usb_endpoint_descriptor>
00169 {
00170 friend class InterfaceDiscovery;
00171
00172 public:
00173 typedef std::map<unsigned char, usb_endpoint_descriptor>base_type;
00174 typedef std::vector<EndpointPair> endpoint_array_type;
00175
00176 private:
00177 bool m_valid;
00178 endpoint_array_type m_endpoints;
00179
00180 BXLOCAL bool Discover(struct usb_interface_descriptor *interface, int epcount);
00181
00182 public:
00183 EndpointDiscovery() : m_valid(false) {}
00184
00185 bool IsValid() const { return m_valid; }
00186
00187 const endpoint_array_type & GetEndpointPairs() const { return m_endpoints; }
00188 };
00189
00190
00191
00192
00193 struct BXEXPORT InterfaceDesc
00194 {
00195 usb_interface_descriptor desc;
00196 EndpointDiscovery endpoints;
00197 };
00198
00199 class BXEXPORT InterfaceDiscovery : public std::map<int, InterfaceDesc>
00200 {
00201 public:
00202 typedef std::map<int, InterfaceDesc> base_type;
00203
00204 private:
00205 bool m_valid;
00206
00207 BXLOCAL bool DiscoverInterface(struct usb_interface *interface);
00208
00209 public:
00210 InterfaceDiscovery() : m_valid(false) {}
00211
00212 bool Discover(Usb::DeviceIDType devid, int cfgidx, int ifcount);
00213 bool IsValid() const { return m_valid; }
00214 };
00215
00216
00217
00218
00219
00220 struct BXEXPORT ConfigDesc
00221 {
00222 usb_config_descriptor desc;
00223 InterfaceDiscovery interfaces;
00224 };
00225
00226 class BXEXPORT ConfigDiscovery : public std::map<unsigned char, ConfigDesc>
00227 {
00228 public:
00229 typedef std::map<unsigned char, ConfigDesc> base_type;
00230
00231 private:
00232 bool m_valid;
00233
00234 public:
00235 ConfigDiscovery() : m_valid(false) {}
00236
00237 bool Discover(Usb::DeviceIDType devid, int cfgcount);
00238 bool IsValid() const { return m_valid; }
00239 };
00240
00241
00242
00243
00244 class BXEXPORT DeviceDiscovery
00245 {
00246 bool m_valid;
00247
00248 public:
00249 usb_device_descriptor desc;
00250 ConfigDiscovery configs;
00251
00252 public:
00253 DeviceDiscovery(Usb::DeviceIDType devid);
00254
00255 bool Discover(Usb::DeviceIDType devid);
00256 bool IsValid() const { return m_valid; }
00257 };
00258
00259 }
00260
00261 #endif
00262