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; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
19 package org.altusmetrum.AltosDroid;
21 import java.io.InputStream;
22 import java.io.OutputStream;
23 import java.util.HashMap;
25 import android.content.Context;
26 import android.hardware.usb.*;
28 import android.os.Handler;
30 import org.altusmetrum.altoslib_13.*;
32 public class AltosUsb extends AltosDroidLink {
34 private Thread input_thread = null;
36 private Handler handler;
38 private UsbManager manager;
39 private UsbDevice device;
40 private UsbDeviceConnection connection;
41 private UsbInterface iface;
42 private UsbEndpoint in, out;
44 private InputStream input;
45 private OutputStream output;
48 public AltosUsb(Context context, UsbDevice device, Handler handler) {
51 this.handler = handler;
57 int niface = device.getInterfaceCount();
59 for (int i = 0; i < niface; i++) {
61 iface = device.getInterface(i);
66 int nendpoints = iface.getEndpointCount();
68 for (int e = 0; e < nendpoints; e++) {
69 UsbEndpoint endpoint = iface.getEndpoint(e);
71 if (endpoint.getType() == UsbConstants.USB_ENDPOINT_XFER_BULK) {
72 switch (endpoint.getDirection()) {
73 case UsbConstants.USB_DIR_OUT:
76 case UsbConstants.USB_DIR_IN:
83 if (in != null && out != null)
87 if (in != null && out != null) {
88 AltosDebug.debug("\tin %s out %s\n", in.toString(), out.toString());
90 manager = (UsbManager) context.getSystemService(Context.USB_SERVICE);
92 if (manager == null) {
93 AltosDebug.debug("USB_SERVICE failed");
97 connection = manager.openDevice(device);
99 if (connection == null) {
100 AltosDebug.debug("openDevice failed");
104 connection.claimInterface(iface, true);
106 input_thread = new Thread(this);
107 input_thread.start();
109 // Configure the newly connected device for telemetry
115 static private boolean isAltusMetrum(UsbDevice device) {
116 if (device.getVendorId() != AltosLib.vendor_altusmetrum)
118 if (device.getProductId() < AltosLib.product_altusmetrum_min)
120 if (device.getProductId() > AltosLib.product_altusmetrum_max)
125 static boolean matchProduct(int want_product, UsbDevice device) {
127 if (!isAltusMetrum(device))
130 if (want_product == AltosLib.product_any)
133 int have_product = device.getProductId();
135 if (want_product == AltosLib.product_basestation)
136 return have_product == AltosLib.product_teledongle ||
137 have_product == AltosLib.product_teleterra ||
138 have_product == AltosLib.product_telebt ||
139 have_product == AltosLib.product_megadongle;
141 if (want_product == AltosLib.product_altimeter)
142 return have_product == AltosLib.product_telemetrum ||
143 have_product == AltosLib.product_telemega ||
144 have_product == AltosLib.product_easymega ||
145 have_product == AltosLib.product_telegps ||
146 have_product == AltosLib.product_easymini ||
147 have_product == AltosLib.product_telemini;
149 if (have_product == AltosLib.product_altusmetrum) /* old devices match any request */
152 if (want_product == have_product)
158 static public boolean request_permission(Context context, UsbDevice device, PendingIntent pi) {
159 UsbManager manager = (UsbManager) context.getSystemService(Context.USB_SERVICE);
161 // if (manager.hasPermission(device))
164 AltosDebug.debug("request permission for USB device " + device.toString());
166 manager.requestPermission(device, pi);
170 static public UsbDevice find_device(Context context, int match_product) {
171 UsbManager manager = (UsbManager) context.getSystemService(Context.USB_SERVICE);
173 HashMap<String,UsbDevice> devices = manager.getDeviceList();
175 for (UsbDevice device : devices.values()) {
176 int vendor = device.getVendorId();
177 int product = device.getProductId();
179 if (matchProduct(match_product, device)) {
180 AltosDebug.debug("found USB device " + device.toString());
188 private void disconnected() {
190 AltosDebug.debug("disconnected after closed");
194 AltosDebug.debug("Sending disconnected message");
195 handler.obtainMessage(TelemetryService.MSG_DISCONNECTED, this).sendToTarget();
198 void close_device() {
199 UsbDeviceConnection tmp_connection;
202 tmp_connection = connection;
206 if (tmp_connection != null) {
207 AltosDebug.debug("Closing USB device");
208 tmp_connection.close();
212 int read(byte[] buffer, int len) {
213 int ret = connection.bulkTransfer(in, buffer, len, -1);
214 AltosDebug.debug("read(%d) = %d\n", len, ret);
218 int write(byte[] buffer, int len) {
219 int ret = connection.bulkTransfer(out, buffer, len, -1);
220 AltosDebug.debug("write(%d) = %d\n", len, ret);
224 // Stubs of required methods when extending AltosLink
225 public boolean can_cancel_reply() { return false; }
226 public boolean show_reply_timeout() { return true; }
227 public void hide_reply_timeout() { }