-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGate.cpp
More file actions
251 lines (220 loc) · 5.26 KB
/
Copy pathGate.cpp
File metadata and controls
251 lines (220 loc) · 5.26 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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
// Gate.cpp
// JMIRY
#include "Gate.h"
#include <iostream>
#include <chrono>
// define static member variable
bool Gate::m_isEnd = false;
Gate::Gate()
{
// set event handler
if ( !SetConsoleCtrlHandler(eventHandler, TRUE) )
{
std::cout << "[WARN] EventHandler registration failed" << std::endl;
}
}
Gate::~Gate()
{
// cleanup libusb
if (m_usbDevHandle != NULL)
libusb_close(m_usbDevHandle);
if (m_usbContext != NULL)
libusb_exit(m_usbContext);
// cleanup winsock
WSACleanup();
}
int
Gate::init()
{
// init winsock2
WSADATA wsaData;
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != NO_ERROR)
{
std::cout << "[ERR] WSAStartup() error" << std::endl;
return -1;
}
std::cout << "[DEBUG] WinSock2 initiated" << std::endl;
// init libusb context
int status = 0;
status = libusb_init(&m_usbContext);
if (status != 0)
{
std::cout << "[ERR] libusb_init() failed: "
<< libusb_strerror(libusb_error(status)) << std::endl;
return -1;
}
std::cout << "[DEBUG] libusb initiated" << std::endl;
// get device list & find target
libusb_device** devList;
int cnt = 0;
cnt = libusb_get_device_list(m_usbContext, &devList);
for (int i = 0; i < cnt; ++i)
{
if (isTarget(devList[i]))
{
// grab device handle of target
status = libusb_open(devList[i], &m_usbDevHandle);
if (status != 0)
{
std::cout << "[ERR] libusb_open() failed: "
<< libusb_strerror(libusb_error(status));
return -1;
}
else
{
std::cout << "[DEBUG] Device handle opened" << std::endl;
break;
}
}
}
// free usb device list
libusb_free_device_list(devList, 1);
// claim data transfer interface
// interface number is HARDCODED
status = libusb_claim_interface(m_usbDevHandle, 5);
if (status != 0)
{
std::cout << "[ERR] libusb_claim_interface() failed: "
<< libusb_strerror(libusb_error(status)) << std::endl;
return -1;
}
std::cout << "[DEBUG] Interface claimed" << std::endl;
return 0;
}
bool
Gate::isTarget(libusb_device* dev)
{
libusb_device_descriptor devDesc;
libusb_get_device_descriptor(dev, &devDesc);
// product / vendor ID is HARDCODED
if (devDesc.idVendor == 0xea0 && devDesc.idProduct == 0x7301)
return true;
else
return false;
}
int
Gate::createSocket()
{
m_sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (m_sock == INVALID_SOCKET)
{
std::cout << "[ERR] socket() failed" << std::endl;
return -1;
}
std::cout << "[DEBUG] Socket created" << std::endl;
// set timeout
int status = 0;
int timeout = 1000;
status = setsockopt(
m_sock,
SOL_SOCKET,
SO_RCVTIMEO,
(const char*)&timeout,
sizeof(timeout)
);
if (status == SOCKET_ERROR)
{
std::cout << "[WARN] setsockopt() failed" << std::endl;
}
return 0;
}
int
Gate::sendSocket(char* buf, int len)
{
int result = 0;
result = send(m_sock, buf, len, 0);
if (result == SOCKET_ERROR)
{
std::cout << "[ERR] send() failed" << std::endl;
}
else
{
std::cout << "[DEBUG] Socket sent "
<< result << "bytes" << std::endl;
}
return result;
}
int
Gate::recvSocket(char* buf, int len)
{
int result = 0;
result = recv(m_sock, buf, len, 0);
if (result == SOCKET_ERROR)
{
if (WSAGetLastError() == WSAETIMEDOUT)
std::cout << "[DEBUG] Socket timed out" << std::endl;
else
std::cout << "[ERR] recv() failed" << std::endl;
}
else
{
std::cout << "[DEBUG] Socket received "
<< result << "bytes" << std::endl;
}
return result;
}
void
Gate::fillAddrInfo()
{
memset(&m_addrInfo, 0, sizeof(struct sockaddr_in));
m_addrInfo.sin_family = AF_INET;
m_addrInfo.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");
m_addrInfo.sin_port = htons(PORT);
}
void
Gate::pollUSB()
{
int result = 0;
char buffer[BUF_LEN] = { 0 };
while(!m_isEnd)
{
result = receiveTransfer(
(unsigned char*)buffer,
BUF_LEN
);
if (result > 0)
{
sendSocket(buffer, result);
memset(buffer, 0, BUF_LEN);
}
}
}
void
Gate::pollSocket()
{
int result = 0;
char buffer[BUF_LEN] = { 0 };
while (!m_isEnd)
{
result = recvSocket(buffer, BUF_LEN);
if (result > 0)
{
sendTransfer((unsigned char*)buffer, result);
memset(buffer, 0, result);
}
else if (result == 0)
{
std::cout << "[DEBUG] Disconnected gracefully" << std::endl;
m_isEnd = true;
}
}
}
void
Gate::flush()
{
unsigned char buf[BUF_LEN];
while (receiveTransfer(buf, BUF_LEN));
}
BOOL WINAPI
Gate::eventHandler(DWORD event)
{
switch (event)
{
case CTRL_C_EVENT:
std::cout << "[DEBUG] Keyboard Interrupt detected" << std::endl;
m_isEnd = true;
return TRUE;
default:
return FALSE;
}
}