Search in sources :

Example 11 with UsbInterface

use of android.hardware.usb.UsbInterface in project android_frameworks_base by crdroidandroid.

the class SinkActivity method connect.

private void connect(UsbDevice device) {
    if (mConnected) {
        disconnect();
    }
    // Check whether we have permission to access the device.
    if (!mUsbManager.hasPermission(device)) {
        mLogger.log("Prompting the user for access to the device.");
        Intent intent = new Intent(ACTION_USB_DEVICE_PERMISSION);
        intent.setPackage(getPackageName());
        PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
        mUsbManager.requestPermission(device, pendingIntent);
        return;
    }
    // Claim the device.
    UsbDeviceConnection conn = mUsbManager.openDevice(device);
    if (conn == null) {
        mLogger.logError("Could not obtain device connection.");
        return;
    }
    UsbInterface iface = device.getInterface(0);
    UsbEndpoint controlEndpoint = iface.getEndpoint(0);
    if (!conn.claimInterface(iface, true)) {
        mLogger.logError("Could not claim interface.");
        return;
    }
    try {
        // If already in accessory mode, then connect to the device.
        if (isAccessory(device)) {
            mLogger.log("Connecting to accessory...");
            int protocolVersion = getProtocol(conn);
            if (protocolVersion < 1) {
                mLogger.logError("Device does not support accessory protocol.");
                return;
            }
            mLogger.log("Protocol version: " + protocolVersion);
            // Setup bulk endpoints.
            UsbEndpoint bulkIn = null;
            UsbEndpoint bulkOut = null;
            for (int i = 0; i < iface.getEndpointCount(); i++) {
                UsbEndpoint ep = iface.getEndpoint(i);
                if (ep.getDirection() == UsbConstants.USB_DIR_IN) {
                    if (bulkIn == null) {
                        mLogger.log(String.format("Bulk IN endpoint: %d", i));
                        bulkIn = ep;
                    }
                } else {
                    if (bulkOut == null) {
                        mLogger.log(String.format("Bulk OUT endpoint: %d", i));
                        bulkOut = ep;
                    }
                }
            }
            if (bulkIn == null || bulkOut == null) {
                mLogger.logError("Unable to find bulk endpoints");
                return;
            }
            mLogger.log("Connected");
            mConnected = true;
            mDevice = device;
            mProtocolVersion = protocolVersion;
            mAccessoryInterface = iface;
            mAccessoryConnection = conn;
            mControlEndpoint = controlEndpoint;
            mTransport = new UsbAccessoryBulkTransport(mLogger, conn, bulkIn, bulkOut);
            if (mProtocolVersion >= 2) {
                registerHid();
            }
            startServices();
            mTransport.startReading();
            return;
        }
        // Do accessory negotiation.
        mLogger.log("Attempting to switch device to accessory mode...");
        // Send get protocol.
        int protocolVersion = getProtocol(conn);
        if (protocolVersion < 1) {
            mLogger.logError("Device does not support accessory protocol.");
            return;
        }
        mLogger.log("Protocol version: " + protocolVersion);
        // Send identifying strings.
        sendString(conn, UsbAccessoryConstants.ACCESSORY_STRING_MANUFACTURER, MANUFACTURER);
        sendString(conn, UsbAccessoryConstants.ACCESSORY_STRING_MODEL, MODEL);
        sendString(conn, UsbAccessoryConstants.ACCESSORY_STRING_DESCRIPTION, DESCRIPTION);
        sendString(conn, UsbAccessoryConstants.ACCESSORY_STRING_VERSION, VERSION);
        sendString(conn, UsbAccessoryConstants.ACCESSORY_STRING_URI, URI);
        sendString(conn, UsbAccessoryConstants.ACCESSORY_STRING_SERIAL, SERIAL);
        // Send start.
        // The device should re-enumerate as an accessory.
        mLogger.log("Sending accessory start request.");
        int len = conn.controlTransfer(UsbConstants.USB_DIR_OUT | UsbConstants.USB_TYPE_VENDOR, UsbAccessoryConstants.ACCESSORY_START, 0, 0, null, 0, 10000);
        if (len != 0) {
            mLogger.logError("Device refused to switch to accessory mode.");
        } else {
            mLogger.log("Waiting for device to re-enumerate...");
        }
    } finally {
        if (!mConnected) {
            conn.releaseInterface(iface);
        }
    }
}
Also used : UsbDeviceConnection(android.hardware.usb.UsbDeviceConnection) UsbInterface(android.hardware.usb.UsbInterface) UsbEndpoint(android.hardware.usb.UsbEndpoint) Intent(android.content.Intent) PendingIntent(android.app.PendingIntent) PendingIntent(android.app.PendingIntent) UsbEndpoint(android.hardware.usb.UsbEndpoint)

