-
Notifications
You must be signed in to change notification settings - Fork 2
/
usb_ll.h
91 lines (74 loc) · 2.86 KB
/
usb_ll.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/*****************************************************************************/
/* usb_ll.h : USB Low-Level communication class */
/*****************************************************************************/
/*
Copyright (C) 2019 Hermann Seib
This program is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation, version 3.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with
this program. If not, see <https://www.gnu.org/licenses/>.
*/
#ifndef _usb_ll_h__defined_
#define _usb_ll_h__defined_
#include <vector>
#include <string>
/*****************************************************************************/
/* UsbLLDevDesc : our internal device descriptor containing what we need */
/*****************************************************************************/
struct UsbLLDevDesc
{
unsigned short VendorID;
unsigned short ProductID;
unsigned short VersionNumber;
unsigned short UsageID;
unsigned short UsagePage;
std::string VendorName;
std::string ProductName;
std::string SerialNumber;
// purely informational items:
unsigned char Bus;
unsigned char DeviceAddress;
UsbLLDevDesc()
{
VendorID = ProductID = (unsigned short )-1;
UsageID = UsagePage = (unsigned short )-1;
VersionNumber = 0;
Bus = DeviceAddress = 0;
}
};
/*****************************************************************************/
/* UsbLL : basic low-level USB communication class */
/*****************************************************************************/
class UsbLL
{
public:
UsbLL() { ctx = NULL; Initialize(); }
virtual ~UsbLL() { Terminate(); }
bool Initialize();
void Terminate();
bool IsInitialized() { return !!ctx; }
int GetDeviceList(std::vector<void *>& devList);
void FreeDeviceList(std::vector<void *>& devList);
int GetDeviceDescriptor(void *dev, UsbLLDevDesc &desc);
int Open(void *dev, void *&handle);
void Close(void *handle);
int Send(void *dev_handle, void *buf, int len, int timeout = 1000);
int Receive(void *dev_handle, void *buf, int len, int timeout = 1000);
int ControlTransfer(void *dev_handle,
unsigned char request_type,
unsigned char bRequest,
unsigned short wValue,
unsigned short wIndex,
void *data,
unsigned short wLength,
int timeout = 1000);
std::string ErrorName(int errcode);
protected:
void *ctx;
};
#endif // defined(_usb_ll_h__defined_)