UsbRequestPool.java
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2011 Google Inc.
00003  * 
00004  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
00005  * use this file except in compliance with the License. You may obtain a copy of
00006  * the License at
00007  * 
00008  * http://www.apache.org/licenses/LICENSE-2.0
00009  * 
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
00012  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
00013  * License for the specific language governing permissions and limitations under
00014  * the License.
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           // NOTE(damonkohler): There appears to be a bug around
00047           // UsbRequest.java:155 that can cause a spurious NPE. This seems safe
00048           // to ignore.
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 }


android_core
Author(s): Damon Kohler
autogenerated on Thu Aug 27 2015 12:11:33