Home | History | Annotate | Download | only in sink
      1 /*
      2  * Copyright (C) 2013 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.android.accessorydisplay.sink;
     18 
     19 import com.android.accessorydisplay.common.Logger;
     20 import com.android.accessorydisplay.common.Transport;
     21 
     22 import android.hardware.usb.UsbDevice;
     23 import android.hardware.usb.UsbDeviceConnection;
     24 import android.hardware.usb.UsbEndpoint;
     25 
     26 import java.io.IOException;
     27 
     28 /**
     29  * Sends or receives messages using bulk endpoints associated with a {@link UsbDevice}
     30  * that represents a USB accessory.
     31  */
     32 public class UsbAccessoryBulkTransport extends Transport {
     33     private static final int TIMEOUT_MILLIS = 1000;
     34 
     35     private UsbDeviceConnection mConnection;
     36     private UsbEndpoint mBulkInEndpoint;
     37     private UsbEndpoint mBulkOutEndpoint;
     38 
     39     public UsbAccessoryBulkTransport(Logger logger, UsbDeviceConnection connection,
     40             UsbEndpoint bulkInEndpoint, UsbEndpoint bulkOutEndpoint) {
     41         super(logger, 16384);
     42         mConnection = connection;
     43         mBulkInEndpoint = bulkInEndpoint;
     44         mBulkOutEndpoint = bulkOutEndpoint;
     45     }
     46 
     47     @Override
     48     protected void ioClose() {
     49         mConnection = null;
     50         mBulkInEndpoint = null;
     51         mBulkOutEndpoint = null;
     52     }
     53 
     54     @Override
     55     protected int ioRead(byte[] buffer, int offset, int count) throws IOException {
     56         if (mConnection == null) {
     57             throw new IOException("Connection was closed.");
     58         }
     59         return mConnection.bulkTransfer(mBulkInEndpoint, buffer, offset, count, -1);
     60     }
     61 
     62     @Override
     63     protected void ioWrite(byte[] buffer, int offset, int count) throws IOException {
     64         if (mConnection == null) {
     65             throw new IOException("Connection was closed.");
     66         }
     67         int result = mConnection.bulkTransfer(mBulkOutEndpoint,
     68                 buffer, offset, count, TIMEOUT_MILLIS);
     69         if (result < 0) {
     70             throw new IOException("Bulk transfer failed.");
     71         }
     72     }
     73 }
     74