usbwrap.h

Go to the documentation of this file.
00001 ///
00002 /// \file       usbwrap.h
00003 ///             USB API wrapper
00004 ///
00005 
00006 /*
00007     Copyright (C) 2005-2008, Chris Frey
00008 
00009     This program is free software; you can redistribute it and/or modify
00010     it under the terms of the GNU General Public License as published by
00011     the Free Software Foundation; either version 2 of the License, or
00012     (at your option) any later version.
00013 
00014     This program is distributed in the hope that it will be useful,
00015     but WITHOUT ANY WARRANTY; without even the implied warranty of
00016     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00017 
00018     See the GNU General Public License in the COPYING file at the
00019     root directory of this project for more details.
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 /// Namespace for the libusb-related wrapper classes.  This namespace
00037 /// may change in the future.
00038 namespace Usb {
00039 
00040 /// \addtogroup exceptions
00041 /// @{
00042 
00043 /// Thrown on low level USB errors.
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         // can return 0 in some case, if unknown error code
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 /// Typedefs used by the wrapper class, in the hope to make it
00067 /// easier to switch from libusb stable to devel and back.
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         // searches for next match, and if found, fills devid with
00089         // something you can pass on to DeviceDiscover, etc
00090         // returns true if next is found, false if no more
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         // Data access
00110 
00111         Usb::DeviceIDType GetID() const { return m_id; }
00112         Usb::DeviceHandleType GetHandle() const { return m_handle; }
00113         int GetLastError() const { return m_lasterror; } //< not thread safe...
00114                 //< use the error code stored in the exceptions to track
00115                 //< errors in threaded usage
00116 
00117 
00118         /////////////////////////////
00119         // Device manipulation
00120 
00121         bool SetConfiguration(unsigned char cfg);
00122         bool ClearHalt(int ep);
00123         bool Reset();
00124 
00125 
00126         /////////////////////////////
00127         // IO functions
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         // Combo functions
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 // Map of Endpoint numbers (not indexes) to endpoint descriptors
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 // Map of Interface numbers (not indexes) to interface descriptors and endpoint map
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 // Map of Config numbers (not indexes) to config descriptors and interface map
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 // Discovers all configurations, interfaces, and endpoints for a given device
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 } // namespace Usb
00260 
00261 #endif
00262 

Generated on Wed Sep 24 21:27:32 2008 for Barry by  doxygen 1.5.1