-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUsbBulk.cpp
More file actions
386 lines (328 loc) · 9.54 KB
/
UsbBulk.cpp
File metadata and controls
386 lines (328 loc) · 9.54 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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
#include "stdafx.h"
#include "UsbBulk.h"
#define MY_VID 0x04B4
#define MY_PID 0x8613
#define MY_MIF 0x01
#define EP_IN 0x81
#define EP_OUT 0x01
#define BUF_SIZE 64
#define TEST_ASYNC
#define USB_TIMEOUT 100
CUsbBulk::CUsbBulk ( void )
{
m_Type = EnumTypeUsbBulk;
m_hUsbDevice = NULL;
}
CUsbBulk::~CUsbBulk ( void )
{
IoClose();
}
VOID CUsbBulk::ReleaseDevice(USB_HANDLE UsbHandle)
{
if (UsbHandle) {
usb_release_interface(UsbHandle, m_nInterface);
usb_close(UsbHandle);
}
}
USB_DEVICE *CUsbBulk::FindDevice(CString &DeviceName, int Interface)
{
struct usb_bus *bus;
USB_DEVICE *dev;
USB_DEVICE *retdev = NULL;
CString FileName;
int pos;
USES_CONVERSION;
for(bus = usb_get_busses(); bus; bus = bus->next) {
for(dev = bus->devices; dev; dev = dev->next) {
FileName = A2T(dev->filename);
pos = FileName.Find(_T("--"));
if (pos >=0 )
FileName = FileName.Mid(pos+2);
if (!FileName.Compare(DeviceName)) {
if (dev->config->interface->altsetting->bInterfaceNumber == Interface) {
retdev = dev;
break;
}
}
}
}
return retdev;
}
BOOL CUsbBulk::IoOpen( CWnd *pWnd, TCHAR *pCfgStr)
{
if (m_hDevice != INVALID_HANDLE_VALUE) {
return FALSE;
}
TCHAR seps[] = _T(";");
TCHAR *ptr;
TCHAR *token;
TCHAR *argstr;
int argcnt = 0;
argstr = _tcsdup(pCfgStr);
token = _tcstok(argstr, seps );
while( token != NULL )
{
if ((ptr = _tcschr(token, _T('='))) > 0) {
*ptr = 0;
if (!_tcsicmp(_T("DeviceName"), token)) {
m_EndPoint.strDeviceName.Format(_T("%s"), ptr + 1);
argcnt ++;
} else if (!_tcsicmp(_T("Interface"), token)) {
m_EndPoint.nInterface = _tstoi(ptr + 1) & 0xFF;
argcnt ++;
} else if (!_tcsicmp(_T("EpIn"), token)) {
m_EndPoint.nEpIn = _tstoi(ptr + 1) & 0xFF;
argcnt ++;
} else if (!_tcsicmp(_T("EpInSize"), token)) {
m_EndPoint.nEpInSize = _tstoi(ptr + 1);
if (m_EndPoint.nEpInSize > 1024)
m_EndPoint.nEpInSize = 1024;
argcnt ++;
} else if (!_tcsicmp(_T("EpOut"), token)) {
m_EndPoint.nEpOut = _tstoi(ptr + 1) & 0xFF;
argcnt ++;
} else if (!_tcsicmp(_T("EpOutSize"), token)) {
m_EndPoint.nEpOutSize = _tstoi(ptr + 1);
if (m_EndPoint.nEpOutSize > 1024)
m_EndPoint.nEpOutSize = 1024;
argcnt ++;
}
*ptr = _T('=');
}
token = _tcstok (NULL, seps);
}
free (argstr);
m_csName = m_EndPoint.strDeviceName;
if (argcnt != 6)
return FALSE;
if (m_EndPoint.strDeviceName.GetLength() < 5)
return FALSE;
int error = 0;
usb_find_busses(); /* find all busses */
usb_find_devices(); /* find all connected devices */
USB_DEVICE *UsbDevcie;
CString UsbVid (m_EndPoint.strDeviceName);
int pos = UsbVid.Find(_T("--"));
if (pos >=0 )
UsbVid = UsbVid.Mid(pos+2);
UsbDevcie = FindDevice(UsbVid, m_EndPoint.nInterface);
if (!UsbDevcie)
return FALSE;
USB_HANDLE UsbHandle;
UsbHandle = usb_open(UsbDevcie);
if (!UsbHandle) {
return FALSE;
}
m_nInterface =UsbDevcie->config->interface->altsetting->bInterfaceNumber;
if (usb_set_configuration(UsbHandle, 1) < 0) {
error = 6;
ReleaseDevice (UsbHandle);
return FALSE;
}
if (usb_claim_interface(UsbHandle, m_nInterface) < 0) {
error = 7;
ReleaseDevice (UsbHandle);
return FALSE;
}
int altidx;
for (altidx = 0 ; altidx < UsbDevcie->config->interface->num_altsetting; altidx++) {
if (UsbDevcie->config->interface->altsetting[altidx].bNumEndpoints) break;
}
if (usb_set_altinterface(UsbHandle, altidx)) {
error = 8;
ReleaseDevice (UsbHandle);
return FALSE;
}
m_hDevice = (HANDLE)UsbHandle;
if (m_hDevice != INVALID_HANDLE_VALUE) {
m_hEventWrite = CreateEvent(NULL, TRUE, FALSE, NULL);
m_hEventExit = CreateEvent(NULL, TRUE, FALSE, NULL);
IoReset ();
// Start a new thread to monitor the pipe
// Start a thread for data polling
THREAD_PARAM *pTheadParam = new THREAD_PARAM;
memset (pTheadParam, 0, sizeof(THREAD_PARAM));
pTheadParam->hDevice = m_hDevice;
pTheadParam->hWnd = pWnd->GetSafeHwnd();
pTheadParam->hEventQuit = m_hEventExit;
pTheadParam->hEventWrite = m_hEventWrite;
pTheadParam->pQueueRx = &m_QueueRx;
pTheadParam->pQueueTx = &m_QueueTx;
pTheadParam->pWriteReady = &m_bWriteReady;
pTheadParam->pContext = &m_EndPoint;
m_pThead = AfxBeginThread ( (AFX_THREADPROC)CallBackThread , (void *)pTheadParam);
if (m_pThead == NULL) {
delete pTheadParam;
IoClose ();
ASSERT (0);
return FALSE;
}
}
return TRUE;
}
VOID CUsbBulk::IoClose ()
{
// Wait for quit
if (m_hEventExit != INVALID_HANDLE_VALUE) {
if (m_pThead) {
DWORD dwExitCode = 0;
GetExitCodeThread(m_pThead->m_hThread, &dwExitCode);
if (dwExitCode == STILL_ACTIVE) {
SetEvent (m_hEventExit);
Sleep (100);
}
}
CloseHandle (m_hEventExit);
m_hEventExit = INVALID_HANDLE_VALUE;
}
if (m_hEventWrite != INVALID_HANDLE_VALUE) {
CloseHandle (m_hEventWrite);
m_hEventWrite = INVALID_HANDLE_VALUE;
}
if (m_hDevice != INVALID_HANDLE_VALUE) {
ReleaseDevice((USB_HANDLE)m_hDevice);
m_hDevice = INVALID_HANDLE_VALUE;
}
}
BOOL CUsbBulk::IoIsValid ()
{
return (m_hDevice != INVALID_HANDLE_VALUE);
}
int CUsbBulk::IoRead ( BYTE *Buffer , int nNumberOfBytesToRead)
{
return m_QueueRx.Get((BYTE *)Buffer, nNumberOfBytesToRead);
}
int CUsbBulk::IoWrite ( BYTE *Buffer , int nNumberOfBytesToWrite)
{
int nLen;
nLen = (int)m_QueueTx.Put((BYTE *)Buffer, nNumberOfBytesToWrite);
if (nLen != nNumberOfBytesToWrite) {
TRACE ("Pipe TX FIFO is full !\n");
}
if (m_bWriteReady)
SetEvent (m_hEventWrite);
return nLen;
}
void CUsbBulk::IoReset ()
{
m_QueueRx.Reset();
m_QueueTx.Reset();
}
/*
* Read/Write using async transfer functions.
*
* NOTE: This function waits for the transfer to complete essentially making
* it a sync transfer function so it only serves as an example of how one might
* implement async transfers into thier own code.
*/
static int transfer_bulk_async(usb_dev_handle *dev,
int ep,
char *bytes,
int size,
int timeout)
{
// Each async transfer requires it's own context. A transfer
// context can be re-used. When no longer needed they must be
// freed with usb_free_async().
//
void* async_context = NULL;
int ret;
// Setup the async transfer. This only needs to be done once
// for multiple submit/reaps. (more below)
//
ret = usb_bulk_setup_async(dev, &async_context, ep);
if (ret < 0)
{
printf("error usb_bulk_setup_async:\n%s\n", usb_strerror());
goto Done;
}
// Submit this transfer. This function returns immediately and the
// transfer is on it's way to the device.
//
ret = usb_submit_async(async_context, bytes, size);
if (ret < 0)
{
printf("error usb_submit_async:\n%s\n", usb_strerror());
usb_free_async(&async_context);
goto Done;
}
// Wait for the transfer to complete. If it doesn't complete in the
// specified time it is cancelled. see also usb_reap_async_nocancel().
//
ret = usb_reap_async(async_context, timeout);
// Free the context.
usb_free_async(&async_context);
Done:
return ret;
}
UINT CUsbBulk::CallBackThread ( LPVOID pParam )
{
THREAD_PARAM paramThread = *(THREAD_PARAM *)pParam;
delete pParam;
ENDPOINT_PARAM *EpParam = (ENDPOINT_PARAM *)paramThread.pContext;
int EpIn = EpParam->nEpIn;
int EpInSize = 64;
int EpOut = EpParam->nEpOut;
int EpOutSize = 64;
int nStage = 0;
int dwError = 0;
DWORD dwBytesInBuffer;
//evtHandles[0] = paramThread.hEventQuit;
ResetEvent (paramThread.hEventQuit);
USB_HANDLE UsbHandle = (USB_HANDLE)paramThread.hDevice;
char EpInBuf [512];
char EpOutBuf[512];
// Clear the buffer
// while (usb_bulk_read(UsbHandle, EpIn, EpInBuf, EpInSize, 50) > 0);
DWORD dwByteWrite;
int ret;
bool bLoop = TRUE;
while (bLoop) {
#ifdef TEST_ASYNC
// Running an async read test
ret = transfer_bulk_async(UsbHandle, EpIn, EpInBuf, EpInSize, USB_TIMEOUT);
#else
// Running a sync read test
ret = usb_bulk_read(UsbHandle, EpIn, EpInBuf, EpInSize, USB_TIMEOUT);
#endif
if (ret >= 0) {
// We have data in buffer
if (ret) {
dwBytesInBuffer = ret;
paramThread.pQueueRx->Put((BYTE *)EpInBuf, dwBytesInBuffer);
if (IsWindow(paramThread.hWnd)) {
PostMessage (paramThread.hWnd, WM_USER, CODE_RECV_FROM_DEVICE, THREAD_ID_USBBULK);
}
}
} else if (ret == -116) {
dwByteWrite =paramThread.pQueueTx->Get((BYTE *)EpOutBuf, EpOutSize);
if (dwByteWrite) {
#ifdef TEST_ASYNC
// Running an async write test
ret = transfer_bulk_async(UsbHandle, EpOut, EpOutBuf,dwByteWrite, USB_TIMEOUT);
#else
// Running a sync write test
ret = usb_bulk_write(UsbHandle, EpOut, EpOutBuf, dwByteWrite, USB_TIMEOUT);
#endif
if (ret == -5) {
dwError = ret;
break;
}
} else {
Sleep (100);
}
} else {
dwError = ret;
break;
}
if ( ::WaitForSingleObject(paramThread.hEventQuit, 0) == WAIT_OBJECT_0 ) {
bLoop = FALSE;
continue;
}
}
TRACE ("UsbBulk Polling Thread Exit! Error = %d Stage = %d\n", dwError, nStage);
if (IsWindow(paramThread.hWnd)) {
PostMessage (paramThread.hWnd, WM_USER, CODE_THREAD_EXIT, THREAD_ID_USBBULK);
}
return 0;
}