void send_lamp_command(char* title, char* message1, char* message2){ usb_reset(launcher); usb_resetep(launcher, 0); usb_resetep(launcher, 1); usb_clear_halt(launcher, 0); usb_clear_halt(launcher, 1); send_cbw(); fprintf(stderr, "Submitting %s\n", title); int ret = send_message(message1, 0); fprintf(stderr, "%d bytes sent\n", ret); ret = send_message(message2, 0); fprintf(stderr, "%d bytes sent\n", ret); }
DWORD TransferThreadProc(struct BENCHMARK_TRANSFER_PARAM* transferParam) { int ret, i; struct BENCHMARK_TRANSFER_HANDLE* handle; char* data; transferParam->IsRunning = TRUE; while (!transferParam->Test->IsCancelled) { data = NULL; handle = NULL; if (transferParam->Test->TransferMode == TRANSFER_MODE_SYNC) { ret = TransferSync(transferParam); if (ret >= 0) data = transferParam->Buffer; } else if (transferParam->Test->TransferMode == TRANSFER_MODE_ASYNC) { ret = TransferAsync(transferParam, &handle); if ((handle) && ret >= 0) data = handle->Data; } else { CONERR("invalid transfer mode %d\n",transferParam->Test->TransferMode); goto Done; } if (ret < 0) { // The user pressed 'Q'. if (transferParam->Test->IsUserAborted) break; // Transfer timed out if (ret == TRANSFER_TIMEDOUT) { transferParam->TotalTimeoutCount++; transferParam->RunningTimeoutCount++; CONWRN("Timeout #%d %s on Ep%02Xh..\n", transferParam->RunningTimeoutCount, TRANSFER_DISPLAY(transferParam,"reading","writing"), transferParam->Ep.bEndpointAddress); if (transferParam->RunningTimeoutCount > transferParam->Test->Retry) break; } else { // An error (other than a timeout) occured. // usb_strerror()is not thread safe and should not be used // in a multi-threaded app. It's used here because // this is a test program. // transferParam->TotalErrorCount++; transferParam->RunningErrorCount++; CONERR("failed %s! %d of %d ret=%d: %s\n", TRANSFER_DISPLAY(transferParam,"reading","writing"), transferParam->RunningErrorCount, transferParam->Test->Retry+1, ret, usb_strerror()); usb_resetep(transferParam->Test->DeviceHandle, transferParam->Ep.bEndpointAddress); if (transferParam->RunningErrorCount > transferParam->Test->Retry) break; } ret = 0; } else { if (ret < transferParam->Test->BufferSize && !transferParam->Test->IsCancelled) { if (ret > 0) { transferParam->ShortTransferCount++; CONWRN("Short transfer on Ep%02Xh expected %d got %d.\n", transferParam->Ep.bEndpointAddress, transferParam->Test->BufferSize, ret); } else { CONWRN("Zero-length transfer on Ep%02Xh expected %d.\n", transferParam->Ep.bEndpointAddress, transferParam->Test->BufferSize); transferParam->TotalErrorCount++; transferParam->RunningErrorCount++; if (transferParam->RunningErrorCount > transferParam->Test->Retry) break; usb_resetep(transferParam->Test->DeviceHandle, transferParam->Ep.bEndpointAddress); } } else { transferParam->RunningErrorCount = 0; transferParam->RunningTimeoutCount = 0; } if ((transferParam->Test->Verify) && (transferParam->Ep.bEndpointAddress & USB_ENDPOINT_DIR_MASK)) { VerifyData(transferParam, data, ret); } } EnterCriticalSection(&DisplayCriticalSection); if (!transferParam->StartTick && transferParam->Packets >= 0) { transferParam->StartTick = GetTickCount(); transferParam->LastStartTick = transferParam->StartTick; transferParam->LastTick = transferParam->StartTick; transferParam->LastTransferred = 0; transferParam->TotalTransferred = 0; transferParam->Packets = 0; } else { if (!transferParam->LastStartTick) { transferParam->LastStartTick = transferParam->LastTick; transferParam->LastTransferred = 0; } transferParam->LastTick = GetTickCount(); transferParam->LastTransferred += ret; transferParam->TotalTransferred += ret; transferParam->Packets++; } LeaveCriticalSection(&DisplayCriticalSection); } Done: for (i=0; i < transferParam->Test->BufferCount; i++) { if (transferParam->TransferHandles[i].Context) { if (transferParam->TransferHandles[i].InUse) { if ((ret = usb_cancel_async(transferParam->TransferHandles[i].Context)) < 0) if (!transferParam->Test->IsUserAborted) CONERR("failed cancelling transfer! ret=%d\n",ret); transferParam->TransferHandles[i].InUse=FALSE; } usb_free_async(&transferParam->TransferHandles[i].Context); } } transferParam->IsRunning = FALSE; return 0; }
int main(int argc, char** argv) { struct BENCHMARK_TEST_PARAM Test; struct BENCHMARK_TRANSFER_PARAM* ReadTest = NULL; struct BENCHMARK_TRANSFER_PARAM* WriteTest = NULL; int key; if (argc == 1) { ShowHelp(); return -1; } ShowCopyright(); // NOTE: This is the log level for the benchmark application. // #if defined __ERROR_H__ usb_log_set_level(255); #endif SetTestDefaults(&Test); // Load the command line arguments. if (ParseBenchmarkArgs(&Test, argc, argv) < 0) return -1; // Initialize the critical section used for locking // the volatile members of the transfer params in order // to update/modify the running statistics. // InitializeCriticalSection(&DisplayCriticalSection); // Initialize the library. usb_init(); // Find all busses. usb_find_busses(); // Find all connected devices. usb_find_devices(); if (Test.UseList) { if (GetTestDeviceFromList(&Test) < 0) goto Done; } else { // Open a benchmark device. see Bench_Open(). Test.DeviceHandle = Bench_Open(Test.Vid, Test.Pid, Test.Intf, Test.Altf, &Test.Device); } if (!Test.DeviceHandle || !Test.Device) { CONERR("device %04X:%04X not found!\n",Test.Vid, Test.Pid); goto Done; } // If "NoTestSelect" appears in the command line then don't send the control // messages for selecting the test type. // if (!Test.NoTestSelect) { if (Bench_SetTestType(Test.DeviceHandle, Test.TestType, Test.Intf) != 1) { CONERR("setting bechmark test type #%d!\n%s\n", Test.TestType, usb_strerror()); goto Done; } } CONMSG("Benchmark device %04X:%04X opened..\n",Test.Vid, Test.Pid); // If reading from the device create the read transfer param. This will also create // a thread in a suspended state. // if (Test.TestType & TestTypeRead) { ReadTest = CreateTransferParam(&Test, Test.Ep | USB_ENDPOINT_DIR_MASK); if (!ReadTest) goto Done; } // If writing to the device create the write transfer param. This will also create // a thread in a suspended state. // if (Test.TestType & TestTypeWrite) { WriteTest = CreateTransferParam(&Test, Test.Ep); if (!WriteTest) goto Done; } // Set configuration #1. if (usb_set_configuration(Test.DeviceHandle, 1) < 0) { CONERR("setting configuration #%d!\n%s\n",1,usb_strerror()); goto Done; } // Claim_interface Test.Intf (Default is #0) if (usb_claim_interface(Test.DeviceHandle, Test.Intf) < 0) { CONERR("claiming interface #%d!\n%s\n", Test.Intf, usb_strerror()); goto Done; } // Set the alternate setting (Default is #0) if (usb_set_altinterface(Test.DeviceHandle, Test.Altf) < 0) { CONERR("selecting alternate setting #%d on interface #%d!\n%s\n", Test.Altf, Test.Intf, usb_strerror()); goto Done; } else { if (Test.Altf > 0) { CONDBG("selected alternate setting #%d on interface #%d\n",Test.Altf, Test.Intf); } } if (Test.Verify) { if (ReadTest && WriteTest) { if (CreateVerifyBuffer(&Test, WriteTest->Ep.wMaxPacketSize) < 0) goto Done; } else if (ReadTest) { if (CreateVerifyBuffer(&Test, ReadTest->Ep.wMaxPacketSize) < 0) goto Done; } } ShowTestInfo(&Test); ShowTransferInfo(ReadTest); ShowTransferInfo(WriteTest); CONMSG0("\nWhile the test is running:\n"); CONMSG0("Press 'Q' to quit\n"); CONMSG0("Press 'T' for test details\n"); CONMSG0("Press 'I' for status information\n"); CONMSG0("Press 'R' to reset averages\n"); CONMSG0("\nPress 'Q' to exit, any other key to begin.."); key = _getch(); CONMSG0("\n"); if (key=='Q' || key=='q') goto Done; // Set the thread priority and start it. if (ReadTest) { SetThreadPriority(ReadTest->ThreadHandle, Test.Priority); ResumeThread(ReadTest->ThreadHandle); } // Set the thread priority and start it. if (WriteTest) { SetThreadPriority(WriteTest->ThreadHandle, Test.Priority); ResumeThread(WriteTest->ThreadHandle); } while (!Test.IsCancelled) { Sleep(Test.Refresh); if (_kbhit()) { // A key was pressed. key = _getch(); switch (key) { case 'Q': case 'q': Test.IsUserAborted = TRUE; Test.IsCancelled = TRUE; break; case 'T': case 't': ShowTestInfo(&Test); break; case 'I': case 'i': // LOCK the display critical section EnterCriticalSection(&DisplayCriticalSection); // Print benchmark test details. ShowTransferInfo(ReadTest); ShowTransferInfo(WriteTest); // UNLOCK the display critical section LeaveCriticalSection(&DisplayCriticalSection); break; case 'R': case 'r': // LOCK the display critical section EnterCriticalSection(&DisplayCriticalSection); // Reset the running status. ResetRunningStatus(ReadTest); ResetRunningStatus(WriteTest); // UNLOCK the display critical section LeaveCriticalSection(&DisplayCriticalSection); break; } // Only one key at a time. while (_kbhit()) _getch(); } // If the read test should be running and it isn't, cancel the test. if ((ReadTest) && !ReadTest->IsRunning) { Test.IsCancelled = TRUE; break; } // If the write test should be running and it isn't, cancel the test. if ((WriteTest) && !WriteTest->IsRunning) { Test.IsCancelled = TRUE; break; } // Print benchmark stats if (ReadTest) ShowRunningStatus(ReadTest); else ShowRunningStatus(WriteTest); } // Wait for the transfer threads to complete gracefully if it // can be done in 10ms. All of the code from this point to // WaitForTestTransfer() is not required. It is here only to // improve response time when the test is cancelled. // Sleep(10); // If the thread is still running, abort and reset the endpoint. if ((ReadTest) && ReadTest->IsRunning) usb_resetep(Test.DeviceHandle, ReadTest->Ep.bEndpointAddress); // If the thread is still running, abort and reset the endpoint. if ((WriteTest) && WriteTest->IsRunning) usb_resetep(Test.DeviceHandle, WriteTest->Ep.bEndpointAddress); // Small delay incase usb_resetep() was called. Sleep(10); // WaitForTestTransfer will not return until the thread // has exited. WaitForTestTransfer(ReadTest); WaitForTestTransfer(WriteTest); // Print benchmark detailed stats ShowTestInfo(&Test); if (ReadTest) ShowTransferInfo(ReadTest); if (WriteTest) ShowTransferInfo(WriteTest); Done: if (Test.DeviceHandle) { usb_close(Test.DeviceHandle); Test.DeviceHandle = NULL; } if (Test.VerifyBuffer) { free(Test.VerifyBuffer); Test.VerifyBuffer = NULL; } FreeTransferParam(&ReadTest); FreeTransferParam(&WriteTest); DeleteCriticalSection(&DisplayCriticalSection); CONMSG0("Press any key to exit.."); _getch(); CONMSG0("\n"); return 0; }
int Endpoint::reset(void) { return usb_resetep(m_parent->handle(), m_EndpointAddress); }