-
Notifications
You must be signed in to change notification settings - Fork 0
/
usb.h
71 lines (61 loc) · 1.65 KB
/
usb.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#ifndef USB_H_
#define USB_H_
#include <cstdint>
#include <iostream>
#include <libusb.h>
#include <unistd.h>
// Holds a USB context. (for libusb-1.0)
class Usb {
public:
~Usb() {
if (!context_)
return;
libusb_exit(context_);
context_ = nullptr;
}
Usb() {
libusb_init(&context_);
}
libusb_context* context() { return context_; }
private:
libusb_context* context_;
};
// A generic usb device, accessed via libusb-1.0
class UsbDevice {
public:
UsbDevice(Usb* usb) : usb_(usb), device_handle_(nullptr) { }
virtual ~UsbDevice() {
if (!device_handle_)
return;
libusb_close(device_handle_);
device_handle_ = nullptr;
}
// return true if the vendor and product id match.
bool deviceMatchesVendorProduct(libusb_device *device, unsigned short idVendor, unsigned short idProduct) {
libusb_device_descriptor desc;
libusb_get_device_descriptor(device, &desc);
return idVendor == desc.idVendor && idProduct == desc.idProduct;
}
void Init(unsigned short vendorId,
unsigned short productIDArray[]) {
libusb_device **device_list=0;
int count=libusb_get_device_list(usb_->context(), &device_list);
for(int i=0;i<count;i++) {
libusb_device *device=device_list[i];
for(int Id=0;Id<4;Id++) {
if(deviceMatchesVendorProduct(device,
vendorId, productIDArray[Id])) {
libusb_open(device, &device_handle_);
goto done;
}
}
}
std::cout << "Failed to find a suitable USB device\n";
done:
libusb_free_device_list(device_list, 0);
}
protected:
Usb* usb_;
libusb_device_handle *device_handle_;
};
#endif // USB_H_