Example usage for android.hardware.usb UsbConstants USB_DIR_IN

List of usage examples for android.hardware.usb UsbConstants USB_DIR_IN

Introduction

In this page you can find the example usage for android.hardware.usb UsbConstants USB_DIR_IN.

Prototype

int USB_DIR_IN

To view the source code for android.hardware.usb UsbConstants USB_DIR_IN.

Click Source Link

Document

Used to signify direction of data for a UsbEndpoint is IN (device to host)

Usage

From source file:org.ros.android.android_acm_serial.AcmDevice.java

/**
 * Goes through the given UsbInterface's endpoints and finds the incoming
 * and outgoing bulk transfer endpoints.
 * @return Array with incoming (first) and outgoing (second) USB endpoints
 * @return <code>null</code>  in case either of the endpoints is not found
 *///from www  . j  a v a  2  s .c  o  m
private AcmUsbEndpoints getAcmEndpoints(UsbInterface usbInterface) {
    UsbEndpoint outgoingEndpoint = null;
    UsbEndpoint incomingEndpoint = null;
    for (int i = 0; i < usbInterface.getEndpointCount(); i++) {
        UsbEndpoint endpoint = usbInterface.getEndpoint(i);
        log.info("Interface: " + i + "/" + "Class: " + usbInterface.getInterfaceClass());
        if (usbInterface.getInterfaceClass() == UsbConstants.USB_CLASS_CDC_DATA) {
            if (endpoint.getDirection() == UsbConstants.USB_DIR_OUT) {
                log.info("Endpoint " + i + "/" + usbInterface.getEndpointCount() + ": " + endpoint + ". Type = "
                        + endpoint.getType());
                outgoingEndpoint = endpoint;
            } else if (endpoint.getDirection() == UsbConstants.USB_DIR_IN) {
                log.info("Endpoint " + i + "/" + usbInterface.getEndpointCount() + ": " + endpoint + ". Type = "
                        + endpoint.getType());
                incomingEndpoint = endpoint;
            }
        }
    }
    if (outgoingEndpoint == null || incomingEndpoint == null) {
        return null;
    } else {
        return new AcmUsbEndpoints(incomingEndpoint, outgoingEndpoint);
    }
}

From source file:com.googlecode.android_scripting.facade.USBHostSerialFacade.java

/**
 * openDevice: open the USB device {{{1//from w ww . j  a  v  a2 s.c om
 */
