-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhidapiw_native.cpp
136 lines (121 loc) · 2.72 KB
/
hidapiw_native.cpp
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
#define NATIVE_CODE
#include "hidapiw_native.h"
hidapiw_native::hidapiw_native()
{
if (hid_init())
{
throw "failed to init hidapi";
}
}
hidapiw_native::~hidapiw_native()
{
hid_exit();
}
void hidapiw_native::enumerate(hid_device_info*& devs, unsigned short vendorID, unsigned short productID)
{
devs = hid_enumerate(vendorID, productID);
}
void hidapiw_native::freeEnumerate(hid_device_info*& devs)
{
hid_free_enumeration(devs);
}
void hidapiw_native::open(int& devIdx, unsigned short vendorID, unsigned short productID, const wchar_t* serialNumber)
{
auto _dev = hid_open(vendorID, productID, serialNumber);
if (_dev == nullptr)
{
throw hid_error(_dev);
}
addDevice(_dev, devIdx);
}
void hidapiw_native::openByPath(int& devIdx, const char* path)
{
auto _dev = hid_open_path(path);
if (_dev == nullptr)
{
throw hid_error(_dev);
}
addDevice(_dev, devIdx);
}
void hidapiw_native::close(int devIdx)
{
hid_device* _dev;
if (findDeviceInMap(devIdx, _dev))
{
hid_close(_dev);
devMap.erase(devIdx);
}
}
int hidapiw_native::findDeviceInMap(int devIdx, hid_device*& _dev)
{
//std::lock_guard<std::recursive_mutex> guard(devMap_mutex);
auto itr = devMap.find(devIdx);
if (itr != devMap.end())
{
_dev = itr->second;
return 1;
}
return 0;
}
int hidapiw_native::write(int devIdx, const unsigned char* data, size_t length)
{
hid_device* _dev;
if (findDeviceInMap(devIdx, _dev))
{
return hid_write(_dev, data, length);
}
return 1;
}
int hidapiw_native::read_timeout(int devIdx, unsigned char*& data, size_t length, int milliseconds)
{
hid_device* _dev;
if (findDeviceInMap(devIdx, _dev))
{
int ret = hid_read_timeout(_dev, data, length, milliseconds);
int ver = data[1];
return ret;
}
return 1;
}
int hidapiw_native::read(int devIdx, unsigned char*& data, size_t length)
{
hid_device* _dev;
if (findDeviceInMap(devIdx, _dev))
{
return hid_read(_dev, data, length);
}
return 1;
}
int hidapiw_native::set_nonblocking(int devIdx, int nonblock)
{
hid_device* _dev;
if (findDeviceInMap(devIdx, _dev))
{
return hid_set_nonblocking(_dev, nonblock);
}
return 1;
}
int hidapiw_native::send_feature_report(int devIdx, const unsigned char* data, size_t length)
{
hid_device* _dev;
if (findDeviceInMap(devIdx, _dev))
{
return hid_send_feature_report(_dev, data, length);
}
return 1;
}
int hidapiw_native::get_feature_report(int devIdx, unsigned char*& data, size_t length)
{
hid_device* _dev;
if (findDeviceInMap(devIdx, _dev))
{
return hid_get_feature_report(_dev, data, length);
}
return 1;
}
void hidapiw_native::addDevice(hid_device* _dev, int& devIdx)
{
/*std::lock_guard<std::recursive_mutex> guard(devMap_mutex);*/
devIdx = (int)devMap.size();
devMap.insert(std::pair<int, hid_device*>(devIdx, _dev));
}