Example 12 with UsbInterface

use of android.hardware.usb.UsbInterface in project platform_frameworks_base by android.

the class UsbHostManager method addUsbInterface.

/* Called from JNI in monitorUsbHostBus() to report new USB interface for the device
       currently being added.  Returns true if successful, false in case of error.
     */
private void addUsbInterface(int id, String name, int altSetting, int Class, int subClass, int protocol) {
    if (mNewInterface != null) {
        mNewInterface.setEndpoints(mNewEndpoints.toArray(new UsbEndpoint[mNewEndpoints.size()]));
        mNewEndpoints.clear();
    }
    mNewInterface = new UsbInterface(id, altSetting, name, Class, subClass, protocol);
    mNewInterfaces.add(mNewInterface);
}
Also used : UsbEndpoint(android.hardware.usb.UsbEndpoint) UsbInterface(android.hardware.usb.UsbInterface)

Example 13 with UsbInterface

use of android.hardware.usb.UsbInterface in project platform_frameworks_base by android.

the class UsbHostManager method beginUsbDeviceAdded.

/* Called from JNI in monitorUsbHostBus() to report new USB devices
       Returns true if successful, in which case the JNI code will continue adding configurations,
       interfaces and endpoints, and finally call endUsbDeviceAdded after all descriptors
       have been processed
     */
private boolean beginUsbDeviceAdded(String deviceName, int vendorID, int productID, int deviceClass, int deviceSubclass, int deviceProtocol, String manufacturerName, String productName, int version, String serialNumber) {
    if (DEBUG) {
        Slog.d(TAG, "usb:UsbHostManager.beginUsbDeviceAdded(" + deviceName + ")");
        // Audio Class Codes:
        // Audio: 0x01
        // Audio Subclass Codes:
        // undefined: 0x00
        // audio control: 0x01
        // audio streaming: 0x02
        // midi streaming: 0x03
        // some useful debugging info
        Slog.d(TAG, "usb: nm:" + deviceName + " vnd:" + vendorID + " prd:" + productID + " cls:" + deviceClass + " sub:" + deviceSubclass + " proto:" + deviceProtocol);
    }
    if (isBlackListed(deviceName) || isBlackListed(deviceClass, deviceSubclass, deviceProtocol)) {
        return false;
    }
    synchronized (mLock) {
        if (mDevices.get(deviceName) != null) {
            Slog.w(TAG, "device already on mDevices list: " + deviceName);
            return false;
        }
        if (mNewDevice != null) {
            Slog.e(TAG, "mNewDevice is not null in endUsbDeviceAdded");
            return false;
        }
        // Create version string in "%.%" format
        String versionString = Integer.toString(version >> 8) + "." + (version & 0xFF);
        mNewDevice = new UsbDevice(deviceName, vendorID, productID, deviceClass, deviceSubclass, deviceProtocol, manufacturerName, productName, versionString, serialNumber);
        mNewConfigurations = new ArrayList<UsbConfiguration>();
        mNewInterfaces = new ArrayList<UsbInterface>();
        mNewEndpoints = new ArrayList<UsbEndpoint>();
    }
    return true;
}
Also used : UsbDevice(android.hardware.usb.UsbDevice) UsbInterface(android.hardware.usb.UsbInterface) UsbEndpoint(android.hardware.usb.UsbEndpoint) UsbConfiguration(android.hardware.usb.UsbConfiguration)

Example 14 with UsbInterface

use of android.hardware.usb.UsbInterface in project platform_frameworks_base by android.

the class UsbAlsaManager method usbDeviceAdded.

