📄 usbpp.h
字号:
// -*- C++;indent-tabs-mode: t; tab-width: 4; c-basic-offset: 4; -*-#ifndef __USBPP_HEADER__#define __USBPP_HEADER__#include <string>#include <list>#include <usb.h>/* * The following usb.h function is not wrapped yet: * char *usb_strerror(void); *//** * \brief Classes to access Universal Serial Bus devices * * The USB Namespace provides a number of classes to work * with Universal Serial Bus (USB) devices attached to the * system. * * \author Brad Hards */namespace USB { class Device; /** * \brief Class representing a device endpoint * * This class represents a device endpoint. You need this class to * perform bulk reads and writes. * */ class Endpoint { /** * Busses is a friend because it fills in the descriptor type * information on initialisation and rescan. */ friend class Busses; public: Endpoint() {};#ifdef USE_UNTESTED_LIBUSBPP_METHODS /** * \brief Bulk write * * This method performs a bulk transfer to the endpoint. * * \param message is the message to be sent. * \param timeout is the USB transaction timeout in milliseconds * * \returns the number of bytes sent, or a negative value on * failure */ int bulkWrite(QByteArray message, int timeout = 100); /** * \brief Bulk read * * This method performs a bulk transfer from the endpoint. * * \param length is the maximum data transfer required. * \param message is the message that was received. * \param timeout is the USB transaction timeout in milliseconds * * \returns the number of bytes received, or a negative value on * failure */ int bulkRead(int length, unsigned char *message, int timeout = 100); /** * \brief Reset endpoint * * This method resets the endpoint. */ int reset(void); /** * \brief Clear halt * * This method clears a halt (stall) on the endpoint. */ int clearHalt(void);#endif /* USE_UNTESTED_LIBUSBPP_METHODS */ /** * \brief Endpoint descriptor information output * * This method dumps out the various characteristics * of the endpoint to standard output. * * It is mostly useful for debugging. */ void dumpDescriptor(void); private: void setDescriptor(struct usb_endpoint_descriptor); void setParent(Device *parent); u_int8_t m_Length; u_int8_t m_DescriptorType; u_int8_t m_EndpointAddress; u_int8_t m_Attributes; u_int16_t m_MaxPacketSize; u_int8_t m_Interval; u_int8_t m_Refresh; u_int8_t m_SynchAddress; Device *m_parent; }; class AltSetting : public std::list<Endpoint *> { /** * Busses is a friend because it fills in the descriptor type * information on initialisation and rescan. */ friend class Busses; public: AltSetting() {}; u_int8_t numEndpoints(void); /** * \brief AltSetting descriptor information output * * This method dumps out the various characteristics * of the alternate setting to standard output. * * It is mostly useful for debugging. */ void dumpDescriptor(void); Endpoint *firstEndpoint(void); Endpoint *nextEndpoint(void); Endpoint *lastEndpoint(void); private: std::list<Endpoint *>::const_iterator iter; void setDescriptor(struct usb_interface_descriptor); /* we don't use a normal usb_interface_descriptor */ /* because that would bring in the endpoint list */ u_int8_t m_Length; u_int8_t m_DescriptorType; u_int8_t m_InterfaceNumber; u_int8_t m_AlternateSetting; u_int8_t m_NumEndpoints; u_int8_t m_InterfaceClass; u_int8_t m_InterfaceSubClass; u_int8_t m_InterfaceProtocol; u_int8_t m_Interface; }; /** * \brief Class representing an interface of a Device * * The Interface class represents a USB interface * for a device attached to a Universal Serial Bus. * * Interfaces are the main element of the USB class * structure. * * \author Brad Hards */ class Interface : public std::list<AltSetting *> { /** * Busses is a friend because it fills in the descriptor type * information on initialisation and rescan. */ friend class Busses; public: Interface() {};#ifdef LIBUSB_HAS_GET_DRIVER_NP /** * \brief get the current driver for an interface * * \param driver a string containing the name of the current * driver for the interface. You can typically pass in an empty * string for this. * * \return length of string, or 0 on error. */ int driverName(std::string &driver);#endif#ifdef USE_UNTESTED_LIBUSBPP_METHODS /** * \brief Claim this interface * * This method claims the interface. You have to claim the * interface before performing any operations on the interface (or * on endpoints that are part of the interface). * * \return 0 on success or negative number on error. */ int claim(void); /** * \brief Release this interface * * This method releases the interface. You should release the * interface after all operations on it (and any lower level * endpoints) are completed. * * \return 0 on success or negative number on error. */ int release(void); /** * \brief Set interface alternate setting * * This method sets the interface to a particular AltSetting. * * \param altSettingNumber the AltSetting that the interface * should be changed to. * * \return 0 on success, or a negative number in case of error. */ int setAltSetting(int altSettingNumber);#endif /* USE_UNTESTED_LIBUSBPP_METHODS */ /** * \brief Number of Alternative Settings that this interface has * * This is a simple accessor method that specifies the number * alternative settings that this device interface has. */ u_int8_t numAltSettings(void); /** * \brief First AltSetting for the Interface * * This method returns a pointer to the first AltSetting * for the Interface. * * See nextAltSetting() for an example of how it might be * used. * * \see nextAltSetting(), lastAltSetting(), numAltSettings() */ AltSetting *firstAltSetting(void); /** * \brief Next AltSetting for the Interface * * This method returns a pointer to the next AltSetting * for the Interface. * * If you want to iterate through each AltSetting on * a device, you can use something like the following: * \code * USB::Configuration *this_Configuration; * this_Configuration = device->firstConfiguration(); * for (i=0; i < device->numConfigurations(); i++) { * this_Configuration->dumpDescriptor(); * USB::Interface *this_Interface; * this_Interface = this_Configuration->firstInterface(); * for (j=0; j < this_Configuration->numInterfaces(); j++) { * USB::AltSetting *this_AltSetting; * this_AltSetting = this_Interface->firstAltSetting(); * for (k=0; k < this_Interface->numAltSettings(); k++) { * // do something with this_AltSetting * this_AltSetting = this_Interface->nextAltSetting(); * } * this_Interface = this_Configuration->nextInterface(); * } * this_Configuration = device->nextConfiguration(); * } * \endcode * * \see firstAltSetting(), lastAltSetting(), numAltSettings() */ AltSetting *nextAltSetting(void); /** * \brief Last AltSetting for the Interface * * This method returns a pointer to the last AltSetting * for the Interface. * * \see firstAltSetting(), nextAltSetting(), numAltSettings() */ AltSetting *lastAltSetting(void); private: std::list<AltSetting *>::const_iterator iter; void setNumAltSettings(u_int8_t); void setParent(Device *parent); u_int8_t m_numAltSettings; Device *m_parent; /* index representing the interface, in this configuration */ int m_interfaceNumber; void setInterfaceNumber(int interfaceNumber); }; /** * \brief Class representing a configuration of a Device * * The Configuration class represents a single configuration * of a device attached to a Universal Serial Bus. * * \author Brad Hards */ class Configuration : public std::list<Interface *> { /** * Busses is a friend because it fills in the descriptor type * information on initialisation and rescan. */ friend class Busses; public: Configuration() {}; /** * \brief Configuration descriptor information output * * This method dumps out the various characteristics * of the configuration to standard output. * * It is mostly useful for debugging. */ void dumpDescriptor(void); /** * \brief Number of Interfaces that this device has * * This is a simple accessor method that specifies the number * Interfaces that this device configuration has. */ u_int8_t numInterfaces(void); /** * \brief First Interface for the Configuration * * This method returns a pointer to the first Interface * for the Configuration. * * See nextInterface() for an example of how it might be * used. * * \see nextInterface(), lastInterface(), numInterfaces() */ Interface *firstInterface(void); /** * \brief Next Interface for the Configuration * * This method returns a pointer to the next Interface * for the Configuration. * * If you want to iterate through each Interface on * a device, you can use something like the following: * \code * USB::Configuration *this_Configuration; * this_Configuration = device->firstConfiguration(); * for (i=0; i < device->numConfigurations(); i++) { * this_Interface = this_Configuration->firstInterface(); * for (j=0; j < this_Configuration->numInterfaces(); j++) { * // do something with this_Interface * this_Interface = this_Configuration->nextInterface(); * } * this_Configuration->nextConfiguration(); * } * \endcode * * \see firstInterface(), lastInterface(), numInterfaces() */ Interface *nextInterface(void); /** * \brief Last Interface for the Configuration * * This method returns a pointer to the last Interface * for the Configuration. * * \see firstInterface(), nextInterface(), numInterfaces() */ Interface *lastInterface(void); private: std::list<Interface *>::const_iterator iter; void setDescriptor(struct usb_config_descriptor); /* we don't use a normal usb_config_descriptor */ /* because that would bring in the interface list */ u_int8_t m_Length; u_int8_t m_DescriptorType; u_int16_t m_TotalLength; u_int8_t m_NumInterfaces; u_int8_t m_ConfigurationValue; u_int8_t m_Configuration; u_int8_t m_Attributes; u_int8_t m_MaxPower; }; /** * \brief Class representing a Device on the Bus * * The Device class represents a single device * attached to a Universal Serial Bus. * * \author Brad Hards */ class Device : public std::list<Configuration *> { /** * Busses is a friend because it fills in the descriptor type * information on initialisation and rescan. */ friend class Busses; /** * Interface is a friend because it needs the handle() function to * perform claim(), release(). */ friend class Interface; /** * Endpoint is a friend because it needs the handle() function to * perform reads, writes, and other transactions. */ friend class Endpoint; public: Device() {}; ~Device(); /**
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -