private void connectToDevice(UsbManager manager, URI deviceUri) throws DataSourceResourceException { connectToDevice(manager, UsbDeviceUtilities.vendorFromUri(deviceUri), UsbDeviceUtilities.productFromUri(deviceUri)); }
public UsbVehicleInterface(Context context, String uriString) throws DataSourceException { this(null, context, createUri(uriString)); }
private void initializeDevice() { try { connectToDevice(mManager, mDeviceUri); } catch(DataSourceException e) { Log.i(TAG, "Unable to load USB device -- " + "waiting for it to appear", e); } }
@Override public boolean setResource(String otherUri) throws DataSourceException { if(mDeviceUri == UsbDeviceUtilities.DEFAULT_USB_DEVICE_URI && otherUri != null && !UriBasedVehicleInterfaceMixin.sameResource(mDeviceUri, otherUri)) { mDeviceUri = createUri(otherUri); stop(); start(); return true; } return false; }
private static URI createUri(URI uri) throws DataSourceResourceException { if(uri == null || uri.toString().isEmpty()) { uri = UsbDeviceUtilities.DEFAULT_USB_DEVICE_URI; Log.i(TAG, "No USB device specified -- using default " + uri); } if(!validateResource(uri)) { throw new DataSourceResourceException( "USB device URI must have the usb:// scheme"); } // will throw an exception if not in the correct format UsbDeviceUtilities.vendorFromUri(uri); UsbDeviceUtilities.productFromUri(uri); return uri; }
@Override public void onResume() { super.onResume(); Log.d(TAG, "USB device proxy listener woke up"); Intent intent = getIntent(); Log.d(TAG, "Resumed with intent: " + intent); String action = intent.getAction(); if (UsbManager.ACTION_USB_DEVICE_ATTACHED.equals(action)) { Intent refreshDeviceIntent = new Intent( UsbVehicleInterface.ACTION_USB_DEVICE_ATTACHED); sendBroadcast(refreshDeviceIntent); } finish(); } }
@Test public void testUriWithBadScheme() throws DataSourceException { try { source = new UsbVehicleInterface(getContext(), incorrectSchemeUri); } catch(DataSourceResourceException e) { return; } Assert.fail("Expected a DataSourceResourceException"); }
private void connectToDevice(UsbManager manager, int vendorId, int productId) throws DataSourceResourceException { UsbDevice device = findDevice(manager, vendorId, productId); if(manager.hasPermission(device)) { Log.d(TAG, "Already have permission to use " + device); openConnection(device); } else { Log.d(TAG, "Requesting permission for " + device); manager.requestPermission(device, mPermissionIntent); } }
@Override public synchronized void start() { super.start(); IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION); getContext().registerReceiver(mBroadcastReceiver, filter); filter = new IntentFilter(); filter.addAction(ACTION_USB_DEVICE_ATTACHED); getContext().registerReceiver(mBroadcastReceiver, filter); filter = new IntentFilter(); filter.addAction(UsbManager.ACTION_USB_DEVICE_DETACHED); getContext().registerReceiver(mBroadcastReceiver, filter); initializeDevice(); }
private void openConnection(UsbDevice device) { if (device != null) { mConnectionLock.writeLock().lock(); try { mConnection = setupDevice(mManager, device); connected(); Log.i(TAG, "Connected to USB device with " + mConnection); } catch(UsbDeviceException e) { Log.w("Couldn't open USB device", e); } finally { mConnectionLock.writeLock().unlock(); } } else { Log.d(TAG, "Permission denied for device " + device); } }
@Override protected void disconnect() { if(!isConnected()) { return; } Log.d(TAG, "Closing connection " + mConnection + " with USB device"); mConnectionLock.writeLock().lock(); try { if(mConnection != null) { mConnection.close(); } mConnection = null; mInEndpoint = null; mOutEndpoint = null; mInterface = null; disconnected(); } finally { mConnectionLock.writeLock().unlock(); } }
@After public void tearDown() throws Exception { if(source != null) { source.stop(); } }
private UsbDeviceConnection setupDevice(UsbManager manager, UsbDevice device) throws UsbDeviceException { if(device.getInterfaceCount() != 1) { throw new UsbDeviceException("USB device didn't have an " + "interface for us to open"); return openInterface(manager, device, iface);
@Override protected int read(byte[] bytes) throws IOException { mConnectionLock.readLock().lock(); int bytesRead = 0; try { if(isConnected()) { bytesRead = mConnection.bulkTransfer(mInEndpoint, bytes, bytes.length, 0); } } finally { mConnectionLock.readLock().unlock(); } return bytesRead; }
/** * Unregister USB device intent broadcast receivers and stop waiting for a * connection. * * This should be called before the object is given up to the garbage * collector to avoid leaking a receiver in the Android framework. */ @Override public void stop() { super.stop(); try { getContext().unregisterReceiver(mBroadcastReceiver); } catch(IllegalArgumentException e) { Log.d(TAG, "Unable to unregister receiver when stopping, probably not registered"); } }
@Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); switch (action) { case ACTION_USB_PERMISSION: UsbDevice device = intent.getParcelableExtra(UsbManager.EXTRA_DEVICE); if (intent.getBooleanExtra( UsbManager.EXTRA_PERMISSION_GRANTED, false)) { openConnection(device); } else { Log.i(TAG, "User declined permission for device " + device); } break; case ACTION_USB_DEVICE_ATTACHED: Log.d(TAG, "Device attached"); try { connectToDevice(mManager, mDeviceUri); } catch (DataSourceException e) { Log.i(TAG, "Unable to load USB device -- waiting for it " + "to appear", e); } break; case UsbManager.ACTION_USB_DEVICE_DETACHED: Log.d(TAG, "Device detached"); disconnect(); break; } } };
private UsbDeviceConnection openInterface(UsbManager manager, UsbDevice device, UsbInterface iface) throws UsbDeviceException { UsbDeviceConnection connection = manager.openDevice(device); if(connection == null) { throw new UsbDeviceException("Couldn't open a connection to " + "device -- user may not have given permission"); } mInterface = iface; connection.claimInterface(mInterface, true); return connection; }
public static URI createUri(String uriString) throws DataSourceException { URI uri; if(uriString == null) { uri = null; } else { uri = UriBasedVehicleInterfaceMixin.createUri(uriString); } return createUri(uri); }
@Test public void testMalformedUri() throws DataSourceException { try { source = new UsbVehicleInterface(getContext(), malformedDeviceUri); } catch(DataSourceResourceException e) { return; } Assert.fail("Expected a DataSourceResourceException"); }
@Test public void testCustomDevice() throws DataSourceException { UsbVehicleInterface.createUri(deviceUri); }