private void openDevice(UsbDevice device) {
    switch (device.getDeviceClass()) {
    case UsbConstants.USB_CLASS_PER_INTERFACE:
        // / 78K Trg-USB Serial
        Log.d("USB openDevice, device => USB_CLASS_PER_INTERFACE");
        break;
    case UsbConstants.USB_CLASS_COMM:
        // / PIC24 USB CDC
        Log.d("USB openDevice, device => USB_CLASS_COMM");
        break;
    default:
        Log.d("USB openDevice, device is not serial...");
        return;
    }

    UsbSerialConnection conn;
    try {
        conn = getConnection(device);
    } catch (IOException e) {
        Log.d("USB openDevice, can't get from connections 1");
        return;
    }
    if (conn == null) {
        Log.d("USB openDevice, can't get from connections 2");
        return;
    }

    Log.d("USB openDevice, try to open...");

    UsbDeviceConnection connection = mUsbManager.openDevice(device);
    if (connection == null) {
        Log.d("connection failed at openDevice...");
        // conn.close();
        return;
    }

    int i, j;

    Log.d("USB open SUCCESS");
    if (conn.mConnection != null) {
        Log.i("already connected? => stop thread");
        conn.stop();
    }
    conn.mConnection = connection;

    if (options.contains("trg78k")) {
        Log.d("USB Host Serial: reset the device (trg78k)");
        // conn.mConnection.controlTransfer(0x21, 0x22, 0x00, 0, null, 0, 0);
        byte[] data = { (byte) 0x03, (byte) 0x01 };
        conn.mConnection.controlTransfer(0x40, 0x00, 0x00, 0, null, 0, 0);
        conn.mConnection.controlTransfer(0x40, 0x0b, 0x00, 0, data, 2, 300);
    }

    for (i = 0; i < device.getInterfaceCount(); i++) {
        UsbInterface ui = device.getInterface(i);
        for (j = 0; j < ui.getEndpointCount(); j++) {
            Log.d(String.format("EndPoint loop...(%d, %d)", i, j));
            UsbEndpoint endPoint = ui.getEndpoint(j);
            switch (endPoint.getType()) {
            case UsbConstants.USB_ENDPOINT_XFER_BULK:
                if (endPoint.getDirection() == UsbConstants.USB_DIR_IN) {
                    conn.mInterfaceIn = ui;
                    conn.mEndpointIn = endPoint;
                    Log.d("USB mEndpointIn initialized!");
                } else {
                    conn.mInterfaceOut = ui;
                    conn.mEndpointOut = endPoint;
                    Log.d("USB mEndpointOut initialized!");
                }
                break;
            case UsbConstants.USB_ENDPOINT_XFER_CONTROL:
                break;
            case UsbConstants.USB_ENDPOINT_XFER_INT:
                conn.mEndpointIntr = endPoint;
                Log.d("USB mEndpointIntr initialized!");
                break;
            case UsbConstants.USB_ENDPOINT_XFER_ISOC:
                break;
            }
        }
    }

    if (options.contains("pl2303")) {
        Log.d("USB Host Serial: setup device (pl2303)");
        int ret;
        byte[] data = new byte[10];
        final byte rdReq = 0x01;
        final byte rdReqType = (byte) 0xC0;
        final byte wrReq = 0x01;
        final byte wrReqType = 0x40;
        final byte glReq = 0x21;
        final byte glReqType = (byte) 0xA1;
        final byte slReq = 0x20;
        final byte slReqType = (byte) 0x21;
        final byte bkReq = 0x23;
        final byte bkReqType = 0x21;
        int pl2303type = 1;

        UsbInterface ui = device.getInterface(0);
        connection.claimInterface(ui, true);
        /*
         * try { /// this cause device close by native_claiminterface. Thread.sleep(1000); } catch
         * (InterruptedException e) { Log.d("USB Host Serial: failed to sleep(1000)"); }
         */
        // / after API Level 13: if (connection.getRawDescriptors()[7] == 64) {
        // pl2303type = 1;
        // }

        connection.controlTransfer(rdReqType, rdReq, 0x8484, 0, data, 1, 100);
        connection.controlTransfer(wrReqType, wrReq, 0x0404, 0, null, 0, 100);
        connection.controlTransfer(rdReqType, rdReq, 0x8484, 0, data, 1, 100);
        connection.controlTransfer(rdReqType, rdReq, 0x8383, 0, data, 1, 100);
        connection.controlTransfer(rdReqType, rdReq, 0x8484, 0, data, 1, 100);
        connection.controlTransfer(wrReqType, wrReq, 0x0404, 1, null, 0, 100);
        connection.controlTransfer(rdReqType, rdReq, 0x8484, 0, data, 1, 100);
        connection.controlTransfer(rdReqType, rdReq, 0x8383, 0, data, 1, 100);
        connection.controlTransfer(wrReqType, wrReq, 0x0000, 1, null, 0, 100);
        connection.controlTransfer(wrReqType, wrReq, 0x0001, 0, null, 0, 100);

        // **type HX**
        // device class != 2
        // packet size == 64
        if (pl2303type == 1) {
            connection.controlTransfer(wrReqType, wrReq, 0x0002, 0x44, null, 0, 100);
        } else {
            connection.controlTransfer(wrReqType, wrReq, 0x0002, 0x24, null, 0, 100);
        }

        /*
         * // reset the device (HX) if (type != 2) { // TODO: halt } else {
         * connection.controlTransfer(wrReqType, wrReq, 0x0008, 0, null, 0, 100);
         * connection.controlTransfer(wrReqType, wrReq, 0x0009, 0, null, 0, 100); }
         */
        // initilize serial
        ret = connection.controlTransfer(glReqType, glReq, 0x0000, 0x00, data, 7, 100);
        Log.d(String.format("pl2303: GetLineRequest: %x => %x-%x-%x-%x-%x-%x-%x", ret, data[0], data[1],
                data[2], data[3], data[4], data[5], data[6]));

        // ret = connection.controlTransfer(wrReqType, wrReq, 0x0000, 0x01, null, 0, 100);
        // Log.d(String.format("pl2303: WriteRequest: %x", ret));

        int baud = 9600;
        data[0] = (byte) (baud & 0xFF);
        data[1] = (byte) ((baud >> 8) & 0xFF);
        data[2] = (byte) ((baud >> 16) & 0xFF);
        data[3] = (byte) ((baud >> 24) & 0xFF);
        data[4] = 0; // stopbit: 1bit, 1: 1.5bits, 2: 2bits
        data[5] = 0; // parity: None, 1:odd, 2:even, 3:mark, 4:space
        data[6] = 8; // data size: 8, 5, 6, 7
        data[7] = 0x00;
        ret = connection.controlTransfer(slReqType, slReq, 0x0000, 0x00, data, 7, 100);
        Log.d(String.format("pl2303: SetLineRequest: %x", ret));

        // set break off
        ret = connection.controlTransfer(bkReqType, bkReq, 0x0000, 0x00, null, 0, 100);

        if (pl2303type == 1) {
            // for RTSCTS and HX device
            ret = connection.controlTransfer(wrReqType, wrReq, 0x0000, 0x61, null, 0, 100);
        } else {
            // for not RTSCTS
            ret = connection.controlTransfer(wrReqType, wrReq, 0x0000, 0x00, null, 0, 100);
        }

        // connection.releaseInterface(ui);
    }

    if (!conn.isConnected()) {
        connectionFailed();
        return;
    }
    conn.start();
}

