-
Notifications
You must be signed in to change notification settings - Fork 2
/
usb_device.hpp
75 lines (64 loc) · 2.19 KB
/
usb_device.hpp
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
72
73
74
75
// Nonolith Connect
// https://github.com/nonolith/connect
// USB device mixin
// Released under the terms of the GNU GPLv3+
// (C) 2012 Nonolith Labs, LLC
// Authors:
// Kevin Mehall <km@kevinmehall.net>
#pragma once
#include <iostream>
#include <libusb/libusb.h>
class USB_device{
public:
int controlTransfer(uint8_t bmRequestType,
uint8_t bRequest,
uint16_t wValue,
uint16_t wIndex,
uint8_t* data,
uint16_t wLength,
unsigned timeout=25){
return libusb_control_transfer(handle,
bmRequestType, bRequest, wValue, wIndex, data, wLength, timeout);
}
protected:
USB_device(libusb_device *dev, libusb_device_descriptor &desc){
int r = libusb_open(dev, &handle);
if (r != 0){
std::cerr << "Could not open device; error "<< r <<std::endl;
throw ErrorStringException("Could not open device");
}
r = libusb_get_string_descriptor_ascii(handle, desc.iSerialNumber, (unsigned char*)&serial, 32);
if (r < 0){
std::cerr << "Could not read string descriptor; error " << r << std::endl;
throw ErrorStringException("Could not read serial number");
}
}
void claimInterface(){
int r = libusb_claim_interface(handle, 0);
if (r != 0){
std::cerr << "Could not claim interface; error "<<r<<std::endl;
throw ErrorStringException("Could not claim interface");
}
}
void releaseInterface(){
if (!handle) return;
int r = libusb_release_interface(handle, 0);
if (r != 0 && r != -4){ // Ignore "device disconnected" errors
std::cerr << "Could not release interface; error "<<r<<std::endl;
throw ErrorStringException("Could not release interface");
}
}
virtual ~USB_device(){
if (handle) libusb_close(handle);
}
libusb_device_handle *handle;
char serial[32];
virtual bool processMessage(ClientConn& session, string& cmd, JSONNode& n);
};
#ifdef __MINGW32__
// Function is not defined in mingw
static inline size_t strnlen (const char *string, size_t maxlen){
const char *end = (const char*) memchr(string, '\0', maxlen);
return end ? (size_t) (end - string) : maxlen;
}
#endif