Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 package org.ros.android.android_acm_serial;
00018
00019 import com.google.common.base.Preconditions;
00020 import com.google.common.collect.Maps;
00021
00022 import android.hardware.usb.UsbDeviceConnection;
00023 import android.hardware.usb.UsbEndpoint;
00024 import android.hardware.usb.UsbRequest;
00025 import android.util.Log;
00026
00027 import java.util.Map;
00028
00029 class UsbRequestPool {
00030
00031 private static final boolean DEBUG = false;
00032 private static final String TAG = "UsbRequestPool";
00033
00034 private final UsbDeviceConnection connection;
00035 private final Map<UsbEndpoint, UsbRequestQueue> usbRequestQueues;
00036 private final RequestWaitThread requestWaitThread;
00037
00038 private final class RequestWaitThread extends Thread {
00039 @Override
00040 public void run() {
00041 while (!Thread.currentThread().isInterrupted()) {
00042 UsbRequest request;
00043 try {
00044 request = connection.requestWait();
00045 } catch (NullPointerException e) {
00046
00047
00048
00049 if (DEBUG) {
00050 Log.e(TAG, "NPE while waiting for UsbRequest.", e);
00051 }
00052 continue;
00053 }
00054 if (request != null) {
00055 UsbEndpoint endpoint = request.getEndpoint();
00056 if (endpoint != null) {
00057 Preconditions.checkState(usbRequestQueues.containsKey(endpoint));
00058 usbRequestQueues.get(endpoint).add(request);
00059 } else {
00060 Log.e(TAG, "Completed UsbRequest is no longer open.");
00061 }
00062 } else {
00063 Log.e(TAG, "USB request error.");
00064 }
00065 if (DEBUG) {
00066 Log.d(TAG, "USB request completed.");
00067 }
00068 }
00069 }
00070 }
00071
00072 public UsbRequestPool(UsbDeviceConnection connection) {
00073 this.connection = connection;
00074 usbRequestQueues = Maps.newConcurrentMap();
00075 requestWaitThread = new RequestWaitThread();
00076 }
00077
00078 public void addEndpoint(UsbEndpoint endpoint, UsbRequestCallback callback) {
00079 usbRequestQueues.put(endpoint, new UsbRequestQueue(connection, endpoint, callback));
00080 }
00081
00082 public UsbRequest poll(UsbEndpoint endpoint) {
00083 Preconditions.checkArgument(usbRequestQueues.containsKey(endpoint),
00084 "Call addEndpoint() before the first call to poll().");
00085 UsbRequestQueue queue = usbRequestQueues.get(endpoint);
00086 UsbRequest request = queue.poll();
00087 return request;
00088 }
00089
00090 public void start() {
00091 requestWaitThread.start();
00092 }
00093
00094 public void shutdown() {
00095 requestWaitThread.interrupt();
00096 }
00097 }