public void testGetStatusNonStatic() {
    /* The method being tested is:
     * public static short getStatus(byte recipient, short target)
     *                              throws usbException, IllegalArgumentException
     */

    StandardRequest standardRequest = new StandardRequest(usbDevice);

    short configurationNumber;
    byte recipientType;
    short featureSelection;
    short target;
    short wIndex;
    short status;
    short statusExpected;
    byte requestType;

    boolean usbExceptionThrown = false;

    /**
     * ********************************************************************** First, getStatus for a
     * new configuration **********************************************************************
     */
    configurationNumber = 1;
    status = 0;
    // no target needed when recipient is a device
    target = 0;
    // set recipientType to Device
    recipientType = UsbConst.REQUESTTYPE_RECIPIENT_DEVICE;
    requestType = UsbConst.REQUESTTYPE_DIRECTION_IN;
    statusExpected = 0;

    // @P2D16
    LastUsbDeviceDataEvent = null;
    LastUsbDeviceErrorEvent = null;

    try {

      status = standardRequest.getStatus(recipientType, target);

      try {
        /*
         * Wait for device event before leaving setFeature routine
         */
        for (int i = 0; i < 100; i++) {
          if ((LastUsbDeviceDataEvent != null) | (LastUsbDeviceErrorEvent != null)) {
            break;
          }
          Thread.sleep(20); // wait 20 ms before checking for event
        }
      } catch (InterruptedException e) {
        fail("Sleep was interrupted");
      }

      /*status is unknown at this point, so don't check the value yet*/

    } catch (UsbException uE) {
      fail("Got exception getting the device status. Exception message: " + uE.getMessage());
    } catch (IllegalArgumentException iE) {
      fail("Got illegal argument exception.  Exception message" + iE.getMessage());
    }

    /* Verify proper listener event received */
    assertNotNull("DeviceDataEvent should not be null.", LastUsbDeviceDataEvent);
    assertNull("DeviceErrorEvent should be null.", LastUsbDeviceErrorEvent);

    verifyGetStatusIrp(target, requestType);

    /**
     * ********************************************************************** Now, do a setFeature
     * for the device and check getStatus
     * **********************************************************************
     */
    featureSelection = UsbConst.FEATURE_SELECTOR_DEVICE_REMOTE_WAKEUP;
    statusExpected = 2;

    try {

      standardRequest.setFeature(recipientType, featureSelection, target);
      try {
        /*Wait for setConfiguration Data event before continuing */
        Thread.sleep(250);
      } catch (InterruptedException e) {
        fail("Sleep was interrupted.");
      }

    } catch (UsbException uE) {
      fail("Got exception setting Feature. Exception message: " + uE.getMessage());
    } catch (IllegalArgumentException iE) {
      fail("Got illegal argument exception.  Exception message" + iE.getMessage());
    }

    LastUsbDeviceDataEvent = null;
    LastUsbDeviceErrorEvent = null;

    try {

      status = standardRequest.getStatus(recipientType, target);

      try {
        /*
         * Wait for device event before leaving setFeature routine
         */
        for (int i = 0; i < 100; i++) {
          if ((LastUsbDeviceDataEvent != null) | (LastUsbDeviceErrorEvent != null)) {
            break;
          }
          Thread.sleep(20); // wait 20 ms before checking for event
        }
      } catch (InterruptedException e) {
        fail("Sleep was interrupted");
      }

      assertEquals(
          "Status is not as expected.  Expected value = "
              + statusExpected
              + ", Actual value = "
              + status,
          statusExpected,
          status);

    } catch (UsbException uE) {
      fail("Got exception getting the device status. Exception message: " + uE.getMessage());
    } catch (IllegalArgumentException iE) {
      fail("Got illegal argument exception.  Exception message" + iE.getMessage());
    }

    /* Verify proper listener event received */
    assertNotNull("DeviceDataEvent should not be null.", LastUsbDeviceDataEvent);
    assertNull("DeviceErrorEvent should be null.", LastUsbDeviceErrorEvent);

    verifyGetStatusIrp(target, requestType);

    /**
     * ********************************************************************** Now, do a clearFeature
     * for the device and check getStatus
     * **********************************************************************
     */
    featureSelection = UsbConst.FEATURE_SELECTOR_DEVICE_REMOTE_WAKEUP;
    statusExpected = 0;

    try {

      standardRequest.clearFeature(recipientType, featureSelection, target);
      try {
        /*Wait for setConfiguration Data event before continuing */
        Thread.sleep(250);
      } catch (InterruptedException e) {
        fail("Sleep was interrupted.");
      }

    } catch (UsbException uE) {
      fail("Got exception clearing Feature. Exception message: " + uE.getMessage());
    } catch (IllegalArgumentException iE) {
      fail("Got illegal argument exception.  Exception message" + iE.getMessage());
    }

    LastUsbDeviceDataEvent = null;
    LastUsbDeviceErrorEvent = null;

    try {

      status = standardRequest.getStatus(recipientType, target);

      try {
        /*
         * Wait for device event before leaving setFeature routine
         */
        for (int i = 0; i < 100; i++) {
          if ((LastUsbDeviceDataEvent != null) | (LastUsbDeviceErrorEvent != null)) {
            break;
          }
          Thread.sleep(20); // wait 20 ms before checking for event
        }
      } catch (InterruptedException e) {
        fail("Sleep was interrupted");
      }

      assertEquals(
          "Status is not as expected.  Expected value = "
              + statusExpected
              + ", Actual value = "
              + status,
          statusExpected,
          status);

    } catch (UsbException uE) {
      fail("Got exception getting the device status. Exception message: " + uE.getMessage());
    } catch (IllegalArgumentException iE) {
      fail("Got illegal argument exception.  Exception message" + iE.getMessage());
    }

    /* Verify proper listener event received */
    assertNotNull("DeviceDataEvent should not be null.", LastUsbDeviceDataEvent);
    assertNull("DeviceErrorEvent should be null.", LastUsbDeviceErrorEvent);

    verifyGetStatusIrp(target, requestType);

    /**
     * ********************************************************************** GetStatus for a device
     * with an illegal argument
     * **********************************************************************
     */
    recipientType = 3;
    LastUsbDeviceDataEvent = null;
    LastUsbDeviceErrorEvent = null;

    try {

      status = standardRequest.getStatus(recipientType, target);

      try {
        /*
         * Wait for device event before leaving setFeature routine
         */
        for (int i = 0; i < 100; i++) {
          if ((LastUsbDeviceDataEvent != null) | (LastUsbDeviceErrorEvent != null)) {
            break;
          }
          Thread.sleep(20); // wait 20 ms before checking for event
        }
      } catch (InterruptedException e) {
        fail("Sleep was interrupted");
      }

    } catch (UsbException uE) {
      fail("Got exception getting the device status. Exception message: " + uE.getMessage());
    } catch (IllegalArgumentException iE) {
      usbExceptionThrown = true;
    } finally {
      assertTrue(
          "UsbException should have been thrown for using an illegal recipient type:  "
              + UsbUtil.toHexString(recipientType),
          usbExceptionThrown);
    }

    try {
      Thread.sleep(250);
    } catch (InterruptedException e) {
      fail("Sleep was interrupted");
    }

    /* Verify proper listener event received */
    /* Don't get data or error event with illegal argument exception*/
    assertNull("DeviceDataEvent should be null.", LastUsbDeviceDataEvent);
    assertNull("DeviceErrorEvent should be null.", LastUsbDeviceErrorEvent);

    /**
     * ********************************************************************** Now getStatus for an
     * interface ***********************************************************************
     */
    configurationNumber = 1;

    // no target needed when recipient is a device
    target = 0;
    // set recipientType to Interface
    recipientType = UsbConst.REQUESTTYPE_RECIPIENT_INTERFACE;
    requestType = UsbConst.REQUESTTYPE_DIRECTION_IN + UsbConst.REQUESTTYPE_RECIPIENT_INTERFACE;
    statusExpected = 0;

    // @P2D16
    LastUsbDeviceDataEvent = null;
    LastUsbDeviceErrorEvent = null;

    try {

      try {
        usbDevice.getActiveUsbConfiguration().getUsbInterface((byte) target).claim();
      } catch (UsbClaimException uCE) {
        fail("Config " + configurationNumber + " Interface " + target + " is already claimed!");
      } catch (UsbNotActiveException uNAE) {
        fail("Config " + configurationNumber + " is not active!");
      } catch (UsbException uE) {
        fail("Config " + configurationNumber + " could not be claimed!");
      } catch (UsbDisconnectedException uDE) // @P1C
      { // @P1A
        fail("A connected device should't throw the UsbDisconnectedException!"); // @P1A
      } // @P1A
      status = standardRequest.getStatus(recipientType, target);

      try {
        /*
         * Wait for device event before leaving setFeature routine
         */
        for (int i = 0; i < 100; i++) {
          if ((LastUsbDeviceDataEvent != null) | (LastUsbDeviceErrorEvent != null)) {
            break;
          }
          Thread.sleep(20); // wait 20 ms before checking for event
        }
      } catch (InterruptedException e) {
        fail("Sleep was interrupted");
      }

      // status should be 0 since interface status cannot be changed
      status = 0;
      assertEquals(
          "Status is not as expected.  Expected value = "
              + statusExpected
              + ", Actual value = "
              + status,
          statusExpected,
          status);

    } catch (UsbException uE) {
      fail("Got exception getting the device status. Exception message: " + uE.getMessage());
    } catch (IllegalArgumentException iE) {
      fail("Got illegal argument exception.  Exception message" + iE.getMessage());
    }

    // Verify proper listener event received
    assertNotNull("DeviceDataEvent should not be null.", LastUsbDeviceDataEvent);
    assertNull("DeviceErrorEvent should be null.", LastUsbDeviceErrorEvent);

    verifyGetStatusIrp(target, requestType);

    /**
     * ********************************************************************** Now getStatus for an
     * endpoint ***********************************************************************
     */
    short endpoint;

    configurationNumber = 1;
    status = 0;
    // no target needed when recipient is a device
    target = 0;
    // set recipientType to Interface
    recipientType = UsbConst.REQUESTTYPE_RECIPIENT_ENDPOINT;
    requestType = UsbConst.REQUESTTYPE_DIRECTION_IN + UsbConst.REQUESTTYPE_RECIPIENT_ENDPOINT;
    statusExpected = 1;
    endpoint = 0x82;

    // @P2D16
    LastUsbDeviceDataEvent = null;
    LastUsbDeviceErrorEvent = null;

    try {

      status = standardRequest.getStatus(recipientType, endpoint);

      try {
        /*
         * Wait for device event before leaving setFeature routine
         */
        for (int i = 0; i < 100; i++) {
          if ((LastUsbDeviceDataEvent != null) | (LastUsbDeviceErrorEvent != null)) {
            break;
          }
          Thread.sleep(20); // wait 20 ms before checking for event
        }
      } catch (InterruptedException e) {
        fail("Sleep was interrupted");
      }

      /*status is unknown at this point, so don't check the value yet	*/

    } catch (UsbException uE) {
      fail("Got exception getting the device status. Exception message: " + uE.getMessage());
    } catch (IllegalArgumentException iE) {
      fail("Got illegal argument exception.  Exception message" + iE.getMessage());
    }

    /* Verify proper listener event received */
    assertNotNull("DeviceDataEvent should not be null.", LastUsbDeviceDataEvent);
    assertNull("DeviceErrorEvent should be null.", LastUsbDeviceErrorEvent);

    verifyGetStatusIrp(endpoint, requestType);

    /**
     * ********************************************************************** getStatus after
     * setting feature for the endpoint
     * ***********************************************************************
     */
    featureSelection = UsbConst.FEATURE_SELECTOR_ENDPOINT_HALT;
    statusExpected = 1;

    try {

      standardRequest.setFeature(recipientType, featureSelection, endpoint);
      try {
        /*Wait for setConfiguration Data event before continuing */
        Thread.sleep(250);
      } catch (InterruptedException e) {
        fail("Sleep was interrupted.");
      }

    } catch (UsbException uE) {
      fail("Got exception setting Feature. Exception message: " + uE.getMessage());
    } catch (IllegalArgumentException iE) {
      fail("Got illegal argument exception.  Exception message" + iE.getMessage());
    }

    LastUsbDeviceDataEvent = null;
    LastUsbDeviceErrorEvent = null;

    try {

      status = standardRequest.getStatus(recipientType, endpoint);

      try {
        /*
         * Wait for device event before leaving setFeature routine
         */
        for (int i = 0; i < 100; i++) {
          if ((LastUsbDeviceDataEvent != null) | (LastUsbDeviceErrorEvent != null)) {
            break;
          }
          Thread.sleep(20); // wait 20 ms before checking for event
        }
      } catch (InterruptedException e) {
        fail("Sleep was interrupted");
      }

      assertEquals(
          "Status is not as expected.  Expected value = "
              + statusExpected
              + ", Actual value = "
              + status,
          statusExpected,
          status);

    } catch (UsbException uE) {
      fail("Got exception getting the device status. Exception message: " + uE.getMessage());
    } catch (IllegalArgumentException iE) {
      fail("Got illegal argument exception.  Exception message" + iE.getMessage());
    }

    /* Verify proper listener event received */
    assertNotNull("DeviceDataEvent should not be null.", LastUsbDeviceDataEvent);
    assertNull("DeviceErrorEvent should be null.", LastUsbDeviceErrorEvent);

    verifyGetStatusIrp(endpoint, requestType);

    /**
     * ********************************************************************** Now, do a clearFeature
     * for the endpoint and check getStatus
     * **********************************************************************
     */
    featureSelection = UsbConst.FEATURE_SELECTOR_ENDPOINT_HALT;
    statusExpected = 0;

    try {

      standardRequest.clearFeature(recipientType, featureSelection, endpoint);
      try {
        /*Wait for setConfiguration Data event before continuing */
        Thread.sleep(250);
      } catch (InterruptedException e) {
        fail("Sleep was interrupted.");
      }

    } catch (UsbException uE) {
      fail("Got exception clearing Feature. Exception message: " + uE.getMessage());
    } catch (IllegalArgumentException iE) {
      fail("Got illegal argument exception.  Exception message" + iE.getMessage());
    }

    LastUsbDeviceDataEvent = null;
    LastUsbDeviceErrorEvent = null;

    try {

      status = standardRequest.getStatus(recipientType, endpoint);

      try {
        /*
         * Wait for device event before leaving setFeature routine
         */
        for (int i = 0; i < 100; i++) {
          if ((LastUsbDeviceDataEvent != null) | (LastUsbDeviceErrorEvent != null)) {
            break;
          }
          Thread.sleep(20); // wait 20 ms before checking for event
        }
      } catch (InterruptedException e) {
        fail("Sleep was interrupted");
      }

      assertEquals(
          "Status is not as expected.  Expected value = "
              + statusExpected
              + ", Actual value = "
              + status,
          statusExpected,
          status);

    } catch (UsbException uE) {
      fail("Got exception getting the device status. Exception message: " + uE.getMessage());
    } catch (IllegalArgumentException iE) {
      fail("Got illegal argument exception.  Exception message" + iE.getMessage());
    }

    /* Verify proper listener event received */
    assertNotNull("DeviceDataEvent should not be null.", LastUsbDeviceDataEvent);
    assertNull("DeviceErrorEvent should be null.", LastUsbDeviceErrorEvent);

    verifyGetStatusIrp(endpoint, requestType);

    /**
     * ********************************************************************** GetStatus for an
     * endpoint with an illegal argument
     * **********************************************************************
     */

    // set recipientType to an illegal value
    recipientType = 3;
    LastUsbDeviceDataEvent = null;
    LastUsbDeviceErrorEvent = null;

    try {

      status = standardRequest.getStatus(recipientType, endpoint);

      try {
        /*
         * Wait for device event before leaving setFeature routine
         */
        for (int i = 0; i < 100; i++) {
          if ((LastUsbDeviceDataEvent != null) | (LastUsbDeviceErrorEvent != null)) {
            break;
          }
          Thread.sleep(20); // wait 20 ms before checking for event
        }
      } catch (InterruptedException e) {
        fail("Sleep was interrupted");
      }

    } catch (UsbException uE) {
      fail("Got exception getting the device status. Exception message: " + uE.getMessage());
    } catch (IllegalArgumentException iE) {
      usbExceptionThrown = true;
    } finally {
      assertTrue(
          "UsbException should have been thrown for using an illegal recipient type:  "
              + UsbUtil.toHexString(recipientType),
          usbExceptionThrown);
    }

    try {
      Thread.sleep(250);
    } catch (InterruptedException e) {
      fail("Sleep was interrupted");
    }

    try {
      usbDevice.getActiveUsbConfiguration().getUsbInterface((byte) target).release();
    } catch (UsbClaimException uCE) {
      fail("Config " + configurationNumber + " Interface " + target + " is not claimed!");
    } catch (UsbException uE) {
      fail("Config " + configurationNumber + " could not be released!");
    } catch (UsbDisconnectedException uDE) // @P1C
    { // @P1A
      fail("A connected device should't throw the UsbDisconnectedException!"); // @P1A
    } // @P1A

    /* Verify proper listener event received */
    assertNull("DeviceDataEvent should be null.", LastUsbDeviceDataEvent);
    assertNull("DeviceErrorEvent should be null.", LastUsbDeviceErrorEvent);
  }
