summaryrefslogtreecommitdiff
path: root/altosdroid/src/org/altusmetrum/AltosDroid/AltosUsb.java
blob: 8d0a725aef5347191e7da8868b89805d4d880c58 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/*
 * Copyright © 2015 Keith Packard <keithp@keithp.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
 */

package org.altusmetrum.AltosDroid;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.UUID;
import java.util.HashMap;

import android.content.Context;
import android.hardware.usb.*;
import android.app.*;
import android.os.Handler;

import org.altusmetrum.altoslib_11.*;

public class AltosUsb extends AltosDroidLink {

	private Thread           input_thread   = null;

	private Handler          handler;

	private UsbManager		manager;
	private UsbDevice		device;
	private UsbDeviceConnection	connection;
	private UsbInterface 		iface;
	private UsbEndpoint		in, out;

	private InputStream      input;
	private OutputStream     output;

	// Constructor
	public AltosUsb(Context context, UsbDevice device, Handler handler) {
		super(handler);
//		set_debug(D);
		this.handler = handler;

		iface = null;
		in = null;
		out = null;

		int	niface = device.getInterfaceCount();

		for (int i = 0; i < niface; i++) {

			iface = device.getInterface(i);

			in = null;
			out = null;

			int nendpoints = iface.getEndpointCount();

			for (int e = 0; e < nendpoints; e++) {
				UsbEndpoint	endpoint = iface.getEndpoint(e);

				if (endpoint.getType() == UsbConstants.USB_ENDPOINT_XFER_BULK) {
					switch (endpoint.getDirection()) {
					case UsbConstants.USB_DIR_OUT:
						out = endpoint;
						break;
					case UsbConstants.USB_DIR_IN:
						in = endpoint;
						break;
					}
				}
			}

			if (in != null && out != null)
				break;
		}

		if (in != null && out != null) {
			AltosDebug.debug("\tin %s out %s\n", in.toString(), out.toString());

			manager = (UsbManager) context.getSystemService(Context.USB_SERVICE);

			if (manager == null) {
				AltosDebug.debug("USB_SERVICE failed");
				return;
			}

			connection = manager.openDevice(device);

			if (connection == null) {
				AltosDebug.debug("openDevice failed");
				return;
			}

			connection.claimInterface(iface, true);

			input_thread = new Thread(this);
			input_thread.start();

			// Configure the newly connected device for telemetry
			print("~\nE 0\n");
			set_monitor(false);
		}
	}

	static private boolean isAltusMetrum(UsbDevice device) {
		if (device.getVendorId() != AltosLib.vendor_altusmetrum)
			return false;
		if (device.getProductId() < AltosLib.product_altusmetrum_min)
			return false;
		if (device.getProductId() > AltosLib.product_altusmetrum_max)
			return false;
		return true;
	}

	static boolean matchProduct(int want_product, UsbDevice device) {

		if (!isAltusMetrum(device))
			return false;

		if (want_product == AltosLib.product_any)
			return true;

		int have_product = device.getProductId();

		if (want_product == AltosLib.product_basestation)
			return have_product == AltosLib.product_teledongle ||
				have_product == AltosLib.product_teleterra ||
				have_product == AltosLib.product_telebt ||
				have_product == AltosLib.product_megadongle;

		if (want_product == AltosLib.product_altimeter)
			return have_product == AltosLib.product_telemetrum ||
				have_product == AltosLib.product_telemega ||
				have_product == AltosLib.product_easymega ||
				have_product == AltosLib.product_telegps ||
				have_product == AltosLib.product_easymini ||
				have_product == AltosLib.product_telemini;

		if (have_product == AltosLib.product_altusmetrum)	/* old devices match any request */
			return true;

		if (want_product == have_product)
			return true;

		return false;
	}

	static public boolean request_permission(Context context, UsbDevice device, PendingIntent pi) {
		UsbManager	manager = (UsbManager) context.getSystemService(Context.USB_SERVICE);

//		if (manager.hasPermission(device))
//			return true;

		AltosDebug.debug("request permission for USB device " + device.toString());

		manager.requestPermission(device, pi);
		return false;
	}

	static public UsbDevice find_device(Context context, int match_product) {
		UsbManager	manager = (UsbManager) context.getSystemService(Context.USB_SERVICE);

		HashMap<String,UsbDevice>	devices = manager.getDeviceList();

		for (UsbDevice	device : devices.values()) {
			int	vendor = device.getVendorId();
			int	product = device.getProductId();

			if (matchProduct(match_product, device)) {
				AltosDebug.debug("found USB device " + device.toString());
				return device;
			}
		}

		return null;
	}

	private void disconnected() {
		if (closed()) {
			AltosDebug.debug("disconnected after closed");
			return;
		}

		AltosDebug.debug("Sending disconnected message");
		handler.obtainMessage(TelemetryService.MSG_DISCONNECTED, this).sendToTarget();
	}

	void close_device() {
		UsbDeviceConnection	tmp_connection;

		synchronized(this) {
			tmp_connection = connection;
			connection = null;
		}

		if (tmp_connection != null) {
			AltosDebug.debug("Closing USB device");
			tmp_connection.close();
		}
	}

	int read(byte[] buffer, int len) {
		int ret = connection.bulkTransfer(in, buffer, len, -1);
		AltosDebug.debug("read(%d) = %d\n", len, ret);
		return ret;
	}

	int write(byte[] buffer, int len) {
		int ret = connection.bulkTransfer(out, buffer, len, -1);
		AltosDebug.debug("write(%d) = %d\n", len, ret);
		return ret;
	}

	// Stubs of required methods when extending AltosLink
	public boolean can_cancel_reply()   { return false; }
	public boolean show_reply_timeout() { return true; }
	public void hide_reply_timeout()    { }

}