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