Exemplo n.º 2
0
  private void sendOUTandIN(
      byte testType,
      boolean SyncOrAsync,
      UsbPipe inPipe,
      UsbPipe outPipe,
      List listOfINIrps,
      List listOfOUTIrps) {
    try {
      if (endpointType == UsbConst.ENDPOINT_TYPE_ISOCHRONOUS) {
        // For isochronous, a list of IN IRPs must be async submitted as the IN for
        // the byte[], IRP, and IRP List tests.  Only the OUT, will be submitted
        // as a byte[], IRP, IRPList and in both synchronous and asynchronous modes.
        try {
          printDebug("About to async Submit IN IRP List for ISOCHRONOUS");
          inPipe.asyncSubmit(listOfINIrps);
          printDebug("Return from async Submit IN IRP List for ISOCHRONOUS");
        } catch (Exception uE) {
          System.out.println("Exception on async submit.  Submission failed." + uE.getMessage());
          Assert.fail("Exception on async submit.  Submission failed." + uE.getMessage());
        }
      }

      if (SyncOrAsync == SYNC_SUBMIT) {
        printDebug("About to sync Submit OUT IRP");
        // for single IRP, send the first IRP in the IRP list
        outPipe.syncSubmit((UsbIrp) listOfOUTIrps.get(0));
        printDebug("Return from sync Submit OUT IRP");
      } else {
        printDebug("About to async Submit OUT IRP");
        // for single IRP, send the first IRP in the IRP list
        outPipe.asyncSubmit((UsbIrp) listOfOUTIrps.get(0));
        printDebug("Return from async Submit OUT IRP");
        // wait for IRP to be complete; wait a max of 5000 ms
        ((UsbIrp) listOfOUTIrps.get(0)).waitUntilComplete(5000);
      }

      // all IRPs should be complete when sync submit returns
      for (int i = 0; i < (listOfOUTIrps.size()); i++) {
        Assert.assertTrue(
            "isComplete() not true for IRP after syncSubmit returned",
            ((UsbIrp) listOfOUTIrps.get(i)).isComplete());
        Assert.assertFalse(
            "isUsbException() is true for IRP after syncSubmit returned",
            ((UsbIrp) listOfOUTIrps.get(i)).isUsbException());
      }

      // isochronous IN IRP List was already submitted (see above)
      if (endpointType != UsbConst.ENDPOINT_TYPE_ISOCHRONOUS) {

        if (SyncOrAsync == SYNC_SUBMIT) {
          printDebug("About to sync Submit IN IRP");
          // for single IRP, send the first IRP in the IRP list
          inPipe.syncSubmit((UsbIrp) listOfINIrps.get(0));
          printDebug("Return from sync Submit IN IRP");
        } else {
          printDebug("About to async Submit IN IRP");
          // for single IRP, send the first IRP in the IRP list
          inPipe.asyncSubmit((UsbIrp) listOfINIrps.get(0));
          printDebug("Return from async Submit IN IRP");
          // wait for IRP to be complete; wait a max of 5000 ms
          ((UsbIrp) listOfINIrps.get(0)).waitUntilComplete(5000);
          printDebug("Finished waiting on async in irp--timeout");
          ((UsbIrp) listOfINIrps.get(0)).waitUntilComplete();
          printDebug("Finished waiting on async in irp");
        }
      }

      // all IRPs should be complete when sync submit returns
      for (int i = 0; i < (listOfINIrps.size()); i++) {
        Assert.assertTrue(
            "isComplete() not true for IRP after syncSubmit returned",
            ((UsbIrp) listOfINIrps.get(i)).isComplete());
        // Assert.assertFalse("isUsbException() is true for IRP after syncSubmit returned",
        //                   ((UsbIrp)listOfINIrps.get(i)).isUsbException());
      }

    } catch (UsbException uE) {

      // exceptions are expected in this test
    } catch (UsbDisconnectedException uDE) // @P1A
    { // @P1A
      Assert.fail("A connected device should't throw the UsbDisconnectedException!"); // @P1A
    } // @P1A

    try {
      /*
       * After all IRPs are complete, wait for all device events.
       * Wait for device event before leaving submit routine.
       * 400 * 5 ms = 2000 ms = 2 seconds max wait for the last event
       * before error.
       * This should be an adequate wait because we don't get here until
       * all of the submits are complete
       */
      // first wait for all OUT events
      for (int i = 0; i < 400; i++) {

        if (outPipeEvents.size() == listOfOUTIrps.size()) {
          // System.out.println("Data event took less than " + ((i+1) * 20 ) +" milliseconds");
          break;
        }

        Thread.sleep(5); // wait 5 ms before checkin for event
      }

      // now wait for all IN events
      for (int i = 0; i < 400; i++) {
        if (inPipeEvents.size() == listOfINIrps.size()) {
          // System.out.println("Data event took less than " + ((i+1) * 20 ) +" milliseconds");
          break;
        }

        Thread.sleep(5); // wait 5 ms before checkin for event
      }

    } catch (InterruptedException e) {
      Assert.fail("Sleep was interrupted");

    } finally {

      Assert.assertEquals(
          "Did not receive all expected IN pipe events after sleep.",
          listOfINIrps.size(),
          inPipeEvents.size());

      Assert.assertEquals(
          "Did not receive all expected OUT pipe event after sleep.",
          listOfOUTIrps.size(),
          outPipeEvents.size());
    }
  };