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