예제 #1
0
 /** Tests the {@link Transfer#status()} methods. */
 @Test
 public void testGetStatus() {
   assumeUsbTestsEnabled();
   final Transfer transfer = LibUsb.allocTransfer(0);
   assertEquals(0, transfer.status());
   LibUsb.freeTransfer(transfer);
 }
예제 #2
0
  /**
   * findDevice - finds and returns the device that is lasted connected, and matches both the vendor
   * and product id (please use enums provided in MindStormDevice for parameters)
   *
   * @param vendorId - vendor id for USB device
   * @param productId - product id for USB device
   * @return
   */
  private static Device findDevice(short vendorId, short productId) {
    // Read the USB device list
    DeviceList list = new DeviceList();
    Context context = new Context();
    LibUsb.init(context);
    int result = LibUsb.getDeviceList(context, list);
    if (result < 0) throw new LibUsbException("Unable to get device list", result);

    try {
      // Iterate over all devices and scan for the right one
      for (Device device : list) {
        DeviceDescriptor descriptor = new DeviceDescriptor();
        result = LibUsb.getDeviceDescriptor(device, descriptor);
        if (result != LibUsb.SUCCESS)
          throw new LibUsbException("Unable to read device descriptor", result);
        if (descriptor.idVendor() == vendorId && descriptor.idProduct() == productId) return device;
      }
    } finally {
      // Ensure the allocated device list is freed
      LibUsb.freeDeviceList(list, true);
    }

    // Device not found
    return null;
  }
예제 #3
0
 /** Tests the {@link Transfer#setTimeout(long)} and {@link Transfer#timeout()} methods. */
 @Test
 public void testTimeout() {
   assumeUsbTestsEnabled();
   final Transfer transfer = LibUsb.allocTransfer(0);
   assertEquals(0, transfer.timeout());
   transfer.setTimeout(1);
   assertEquals(1, transfer.timeout());
   transfer.setTimeout(0);
   assertEquals(0, transfer.timeout());
   LibUsb.freeTransfer(transfer);
 }
예제 #4
0
 /** Tests the {@link Transfer#setType(byte)} and {@link Transfer#type()} methods. */
 @Test
 public void testType() {
   assumeUsbTestsEnabled();
   final Transfer transfer = LibUsb.allocTransfer(0);
   assertEquals(0, transfer.type());
   transfer.setType((byte) 1);
   assertEquals(1, transfer.type());
   transfer.setType((byte) 0);
   assertEquals(0, transfer.type());
   LibUsb.freeTransfer(transfer);
 }
예제 #5
0
  /** Tests allocating and freeing a transfer object. */
  @Test
  public void testAllocAndFree() {
    assumeUsbTestsEnabled();
    final Transfer transfer = LibUsb.allocTransfer(0);
    assertNotNull(transfer);
    LibUsb.freeTransfer(transfer);

    try {
      LibUsb.freeTransfer(transfer);
      fail("Double-free should throw IllegalStateException");
    } catch (final IllegalStateException e) {
      // Expected behavior
    }
  }
 /** Setup test. */
 @Before
 public void setUp() {
   if (isUsbTestsEnabled()) {
     LibUsb.init(null);
     this.descriptor = new Usb20ExtensionDescriptor();
   }
 }
예제 #7
0
 /**
  * Tests the {@link Transfer#setDevHandle(DeviceHandle)} and {@link Transfer#devHandle()} methods.
  */
 @Test
 public void testDevHandle() {
   assumeUsbTestsEnabled();
   final Transfer transfer = LibUsb.allocTransfer(0);
   final DeviceHandle handle = new DeviceHandle();
   this.setPointer(handle, 1);
   final DeviceHandle handle2 = new DeviceHandle();
   this.setPointer(handle2, 2);
   assertNull(transfer.devHandle());
   transfer.setDevHandle(handle);
   assertNotNull(transfer.devHandle());
   assertNotSame(handle, transfer.devHandle());
   assertNotEquals(handle2, transfer.devHandle());
   assertEquals(handle, transfer.devHandle());
   transfer.setDevHandle(null);
   assertNull(transfer.devHandle());
   LibUsb.freeTransfer(transfer);
 }
예제 #8
0
 /** Set up the test. */
 @Before
 public void setUp() {
   this.context = new Context();
   try {
     LibUsb.init(this.context);
   } catch (final Throwable e) {
     this.context = null;
   }
 }
예제 #9
0
  /**
   * connect - sends a byte buffer to a mindstorm device
   *
   * @param mindStormDevice - the mindstorm device that is connected
   * @param commandBuffer - the byte buffer
   * @return - error / success values (if an error, stream to System.err will be produced)
   */
  public static int connect(MindStormDevice mindStormDevice, ByteBuffer commandBuffer) {
    int result = LibUsb.init(null);
    if (result != LibUsb.SUCCESS) {
      throw new LibUsbException("unable to initialize libusb", result);
    }

    Device device = findDevice(mindStormDevice.vendor, mindStormDevice.product);
    DeviceHandle dh = new DeviceHandle();
    LibUsb.open(device, dh);
    int usbInterface = mindStormDevice.usbInterface;
    LibUsb.claimInterface(dh, usbInterface); // handle, Interface
    if (result != LibUsb.SUCCESS) {
      throw new LibUsbException("Unable to claim interface", result);
    }

    // Check if kernel driver must be detached
    boolean detach = LibUsb.hasCapability(LibUsb.CAP_SUPPORTS_DETACH_KERNEL_DRIVER);

    // Detach the kernel driver
    if (detach) {
      result = LibUsb.detachKernelDriver(dh, usbInterface);
      if (result != LibUsb.SUCCESS)
        throw new LibUsbException("Unable to detach kernel driver", result);
    }

    int value =
        LibUsb.bulkTransfer(
            dh, // handle - A handle for the device to communicate with.
            mindStormDevice
                .endOut, // endpoint - The address of a valid endpoint to communicate with.
            commandBuffer, // data - A suitably-sized data buffer for either input or output
                           // (depending on endpoint).
            IntBuffer.allocate(
                1), // transferred - Output location for the number of bytes actually transferred.
            5000 // timeout - timeout (in millseconds) that this function should wait before giving
                 // up due to no response being received. For an unlimited timeout, use value 0.
            );
    if (value < 0) {
      // if there is an error, produce the error name and value
      System.err.print(LibUsb.errorName(value));
      System.err.println(", " + value);
    }
    LibUsb.close(dh);
    LibUsb.exit(null);
    return value;
  }
예제 #10
0
 /** Tear down the test. */
 @After
 public void tearDown() {
   if (this.context != null) {
     LibUsb.exit(this.context);
   }
 }
예제 #11
0
 /**
  * isConnected - returns if mindstorm device is connected via USB
  *
  * @param mindStormDevice
  * @return true if at least one mindstorm device (matching parameters) is connected
  */
 public static boolean isConnected(MindStormDevice mindStormDevice) {
   LibUsb.init(null);
   Device device = findDevice(mindStormDevice.vendor, mindStormDevice.product);
   return device != null;
 }
 /** Tear down test. */
 @After
 public void tearDown() {
   if (isUsbTestsEnabled()) {
     LibUsb.exit(null);
   }
 }