/* package */
void usbDeviceAdded(UsbDevice usbDevice) {
    if (DEBUG) {
        Slog.d(TAG, "deviceAdded(): " + usbDevice.getManufacturerName() + " nm:" + usbDevice.getProductName());
    }
    // Is there an audio interface in there?
    boolean isAudioDevice = false;
    // FIXME - handle multiple configurations?
    int interfaceCount = usbDevice.getInterfaceCount();
    for (int ntrfaceIndex = 0; !isAudioDevice && ntrfaceIndex < interfaceCount; ntrfaceIndex++) {
        UsbInterface ntrface = usbDevice.getInterface(ntrfaceIndex);
        if (ntrface.getInterfaceClass() == UsbConstants.USB_CLASS_AUDIO) {
            isAudioDevice = true;
        }
    }
    if (DEBUG) {
        Slog.d(TAG, "  isAudioDevice: " + isAudioDevice);
    }
    if (!isAudioDevice) {
        return;
    }
    int addedCard = mCardsParser.getDefaultUsbCard();
    // handle the selection.
    if (DEBUG) {
        Slog.d(TAG, "  mCardsParser.isCardUsb(" + addedCard + ") = " + mCardsParser.isCardUsb(addedCard));
    }
    if (mCardsParser.isCardUsb(addedCard)) {
        UsbAudioDevice audioDevice = selectAudioCard(addedCard);
        if (audioDevice != null) {
            mAudioDevices.put(usbDevice, audioDevice);
            Slog.i(TAG, "USB Audio Device Added: " + audioDevice);
        }
        // look for MIDI devices
        // Don't need to call mDevicesParser.scan() because selectAudioCard() does this above.
        // Uncomment this next line if that behavior changes in the fugure.
        // mDevicesParser.scan()
        boolean hasMidi = mDevicesParser.hasMIDIDevices(addedCard);
        if (hasMidi && mHasMidiFeature) {
            int device = mDevicesParser.getDefaultDeviceNum(addedCard);
            AlsaDevice alsaDevice = waitForAlsaDevice(addedCard, device, AlsaDevice.TYPE_MIDI);
            if (alsaDevice != null) {
                Bundle properties = new Bundle();
                String manufacturer = usbDevice.getManufacturerName();
                String product = usbDevice.getProductName();
                String version = usbDevice.getVersion();
                String name;
                if (manufacturer == null || manufacturer.isEmpty()) {
                    name = product;
                } else if (product == null || product.isEmpty()) {
                    name = manufacturer;
                } else {
                    name = manufacturer + " " + product;
                }
                properties.putString(MidiDeviceInfo.PROPERTY_NAME, name);
                properties.putString(MidiDeviceInfo.PROPERTY_MANUFACTURER, manufacturer);
                properties.putString(MidiDeviceInfo.PROPERTY_PRODUCT, product);
                properties.putString(MidiDeviceInfo.PROPERTY_VERSION, version);
                properties.putString(MidiDeviceInfo.PROPERTY_SERIAL_NUMBER, usbDevice.getSerialNumber());
                properties.putInt(MidiDeviceInfo.PROPERTY_ALSA_CARD, alsaDevice.mCard);
                properties.putInt(MidiDeviceInfo.PROPERTY_ALSA_DEVICE, alsaDevice.mDevice);
                properties.putParcelable(MidiDeviceInfo.PROPERTY_USB_DEVICE, usbDevice);
                UsbMidiDevice usbMidiDevice = UsbMidiDevice.create(mContext, properties, alsaDevice.mCard, alsaDevice.mDevice);
                if (usbMidiDevice != null) {
                    mMidiDevices.put(usbDevice, usbMidiDevice);
                }
            }
        }
    }
    if (DEBUG) {
        Slog.d(TAG, "deviceAdded() - done");
    }
}
Also used : Bundle(android.os.Bundle) UsbInterface(android.hardware.usb.UsbInterface)

Example 15 with UsbInterface

use of android.hardware.usb.UsbInterface in project android_frameworks_base by DirtyUnicorns.

the class UsbHostManager method addUsbInterface.

/* Called from JNI in monitorUsbHostBus() to report new USB interface for the device
       currently being added.  Returns true if successful, false in case of error.
     */
private void addUsbInterface(int id, String name, int altSetting, int Class, int subClass, int protocol) {
    if (mNewInterface != null) {
        mNewInterface.setEndpoints(mNewEndpoints.toArray(new UsbEndpoint[mNewEndpoints.size()]));
        mNewEndpoints.clear();
    }
    mNewInterface = new UsbInterface(id, altSetting, name, Class, subClass, protocol);
    mNewInterfaces.add(mNewInterface);
}
Also used : UsbEndpoint(android.hardware.usb.UsbEndpoint) UsbInterface(android.hardware.usb.UsbInterface)

Aggregations

UsbInterface (android.hardware.usb.UsbInterface)21 UsbEndpoint (android.hardware.usb.UsbEndpoint)13 UsbConfiguration (android.hardware.usb.UsbConfiguration)8 UsbDevice (android.hardware.usb.UsbDevice)5 PendingIntent (android.app.PendingIntent)4 Intent (android.content.Intent)4 UsbDeviceConnection (android.hardware.usb.UsbDeviceConnection)4 Bundle (android.os.Bundle)4 Parcelable (android.os.Parcelable)1