Austin Schuh | 9b360e9 | 2013-03-27 04:47:04 +0000 | [diff] [blame] | 1 | #ifndef LIBUSB_H_ |
| 2 | #define LIBUSB_H_ |
| 3 | |
Brian Silverman | 798c778 | 2013-03-28 16:48:02 -0700 | [diff] [blame] | 4 | #include <libusb-1.0/libusb.h> |
Austin Schuh | 9b360e9 | 2013-03-27 04:47:04 +0000 | [diff] [blame] | 5 | |
Brian Silverman | 4ad1783 | 2013-03-31 01:26:08 -0700 | [diff] [blame] | 6 | #include "aos/common/macros.h" |
| 7 | |
Austin Schuh | 9b360e9 | 2013-03-27 04:47:04 +0000 | [diff] [blame] | 8 | class LibUSBDeviceHandle; |
Brian Silverman | 4ad1783 | 2013-03-31 01:26:08 -0700 | [diff] [blame] | 9 | namespace libusb { |
| 10 | class Transfer; |
Brian Silverman | c554a8f | 2013-03-31 19:07:49 -0700 | [diff] [blame^] | 11 | class IsochronousTransfer; |
Brian Silverman | 4ad1783 | 2013-03-31 01:26:08 -0700 | [diff] [blame] | 12 | } |
Austin Schuh | 9b360e9 | 2013-03-27 04:47:04 +0000 | [diff] [blame] | 13 | |
| 14 | class LibUSB { |
| 15 | public: |
| 16 | explicit LibUSB(); |
Brian Silverman | 4ad1783 | 2013-03-31 01:26:08 -0700 | [diff] [blame] | 17 | ~LibUSB(); |
| 18 | |
Austin Schuh | 9b360e9 | 2013-03-27 04:47:04 +0000 | [diff] [blame] | 19 | // Return a device handle or NULL with the correct VID and PID. |
| 20 | LibUSBDeviceHandle *FindDeviceWithVIDPID( |
| 21 | int vid, int pid); |
| 22 | |
Brian Silverman | 4ad1783 | 2013-03-31 01:26:08 -0700 | [diff] [blame] | 23 | void HandleEvents(); |
| 24 | |
Austin Schuh | 9b360e9 | 2013-03-27 04:47:04 +0000 | [diff] [blame] | 25 | private: |
| 26 | libusb_context *ctx_; |
Brian Silverman | 4ad1783 | 2013-03-31 01:26:08 -0700 | [diff] [blame] | 27 | |
| 28 | DISALLOW_COPY_AND_ASSIGN(LibUSB); |
Austin Schuh | 9b360e9 | 2013-03-27 04:47:04 +0000 | [diff] [blame] | 29 | }; |
| 30 | |
| 31 | class LibUSBDeviceHandle { |
| 32 | public: |
| 33 | virtual ~LibUSBDeviceHandle(); |
| 34 | // Transfers data using an interrupt transfer. |
| 35 | int interrupt_transfer(unsigned char endpoint, unsigned char *data, |
| 36 | int length, int *transferred, unsigned int timeout); |
| 37 | |
| 38 | // Transfers data using a bulk transfer. |
| 39 | int bulk_transfer(unsigned char endpoint, unsigned char *data, |
| 40 | int length, int *transferred, unsigned int timeout); |
| 41 | |
| 42 | private: |
| 43 | friend class LibUSB; // For constructor |
Brian Silverman | 4ad1783 | 2013-03-31 01:26:08 -0700 | [diff] [blame] | 44 | friend class libusb::Transfer; // for access to dev_handle_ |
Brian Silverman | c554a8f | 2013-03-31 19:07:49 -0700 | [diff] [blame^] | 45 | friend class libusb::IsochronousTransfer; // for access to dev_handle_ |
Austin Schuh | 9b360e9 | 2013-03-27 04:47:04 +0000 | [diff] [blame] | 46 | // Takes ownership of the device handle and frees it when destructed. |
| 47 | explicit LibUSBDeviceHandle(libusb_device_handle *dev_handle); |
Brian Silverman | 4ad1783 | 2013-03-31 01:26:08 -0700 | [diff] [blame] | 48 | libusb_device_handle *dev_handle_; |
| 49 | |
| 50 | DISALLOW_COPY_AND_ASSIGN(LibUSBDeviceHandle); |
Austin Schuh | 9b360e9 | 2013-03-27 04:47:04 +0000 | [diff] [blame] | 51 | }; |
| 52 | |
Brian Silverman | 4ad1783 | 2013-03-31 01:26:08 -0700 | [diff] [blame] | 53 | // TODO(brians): move everything in here |
| 54 | namespace libusb { |
| 55 | |
| 56 | // Wraps a libusb_transfer*. |
| 57 | // Represents an asynchronous transfer. |
| 58 | // Only designed to support interrupt and bulk transfers because they are very |
| 59 | // similar and simple. |
| 60 | class Transfer { |
| 61 | public: |
| 62 | Transfer(size_t data_length, |
| 63 | void (*callback)(Transfer *, void *), |
| 64 | void *user_data); |
| 65 | ~Transfer(); |
| 66 | |
| 67 | void FillInterrupt(LibUSBDeviceHandle *device, |
| 68 | unsigned char endpoint, |
| 69 | unsigned int timeout); |
| 70 | |
| 71 | void Submit(); |
| 72 | void Cancel(); |
| 73 | |
| 74 | libusb_transfer_status status() { return transfer_->status; } |
| 75 | int read_bytes() { return transfer_->actual_length; } |
| 76 | |
| 77 | const uint8_t *data() { return data_; } |
| 78 | |
Brian Silverman | c554a8f | 2013-03-31 19:07:49 -0700 | [diff] [blame^] | 79 | protected: |
Brian Silverman | 4ad1783 | 2013-03-31 01:26:08 -0700 | [diff] [blame] | 80 | static void StaticTransferCallback(libusb_transfer *self) { |
| 81 | static_cast<Transfer *>(self->user_data)->TransferCallback(); |
| 82 | } |
Brian Silverman | c554a8f | 2013-03-31 19:07:49 -0700 | [diff] [blame^] | 83 | |
| 84 | private: |
Brian Silverman | 4ad1783 | 2013-03-31 01:26:08 -0700 | [diff] [blame] | 85 | void TransferCallback(); |
| 86 | |
| 87 | libusb_transfer *const transfer_; |
| 88 | |
| 89 | uint8_t *const data_; |
| 90 | size_t data_length_; |
| 91 | |
| 92 | void (*const callback_)(Transfer *, void *); |
| 93 | void *const user_data_; |
| 94 | |
| 95 | DISALLOW_COPY_AND_ASSIGN(Transfer); |
| 96 | }; |
| 97 | |
Brian Silverman | c554a8f | 2013-03-31 19:07:49 -0700 | [diff] [blame^] | 98 | class IsochronousTransfer : public Transfer { |
| 99 | public: |
| 100 | IsochronousTransfer(size_t packet_length, |
| 101 | int num_packets, |
| 102 | void (*callback)(Transfer *, void *), |
| 103 | void *user_data); |
| 104 | |
| 105 | void FillIsochronous(LibUSBDeviceHandle *device, |
| 106 | unsigned char endpoint, |
| 107 | unsigned int timeout); |
| 108 | |
| 109 | private: |
| 110 | const int num_packets_; |
| 111 | }; |
| 112 | |
Brian Silverman | 4ad1783 | 2013-03-31 01:26:08 -0700 | [diff] [blame] | 113 | } // namespace libusb |
| 114 | |
Austin Schuh | 9b360e9 | 2013-03-27 04:47:04 +0000 | [diff] [blame] | 115 | #endif // LIBUSB_H_ |