-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathyk_usb_device.cpp
More file actions
203 lines (160 loc) · 4.48 KB
/
yk_usb_device.cpp
File metadata and controls
203 lines (160 loc) · 4.48 KB
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
186
187
188
189
190
191
192
193
194
/*******************************************************************************
Copyright 2019 Yepkit Lda (www.yepkit.com)
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*******************************************************************************/
#include "yk_usb_device.h"
#ifdef _LIBUSB_
#include <usbhid.h>
#else
#include <hidapi.h>
#include <string.h>
#endif
#include <iostream>
#include <string>
#include <stdlib.h>
#ifdef _LIBUSB_
// Uses libusb directly
int UsbDevice::listConnected()
{
UsbHid *usbhid = new UsbHid();
struct hid_device_info *devs, *cur_dev;
int i = 1;
devs = usbhid->enumerate(vid, pid);
if (devs == NULL)
return 0;
cur_dev = devs;
while (cur_dev) {
std::cout << i << ". Board found with serial number: " << cur_dev->serial_number_ascii << "\n";
cur_dev = cur_dev->next;
i++;
}
usbhid->free_enumeration(devs);
return i;
}
#else
// Uses hidapi
int UsbDevice::listConnected()
{
int i=0;
struct hid_device_info *devs, *cur_dev;
devs = hid_enumerate(vid, pid);
if (devs == NULL)
return 0;
cur_dev = devs;
while (cur_dev) {
i++;
printf("%d. Board found with serial number: %ls\n", i, cur_dev->serial_number);
cur_dev = cur_dev->next;
}
hid_free_enumeration(devs);
return i;
}
#endif
UsbDevice::UsbDevice(unsigned int vendor_id, unsigned int product_id) {
pid = product_id;
vid = vendor_id;
usb_serial = NULL;
}
/**
* Sends HID report with the data provided in the input buffer "msg".
*
* \param [in] serial Pointer to a char array containing the serial number.
* \param [in] msg Pointer to buffer containing the HID report message to be sent to USB device.
* \param [out] resp_msg Pointer to buffer to which the HID report message replied by the device.
*
* \retval 0 No error.
* \retval -1 Unable to open USB device.
* \retval -2 Unable to write HID report to USB device.
* \retval -3 Unable to read HID report from USB device.
*
*
* In the case of error, a message is printed into the standard
* output.
*
*
* Precedences:
*
* Requires that VENDOR_ID and PRODUCT_ID constants are defined.
*
*
*****************************************************************/
#ifdef _LIBUSB_
int UsbDevice::sendHidReport(char *serial, unsigned char *msg, unsigned char *resp_msg, int report_size)
{
UsbHid *usbhid = new UsbHid();
int res;
res = usbhid->open(vid, pid, serial);
if (res < 0) {
std::cout << "Unable to open device\n";
return -1;
}
res = usbhid->write(msg, report_size);
if (res < 0) {
std::cout << "Unable to write to device\n";
return -2;
}
res = usbhid->read(resp_msg, report_size);
if (res < 0) {
std::cout << "Unable to read from device\n";
return -3;
}
usbhid->close();
return 0;
}
#else
int UsbDevice::sendHidReport(char *serial, unsigned char *msg, unsigned char *resp_msg, int report_size)
{
const size_t newsize = 100;
wchar_t cserial[newsize];
int res, i;
unsigned char out_buf[65];
unsigned char in_buf[65];
if (report_size <= 64) {
out_buf[0] = 0;
for (i = 0; i < report_size; i++) {
out_buf[i+1] = msg[i];
}
} else {
std::cout << "Invalid report size\n";
return -1;
}
if (serial) {
// Convert to a wchar_t*
size_t origsize = strlen(serial) + 1;
size_t convertedChars = 0;
mbstowcs_s(&convertedChars, cserial, origsize, serial, _TRUNCATE);
}
// Open the USB device
handle = hid_open(vid, pid, serial ? cserial : NULL);
if (handle == NULL) {
//printf("\n\nERROR: Unable to open USB device\n");
return -1;
}
// Set the hid_read() function to be blocking (wait for response from the device).
hid_set_nonblocking(handle, USB_CMD_NON_BLOCKING);
//send HID report
res = hid_write(handle, out_buf, report_size++);
if (res < 0) {
std::cout << "Unable to write HID report to USB device\n";
return -1;
}
//read HID report
res = hid_read(handle, in_buf, report_size++);
if (res < 0) {
std::cout << "Unable to read HID report from USB device\n";
return -1;
}
for (i = 0; i < report_size; i++) {
resp_msg[i] = in_buf[i];
}
return 0;
}
#endif