BOOL UsbDevice::ClaimInterface(DWORD dwInterfaceValue, LPVOID Context) { WriteLocker lock(mCloseMutex); if (Closed()) { // Don't allow closed devices to be claimed SetLastError(ERROR_INVALID_HANDLE); return FALSE; } for (DWORD i = 0; i < mInterfaceClaimersCount; ++i) { if (mInterfaceClaimers[i].InterfaceValue() == dwInterfaceValue) { BOOL IsFirst = FALSE; if (!mInterfaceClaimers[i].Claim(Context, IsFirst)) { return FALSE; } // If this was the first claim then need to open pipes if (IsFirst) { if (!OpenPipes(mInterfaceClaimers[i])) { ClosePipes(mInterfaceClaimers[i]); SetLastError(ERROR_INTERNAL_ERROR); return FALSE; } } return TRUE; } } SetLastError(ERROR_INVALID_PARAMETER); return FALSE; }
int main(int argc, char* argv[]) { PrintHello(); if (ProcessCmdLine(argc, argv) != 0) return -1; OpenUsbDevice(); if (dwRC == USBIO_ERR_SUCCESS) { GetDeviceDescriptor(); } if (dwRC == USBIO_ERR_SUCCESS) { GetConfigurationDescriptor(); } if (dwRC == USBIO_ERR_SUCCESS) { GetStringDescriptor(); } if (dwRC == USBIO_ERR_SUCCESS) { SetConfiguration(); } if (dwRC == USBIO_ERR_SUCCESS) { // In order to give the USB device-side program (t_usb) // enough time after getting configured to carry out // some device tests, we wait here for a short while // before proceeding: Delay(2000); GetConfigurationInfo(); } if (dwRC == USBIO_ERR_SUCCESS) { OpenPipes(); } if (dwRC == USBIO_ERR_SUCCESS) { ExchangeVersions(); } if (dwRC == USBIO_ERR_SUCCESS) { DoTransfers(); } if (dwRC == USBIO_ERR_SUCCESS) { ClosePipes(); } CloseUsbDevice(); return 0; }
/// Runs as a thread and handles each packet. It is responsible /// for reading each packet in its entirety from the input pipe, /// filtering it, and then writing it to the output pipe. The /// single void* parameter matches what is expected by pthread. /// @param args An IpPktFilter /// @return Always NULL static void* FilterThread(void* args) { // TODO: implement function IpPktFilter filter = (IpPktFilter) args; unsigned char buffer[MAX_PACKET_LENGTH]; int pktLength; int charsRead; bool isFinished = true; bool isSuccess; OpenPipes(); while(Mode){ if (feof(InPipe)==0){ if (isFinished ){ isSuccess = fread(&pktLength, sizeof(int), 1, InPipe); if (isSuccess){}; } isFinished = ReadPacket(buffer, &charsRead, &pktLength); bool isBlocked = false; if (Mode == MODE_BLOCK_ALL){ isBlocked = true; } else if (Mode == MODE_ALLOW_ALL){ isBlocked = false; } else if (Mode == MODE_FILTER){ isBlocked = ! FilterPacket(filter, buffer); } else if (Mode == 0){ break; } if (!isBlocked){ fwrite(&pktLength, sizeof(int), 1, OutPipe); fwrite(buffer, sizeof(unsigned char), pktLength, OutPipe); fflush(OutPipe); } pktLength -= charsRead; } } fclose(InPipe); fclose(OutPipe); return NULL; }