int main() { // Setup the variables we will need. int r = 0; // For checking return values HANDLE devHandle = 0; BYTE sendBuffer[CONFIGU6_COMMAND_LENGTH], recBuffer[CONFIGU6_RESPONSE_LENGTH]; // Open the U6 devHandle = LJUSB_OpenDevice(1, 0, U6_PRODUCT_ID); if( devHandle == NULL ) { printf("Couldn't open U6. Please connect one and try again.\n"); exit(-1); } // Builds the ConfigU6 command buildConfigU6Bytes(sendBuffer); // Write the command to the device. // LJUSB_Write( handle, sendBuffer, length of sendBuffer ) r = LJUSB_Write( devHandle, sendBuffer, CONFIGU6_COMMAND_LENGTH ); if( r != CONFIGU6_COMMAND_LENGTH ) { printf("An error occurred when trying to write the buffer. The error was: %d\n", errno); // *Always* close the device when you error out. LJUSB_CloseDevice(devHandle); exit(-1); } // Read the result from the device. // LJUSB_Read( handle, recBuffer, number of bytes to read) r = LJUSB_Read( devHandle, recBuffer, CONFIGU6_RESPONSE_LENGTH ); if( r != CONFIGU6_RESPONSE_LENGTH ) { printf("An error occurred when trying to read from the U6. The error was: %d\n", errno); LJUSB_CloseDevice(devHandle); exit(-1); } // Check the command for errors if( checkResponseForErrors(recBuffer) != 0 ){ LJUSB_CloseDevice(devHandle); exit(-1); } // Parse the response into something useful parseConfigU6Bytes(recBuffer); //Close the device. LJUSB_CloseDevice(devHandle); return 0; }
void closeUSBConnection(HANDLE hDevice) { LJUSB_CloseDevice(hDevice); }
HANDLE openUSBConnection(int localID) { BYTE sendBuffer[26], recBuffer[38]; uint16 checksumTotal = 0; HANDLE hDevice = 0; uint32 numDevices = 0; uint32 dev; int i; numDevices = LJUSB_GetDevCount(U6_PRODUCT_ID); if(numDevices == 0) { printf("Open error: No U6 devices could be found\n"); return NULL; } for(dev = 1; dev <= numDevices; dev++) { hDevice = LJUSB_OpenDevice(dev, 0, U6_PRODUCT_ID); if(hDevice != NULL) { if(localID < 0) { return hDevice; } else { checksumTotal = 0; //setting up a U6Config sendBuffer[1] = (uint8)(0xF8); sendBuffer[2] = (uint8)(0x0A); sendBuffer[3] = (uint8)(0x08); for(i = 6; i < 26; i++) sendBuffer[i] = (uint8)(0x00); extendedChecksum(sendBuffer, 26); if(LJUSB_BulkWrite(hDevice, U6_PIPE_EP1_OUT, sendBuffer, 26) != 26) goto locid_error; if(LJUSB_BulkRead(hDevice, U6_PIPE_EP2_IN, recBuffer, 38) != 38) goto locid_error; checksumTotal = extendedChecksum16(recBuffer, 38); if( (uint8)((checksumTotal / 256) & 0xff) != recBuffer[5]) goto locid_error; if( (uint8)(checksumTotal & 0xff) != recBuffer[4]) goto locid_error; if( extendedChecksum8(recBuffer) != recBuffer[0]) goto locid_error; if( recBuffer[1] != (uint8)(0xF8) || recBuffer[2] != (uint8)(0x10) || recBuffer[3] != (uint8)(0x08) ) goto locid_error; if( recBuffer[6] != 0) goto locid_error; if( (int)recBuffer[21] == localID) return hDevice; else LJUSB_CloseDevice(hDevice); } //else localID >= 0 end } //if hDevice != NULL end } //for end printf("Open error: could not find a U6 with a local ID of %d\n", localID); return NULL; locid_error: printf("Open error: problem when checking local ID\n"); return NULL; }
HANDLE LJUSB_OpenDevice(UINT DevNum, unsigned int dwReserved, unsigned long ProductID) { (void)dwReserved; libusb_device **devs = NULL, *dev = NULL; struct libusb_device_handle *devh = NULL; struct libusb_device_descriptor desc; ssize_t cnt = 0; int r = 1; unsigned int i = 0; unsigned int ljFoundCount = 0; HANDLE handle = NULL; if (!gIsLibUSBInitialized) { r = libusb_init(&gLJContext); if (r < 0) { fprintf(stderr, "failed to initialize libusb\n"); LJUSB_libusbError(r); return NULL; } gIsLibUSBInitialized = true; } cnt = libusb_get_device_list(gLJContext, &devs); if (cnt < 0) { fprintf(stderr, "failed to get device list\n"); LJUSB_libusbError(cnt); LJUSB_libusb_exit(); return NULL; } while ((dev = devs[i++]) != NULL) { #if LJ_DEBUG fprintf(stderr, "LJUSB_OpenDevice: calling libusb_get_device_descriptor\n"); #endif r = libusb_get_device_descriptor(dev, &desc); if (r < 0) { fprintf(stderr, "failed to get device descriptor"); LJUSB_libusbError(r); LJUSB_libusb_exit(); return NULL; } if (LJ_VENDOR_ID == desc.idVendor && ProductID == desc.idProduct) { ljFoundCount++; if (ljFoundCount == DevNum) { // Found the one requested r = libusb_open(dev, &devh); if (r < 0) { LJUSB_libusbError(r); return NULL; } // Test if the kernel driver has the device. // Should only be true for HIDs. if (libusb_kernel_driver_active(devh, 0)) { #if LJ_DEBUG fprintf(stderr, "Kernel Driver was active, detaching...\n"); #endif // Detaches U12s from kernel driver. r = libusb_detach_kernel_driver(devh, 0); // Check the return value if ( r != 0 ) { fprintf(stderr, "failed to detach from kernel driver. Error Number: %i", r); return NULL; } } r = libusb_claim_interface(devh, 0); if (r < 0) { LJUSB_libusbError(r); libusb_close(devh); return NULL; } handle = (HANDLE) devh; #if LJ_DEBUG fprintf(stderr, "LJUSB_OpenDevice: Found handle for product ID %ld\n", ProductID); #endif break; } } } libusb_free_device_list(devs, 1); if (handle != NULL) { //We found a device, now check if it meets the minimum firmware requirement if (!LJUSB_isMinFirmware(handle, ProductID)) { //Does not meet the requirement. Close device and return an invalid handle. LJUSB_CloseDevice(handle); return NULL; } } #if LJ_DEBUG fprintf(stderr, "LJUSB_OpenDevice: Returning handle\n"); #endif return handle; }
HANDLE openUSBConnection(int localID) { BYTE buffer[38]; uint16 checksumTotal = 0; HANDLE hDevice = 0; uint32 numDevices = 0; uint32 dev; int i; numDevices = LJUSB_GetDevCount(UE9_PRODUCT_ID); if(numDevices == 0) { printf("Open error: No UE9 devices could be found\n"); return NULL; } for(dev = 1; dev <= numDevices; dev++) { hDevice = LJUSB_OpenDevice(dev, 0, UE9_PRODUCT_ID); if(hDevice != NULL) { if(localID < 0) { return hDevice; } else { checksumTotal = 0; //setting up a CommConfig command buffer[1] = (BYTE)(0x78); buffer[2] = (BYTE)(0x10); buffer[3] = (BYTE)(0x01); for(i = 6; i < 38; i++) buffer[i] = (BYTE)(0x00); extendedChecksum(buffer,38); if(LJUSB_BulkWrite(hDevice, UE9_PIPE_EP1_OUT, buffer, 38) != 38) goto locid_error; for(i = 0; i < 38; i++) buffer[i] = 0; if(LJUSB_BulkRead(hDevice, UE9_PIPE_EP1_IN, buffer, 38) != 38) goto locid_error; checksumTotal = extendedChecksum16(buffer, 38); if( (BYTE)((checksumTotal / 256) & 0xff) != buffer[5]) goto locid_error; if( (BYTE)(checksumTotal & 0xff) != buffer[4]) goto locid_error; if( extendedChecksum8(buffer) != buffer[0]) goto locid_error; if( buffer[1] != (BYTE)(0x78) || buffer[2] != (BYTE)(0x10) || buffer[3] != (BYTE)(0x01) ) goto locid_error; if( (int)buffer[8] == localID) return hDevice; else LJUSB_CloseDevice(hDevice); } //else localID >= 0 end } //if hDevice != NULL end } //for end printf("Open error: could not find a UE9 with a local ID of %d\n", localID); return NULL; locid_error: printf("Open error: problem when checking local ID\n"); return NULL; }
HANDLE openUSBConnection(int localID) { BYTE buffer[38]; uint16 checksumTotal = 0; uint32 numDevices = 0; uint32 dev; int i, serial; HANDLE hDevice = 0; numDevices = LJUSB_GetDevCount(UE9_PRODUCT_ID); if( numDevices == 0 ) { printf("Open error: No UE9 devices could be found\n"); return NULL; } for( dev = 1; dev <= numDevices; dev++ ) { hDevice = LJUSB_OpenDevice(dev, 0, UE9_PRODUCT_ID); if( hDevice != NULL ) { if( localID < 0 ) { return hDevice; } else { checksumTotal = 0; //Setting up a CommConfig command buffer[1] = (BYTE)(0x78); buffer[2] = (BYTE)(0x10); buffer[3] = (BYTE)(0x01); for( i = 6; i < 38; i++ ) buffer[i] = (BYTE)(0x00); extendedChecksum(buffer,38); if( LJUSB_Write(hDevice, buffer, 38) != 38 ) goto locid_error; for( i = 0; i < 38; i++ ) buffer[i] = 0; if( LJUSB_Read(hDevice, buffer, 38) != 38 ) goto locid_error; checksumTotal = extendedChecksum16(buffer, 38); if( (BYTE)((checksumTotal / 256) & 0xFF) != buffer[5] ) goto locid_error; if( (BYTE)(checksumTotal & 0xFF) != buffer[4] ) goto locid_error; if( extendedChecksum8(buffer) != buffer[0] ) goto locid_error; if( buffer[1] != (BYTE)(0x78) || buffer[2] != (BYTE)(0x10) || buffer[3] != (BYTE)(0x01) ) goto locid_error; //Check local ID if( (int)buffer[8] == localID ) return hDevice; //Check serial number serial = buffer[28] + buffer[29]*256 + buffer[30]*65536 + 0x10000000; if( serial == localID ) return hDevice; //No matches. Not our device. LJUSB_CloseDevice(hDevice); } //else localID >= 0 end } //if hDevice != NULL end } //for end printf("Open error: could not find a UE9 with a local ID or serial number of %d\n", localID); return NULL; locid_error: printf("Open error: problem when checking local ID and serial number\n"); return NULL; }