2 * Copyright © 2015 Keith Packard <keithp@keithp.com>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 2 of the License.
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
18 package org.altusmetrum.AltosDroid;
20 import java.io.IOException;
21 import java.io.InputStream;
22 import java.io.OutputStream;
23 import java.util.UUID;
24 import java.util.HashMap;
26 import android.content.Context;
27 import android.hardware.usb.*;
29 import android.os.Handler;
31 import org.altusmetrum.altoslib_7.*;
33 public class AltosUsb extends AltosDroidLink {
35 private Thread input_thread = null;
37 private Handler handler;
39 private UsbManager manager;
40 private UsbDevice device;
41 private UsbDeviceConnection connection;
42 private UsbInterface iface;
43 private UsbEndpoint in, out;
45 private InputStream input;
46 private OutputStream output;
49 public AltosUsb(Context context, UsbDevice device, Handler handler) {
52 this.handler = handler;
58 int niface = device.getInterfaceCount();
60 for (int i = 0; i < niface; i++) {
62 iface = device.getInterface(i);
67 int nendpoints = iface.getEndpointCount();
69 for (int e = 0; e < nendpoints; e++) {
70 UsbEndpoint endpoint = iface.getEndpoint(e);
72 if (endpoint.getType() == UsbConstants.USB_ENDPOINT_XFER_BULK) {
73 switch (endpoint.getDirection()) {
74 case UsbConstants.USB_DIR_OUT:
77 case UsbConstants.USB_DIR_IN:
84 if (in != null && out != null)
88 if (in != null && out != null) {
89 AltosDebug.debug("\tin %s out %s\n", in.toString(), out.toString());
91 manager = (UsbManager) context.getSystemService(Context.USB_SERVICE);
93 if (manager == null) {
94 AltosDebug.debug("USB_SERVICE failed");
98 connection = manager.openDevice(device);
100 if (connection == null) {
101 AltosDebug.debug("openDevice failed");
105 connection.claimInterface(iface, true);
107 input_thread = new Thread(this);
108 input_thread.start();
110 // Configure the newly connected device for telemetry
116 static private boolean isAltusMetrum(UsbDevice device) {
117 if (device.getVendorId() != AltosLib.vendor_altusmetrum)
119 if (device.getProductId() < AltosLib.product_altusmetrum_min)
121 if (device.getProductId() > AltosLib.product_altusmetrum_max)
126 static boolean matchProduct(int want_product, UsbDevice device) {
128 if (!isAltusMetrum(device))
131 if (want_product == AltosLib.product_any)
134 int have_product = device.getProductId();
136 if (want_product == AltosLib.product_basestation)
137 return have_product == AltosLib.product_teledongle ||
138 have_product == AltosLib.product_teleterra ||
139 have_product == AltosLib.product_telebt ||
140 have_product == AltosLib.product_megadongle;
142 if (want_product == AltosLib.product_altimeter)
143 return have_product == AltosLib.product_telemetrum ||
144 have_product == AltosLib.product_telemega ||
145 have_product == AltosLib.product_easymega ||
146 have_product == AltosLib.product_telegps ||
147 have_product == AltosLib.product_easymini ||
148 have_product == AltosLib.product_telemini;
150 if (have_product == AltosLib.product_altusmetrum) /* old devices match any request */
153 if (want_product == have_product)
159 static public boolean request_permission(Context context, UsbDevice device, PendingIntent pi) {
160 UsbManager manager = (UsbManager) context.getSystemService(Context.USB_SERVICE);
162 // if (manager.hasPermission(device))
165 AltosDebug.debug("request permission for USB device " + device.toString());
167 manager.requestPermission(device, pi);
171 static public UsbDevice find_device(Context context, int match_product) {
172 UsbManager manager = (UsbManager) context.getSystemService(Context.USB_SERVICE);
174 HashMap<String,UsbDevice> devices = manager.getDeviceList();
176 for (UsbDevice device : devices.values()) {
177 int vendor = device.getVendorId();
178 int product = device.getProductId();
180 if (matchProduct(match_product, device)) {
181 AltosDebug.debug("found USB device " + device.toString());
189 private void disconnected() {
191 AltosDebug.debug("disconnected after closed");
195 AltosDebug.debug("Sending disconnected message");
196 handler.obtainMessage(TelemetryService.MSG_DISCONNECTED, this).sendToTarget();
199 void close_device() {
200 UsbDeviceConnection tmp_connection;
203 tmp_connection = connection;
207 if (tmp_connection != null) {
208 AltosDebug.debug("Closing USB device");
209 tmp_connection.close();
213 int read(byte[] buffer, int len) {
214 int ret = connection.bulkTransfer(in, buffer, len, -1);
215 AltosDebug.debug("read(%d) = %d\n", len, ret);
219 int write(byte[] buffer, int len) {
220 int ret = connection.bulkTransfer(out, buffer, len, -1);
221 AltosDebug.debug("write(%d) = %d\n", len, ret);
225 // Stubs of required methods when extending AltosLink
226 public boolean can_cancel_reply() { return false; }
227 public boolean show_reply_timeout() { return true; }
228 public void hide_reply_timeout() { }