-
Notifications
You must be signed in to change notification settings - Fork 27
/
HID.cpp
176 lines (158 loc) · 6.43 KB
/
HID.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
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
/***********************************************************************************************************************
* *
* ANTIKERNEL *
* *
* Copyright (c) 2012-2024 Andrew D. Zonenberg *
* All rights reserved. *
* *
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the *
* following conditions are met: *
* *
* * Redistributions of source code must retain the above copyright notice, this list of conditions, and the *
* following disclaimer. *
* *
* * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the *
* following disclaimer in the documentation and/or other materials provided with the distribution. *
* *
* * Neither the name of the author nor the names of any contributors may be used to endorse or promote products *
* derived from this software without specific prior written permission. *
* *
* THIS SOFTWARE IS PROVIDED BY THE AUTHORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL *
* THE AUTHORS BE HELD LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES *
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR *
* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT *
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE *
* POSSIBILITY OF SUCH DAMAGE. *
* *
***********************************************************************************************************************/
/**
@file
@author Frederic BORRY
@brief Implementation of HID transport layer
*/
//#include "jtaghal.h"
#include "HID.h"
#include <stdio.h>
#include <memory.h>
#include <cstring>
using namespace std;
/**
@brief Constructor
*/
HID::HID() : m_handle(NULL)
{
}
/**
@brief Destructor
*/
HID::~HID()
{
Close();
}
/** @brief Open a HID device using a Vendor ID (VID), Product ID
(PID) and optionally a serial number.
If @p serial_number is NULL, the first device with the
specified VID and PID is opened.
@param vendorId The Vendor ID (VID) of the device to open.
@param productId The Product ID (PID) of the device to open.
@param serialNumber The Serial Number of the device to open
(Optionally NULL).
@returns
This function returns true if the device could be connected, false otherwise.
*/
bool HID::Connect(unsigned short vendorId, unsigned short productId, const char* serialNumber)
{
wchar_t serialWc[128];
if(serialNumber)
{
const size_t cSize = strlen(serialNumber)+1;
if(cSize>=128)
{
LogError("Invalid serial number '%s' (too long)\n", serialNumber);
return false;
}
mbstowcs (serialWc, serialNumber, cSize);
}
// Initialize the hidapi library
int res = hid_init();
if(res < 0)
{
LogError("HID init failed with error %d\n",res);
return false;
}
m_handle = hid_open(vendorId, productId, serialNumber ? serialWc : NULL);
if (!m_handle) {
LogError("Could not open HID device %x:%x:%s\n",vendorId,productId,serialNumber);
hid_exit();
return false;
}
#define MAX_STR 255
wchar_t wstr[MAX_STR];
// Read the Manufacturer String
wstr[0] = 0x0000;
std::wstring ws;
res = hid_get_manufacturer_string(m_handle, wstr, MAX_STR);
if (res < 0)
LogError("Unable to read manufacturer string\n");
else
{
LogDebug("Manufacturer String: %ls\n", wstr);
ws = std::wstring(wstr);
m_manufacturerName = std::string(ws.begin(), ws.end());
}
// Read the Product String
wstr[0] = 0x0000;
res = hid_get_product_string(m_handle, wstr, MAX_STR);
if (res < 0)
LogError("Unable to read product string\n");
else
{
LogDebug("Product String: %ls\n", wstr);
ws = std::wstring(wstr);
m_productName = std::string(ws.begin(), ws.end());
}
// Read the Serial Number String
wstr[0] = 0x0000;
res = hid_get_serial_number_string(m_handle, wstr, MAX_STR);
if (res < 0)
LogError("Unable to read serial number string\n");
else
{
LogDebug("Serial Number String: (%d) %ls\n", wstr[0], wstr);
ws = std::wstring(wstr);
m_serialNumber = std::string(ws.begin(), ws.end());
}
return true;
}
/**
@brief Disconnects from the serial port
*/
void HID::Close()
{
hid_close(m_handle);
/* Free static HIDAPI objects. */
hid_exit();
m_handle = NULL;
}
int HID::Read(unsigned char* data, int len)
{
// HID report has to be read all at once
int x = hid_read(m_handle, data, len);
if(x < 0)
{
LogError("HID read failed with error %d : %ls\n",x,hid_error(m_handle));
}
return x;
}
int HID::Write(const unsigned char* data, int len)
{
int x = 0;
// Send report all at once
x = hid_write(m_handle, data, len);
if(x < 0)
{
LogError("HID write failed with error %d : %ls\n",x,hid_error(m_handle));
}
return x;
}