-
Notifications
You must be signed in to change notification settings - Fork 6
/
hidapi++.h
185 lines (143 loc) · 4.54 KB
/
hidapi++.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#pragma once
#include <vector>
#include <map>
#include <ostream>
#include <codecvt>
#include <hidapi.h>
std::ostream& operator<<(std::ostream& stream, const hid_device_info& info ) {
stream << "{ " << info.path << "}";
return stream;
}
class HidApi {
struct CreateProtect {};
public:
static std::shared_ptr<HidApi> create() {
static std::weak_ptr<HidApi> hidApi;
if(std::shared_ptr<HidApi> p = hidApi.lock()) {
return p;
}
std::shared_ptr<HidApi> p = std::make_shared<HidApi>(CreateProtect());
if(hid_init() != 0)
return nullptr;
hidApi = p;
return p;
}
class Enumerate {
public:
class iterator {
public:
iterator(hid_device_info* deviceInfo) : _currentDevice(deviceInfo) { }
hid_device_info operator*() {
return *_currentDevice;
}
void operator++() {
if(_currentDevice) {
_currentDevice = _currentDevice->next;
}
}
bool operator!=(const iterator& it) {
return it._currentDevice != _currentDevice;
}
bool operator==(const iterator& it) {
return it._currentDevice == _currentDevice;
}
hid_device_info* _currentDevice;
};
public:
Enumerate(std::shared_ptr<HidApi> hidApi, unsigned short vendorId = 0, unsigned short productId = 0)
: _hidApi(hidApi)
{
_start = hid_enumerate(vendorId, productId);
}
Enumerate(const Enumerate&) = delete;
~Enumerate() {
hid_free_enumeration(_start);
}
public:
iterator begin() {
return iterator(_start);
}
iterator end() {
return iterator(nullptr);
}
private:
hid_device_info* _start;
std::shared_ptr<HidApi> _hidApi;
};
class Device {
public:
using Data = std::vector<unsigned char>;
public:
Device(CreateProtect, const hid_device_info& device_info, hid_device* hidDevice)
: deviceInfo(device_info)
, device(hidDevice)
{
using convert_type = std::codecvt_utf8<wchar_t>;
std::wstring_convert<convert_type, wchar_t> converter;
if(hid_get_manufacturer_string(device, wcharBuffer, 1024) == 0) {
manufacturer = converter.to_bytes( std::wstring(wcharBuffer) );
}
if(hid_get_product_string(device, wcharBuffer, 1024) == 0) {
product = converter.to_bytes( std::wstring(wcharBuffer) );
}
if(hid_get_serial_number_string(device, wcharBuffer, 1024) == 0) {
serial = converter.to_bytes( std::wstring(wcharBuffer) );
}
}
~Device() {
hid_close(device);
}
size_t send_feature_report(std::vector<unsigned char> data) {
return (size_t)hid_send_feature_report(device, &data[0], data.size());
}
Data read(size_t numberOfBytes) {
if(numberOfBytes > 1024) {
return {};
}
int nRead = hid_read(device, charBuffer, numberOfBytes );
if(nRead > 0) {
return Data(charBuffer, charBuffer + nRead);
}
return {};
}
private:
hid_device_info deviceInfo;
hid_device* device;
wchar_t wcharBuffer[1024];
unsigned char charBuffer[1024];
std::string manufacturer;
std::string product;
std::string serial;
};
public:
HidApi(CreateProtect) {
}
public:
~HidApi() {
hid_exit();
}
std::shared_ptr<Device> openDevice(const hid_device_info& info) {
auto it = _openedDevices.find(info.path);
if(it != _openedDevices.end()) {
if(std::shared_ptr<Device> device = it->second.lock()) {
return device;
}
}
auto hidDevice = hid_open_path(info.path);
if(!hidDevice)
return nullptr;
auto device = std::make_shared<Device>(CreateProtect(), info, hidDevice);
_openedDevices[info.path] = device;
return device;
}
private:
std::map<std::string, std::weak_ptr<Device> > _openedDevices;
};
std::ostream& operator<<(std::ostream& stream, const HidApi::Device::Data& data) {
stream << "{";
for(auto c : data) {
stream << " 0x" << std::hex << (int)c;
}
stream << " }";
return stream;
}