blob: 9713534414a635fe5caa3492c23b98abc33a6a7c [file] [log] [blame]
Austin Schuh9b360e92013-03-27 04:47:04 +00001#ifndef LIBUSB_H_
2#define LIBUSB_H_
3
Brian Silverman798c7782013-03-28 16:48:02 -07004#include <libusb-1.0/libusb.h>
Austin Schuh9b360e92013-03-27 04:47:04 +00005
Brian Silverman4ad17832013-03-31 01:26:08 -07006#include "aos/common/macros.h"
7
Austin Schuh9b360e92013-03-27 04:47:04 +00008class LibUSBDeviceHandle;
Brian Silverman4ad17832013-03-31 01:26:08 -07009namespace libusb {
10class Transfer;
11}
Austin Schuh9b360e92013-03-27 04:47:04 +000012
13class LibUSB {
14 public:
15 explicit LibUSB();
Brian Silverman4ad17832013-03-31 01:26:08 -070016 ~LibUSB();
17
Austin Schuh9b360e92013-03-27 04:47:04 +000018 // Return a device handle or NULL with the correct VID and PID.
19 LibUSBDeviceHandle *FindDeviceWithVIDPID(
20 int vid, int pid);
21
Brian Silverman4ad17832013-03-31 01:26:08 -070022 void HandleEvents();
23
Austin Schuh9b360e92013-03-27 04:47:04 +000024 private:
25 libusb_context *ctx_;
Brian Silverman4ad17832013-03-31 01:26:08 -070026
27 DISALLOW_COPY_AND_ASSIGN(LibUSB);
Austin Schuh9b360e92013-03-27 04:47:04 +000028};
29
30class 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 Silverman4ad17832013-03-31 01:26:08 -070043 friend class libusb::Transfer; // for access to dev_handle_
Austin Schuh9b360e92013-03-27 04:47:04 +000044 // Takes ownership of the device handle and frees it when destructed.
45 explicit LibUSBDeviceHandle(libusb_device_handle *dev_handle);
Brian Silverman4ad17832013-03-31 01:26:08 -070046 libusb_device_handle *dev_handle_;
47
48 DISALLOW_COPY_AND_ASSIGN(LibUSBDeviceHandle);
Austin Schuh9b360e92013-03-27 04:47:04 +000049};
50
Brian Silverman4ad17832013-03-31 01:26:08 -070051// TODO(brians): move everything in here
52namespace 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.
58class 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 Schuh9b360e92013-03-27 04:47:04 +000096#endif // LIBUSB_H_