From source file:org.chromium.ChromeUsb.java

private void controlTransfer(CordovaArgs args, JSONObject params, final CallbackContext callbackContext)
        throws JSONException, UsbError {
    ConnectedDevice dev = getDevice(params);

    int direction = directionFromName(params.getString("direction"));
    int requestType = controlRequestTypeFromName(params.getString("requestType"));
    int recipient = recipientFromName(params.getString("recipient"));

    byte[] transferBuffer = getByteBufferForTransfer(args, params, UsbConstants.USB_DIR_OUT);
    byte[] receiveBuffer = getByteBufferForTransfer(args, params, UsbConstants.USB_DIR_IN);

    int ret = dev.controlTransfer(direction | requestType | recipient, params.getInt("request"),
            params.getInt("value"), params.getInt("index"), transferBuffer, receiveBuffer,
            params.getInt("timeout"));
    if (ret < 0) {
        throw new UsbError("Control transfer returned " + ret);
    }/*from  ww w .  j  a v  a 2  s  . co m*/

    /* control transfer is bidirectional, buffer should alway be passed */
    callbackContext.success(Arrays.copyOf(receiveBuffer, receiveBuffer.length));
}

From source file:org.chromium.ChromeUsb.java

private void bulkTransfer(CordovaArgs args, JSONObject params, final CallbackContext callbackContext)
        throws JSONException, UsbError {
    ConnectedDevice dev = getDevice(params);
    int endpointAddress = params.getInt("endpoint");
    int interfaceNumber = endpointAddress >> ENDPOINT_IF_SHIFT;
    int endpointNumber = endpointAddress & ((1 << ENDPOINT_IF_SHIFT) - 1);
    if (interfaceNumber >= dev.getInterfaceCount() || endpointNumber >= dev.getEndpointCount(interfaceNumber)) {
        throw new UsbError("Enpoint not found: " + endpointAddress);
    }//from  ww  w  .  j  a  v a  2  s  . c  o  m
    int direction = directionFromName(params.getString("direction"));
    byte[] buffer = getByteBufferForTransfer(args, params, direction);

    int ret = dev.bulkTransfer(interfaceNumber, endpointNumber, direction, buffer, params.getInt("timeout"));
    if (ret < 0) {
        throw new UsbError("Bulk transfer returned " + ret);
    }
    if (direction == UsbConstants.USB_DIR_IN) {
        callbackContext.success(Arrays.copyOf(buffer, buffer.length));
    } else {
        callbackContext.success();
    }
}

From source file:org.chromium.ChromeUsb.java

private void interruptTransfer(CordovaArgs args, JSONObject params, final CallbackContext callbackContext)
        throws JSONException, UsbError {
    ConnectedDevice dev = getDevice(params);
    int endpointAddress = params.getInt("endpoint");
    int interfaceNumber = endpointAddress >> ENDPOINT_IF_SHIFT;
    int endpointNumber = endpointAddress & ((1 << ENDPOINT_IF_SHIFT) - 1);
    if (interfaceNumber >= dev.getInterfaceCount() || endpointNumber >= dev.getEndpointCount(interfaceNumber)) {
        throw new UsbError("Enpoint not found: " + endpointAddress);
    }/*ww w .j  av a  2  s.c o m*/

    int direction = directionFromName(params.getString("direction"));
    byte[] buffer = getByteBufferForTransfer(args, params, direction);

    int ret = dev.interruptTransfer(interfaceNumber, endpointNumber, direction, buffer,
            params.getInt("timeout"));
    if (ret < 0) {
        throw new UsbError("Interrupt transfer returned " + ret);
    }
    if (direction == UsbConstants.USB_DIR_IN) {
        callbackContext.success(Arrays.copyOf(buffer, buffer.length));
    } else {
        callbackContext.success();
    }
}

From source file:org.chromium.ChromeUsb.java

static String directionName(int direction) {
    switch (direction) {
    case UsbConstants.USB_DIR_IN:
        return "in";
    case UsbConstants.USB_DIR_OUT:
        return "out";
    default:/*from  ww w  . j  a v a2s .c  o m*/
        return "ERR:" + direction;
    }
}

From source file:org.chromium.ChromeUsb.java

private static int directionFromName(String direction) throws UsbError {
    direction = direction.toLowerCase();
    if ("out".equals(direction)) {
        return UsbConstants.USB_DIR_OUT; /* 0x00 */
    } else if ("in".equals(direction)) {
        return UsbConstants.USB_DIR_IN; /* 0x80 */
    } else {//from  w w  w.  j  a  v a 2s  .c om
        throw new UsbError("Unknown transfer direction: " + direction);
    }
}